text:
import numpy as np
import cv2
import glob
import json
import os

def findChessboardCorners(img, corner_size):
    # termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((corner_size[0]*corner_size[1],3), np.float32)
    objp[:,:2] = np.mgrid[0:corner_size[0],0:corner_size[1]].T.reshape(-1,2)

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (corner_size[0],corner_size[1]),None)

    # If found, add object points, image points (after refining them)
    if ret == True:
        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        return gray, objp, corners2, ret
    else:
        return None, [], [], ret

def savePoints(object_points, image_points, images_path, path):
    jstr = json.dumps({"objp": object_points, "imgp": image_points, "path": images_path})
    f = open(path, 'w')
    f.write(jstr)
    f.close()

def loadPoints(path):
    if not os.path.exists(path):
        return None, None, None
    f = open(path, 'r')
    jstr = f.read()
    f.close()
    jstr = json.loads(jstr)
    return jstr['objp'], jstr['imgp'], jstr['path']

max_stars_repo_name: PastelDew/RGB-D-Mask-R-CNN

max_issues_repo_name: PastelDew/RGB-D-Mask-R-CNN

max_forks_repo_name: PastelDew/PDStereo


text:
"""
    Title: test_convo.py
    Author: Tina Yang, tina.yang@ga.gov.au
    CreationDate: 2015-07-01
    Description: Unit testing module for convo function in calculate_bal.py
"""

import sys
import os.path
import unittest
import numpy as np
from inspect import getfile, currentframe
from numpy.testing import assert_array_equal

class TestConvo(unittest.TestCase):

    def test_convo(self):

        bal_exp_w = np.array([[-99., 29., 100., 19., 100., 40., 200., 200.],
                              [-99., 19., 100., 100., 40., 40., 200., 200.],
                              [-99., 40., 29., 100., 100., 29., 100., 29.],
                              [-99., 100., 40., 29., 40., 12.5, 29., 40.],
                              [-99., -99., 29., 40., 19., 29., 29., 29.],
                              [-99., 40., 100., 100., 29., 40., 29., 40.],
                              [-99., 100., 40., 29., 100., 100., 40., 100.],
                              [-99., 100., 29., 19., 29., 100., 100., 200.]])

        bal_exp_e = np.array([[100., 200., 200., 200., 200., 40., -99., -99.],
                              [100., 200., 200., 200., 200., 19., 29., -99.],
                              [100., 100., 100., 29., 100., 29., 29., -99.],
                              [40., 29., 40., 12.5, 29., 40., 19., -99.],
                              [29., 40., 19., 29, 29., 29, 40., -99.],
                              [100., 100., 29., 40., 29., 40., 29., -99.],
                              [40., 40., 100., 100., 40., 100., 40., -99.],
                              [29, 19., 200., 200., 200., 200., -99., -99.]])

        bal_exp_n = np.array([[-99., -99., -99., -99., -99., -99., -99., -99.],
                              [29., 100., -99., 100., 40., 200., 40., -99.],
                              [19, 100., 100, 40., 40., 200., 19., 29.],
                              [40., 29., 100., 100., 29., 200., 29., 29.],
                              [100., 40., 100., 40., 12.5, 200., 40., 19.],
                              [29., 29., 40., 19., 29, 200, 29, 40.],
                              [40., 100., 100., 29., 40., 29., 40., 29],
                              [100., 40., 29., 100., 100., 40., 100., 40.]])

        bal_exp_s = np.array([[19., 100., 100., 40., 40., 200., 19., 29.],
                              [40., 29, 100., 100., 29., 100., 29., 29.],
                              [100., 40., 29., 40., 12.5, 29., 40., 19.],
                              [12.5, 29., 40., 19., 29, 29., 200., 40.],
                              [40., 100., 100., 40., 40., 29, 200., 29],
                              [100., 40., 29., 100., 100., 100., 200., 40.],
                              [100., 29., 19., 29., 100., 100., 200., -99.],
                              [-99., -99., -99., -99., -99., -99., -99.,
                               -99.]])

        bal_exp_nw = np.array([[-99., -99., -99., -99., -99., -99., -99.,
                                -99.],
                               [-99., 19., 100., -99., 40., 29., 200., 29.],
                               [-99., 12.5, 100, 100., 29., 29., 200., 200.],
                               [-99., 29., 19., 100., 40., 29., 40., 200.],
                               [-99., 100., 29., 19., 29., 12.5, 19., 29.],
                               [-99., -99., 19., 29., 12.5, 19, 19., 19],
                               [-99., 29., 100., 40., 19., 29., 19., 29.],
                               [-99., 40., 29., 19., 100., 100., 29., 40.]])

        bal_exp_ne = np.array([[-99., -99., -99., -99., -99., -99., -99.,
                                -99.],
                               [100., -99., 40., 29., 200., 29., -99., -99.],
                               [100., 100., 29, 200., 200., 12.5, 19., -99.],
                               [19., 100., 200., 200., 40., 29., 29., -99.],
                               [29., 19., 200., 12.5, 19., 29., 12.5, -99.],
                               [19., 29., 12.5, 19., 19., 19., 29., -99.],
                               [100., 40., 19., 29., 19., 29., 19., -99.],
                               [29., 19., 100., 100., 29., 40., 29., -99.]])

        bal_exp_se = np.array([[100., 100., 29., 29., 200., 12.5, 19., -99.],
                               [19, 100., 40., 29., 40., 29., 29., -99.],
                               [29., 19., 29, 12.5, 19., 29., 12.5, -99.],
                               [19., 29., 12.5, 19, 19., 19., 29., -99.],
                               [100., 40., 19., 200., 19., 29, 19., -99.],
                               [29., 19., 100., 100., 200., 40., 29., -99.],
                               [29., 12.5, 19., 40., 100., 200., -99., -99.],
                               [-99., -99., -99., -99., -99., -99., -99.,
                                -99.]])

        bal_exp_sw = np.array([[-99., 12.5, 100., 100., 29., 29., 200., 12.5],
                               [-99., 29., 19., 100., 40., 29., 40., 29.],
                               [-99., 100., 29, 19., 29., 12.5, 19., 29.],
                               [-99., -99., 19., 29., 12.5, 19, 19., 19],
                               [-99., 29., 100., 40., 19., 29., 19., 29.],
                               [-99., 40., 29., 19., 100., 100., 29., 40.],
                               [-99., 40., 29., 12.5, 19., 40., 100., 200.],
                               [-99., -99., -99., -99., -99., -99., -99.,
                                -99.]])

        result_list = [bal_exp_w, bal_exp_e, bal_exp_n, bal_exp_s, bal_exp_nw,
                       bal_exp_ne, bal_exp_se, bal_exp_sw]

        cmd_folder = os.path.realpath(
                     os.path.abspath(os.path.split(
                     getfile(currentframe()))[0]))

        parent = os.path.abspath(os.path.join(cmd_folder, os.pardir))

        if parent not in sys.path:
            sys.path.insert(0, parent)

        from calculate_bal import convo

        veg_data = np.array([[  3.,  1.,  2.,  4.,  5.,  3.,  3.,  -99.],
                             [  7.,  1.,  1.,  3.,  3.,  4.,  5.,  6.],
                             [  6.,  7.,  1.,  4.,  5.,  4.,  5.,  5.],
                             [  2.,  5.,  6.,  4.,  5.,  3.,  4.,  5.],
                             [-99.,  3.,  4.,  5.,  7.,  5.,  7.,  6.],
                             [  4.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
                             [  4.,  4.,  5.,  1.,  2.,  3.,  4.,  5.],
                             [  6.,  7.,  5.,  5.,  6.,  1.,  2.,  3.]])

        slope_data = np.array([[ 1., 2.,  -99., 4., 5., 6., 5., 5.],
                               [ 1., 2.,  3.,  4., 5., 6., 1., 2.],
                               [ 3., 3., 5., 4., 4., 5., 4., 4.],
                               [ 5., 5., 6., 2., -99., 2., 3., 1.],
                               [-99., 2.,  3., 5., 2., 3., 2., 3.],
                               [  2., 3.,  3., 3., 3., 3., 3., 3.],
                               [ 4., 3., 3., 4., 4., 4., 4., 5.],
                               [ 4., 4., 1., 3., 4., 5., 6., -99.]])

        aspect_data = dict([('w', np.array([
                                           [7., 7., 7., 7., 7., 7., 7., 9.],
                                           [7., 7., 7., 7., 7., 7., 7., 7.],
                                           [7., 7., 7., 7., 7., 7., 7., 7.],
                                           [7., 7., 1., 7., 2., 7., 7., 7.],
                                           [3., 7., 7., 4., 7., 7., 7., 7.],
                                           [7., 7., 7., 7., 7., 7., 7., 7.],
                                           [7., 7., 7., 7., 7., 7., 7., 7.],
                                           [7., 7., 7., 7., 7., 7., 7., 7.]])),
                             ('e', np.array([
                                           [3., 3., 3., 3., 3., 3., 3., 9.],
                                           [3., 3., 3., 3., 3., 3., 3., 3.],
                                           [3., 3., 3., 3., 3., 3., 3., 3.],
                                           [3., 3., 9., 3., 9., 3., 3., 3.],
                                           [9., 3., 3., 9., 3., 3., 3., 3.],
                                           [3., 3., 3., 3., 3., 3., 3., 3.],
                                           [3., 3., 3., 3., 3., 3., 3., 3.],
                                           [3., 3., 3., 3., 3., 3., 3., 3.]])),
                             ('n', np.array([
                                           [1., 1., 1., 1., 1., 1., 1., 9.],
                                           [1., 1., 1., 1., 1., 1., 1., 1.],
                                           [1., 1., 1., 1., 1., 1., 1., 1.],
                                           [1., 1., 9., 1., 9., 1., 1., 1.],
                                           [9., 1., 1., 9., 1., 1., 1., 1.],
                                           [1., 1., 1., 1., 1., 1., 1., 1.],
                                           [1., 1., 1., 1., 1., 1., 1., 1.],
                                           [1., 1., 1., 1., 1., 1., 1., 1.]])),
                             ('s', np.array([
                                           [5., 5., 5., 5., 5., 5., 5., 9.],
                                           [5., 5., 5., 5., 5., 5., 5., 5.],
                                           [5., 5., 5., 5., 5., 5., 5., 5.],
                                           [5., 5., 9., 5., 9., 5., 5., 5.],
                                           [9., 5., 5., 9., 5., 5., 5., 5.],
                                           [5., 5., 5., 5., 5., 5., 5., 5.],
                                           [5., 5., 5., 5., 5., 5., 5., 5.],
                                           [5., 5., 5., 5., 5., 5., 5., 5.]])),
                             ('nw', np.array([
                                           [8., 8., 8., 8., 8., 8., 8., 9.],
                                           [8., 8., 8., 8., 8., 8., 8., 8.],
                                           [8., 8., 8., 8., 8., 8., 8., 8.],
                                           [8., 8., 9., 8., 9., 8., 8., 8.],
                                           [9., 8., 8., 9., 8., 8., 8., 8.],
                                           [8., 8., 8., 8., 8., 8., 8., 8.],
                                           [8., 8., 8., 8., 8., 8., 8., 8.],
                                           [8., 8., 8., 8., 8., 8., 8., 8.]])),
                             ('ne', np.array([
                                           [2., 2., 2., 2., 2., 2., 2., 9.],
                                           [2., 2., 2., 2., 2., 2., 2., 2.],
                                           [2., 2., 2., 2., 2., 2., 2., 2.],
                                           [2., 2., 9., 2., 9., 2., 2., 2.],
                                           [9., 2., 2., 9., 2., 2., 2., 2.],
                                           [2., 2., 2., 2., 2., 2., 2., 2.],
                                           [2., 2., 2., 2., 2., 2., 2., 2.],
                                           [2., 2., 2., 2., 2., 2., 2., 2.]])),
                             ('se', np.array([
                                           [4., 4., 4., 4., 4., 4., 4., 9.],
                                           [4., 4., 4., 4., 4., 4., 4., 4.],
                                           [4., 4., 4., 4., 4., 4., 4., 4.],
                                           [4., 4., 9., 4., 9., 4., 4., 4.],
                                           [9., 4., 4., 9., 4., 4., 4., 4.],
                                           [4., 4., 4., 4., 4., 4., 4., 4.],
                                           [4., 4., 4., 4., 4., 4., 4., 4.],
                                           [4., 4., 4., 4., 4., 4., 4., 4.]])),
                             ('sw', np.array([
                                           [6., 6., 6., 6., 6., 6., 6., 9.],
                                           [6., 6., 6., 6., 6., 6., 6., 6.],
                                           [6., 6., 6., 6., 6., 6., 6., 6.],
                                           [6., 6., 9., 6., 9., 6., 6., 6.],
                                           [9., 6., 6., 9., 6., 6., 6., 6.],
                                           [6., 6., 6., 6., 6., 6., 6., 6.],
                                           [6., 6., 6., 6., 6., 6., 6., 6.],
                                           [6., 6., 6., 6., 6., 6., 6., 6.]]))
                            ])

        pixel_width = 25

        dire = ['w', 'e', 'n', 's', 'nw', 'ne', 'se', 'sw']

        fdi = 80

        index = 0

        for one_dir in dire:

            outdata = convo(one_dir, veg_data, slope_data,
                            aspect_data[one_dir], pixel_width, fdi)

            bal_exp = result_list[index]

            assert_array_equal(outdata,  bal_exp)

            index += 1

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

ext: py

reason: import numpy,from numpy


text:
import pytest
import numpy as np
from numpy.testing import assert_array_almost_equal

from sktime.forecasters import DummyForecaster
from sktime.datasets import load_shampoo_sales

__author__ = ["Markus Löning"]

# forecast horizons
FHS = (None, [1], [1, 3], np.array([1]), np.array([1, 3]), np.arange(1, 5))

# load test data
y = load_shampoo_sales()

# test strategies of dummy forecaster
# TODO add test for linear strategy
@pytest.mark.filterwarnings('ignore::FutureWarning')
@pytest.mark.parametrize("fh", FHS)
@pytest.mark.parametrize("strategy, expected", [("mean", y.iloc[0].mean()), ("last", y.iloc[0].iloc[-1])])
def test_DummyForecaster_strategies(fh, strategy, expected):
    m = DummyForecaster(strategy=strategy)
    m.fit(y, fh=fh)
    y_pred = m.predict()
    assert_array_almost_equal(y_pred, expected, decimal=4)

max_issues_repo_name: ClaudiaSanches/sktime

max_forks_repo_name: ClaudiaSanches/sktime


text:
from hypothesis import given, assume
import hypothesis.strategies as st
import numpy as np

from latticegen.latticegeneration import *

@given(st.floats(0., exclude_min=True, allow_infinity=False),
       st.floats(0, np.pi),
       st.floats(0., 10, exclude_min=True),
       st.floats(0, np.pi),
       st.integers(4, 7),
       )
def test_generate_ks(r, t, k, p, sym):
    ks = generate_ks(r, t, k, p, sym)
    assert ks.shape == (sym + 1, 2)
    assert ks.max() <= max(r * k, r)

@given(r=st.floats(1e-20, 1e8, exclude_min=True, allow_infinity=False),
       t=st.floats(0, np.pi),
       o=st.integers(1, 3),
       k=st.floats(0.001, 10, exclude_min=True),
       p=st.floats(0, np.pi),
       size=st.one_of([st.integers(2, 50),
                       st.tuples(st.integers(2, 50), st.integers(2, 50))]),
       )
def test_fast_gen(r, t, o, k, p, size):
    # Don't use more than float max periods.
    assume(np.isfinite(r*np.max(size)*np.pi*2))
    ref = hexlattice_gen(r, t, o, size, k, p)
    fast = hexlattice_gen_fast(r, t, o, size, k, p)
    assert fast.shape == ref.shape
    ref = ref.compute()
    fast = fast.compute()
    atol = max(1e-10*r, 1e-10)
    assert np.abs((ref-fast)).max() < atol
    assert np.allclose(ref, fast, rtol=1e-5, atol=atol)

"""
@given(r=st.floats(1e-250, 1e250, allow_infinity=False),
       t=st.floats(0, np.pi),
       o=st.integers(1, 3),
       k=st.floats(1e-10, 1e10, exclude_min=True),
       p=st.floats(0, np.pi),
       size=st.tuples(st.integers(2, 50), st.integers(2, 50)),
       )
def test_new_hex_gen(r, t, o, k, p, size):
    # Don't use more than float max periods.
    assume(np.isfinite(r*np.max(size)*np.pi*2))
    ref = hexlattice_gen(r, t, o, size, k, p)
    new = hexlattice_gen2(r, t, o, size, k, p)
    assert new.shape == ref.shape
    ref = ref.compute()
    new = new.compute()
    assert np.allclose(ref, new)
"""

@given(r=st.floats(0.0, exclude_min=True, allow_infinity=False),
       t=st.floats(0, np.pi),
       o=st.integers(1, 3),
       k=st.floats(1e-6, 10, exclude_min=True),
       p=st.floats(0, np.pi),
       size=st.tuples(st.integers(2, 70),
                      st.integers(2, 70)),
       sym=st.integers(4, 7),
       )
def test_gen(r, t, o, sym, k, p, size):
    # Don't use more than float max periods.
    assume(np.isfinite(r*max(size)*np.pi*2))
    ref = anylattice_gen(r, t, o, sym, size, k, p)
    assert ref.shape == size
    ref = ref.compute()
    assert np.all(~np.isnan(ref))

@given(st.integers(3, 500))
def test_shapes_square(s):
    assert hexlattice_gen(0.1, 0., 1, s).shape == (s, s)
    assert anylattice_gen(0.1, 0., 1, 4, s).shape == (s, s)

max_stars_repo_name: Arfaouim/moire-lattice-generator

max_issues_repo_name: Arfaouim/moire-lattice-generator

max_forks_repo_name: Arfaouim/moire-lattice-generator

reason: import numpy


text:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ===================================================
# 8021aqGraphView_v1.0 - created by Benjamin Domroese in 2021.
# GraphView 1.0 created.
# GraphView 1.1 - added plausibility check for duplicate ISIDs on one node.

import math
import time
import os
import networkx as nx  # For graphical graph
import matplotlib.pyplot as plt
from itertools import product
from prettytable import PrettyTable
from spbnetworks import d_node

class AbstractDijkstraSPF():

    def __init__(self, G, s, bvl):
        """ Calculate shortest path from s to other nodes in G. """
        self.__dist = dist = dict()
        self.__prev = prev = dict()
        self.ectcnt = 0
        visited = set()
        queue = set()

        dist[s] = 0
        prev[s] = s
        queue.add(s)

        while queue:
            u = min(queue, key=dist.get)
            for v in self.get_adjacent_nodes(G, u):
                if v in visited:
                    continue
                alt = self.get_distance(u) + self.get_edge_weight(G, u, v)

                # Check if alternate path is cheaper than actual path.
                if alt < self.get_distance(v):
                    dist[v] = alt
                    prev[v] = u
                    queue.add(v)

                # Check for equal cost path AND less hops on alternate path.
                elif alt == self.get_distance(v) and \
                        len(self.get_path(v))-1 > len(self.get_path(u)):
                    dist[v] = alt
                    prev[v] = u
                    queue.add(v)

                # Check for equal cost path AND equal number of hops.
                elif alt == self.get_distance(v) and \
                        len(self.get_path(v))-1 == len(self.get_path(u)):

                    # Get hop (used) before v, that be taken for XOR
                    # calculation.
                    v2 = self.get_path(v)[-2]

                    # v2_0: Get PATHID and find lowest SystemID
                    # (after XOR calc).
                    # Get all unique Nodes between FORK and JOIN
                    # points of subpath_used and subpath_alt.
                    subpath_used = list(filter(lambda x: x not in self.
                                        get_path(u), self.get_path(v2)))
                    subpath_alt = list(filter(lambda x: x not in self.
                                              get_path(v2), self.get_path(u)))

                    # Get LowPathID for subpath_used/alt.
                    # This is done by using the lowest first byte of (xored)
                    # SystemIDs in subpath_used, and compare it with the
                    # lowest first byte of (xored) SystemIDs in subpath_alt.
                    for i in range(8):
                        list_bytes_subpath_used = []
                        list_bytes_subpath_alt = []
                        for hop in subpath_used:
                            list_bytes_subpath_used.append(
                                [d_node_xor[int(bvl)][hop][i]])
                            xor_v = min(list_bytes_subpath_used)
                        for hop in subpath_alt:
                            list_bytes_subpath_alt.append(
                                [d_node_xor[int(bvl)][hop][i]])
                            xor_u = min(list_bytes_subpath_alt)
                        self.ectcnt += 1
                    # If min of first byte is the same, continue until byte 8.
                    # If no difference would be found, a duplicate SystemID
                    # would be present which causes an exception.
                        if xor_v != xor_u:
                            # For statistic purpose, save tiebreaker position.
                            d_tiebreaker_byte[i] = d_tiebreaker_byte[i]+1
                            break

                    # Compare XORed values.
                    if xor_v < xor_u:
                        dist[v] = alt
                        prev[v] = v2
                        queue.add(v)
                    elif xor_v > xor_u:
                        prev[v] = u

            queue.remove(u)
            visited.add(u)

    @staticmethod
    def get_adjacent_nodes(G, u):
        raise NotImplementedError()

    @staticmethod
    def get_edge_weight(G, u, v):
        raise NotImplementedError()

    def get_distance(self, u):
        """ Return the length of shortest path from s to u. """
        return self.__dist.get(u, math.inf)

    def get_path(self, v):
        """ Return the shortest path to v. """
        path = [v]
        while self.__prev[v] != v:
            v = self.__prev[v]
            path.append(v)
        return path[::-1]

class DijkstraSPF(AbstractDijkstraSPF):

    @staticmethod
    def get_adjacent_nodes(G, u):
        return G.get_adjacent_nodes(u)

    @staticmethod
    def get_edge_weight(G, u, v):
        return G.get_edge_weight(u, v)

    @staticmethod
    def byte_xor(self, ba1, ba2):
        return bytes([a ^ b for a, b in zip(ba1, ba2)])

class Graph():

    def __init__(self, adjacency_list=dict(), edge_weights=dict()):
        self.__adjacency_list = adjacency_list.copy()
        self.__edge_weights = edge_weights.copy()

    def add_edge(self, u, v, w):
        """ Add a new edge u -> v to graph with edge weight w. """
        self.__edge_weights[u, v] = w
        if u not in self.__adjacency_list:
            self.__adjacency_list[u] = set()
        self.__adjacency_list[u].add(v)

    def get_edge_weight(self, u, v):
        """ Get edge weight of edge between u and v. """
        return self.__edge_weights[u, v]

    def get_adjacent_nodes(self, u):
        """ Get nodes adjacent to u. """
        return self.__adjacency_list.get(u, set())

    def get_number_of_nodes(self):
        """ Return the total number of nodes in graph. """
        return len(self.__adjacency_list)

    def get_nodes(self):
        """ Return all nodes in this graph. """
        return self.__adjacency_list.keys()

def byte_xor(ba1, ba2):
    return bytes([a ^ b for a, b in zip(ba1, ba2)])

def get_max_possible_path_a_to_b(a, b):
    set_unique_path = set()  # Remove duplicate paths, keep only unique.
    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        path_as_string = str(d_dijkstra[a][bvl].get_path(b))
        set_unique_path.add(path_as_string)  # Add string to set
    return (set_unique_path)

def get_max_possible_path_a_to_b_filtered(a, b, listbvls):
    set_unique_path = set()  # Remove duplicate paths, keep only unique.
    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        if bvl in listbvls:
            path_as_string = str(d_dijkstra[a][bvl].get_path(b))
            set_unique_path.add(path_as_string)  # Add string to set.
    return (set_unique_path)

def get_max_possible_path_table():
    for k in sorted(d_ect_path, reverse=True):
        print("Number of connections with maximum {:>3d} possible ECT pathes: \
         {:>4d}".format(int(k), int(len(d_ect_path[k]))))
    return()

def get_path_a_to_b(a, b):
    print("Possible pathes from {} to {}:".format(a, b))
    print("===========================================")
    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        print("BVLAN {}: {}".format(bvl,
              " -> ".join(d_dijkstra[a][bvl].get_path(b))))

def get_path_a_to_b_filtered(a, b, listbvls):
    print("Pathes from {} to {} in used BVLAN`s:".format(a, b))
    print("===========================================")
    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        if bvl in listbvls:
            print("BVLAN {}: {}".format(bvl,
                  " -> ".join(d_dijkstra[a][bvl].get_path(b))))

def get_path_a_to_b_mc(a, b, bvl):
    print("BVLAN {}: {}".format(bvl, d_dijkstra[a][bvl].get_path(b)))

def reset_graph_nx():
    for u, v, a in graph_nx.edges(data=True):
        graph_nx.add_edge(u, v, weight=1.0, color="black")

def reset_graph_nx_small_lines():
    for u, v, a in graph_nx.edges(data=True):
        graph_nx.add_edge(u, v, weight=0.1, color="black")

def print_path_a_to_b(a, b):
    reset_graph_nx_small_lines()
    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        for x in range(len(d_dijkstra[a][bvl].get_path(b))-1):
            v1 = d_dijkstra[a][bvl].get_path(b)[x]
            v2 = d_dijkstra[a][bvl].get_path(b)[x+1]
            # Get actual weight of edge and add one:
            graph_nx[v1][v2]["weight"] = graph_nx[v1][v2]["weight"] + 1
            graph_nx.add_edge(v1, v2, color="blue")
    colors = nx.get_edge_attributes(graph_nx, "color").values()
    weights = nx.get_edge_attributes(graph_nx, "weight").values()
    weights_mod = [v_resize * x for x in weights]
    nx.draw(graph_nx, dict_node_position, node_color="darkorchid",
            edge_color=colors, width=weights_mod, with_labels="True")
    plt.show()
    reset_graph_nx()

def print_path_a_to_b_filtered(a, b, listbvl):
    reset_graph_nx_small_lines()
    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        if bvl in listbvl:
            for x in range(len(d_dijkstra[a][bvl].get_path(b))-1):
                v1 = d_dijkstra[a][bvl].get_path(b)[x]
                v2 = d_dijkstra[a][bvl].get_path(b)[x+1]
                # Get actual weight of edge and add one:
                graph_nx[v1][v2]["weight"] = graph_nx[v1][v2]["weight"] + 1
                graph_nx.add_edge(v1, v2, color="blue")
    colors = nx.get_edge_attributes(graph_nx, "color").values()
    weights = nx.get_edge_attributes(graph_nx, "weight").values()
    weights_mod = [v_resize * x for x in weights]
    nx.draw(graph_nx, dict_node_position, node_color="darkorchid",
            edge_color=colors, width=weights_mod, with_labels="True")
    plt.show()
    reset_graph_nx()

def main_menu():
    print(r" ___   __  ___     _                 __   __ _")
    print(r"( _ ) /  \|_  )   / | __ _  __ _  ___\ \ / /(_) ___ __ __ __")
    print(r"/ _ \| () |/ /  _ | |/ _` |/ _` ||___|\ V / | |/ -_)\ V  V /")
    print(r"\___/ \__//___|(_)|_|\__,_|\__, |      \_/  |_|\___| \_/\_/")
    print(r"                              |_|")
    print(" 1: Show Node List")
    print(" 2: Maximum possible path diversity as summary")
    print(" 3: Maximum possible path diversity between A and B")
    print(" 4: Display connections with n possible pathes")
    print(" 5: Show BVLANs and ISIDs in use")
    print("   ---------- Multicast view ---------------")
    print("10: Number of Multicast states per Node")
    print("11: Multicast states of a specific node")
    print("   ---------- Edge view --------------------")
    print("20: Show all edges")
    print("21: Usage of edges as summary (BVLAN count)")
    print("22: Usage of a specific edge  (BVLAN count)")
    print("23: Usage of edges as summary  (ISID count)")
    print("24: Usage of a specific edge   (ISID count)")
    print("   ---------- Performance view -------------")
    print("30: Number of ECT Calculations")
    print("31: Time for calculation of SPF and MCast")
    print("   ---------- Graphical View ---------------")
    print("40: Show pathes between A and B")
    print("41: Show usage of all edges (BVLAN view)")
    print("42: Show usage of all edges (ISID  view)")
    print("43: Change resizing factor for GraphView (actual: {})"
          .format(v_resize))
    print(" 0: Quit program\n")

    # Interactive usermenu.
    userchoice = input("Select Option:")
    if userchoice == "1":
        menu1()
    elif userchoice == "2":
        menu2()
    elif userchoice == "3":
        menu3()
    elif userchoice == "4":
        menu4()
    elif userchoice == "5":
        menu5()
    elif userchoice == "10":
        menu10()
    elif userchoice == "11":
        menu11()
    elif userchoice == "20":
        menu20()
    elif userchoice == "21":
        menu21()
    elif userchoice == "22":
        menu22()
    elif userchoice == "23":
        menu23()
    elif userchoice == "24":
        menu24()
    elif userchoice == "30":
        menu30()
    elif userchoice == "31":
        menu31()
    elif userchoice == "40":
        menu40()
    elif userchoice == "41":
        menu41()
    elif userchoice == "42":
        menu42()
    elif userchoice == "43":
        menu43()
    elif userchoice == "0":
        menu_exit()
    else:
        print("Wrong input! Please choose valid option.")
        main_menu()

def key_error_node():
    input1 = input("Key Error! Please check node name. " +
                   "Display list of nodes (y/n)?:")
    if input1 == "y" or input1 == "Y":
        print("List of Nodes: ")
        print("============== ")
        for k in d_node.keys():
            print(k)

def decorator_cls_and_return_mainmenu(func):
    def wrapper():
        os.system("cls")
        func()
        input("\nType anykey to continue")
        os.system("cls")
        main_menu()
    return wrapper

@decorator_cls_and_return_mainmenu
def menu1():
    print("List of nodes: ")
    print("============== ")
    for k in d_node.keys():
        print(k)

@decorator_cls_and_return_mainmenu
def menu2():
    print("Connections are counted bidirectional (A -> B and B -> A)")
    print("Counted are all paths available in BVLAN range {} to {}".format(
        low_bvl_to_test, max_bvl_to_test - 1))
    print("=========================================================")
    get_max_possible_path_table()

@decorator_cls_and_return_mainmenu
def menu3():
    input1 = input("Select source node: ")
    input2 = input("Select destination node: ")
    input3 = input("Only for used BVLAN`s (y/n): ")
    print("===========================================")
    try:
        if input3 == "y" or input3 == "Y":
            get_path_a_to_b_filtered(input1, input2, set_bvls_in_use)
            print("\nNumber of different pathes available " +
                  "(only for used BVLAN`s): ")
            print("-> {}".format(len(get_max_possible_path_a_to_b_filtered(
                input1, input2, set_bvls_in_use))))
        else:
            get_path_a_to_b(input1, input2)
            print("\nNumber of different pathes available " +
                  "(for all calculated BVLANs): ")
            print("-> {}".format(len(get_max_possible_path_a_to_b(
                input1, input2))))
    except Exception:
        key_error_node()

@decorator_cls_and_return_mainmenu
def menu4():
    input1 = input("Type number of pathes that should be available: ")
    try:
        print("List of connections with {} possibilities: ".format(input1))
        print("===========================================")
        if int(input1) not in d_ect_path.keys():
            print("There are no connections with {} possibilities.".format(
                  input1))
            print("Please check pathes summary.")
        else:
            for i in range(len(d_ect_path[int(input1)])):
                print(d_ect_path[int(input1)][i])
    except Exception:
        print("Please type a number!")
        input("Type anykey to continue")
        os.system("cls")

@decorator_cls_and_return_mainmenu
def menu5():
    print("BVLANs in use: {}".format(set_bvls_in_use))
    print("ISIDs in use : {}".format(set_isids_in_use))

@decorator_cls_and_return_mainmenu
def menu10():
    print("Multicast states per node: ")
    print("==========================")
    for k in d_mcast_states:
        print("Node {:>{width}} has {:>5d} multicast states".format(
            k, len(d_mcast_states[k]), width=max_nodename))

@decorator_cls_and_return_mainmenu
def menu11():
    input1 = input("Select node: ")
    try:
        print("Multicast states of node {}".format(input1))
        x = PrettyTable()
        x.field_names = ["Source", "ISID", "BVLAN", "IIF", "OIL"]
        newlist = []  # To remove duplicates.
        for i in range(len(d_mcast_states[input1])):
            if d_mcast_states[input1][i] not in newlist:
                newlist.append(d_mcast_states[input1][i])
        for i in range(len(newlist)):
            x.add_row(newlist[i])
        x.sortby = "ISID"
        print(x)
    except Exception:
        key_error_node()

@decorator_cls_and_return_mainmenu
def menu20():
    print("List of all edges: ")
    print("=================  ")
    d_edges_weight_table = PrettyTable()
    d_edges_weight_table.field_names = ["Edge", "Metric"]
    for k, v in d_edges_weight.items():
        d_edges_weight_table.add_row([k, v])
    print(d_edges_weight_table)
    print("Total number of edges: {}".format(len(d_edges.keys())))

@decorator_cls_and_return_mainmenu
def menu21():
    input1 = input("Enter BVLAN to filter, or press enter for all BVLANs : ")
    print("===========================================")
    try:
        if int(input1) in set_bvls_in_use:
            d_edges_table = PrettyTable()
            d_edges_table.field_names = ["Edge", "Connections (#BVLANs)"]
            for key in d_edges.keys():
                len_d_edges_filtered = 0
                for i in range(len(d_edges[key])):
                    if int(input1) == d_edges[key][i][2]:
                        len_d_edges_filtered += 1
                d_edges_table.add_row([key, len_d_edges_filtered])
            d_edges_table.sortby = "Connections (#BVLANs)"
            print(d_edges_table)
            print("Output filtered by BVLAN {}.".format(input1))
        else:
            d_edges_table = PrettyTable()
            d_edges_table.field_names = ["Edge", "Connections (#BVLANs)"]
            for key in d_edges.keys():
                d_edges_table.add_row([key, len(d_edges[key])])
            d_edges_table.sortby = "Connections (#BVLANs)"
            print(d_edges_table)
            print("Warning: BVLAN {} was not found in used BVLANs {}!".format(
                input1, set_bvls_in_use))
            print("Therefore all BVLANs are counted in output.  ")
    except Exception:
        d_edges_table = PrettyTable()
        d_edges_table.field_names = ["Edge", "Connections (#BVLANs)"]
        for key in d_edges.keys():
            d_edges_table.add_row([key, len(d_edges[key])])
        d_edges_table.sortby = "Connections (#BVLANs)"
        print(d_edges_table)

@decorator_cls_and_return_mainmenu
def menu22():
    d_edges_connections = PrettyTable()
    d_edges_connections.field_names = ["Source", "Destination", "BVLAN"]
    input1 = input("Select source node: ")
    input2 = input("Select destination node: ")
    input3 = input("Enter BVLAN to filter, or press enter for all BVLANs : ")
    print("===========================================")
    edge = input1 + "___" + input2
    try:
        if int(input3) in set_bvls_in_use:
            try:
                for i in d_edges[edge]:
                    if i[2] == int(input3):
                        d_edges_connections.add_row([i[0], i[1], i[2]])
                print("Connections using edge {}".format(edge))
                print(d_edges_connections)
                print("Output filtered by BVLAN {}.".format(input3))
            except Exception:
                input3 = input("Edge does not exist! Select correct source " +
                               "and destination node. Display list of " +
                               "edges (y/n)?: ")
                if input3 == "y" or input3 == "Y":
                    for key in d_edges.keys():
                        print(key)
                    input("\nType anykey to continue")
                    os.system("cls")
                else:
                    os.system("cls")
        else:
            print("Warning: BVLAN {} was not found in used BVLANs {}!".format(
                input3, set_bvls_in_use))
    except Exception:
        try:
            for i in d_edges[edge]:
                d_edges_connections.add_row([i[0], i[1], i[2]])
            print("Connections using edge {}".format(edge))
            print(d_edges_connections)
        except Exception:
            input3 = input("Edge does not exist! Select correct source and " +
                           "destination node. Display list of edges (y/n)?: ")
            if input3 == "y" or input3 == "Y":
                for key in d_edges.keys():
                    print(key)
                input("\nType anykey to continue")
                os.system("cls")
            else:
                os.system("cls")

@decorator_cls_and_return_mainmenu
def menu23():
    input1 = input("Enter ISID to filter, or press enter for all ISIDs : ")
    print("===========================================")
    try:
        if int(input1) in set_isids_in_use:
            d_edges_isid_table = PrettyTable()
            d_edges_isid_table.field_names = ["Edge", "Connections (#ISIDs)"]
            for key in d_edges_isid.keys():
                len_d_edges_filtered = 0
                for i in range(len(d_edges_isid[key])):
                    if int(input1) == d_edges_isid[key][i][3]:
                        len_d_edges_filtered += 1
                d_edges_isid_table.add_row([key, len_d_edges_filtered])
            d_edges_isid_table.sortby = "Connections (#ISIDs)"
            print(d_edges_isid_table)
            print("Output filtered by ISID {}.".format(input1))
        else:
            d_edges_isid_table = PrettyTable()
            d_edges_isid_table.field_names = ["Edge", "Connections (#ISIDs)"]
            for key in d_edges_isid.keys():
                d_edges_isid_table.add_row([key, len(d_edges_isid[key])])
            d_edges_isid_table.sortby = "Connections (#ISIDs)"
            print(d_edges_isid_table)
            print("Warning: ISID {} was not found in used ISIDs!".format(
                input1))
            print("Therefore all ISIDs are counted in output.  ")
    except Exception:
        d_edges_isid_table = PrettyTable()
        d_edges_isid_table.field_names = ["Edge", "Connections (#ISIDs)"]
        for key in d_edges_isid.keys():
            d_edges_isid_table.add_row([key, len(d_edges_isid[key])])
        d_edges_isid_table.sortby = "Connections (#ISIDs)"
        print(d_edges_isid_table)

@decorator_cls_and_return_mainmenu
def menu24():
    d_edges_isid_connections = PrettyTable()
    d_edges_isid_connections.field_names = \
        ["Source", "Destination", "BVLAN", "ISID"]
    input1 = input("Select source node: ")
    input2 = input("Select destination node: ")
    input3 = input("Enter ISID to filter, or press enter for all ISIDs : ")
    print("===========================================")
    edge = input1 + "___" + input2
    try:
        if int(input3) in set_isids_in_use:
            try:
                for i in d_edges_isid[edge]:
                    if i[3] == int(input3):
                        d_edges_isid_connections.add_row(
                            [i[0], i[1], i[2], i[3]])
                print("Connections using edge {}".format(edge))
                d_edges_isid_connections.sortby = "ISID"
                print(d_edges_isid_connections)
                print("Output filtered by ISID {}.".format(input3))
            except Exception:
                input3 = input("Edge does not exist! Select correct source " +
                               "and destination node. Display list of " +
                               "edges (y/n)?: ")
                if input3 == "y" or input3 == "Y":
                    for key in d_edges.keys():
                        print(key)
                    input("\nType anykey to continue")
                    os.system("cls")
                else:
                    os.system("cls")
        else:
            print("Warning: ISID {} was not found in used ISIDs!".format(
                input3))
    except Exception:
        try:
            for i in d_edges_isid[edge]:
                d_edges_isid_connections.add_row([i[0], i[1], i[2], i[3]])
            print("Connections using edge {}".format(edge))
            d_edges_isid_connections.sortby = "ISID"
            print(d_edges_isid_connections)
        except Exception:
            input3 = input("Edge does not exist! Select correct source and " +
                           "destination node. Display list of edges (y/n)?: ")
            if input3 == "y" or input3 == "Y":
                for key in d_edges.keys():
                    print(key)
                input("\nType anykey to continue")
                os.system("cls")
            else:
                os.system("cls")

@decorator_cls_and_return_mainmenu
def menu30():
    apsp_ect_counter = 0
    print("Info: Every byte of BridgeID that needs to be compared, \
will increment the value (i.e. one ECT decision with tiebreaker in 8 byte is \
counted as 8!")
    print("Tip : An earlier tiebreaker in BridgeID will reduce the number of \
computations.\n")
    for k in d_node:
        print("Node {:>{width}} performed {:>4d} ECT computations \
(XOR comparisations) per BVLAN - hence in total: {}".format(
                 k, d_dijkstra[k][low_bvl_to_test].ectcnt,
                 d_dijkstra[k][low_bvl_to_test].ectcnt*len(set_bvls_in_use),
                 width=max_nodename))
        apsp_ect_counter += d_dijkstra[k][low_bvl_to_test].ectcnt*len(
            set_bvls_in_use)
    print("================================================")
    print("APSP would need {} ECT computations (XOR comparisations) ".format(
        apsp_ect_counter))
    # v2_0: Print d_tiebreaker_byte.
    print("\nTiebreaker statistics (position of BridgeID it was found): ")
    print("Info: This is calculated for all BVLANs in range {} to {} \
(not only used BVLANs)!\n".format(low_bvl_to_test, max_bvl_to_test - 1))
    for k in d_tiebreaker_byte:
        print("Tiebreaker found in byte #{}: {}".format(
            k, d_tiebreaker_byte[k]))

@decorator_cls_and_return_mainmenu
def menu31():
    print("-> Time for all-pair-shortest-path (apsp) calculation: {}".format(
        time_apsp_calculation))
    print("-> Time for apsp calculation per BVLAN               : {}".format(
        time_apsp_calculation / (max_bvl_to_test - low_bvl_to_test)))
    print("-> Time for multicast-table population               : {}".format(
        time_mcast_calculation))
    print("\nInfo:")
    print("Multicast computation time might appear lower than experienced.")
    print("This is due to a remarkable time needed for output formatting.")

@decorator_cls_and_return_mainmenu
def menu40():
    input1 = input("Select source node: ")
    input2 = input("Select destination node: ")
    input3 = input("Only for used BVLAN`s (y/n): ")
    print("===========================================")
    try:
        if input3 == "y" or input3 == "Y":
            print_path_a_to_b_filtered(input1, input2, set_bvls_in_use)
        else:
            print_path_a_to_b(input1, input2)
    except Exception:
        key_error_node()

@decorator_cls_and_return_mainmenu
def menu41():
    reset_graph_nx()
    input1 = input("Enter BVLAN to filter, or press enter for all BVLANs : ")
    print("===========================================")
    max_count = 1
    for key in d_edges.keys():
        if len(d_edges[key]) > max_count:
            max_count = len(d_edges[key])
    try:
        if int(input1) in set_bvls_in_use:
            # Set different max_count, if filter is applied:
            # max_count is used, for relative line width.
            max_count = 1
            for key in d_edges.keys():
                for i in range(len(d_edges[key])):
                    if int(input1) == d_edges[key][i][2]:
                        if len(d_edges[key]) > max_count:
                            max_count = len(d_edges[key])
            for key in d_edges.keys():
                len_d_edges_filtered = v_max_lwidth / v_steps / 4
                for i in range(len(d_edges[key])):
                    if int(input1) == d_edges[key][i][2]:
                        len_d_edges_filtered += 1
                a = key.split("___")
                tmp_weight = (len_d_edges_filtered/max_count) // (1/v_steps) \
                    * (v_max_lwidth / v_steps)
                # Set line width to one quarter, if edge is not used at all.
                if len_d_edges_filtered == v_max_lwidth / v_steps / 4:
                    tmp_weight = v_max_lwidth / v_steps / 4
                # Set line width to one half, if line is used, but does not
                # meet minimum step size.
                elif tmp_weight == 0:
                    tmp_weight = v_max_lwidth / v_steps / 2
                graph_nx.add_edge(a[0], a[1], color="blue",
                                  weight=tmp_weight * v_resize)
        else:
            print("Warning - BVLAN {} not found, displaying unfiltered graph!".
                  format(input1))
            for key in d_edges.keys():
                len_d_edges_filtered = v_max_lwidth / v_steps / 4
                for i in range(len(d_edges[key])):
                    len_d_edges_filtered += 1
                a = key.split("___")
                tmp_weight = (len_d_edges_filtered/max_count) // (1/v_steps) \
                    * (v_max_lwidth / v_steps)
                if (tmp_weight == 0 and
                   len_d_edges_filtered == v_max_lwidth / v_steps / 4):
                    tmp_weight = v_max_lwidth / v_steps / 4
                elif tmp_weight == 0:
                    tmp_weight = v_max_lwidth / v_steps / 2
                graph_nx.add_edge(a[0], a[1], color="blue",
                                  weight=tmp_weight * v_resize)
    except Exception:
        for key in d_edges.keys():
            len_d_edges_filtered = v_max_lwidth / v_steps / 4
            for i in range(len(d_edges[key])):
                len_d_edges_filtered += 1
            a = key.split("___")
            tmp_weight = ((len_d_edges_filtered/max_count) //
                          (1/v_steps) * (v_max_lwidth / v_steps))
            if (tmp_weight == 0 and
               len_d_edges_filtered == v_max_lwidth / v_steps / 4):
                tmp_weight = v_max_lwidth / v_steps / 4
            elif tmp_weight == 0:
                tmp_weight = v_max_lwidth / v_steps / 2
            graph_nx.add_edge(a[0], a[1], color="blue",
                              weight=tmp_weight * v_resize)
    colors = nx.get_edge_attributes(graph_nx, "color").values()
    weights = nx.get_edge_attributes(graph_nx, "weight").values()
    nx.draw(graph_nx, dict_node_position, node_color="darkorchid",
            edge_color=colors, width=list(weights), with_labels="True")
    plt.show()
    reset_graph_nx()

@decorator_cls_and_return_mainmenu
def menu42():
    reset_graph_nx()
    input1 = input("Enter ISID to filter, or press enter for all ISIDs : ")
    print("===========================================")
    max_count = 1
    for key in d_edges_isid.keys():
        if len(d_edges_isid[key]) > max_count:
            max_count = len(d_edges_isid[key])
    try:
        if int(input1) in set_isids_in_use:
            # Set different max_count, if filter is applied:
            max_count = 1
            for key in d_edges_isid.keys():
                counter = 0
                for i in range(len(d_edges_isid[key])):
                    if int(input1) == d_edges_isid[key][i][3]:
                        counter = counter + 1
                if counter > max_count:
                    max_count = counter
            for key in d_edges_isid.keys():
                len_d_edges_filtered = v_max_lwidth / v_steps / 4
                for i in range(len(d_edges_isid[key])):
                    if int(input1) == d_edges_isid[key][i][3]:
                        len_d_edges_filtered += 1
                a = key.split("___")
                tmp_weight = ((len_d_edges_filtered/max_count) //
                              (1/v_steps) * (v_max_lwidth / v_steps))
                color_edge = "blue"
                # Set line width to one quarter, if edge is not used at all,
                # for filtered ISID.
                if len_d_edges_filtered == v_max_lwidth / v_steps / 4:
                    tmp_weight = v_max_lwidth / v_steps / 4
                    color_edge = "black"
                # Set line width to one half, if edge is used but,
                # does not hit minimum step size.
                elif tmp_weight == 0:
                    tmp_weight = v_max_lwidth / v_steps / 2
                graph_nx.add_edge(a[0], a[1], color=color_edge,
                                  weight=tmp_weight * v_resize)
        else:
            print("Warning - ISID {} not found, displaying unfiltered graph!"
                  .format(input1))
            for key in d_edges_isid.keys():
                len_d_edges_filtered = v_max_lwidth / v_steps / 4
                for i in range(len(d_edges_isid[key])):
                    len_d_edges_filtered += 1
                a = key.split("___")
                tmp_weight = ((len_d_edges_filtered/max_count) // (1/v_steps)
                              * (v_max_lwidth / v_steps))
                if (tmp_weight == 0 and
                   len_d_edges_filtered == v_max_lwidth / v_steps / 4):
                    tmp_weight = v_max_lwidth / v_steps / 4
                elif tmp_weight == 0:
                    tmp_weight = v_max_lwidth / v_steps / 2
                graph_nx.add_edge(a[0], a[1], color="blue",
                                  weight=tmp_weight * v_resize)
    except Exception:
        for key in d_edges.keys():
            len_d_edges_filtered = v_max_lwidth / v_steps / 4
            for i in range(len(d_edges_isid[key])):
                len_d_edges_filtered += 1
            a = key.split("___")
            tmp_weight = ((len_d_edges_filtered/max_count) //
                          (1/v_steps) * (v_max_lwidth / v_steps))
            if (tmp_weight == 0 and
               len_d_edges_filtered == v_max_lwidth / v_steps / 4):
                tmp_weight = v_max_lwidth / v_steps / 4
            elif tmp_weight == 0:
                tmp_weight = v_max_lwidth / v_steps / 2
            graph_nx.add_edge(a[0], a[1], color="blue",
                              weight=tmp_weight * v_resize)

    colors = nx.get_edge_attributes(graph_nx, "color").values()
    weights = nx.get_edge_attributes(graph_nx, "weight").values()
    nx.draw(graph_nx, dict_node_position, node_color="darkorchid",
            edge_color=colors, width=list(weights), with_labels="True")
    plt.show()
    reset_graph_nx()

@decorator_cls_and_return_mainmenu
def menu43():
    global v_resize
    print("Resizing factor less than 1 means, smaller lines.")
    print("Resizing facotor more than 1 means, bigger lines")
    print("------------------------------------------------")
    input_v_resize = input("Enter resizing factor (actual value: {}): "
                           .format(str(v_resize)))
    try:
        v_resize = float(input_v_resize)
    except Exception:
        print("Please enter valid numeric value between 0.01 and 100")

def menu_exit():
    os.system("exit")
    os.system("cls")

def main():

    # Assign dict with ECT to BVLAN.
    # (assuming BVLAN 4000 has ECT#1 and 4015 has ECT#15).
    global d_ect  # Global declaration of dict, as also accessed by other cls.
    d_ect = {"4000": b"\x00",
             "4001": b"\xff",
             "4002": b"\x88",
             "4003": b"\x77",
             "4004": b"\x44",
             "4005": b"\x33",
             "4006": b"\xcc",
             "4007": b"\xbb",
             "4008": b"\x22",
             "4009": b"\x11",
             "4010": b"\x66",
             "4011": b"\x55",
             "4012": b"\xaa",
             "4013": b"\x99",
             "4014": b"\xdd",
             "4015": b"\xee",
             }
    # Add experimental ECT ID´s, for calculating max possible path diversity.
    # Yeah right, VLAN´s > 4096 doesn´t exist - but that doesn´t matter here.
    for x in range(4016, 4256, 1):
        ectvalue = hex(x-4000)  # ECT value as string.
        ectvalue_hex = ectvalue.replace("0x", "")
        d_ect[str(x)] = bytes.fromhex(ectvalue_hex)

    # GraphView_v1
    global v_resize, v_steps, v_max_lwidth
    v_resize = 1
    v_steps = 30
    v_max_lwidth = 15
    global d_ect_path
    d_ect_path = {}
    global d_node
    global d_edges_weight
    d_edges_weight = {}
    # v3_0: Create d_edges_isid, to count ISID´s using an edge.
    global d_edges_isid
    d_edges_isid = {}
    global d_edges
    d_edges = {}
    global d_node_xor
    d_node_xor = {}
    # v2_0: Statistics, on which byte a tiebreaker was found for ECT.
    global d_tiebreaker_byte
    d_tiebreaker_byte = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}
    # To speed up computation in large networks, reduce here the number of
    # BVLANs to be tested (range is "low_bvl_to_test" - "num_bv_to_test" ).
    global max_bvl_to_test
    # max_bvl_to_test should be your highest BVLAN plus one.
    max_bvl_to_test = 4016
    global low_bvl_to_test
    low_bvl_to_test = 4000
    # Plausibility checks, for correct BVLAN range.
    assert max_bvl_to_test > low_bvl_to_test, "Please correct \
max_bvl_to_test and low_bvl_to_test. max value must be larger than low value!"
    assert 4256 >= max_bvl_to_test >= 4000, "Please correct \
max_bvl_to_test! Specify value between 4000 and 4256"
    assert 4256 >= low_bvl_to_test >= 4000, "Please correct \
low_bvl_to_test! Specify value between 4000 and 4256"

    # Plausibility check on d_node.
    # Check for unidirectional links and cost mismatch.
    for i in d_node:
        for j in d_node[i]["Links"]:
            unidirectional_link_check = True
            link_cost_mismatch = True
            for k in d_node[j[0]]["Links"]:
                if i == k[0]:
                    unidirectional_link_check = False
                if j[1] == k[1]:
                    link_cost_mismatch = False
            assert unidirectional_link_check is False,\
                "Unidirectional Link found on Node {}!".format(i)
            assert link_cost_mismatch is False,\
                "Link cost mismatch found on Node {}!".format(i)

    # Check for ISID/BVL mismatch and same BridgeID.
    for i in d_node:
        for isid in d_node[i]["ISID"]:
            check_isid = isid[0]
            check_bvl = isid[1]
            for j in d_node:
                if j != i:
                    assert d_node[i]["BridgeID"] != d_node[j]["BridgeID"],\
                     "Same BridgeID found on Node {} and {}!".format(i, j)
                for isid in d_node[j]["ISID"]:
                    if isid[0] == check_isid:
                        assert check_bvl == isid[1],\
                             "Wrong ISID/BVLAN relation on Node {}!".format(j)

    """ Modify your SPB network here to test behavior
    with more ISID`s / BVLAN`s etc. """

    # Temporary modification of d_node (i.e. to test more ISID`s):
    #for k in d_node.keys():
    # for i in range(1000):
    #    d_node["DC1_L1"]["ISID"].append([100000+i, 4000 + i % 8, 0, 0])
    #    d_node["DC2_L1"]["ISID"].append([100000+i, 4000 + i % 8, 0, 0])
    # d_node["DC1_L1"]["ISID"].append([8000, 4008, 0, 0])
    # d_node["DC2_L1"]["ISID"].append([8000, 4008, 0, 0])
    # d_node["DC4_L2"]["ISID"].append([8000, 4008, 0, 0])
    # d_node["DC3_SS1"]["ISID"].append([8000, 4008, 0, 0])
    #       d_node["Node-5"]["ISID"].append([8002, 4008, 1, 1])
    #       d_node["Node-5"]["ISID"].append([8003, 4008, 1, 1])
    #       d_node["Node-5"]["ISID"].append([8004, 4008, 1, 1])
    #       d_node["Node-5"]["ISID"].append([8005, 4008, 1, 1])
    #       d_node["Node-5"]["ISID"].append([8006, 4008, 1, 1])
    #       d_node["Node-5"]["ISID"].append([8007, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8000, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8001, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8002, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8003, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8004, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8005, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8006, 4008, 1, 1])
    #       d_node["Node-3"]["ISID"].append([8007, 4008, 1, 1])

    # Remove "off" nodes from dict.
    list_keys_to_remove = []
    for k in d_node:
        if d_node.get(k)["SystemState"] != "on":
            list_keys_to_remove.append(k)
    for k in range(len(list_keys_to_remove)):
        d_node.pop(list_keys_to_remove[k])

    # v2_0: Populate d_node_xor, results of xor operation of all BVLANs.
    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        d_node_xor[bvl] = {}
        for k in d_node:
            d_node_xor[bvl][k] = []

    for bvl in range(low_bvl_to_test, max_bvl_to_test):
        for k in d_node:
            # Populate dict with xor result of SystemID.
            for i in range(8):
                d_node_xor[bvl][k].append([byte_xor(
                    d_node.get(k)["BridgeID"][i][0], d_ect.get(str(bvl)))])

    # Get maximum string lenght of node names.
    global max_nodename
    max_nodename = 0
    for k in d_node.keys():
        if len(k) > max_nodename:
            max_nodename = len(k)

    # v4_0: Get a set of all BVLANs and ISIDs in loaded dict,
    # for having better display filter possibilites.
    global set_bvls_in_use
    global set_isids_in_use
    set_bvls_in_use = set()
    set_isids_in_use = set()
    for k in d_node:
        for i in range(len(d_node.get(k)["ISID"])):
            set_bvls_in_use.add(d_node.get(k)["ISID"][i][1])
            set_isids_in_use.add(d_node.get(k)["ISID"][i][0])

    # Create Graph for networksx
    global graph_nx
    graph_nx = nx.Graph()

    # Plausibility check, that all used BVLANs are calculated.
    for i in set_bvls_in_use:
        assert i in range(low_bvl_to_test, max_bvl_to_test), "Please correct \
max_bvl_to_test and low_bvl_to_test! BVLAN " + str(i) + " is used, bot not \
in that range!"

    # GraphView_v1_1 and View_v5 plausibility check, that no node
    # has duplicate ISIDs.
    for k in d_node:
        for x in range(len(d_node[k]["ISID"])):
            dup_counter = 0
            for y in range(len(d_node[k]["ISID"])):
                if d_node[k]["ISID"][x][0] == d_node[k]["ISID"][y][0]:
                    dup_counter += 1
                assert dup_counter <= 1, "Error, Node {} has ISID {} configured \
multiple times!".format(k, d_node[k]["ISID"][x][0])

    # Create Graph and add all edges to d_edges, d_edges_isid,
    # and d_edges_weight.
    graph = Graph()
    for k in d_node:
        for i in range(len(d_node.get(k)["Links"])):
            if d_node.get(k)["SystemState"] == "on":
                # Only if node v is active (is a key in the d_node).
                a = d_node.get(k)["Links"][i][0]
                if a in d_node.keys():
                    graph.add_edge(k, d_node.get(k)["Links"][i][0],
                                   d_node.get(k)["Links"][i][1])
                    key_d_edges = str(k) + "___" + str(a)
                    d_edges[key_d_edges] = []
                    d_edges_isid[key_d_edges] = []
                    d_edges_weight[key_d_edges] = d_node.get(k)["Links"][i][1]
                    graph_nx.add_edge(k, d_node.get(k)["Links"][i][0],
                                      weight=d_node.get(k)["Links"][i][1] / 10,
                                      color="black")

    global d_dijkstra  # Global declaration of dict, as accessed by other fct.
    d_dijkstra = {}  # Set every node as S.
    start_time_dijkstra = time.time()
    for n in d_node:
        d_dijkstra[n] = {}  # Initialize dict.
        for bvl in range(low_bvl_to_test, max_bvl_to_test):
            dijkstra = DijkstraSPF(graph, n, str(bvl))
            d_dijkstra[n][bvl] = dijkstra
    global time_apsp_calculation
    time_apsp_calculation = time.time() - start_time_dijkstra

    # Populate d_edges.
    for src, dst, bvl in product(d_node, d_node, set_bvls_in_use):
        for i in range(len(d_dijkstra[src][bvl].get_path(dst))):
            if len(d_dijkstra[src][bvl].get_path(dst)) > 0 and \
                  i < len(d_dijkstra[src][bvl].get_path(dst))-1:
                key_d_edges = d_dijkstra[src][bvl].get_path(dst)[i] + "___" +\
                              d_dijkstra[src][bvl].get_path(dst)[i+1]
                if key_d_edges in d_edges.keys():
                    d_edges[key_d_edges].append([src, dst, bvl])

    # Calculate tandem multicast mode.
    start_time_mcast = time.time()
    d_tandem_isid = {}
    set_bvl = set()
    d_tandem_mcast_src = {}
    d_tandem_mcast_rcv = {}
    global d_mcast_states
    d_mcast_states = {}  # End results to be displayed per node or in total.
    for node in d_node:
        d_mcast_states[node] = []

    # Populate d_tandem_mcast_src.
    for k in d_node:
        for i in range(len(d_node.get(k)["ISID"])):
            if d_node.get(k)["ISID"][i][2] == 1:  # Check T Bit of ISID.
                # Create the dict for MCSource and bvl.
                if k in d_tandem_mcast_src.keys():
                    d_tandem_mcast_src[k].\
                        append(d_node.get(k)["ISID"][i][0])
                else:
                    d_tandem_mcast_src[k] = \
                        [d_node.get(k)["ISID"][i][0]]
                if d_node.get(k)["ISID"][i][0] not in d_tandem_isid.keys():
                    d_tandem_isid[d_node.get(k)["ISID"][i][0]] = \
                                    [d_node.get(k)["ISID"][i][1]]
                    set_bvl.add(d_node.get(k)["ISID"][i][1])
                else:
                    pass  # Only add Tandem ISID to dict, if key not yet exist.

    # Populate d_tandem_mcast_rcv.
    for k in d_node:
        for i in range(len(d_node.get(k)["ISID"])):
            if d_node.get(k)["ISID"][i][3] == 1:  # Check R Bit of ISID.
                if k in d_tandem_mcast_rcv.keys():
                    d_tandem_mcast_rcv[k].append(d_node.get(k)["ISID"][i][0])
                else:
                    d_tandem_mcast_rcv[k] = \
                     [d_node.get(k)["ISID"][i][0]]
                if d_node.get(k)["ISID"][i][0] not in d_tandem_isid.keys():
                    d_tandem_isid[d_node.get(k)["ISID"][i][0]] = \
                     [d_node.get(k)["ISID"][i][1]]
                    set_bvl.add(d_node.get(k)["ISID"][i][1])
                else:
                    pass  # Only add Tandem ISID to dict, if key not yet exist.

    # Calculate MC States, for every node, in every BVLAN.
    for node, mc_src_a in product(d_node, d_tandem_mcast_src):
        for isid in d_tandem_mcast_src[mc_src_a]:  # For every isid.
            for bvl, mc_rcv_b in product(
             d_tandem_isid[isid], d_tandem_mcast_rcv):
                if isid in d_tandem_mcast_rcv[mc_rcv_b] and node in \
                 d_dijkstra[mc_src_a][bvl].get_path(mc_rcv_b)[1:-1]:
                    pos_node_path = d_dijkstra[mc_src_a][bvl].get_path(
                     mc_rcv_b).index(node)
                    iil = d_dijkstra[mc_src_a][bvl].\
                        get_path(mc_rcv_b)[pos_node_path-1]
                    oil = d_dijkstra[mc_src_a][bvl].\
                        get_path(mc_rcv_b)[pos_node_path+1]
                    d_mcast_states[node].append(
                        [mc_src_a, isid, bvl, iil, oil])
                elif isid in d_tandem_mcast_rcv[mc_rcv_b] and node in \
                    d_dijkstra[mc_src_a][bvl].get_path(mc_rcv_b)[0] and \
                        len(d_dijkstra[mc_src_a][bvl].get_path(mc_rcv_b)) > 1:
                    pos_node_path = d_dijkstra[mc_src_a][bvl].get_path(
                        mc_rcv_b).index(node)
                    iil = "-"  # Node is START of path.
                    oil = d_dijkstra[mc_src_a][bvl].\
                        get_path(mc_rcv_b)[pos_node_path+1]
                    d_mcast_states[node].append(
                     [mc_src_a, isid, bvl, iil, oil])
                elif isid in d_tandem_mcast_rcv[mc_rcv_b] and node in \
                    d_dijkstra[mc_src_a][bvl].get_path(
                     mc_rcv_b)[-1] and len(d_dijkstra[mc_src_a][bvl].get_path(
                         mc_rcv_b)) > 1:
                    pos_node_path = d_dijkstra[mc_src_a][bvl].get_path(
                        mc_rcv_b).index(node)
                    iil = d_dijkstra[mc_src_a][bvl].get_path(
                        mc_rcv_b)[pos_node_path-1]
                    oil = "-"  # Node is END of path.
                    d_mcast_states[node].append(
                        [mc_src_a, isid, bvl, iil, oil])
    global time_mcast_calculation
    time_mcast_calculation = time.time() - start_time_mcast

    # Consolidate OIL of d_mcast_states.
    # First step, remove entry`s with empty OIL, if:
    #    there are also entry´s with an OIL
    # That is the case, if node is receiver and also forkout point.

    for k in d_mcast_states.keys():
        mcast_states_to_delete = []  # Empty initialize for all nodes:
        for i in range(len(d_mcast_states[k])):
            if d_mcast_states[k][i][4] == "-":  # Check if node has emtpy OIL.
                # If so, check if node is also forkout point.
                for j in range(len(d_mcast_states[k])):
                    if d_mcast_states[k][i][0] == d_mcast_states[k][j][0] and \
                       d_mcast_states[k][i][1] == d_mcast_states[k][j][1] and \
                       d_mcast_states[k][i][2] == d_mcast_states[k][j][2] and \
                       d_mcast_states[k][i][3] == d_mcast_states[k][j][3] and \
                       d_mcast_states[k][i][4] != d_mcast_states[k][j][4]:
                        # Add entry with empty OIL to delete list.
                        mcast_states_to_delete.append(d_mcast_states[k][i])

        for x in range(len(mcast_states_to_delete)):
            if mcast_states_to_delete[x] in d_mcast_states[k]:
                d_mcast_states[k].remove(mcast_states_to_delete[x])

    # Second step, if OIL is not empty and has several entries - merge them.
    # That is the case, if node is forkout point with several OIL`s or sender.

    for k in d_mcast_states.keys():
        mcast_states_to_delete = []  # Empty initialize for all nodes:
        for i in range(len(d_mcast_states[k])):
            # Initialize the OIL that will be applied to:
            #     mcast state after iteration.
            new_oil = d_mcast_states[k][i][4]
            # Check that entry was not already checked.
            if d_mcast_states[k][i] not in mcast_states_to_delete:
                # Check if node has OIL (is a forkout point).
                if d_mcast_states[k][i][4] != "-":
                    for j in range(i+1, len(d_mcast_states[k])):
                        if d_mcast_states[k][i][0] == \
                            d_mcast_states[k][j][0] and \
                           d_mcast_states[k][i][1] == \
                            d_mcast_states[k][j][1] and \
                           d_mcast_states[k][i][2] == \
                            d_mcast_states[k][j][2] and \
                           d_mcast_states[k][i][3] == \
                           d_mcast_states[k][j][3]:
                            # Add duplicate entry to delete list.
                            mcast_states_to_delete.append(d_mcast_states[k][j])
                            # If OIL not already in OIL -> merge it.
                            if d_mcast_states[k][j][4] not in new_oil:
                                new_oil += ", " + d_mcast_states[k][j][4]
            # Finally, assign new OIL to mcast entry.
            d_mcast_states[k][i][4] = new_oil

        for x in range(len(mcast_states_to_delete)):
            if mcast_states_to_delete[x] in d_mcast_states[k]:
                d_mcast_states[k].remove(mcast_states_to_delete[x])

    #  Population of d_ect_path.
    for a in d_node:
        for b in d_node:
            k = len(get_max_possible_path_a_to_b(a, b))  # Get number of hops.
            if k in d_ect_path.keys():
                if (a + " -> " + b) not in d_ect_path[k] and a != b:
                    d_ect_path[k].append(a + " -> " + b)
                else:
                    pass  # Path already exist for this key.
            elif a != b:
                d_ect_path[k] = [a + " -> " + b]

    # v3_0: Populate d_edges_isid.
    # For every edge:
    for k in d_edges.keys():
        # For every connection of an edge:
        for i in range(len(d_edges[k])):
            # For all ISIDs on source:
            for i_src in range(len(d_node[d_edges[k][i][0]]["ISID"])):
                # If ISID is on BVLAN of connection:
                if d_edges[k][i][2] == \
                   d_node[d_edges[k][i][0]]["ISID"][i_src][1]:
                    # For all ISIDs on destination:
                    for i_dst in range(len(
                                 d_node[d_edges[k][i][1]]["ISID"])):
                        # Check if ISID is on source AND destination:
                        # If yes: Add connection to "d_edges_isid".
                        if d_node[d_edges[k][i][0]]["ISID"][i_src][0] == \
                           d_node[d_edges[k][i][1]]["ISID"][i_dst][0]:
                            d_edges_isid[k].append(
                                [d_edges[k][i][0], d_edges[k][i][1],
                                 d_node[d_edges[k][i][0]]["ISID"][i_src][1],
                                 d_node[d_edges[k][i][0]]["ISID"][i_src][0]])
    # START -> Graph Positioning:
    global dict_node_position
    dict_node_position = {"DC1_L1": [0.5, -1.5],
                          "DC1_L2": [1.9, -1.5],
                          "DC1_S10": [0.2, -1.0],
                          "DC1_S11": [0.4, -1.0],
                          "DC1_S12": [0.6, -1.0],
                          "DC1_S13": [0.8, -1.0],
                          "DC1_S20": [1.6, -1.0],
                          "DC1_S21": [1.8, -1.0],
                          "DC1_S22": [2.0, -1.0],
                          "DC1_S23": [2.2, -1.0],
                          "DC1_SS1": [0.5, -0.5],
                          "DC1_SS2": [0.7, -0.5],
                          "DC1_SS3": [0.9, -0.5],
                          "DC1_SS4": [1.1, -0.5],
                          "DC1_SS5": [1.3, -0.5],
                          "DC1_SS6": [1.5, -0.5],
                          "DC1_SS7": [1.7, -0.5],
                          "DC1_SS8": [1.9, -0.5],
                          "DCIX_11": [1.0, 0.0],
                          "DCIX_12": [1.4, 0.0],
                          "DCIX_21": [1.0, 0.5],
                          "DCIX_22": [1.4, 0.5],
                          "DC2_L1": [0.5, 2.0],
                          "DC2_L2": [1.9, 2.0],
                          "DC2_S10": [0.2, 1.5],
                          "DC2_S11": [0.4, 1.5],
                          "DC2_S12": [0.6, 1.5],
                          "DC2_S13": [0.8, 1.5],
                          "DC2_S20": [1.6, 1.5],
                          "DC2_S21": [1.8, 1.5],
                          "DC2_S22": [2.0, 1.5],
                          "DC2_S23": [2.2, 1.5],
                          "DC2_SS1": [0.5, 1.0],
                          "DC2_SS2": [0.7, 1.0],
                          "DC2_SS3": [0.9, 1.0],
                          "DC2_SS4": [1.1, 1.0],
                          "DC2_SS5": [1.3, 1.0],
                          "DC2_SS6": [1.5, 1.0],
                          "DC2_SS7": [1.7, 1.0],
                          "DC2_SS8": [1.9, 1.0]}
    # Add default position, if node is not in dict_node_position:
    for node in d_node:
        if node not in dict_node_position:
            dict_node_position[node] = [0.2, 0.2]
    # END -> Graph Positioning:

    global colors, weights
    colors = nx.get_edge_attributes(graph_nx, "color").values()
    weights = nx.get_edge_attributes(graph_nx, "weight").values()
    main_menu()

if __name__ == "__main__":
    main()

reason: import networkx


text:
# Library Imports
import os
import numpy as np
import matplotlib.pyplot as plt

# Import Data
avg = np.load(os.getcwd()+'/MultiAgentProfiling/data/agent1_avglog.npy')
sum = np.load(os.getcwd()+'/MultiAgentProfiling/data/agent1_scorelog.npy')

#Plot and Save the Graphs
plt.figure(1)
plt.plot(sum, color='red', alpha=0.2, label='Summed Rewards')
plt.plot(avg, color='red', label='Average Rewards')
plt.grid(True)
plt.xlabel('Episodes')
plt.ylabel('Rewards')
plt.title("'agent1' Training Profile")
plt.legend(loc='best')
plt.savefig(os.getcwd()+'/MultiAgentProfiling/data/agent1 Training Profile.png')

avg = np.load(os.getcwd()+'/MultiAgentProfiling/data/agent2_avglog.npy')
sum = np.load(os.getcwd()+'/MultiAgentProfiling/data/agent2_scorelog.npy')

#Plot and Save the Graphs
plt.figure(2)
plt.plot(sum, color='green', alpha=0.2, label='Summed Rewards')
plt.plot(avg, color='green', label='Average Rewards')
plt.grid(True)
plt.xlabel('Episodes')
plt.ylabel('Rewards')
plt.title("'agent2' Training Profile")
plt.legend(loc='best')
plt.savefig(os.getcwd()+'/MultiAgentProfiling/data/agent2 Training Profile.png')

avg = np.load(os.getcwd()+'/MultiAgentProfiling/data/agent3_avglog.npy')
sum = np.load(os.getcwd()+'/MultiAgentProfiling/data/agent3_scorelog.npy')

#Plot and Save the Graphs
plt.figure(3)
plt.plot(sum, color='blue', alpha=0.2, label='Summed Rewards')
plt.plot(avg, color='blue', label='Average Rewards')
plt.grid(True)
plt.xlabel('Episodes')
plt.ylabel('Rewards')
plt.title("'agent3' Training Profile")
plt.legend(loc='best')
plt.savefig(os.getcwd()+'/MultiAgentProfiling/data/agent3 Training Profile.png')


text:
# -*- coding: utf-8 -*-
"""names_orig_spacy.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/15sQmoJNlMVMLkh9vl8eQk3meZ9V6L6mV
"""

TRAIN_DATA=[('Shri Narendra Modi,Ministry of Personnel, Public Grievances and PensionsDepartment of Atomic EnergyDepartment of Space', {'entities': [(19, 118, 'Label')]}), ('Shri Raj Nath Singh,Ministry of Defence  Contact Facebook Account Twitter Account', {'entities': [(20, 39, 'Label')]}), ('Shri Amit Shah,Ministry of Home Affairs  Contact Facebook Account Twitter Account', {'entities': [(15, 39, 'Label')]}), ('Ministry of Home Affairs  Contact Facebook Account Twitter Account,Shri Amit Shah', {'entities': [(0, 24, 'Label')]}), ('Shri Nitin Jairam Gadkari,Ministry of Road Transport and HighwaysMinistry of Micro, Small and Medium Enterprises  Contact Facebook Account Twitter Account', {'entities': [(26, 112, 'Label')]}), ('Ministry of Road Transport and HighwaysMinistry of Micro, Small and Medium Enterprises  Contact Facebook Account Twitter Account,Shri Nitin Jairam Gadkari', {'entities': [(0, 87, 'Label')]}), ('Shri D.V. Sadananda Gowda,Ministry of Chemicals and Fertilizers  Contact Facebook Account Twitter Account', {'entities': [(26, 63, 'Label')]}), ('Ministry of Chemicals and Fertilizers  Contact Facebook Account Twitter Account,Shri D.V. Sadananda Gowda', {'entities': [(0, 37, 'Label')]}), ('Smt. Nirmala Sitharaman,Ministry of FinanceMinistry of Corporate Affairs  Contact Facebook Account Twitter Account', {'entities': [(24, 72, 'Label')]}), ('Ministry of FinanceMinistry of Corporate Affairs  Contact Facebook Account Twitter Account,Smt. Nirmala Sitharaman', {'entities': [(0, 48, 'Label')]}), ('Shri Ramvilas Paswan,Ministry of Consumer Affairs, Food and Public Distribution  Contact Facebook Account Twitter Account', {'entities': [(21, 80, 'Label')]}), ('Ministry of Consumer Affairs, Food and Public Distribution  Contact Facebook Account Twitter Account,Shri Ramvilas Paswan', {'entities': [(0, 59, 'Label')]}), ('Shri Narendra Singh Tomar,Ministry of Agriculture & Farmers WelfareMinistry of Rural DevelopmentMinistry of Panchayati Raj  Contact Facebook Account Twitter Account', {'entities': [(26, 122, 'Label')]}), ('Ministry of Agriculture & Farmers WelfareMinistry of Rural DevelopmentMinistry of Panchayati Raj  Contact Facebook Account Twitter Account,Shri Narendra Singh Tomar', {'entities': [(0, 96, 'Label')]}), ('Shri Ravi Shankar Prasad,Ministry of Law and JusticeMinistry of CommunicationsMinistry of Electronics and Information Technology  Contact Facebook Account Twitter Account', {'entities': [(25, 128, 'Label')]}), ('Ministry of Law and JusticeMinistry of CommunicationsMinistry of Electronics and Information Technology  Contact Facebook Account Twitter Account,Shri Ravi Shankar Prasad', {'entities': [(0, 103, 'Label')]}), ('Smt. Harsimrat Kaur Badal,Ministry of Food Processing Industries  Contact Facebook Account Twitter Account', {'entities': [(26, 64, 'Label')]}), ('Ministry of Food Processing Industries  Contact Facebook Account Twitter Account,Smt. Harsimrat Kaur Badal', {'entities': [(0, 38, 'Label')]}), ('Shri Thaawar Chand Gehlot,Ministry of Social Justice and Empowerment  Contact Facebook Account Twitter Account', {'entities': [(26, 68, 'Label')]}), ('Ministry of Social Justice and Empowerment  Contact Facebook Account Twitter Account,Shri Thaawar Chand Gehlot', {'entities': [(0, 42, 'Label')]}), ('Dr. Subrahmanyam Jaishankar,Ministry of External Affairs  Contact Twitter Account', {'entities': [(28, 56, 'Label')]}), ('Ministry of External Affairs  Contact Twitter Account,Dr. Subrahmanyam Jaishankar', {'entities': [(0, 29, 'Label')]}), ("Shri Ramesh Pokhriyal 'Nishank',Ministry of Education  Contact Facebook Account Twitter Account", {'entities': [(32, 53, 'Label')]}), ("Ministry of Education  Contact Facebook Account Twitter Account,Shri Ramesh Pokhriyal 'Nishank'", {'entities': [(0, 22, 'Label')]}), ('Shri Arjun Munda,Ministry of Tribal Affairs  Contact Facebook Account Twitter Account', {'entities': [(17, 43, 'Label')]}), ('Ministry of Tribal Affairs  Contact Facebook Account Twitter Account,Shri Arjun Munda', {'entities': [(0, 27, 'Label')]}), ('Smt. Smriti Zubin Irani,Ministry of Women and Child DevelopmentMinistry of Textiles  Contact Facebook Account Twitter Account', {'entities': [(24, 83, 'Label')]}), ('Ministry of Women and Child DevelopmentMinistry of Textiles  Contact Facebook Account Twitter Account,Smt. Smriti Zubin Irani', {'entities': [(0, 59, 'Label')]}), ('Dr. Harsh Vardhan,Ministry of Health and Family WelfareMinistry of Science and TechnologyMinistry of Earth Sciences  Contact Facebook Account Twitter Account', {'entities': [(18, 115, 'Label')]}), ('Ministry of Health and Family WelfareMinistry of Science and TechnologyMinistry of Earth Sciences  Contact Facebook Account Twitter Account,Dr. Harsh Vardhan', {'entities': [(0, 98, 'Label')]}), ('Shri Prakash Javadekar,Ministry of Environment, Forest and Climate ChangeMinistry of Information and BroadcastingMinistry of Heavy Industries and Public Enterprises  Contact Facebook Account Twitter Account', {'entities': [(23, 165, 'Label')]}), ('Ministry of Environment, Forest and Climate ChangeMinistry of Information and BroadcastingMinistry of Heavy Industries and Public Enterprises  Contact Facebook Account Twitter Account,Shri Prakash Javadekar', {'entities': [(0, 141, 'Label')]}), ('Shri Piyush Goyal,Ministry of RailwaysMinistry of Commerce and Industry  Contact Facebook Account Twitter Account', {'entities': [(18, 72, 'Label')]}), ('Ministry of RailwaysMinistry of Commerce and Industry  Contact Facebook Account Twitter Account,Shri Piyush Goyal', {'entities': [(0, 53, 'Label')]}), ('Shri Dharmendra Pradhan,Ministry of Petroleum and Natural GasMinistry of Steel  Contact Facebook Account Twitter Account', {'entities': [(24, 78, 'Label')]}), ('Ministry of Petroleum and Natural GasMinistry of Steel  Contact Facebook Account Twitter Account,Shri Dharmendra Pradhan', {'entities': [(0, 54, 'Label')]}), ('Shri Mukhtar Abbas Naqvi,Ministry of Minority Affairs  Contact Facebook Account Twitter Account', {'entities': [(25, 53, 'Label')]}), ('Ministry of Minority Affairs  Contact Facebook Account Twitter Account,Shri Mukhtar Abbas Naqvi', {'entities': [(0, 28, 'Label')]}), ('Shri Pralhad Joshi,Ministry of Parliamentary AffairsMinistry of CoalMinistry of Mines  Contact Facebook Account', {'entities': [(19, 85, 'Label')]}), ('Ministry of Parliamentary AffairsMinistry of CoalMinistry of Mines  Contact Facebook Account,Shri Pralhad Joshi', {'entities': [(0, 66, 'Label')]}), ('Dr. Mahendra Nath Pandey,Ministry of Skill Development and Entrepreneurship  Contact Facebook Account Twitter Account', {'entities': [(25, 76, 'Label')]}), ('Ministry of Skill Development and Entrepreneurship  Contact Facebook Account Twitter Account,Dr. Mahendra Nath Pandey', {'entities': [(0, 50, 'Label')]}), ('Shri Giriraj Singh,Ministry of Animal Husbandry, Dairying and Fisheries  Contact Facebook Account Twitter Account', {'entities': [(19, 71, 'Label')]}), ('Ministry of Animal Husbandry, Dairying and Fisheries  Contact Facebook Account Twitter Account,Shri Giriraj Singh', {'entities': [(0, 52, 'Label')]}), ('Shri Gajendra Singh Shekhawat,Ministry of Jal Shakti  Contact Facebook Account Twitter Account', {'entities': [(30, 52, 'Label')]}), ('Ministry of Jal Shakti  Contact Facebook Account Twitter Account,Shri Gajendra Singh Shekhawat', {'entities': [(0, 22, 'Label')]}), (',Ministry of Labour and Employment', {'entities': [(1, 34, 'Label')]}), ('Ministry of Labour and Employment,', {'entities': [(0, 33, 'Label')]}), ('Shri Santosh Kumar Gangwar,Ministry of Statistics and Programme ImplementationMinistry of Planning', {'entities': [(27, 98, 'Label')]}), ('Ministry of Statistics and Programme ImplementationMinistry of Planning,Shri Santosh Kumar Gangwar', {'entities': [(0, 71, 'Label')]}), ('Shri Rao Inderjit Singh,Ministry of Ayurveda, Yoga and Naturopathy, Unani, Siddha and Homoeopathy (AYUSH)', {'entities': [(24, 105, 'Label')]}), ('Ministry of Ayurveda, Yoga and Naturopathy, Unani, Siddha and Homoeopathy (AYUSH),Shri Rao Inderjit Singh', {'entities': [(0, 81, 'Label')]}), ('Shri Shripad Yesso Naik,Ministry of Development of North Eastern Region', {'entities': [(24, 71, 'Label')]}), ('Ministry of Development of North Eastern Region,Shri Shripad Yesso Naik', {'entities': [(0, 47, 'Label')]}), ('Dr. Jitendra Singh,Ministry of Youth Affairs and Sports', {'entities': [(19, 55, 'Label')]}), ('Ministry of Youth Affairs and Sports,Dr. Jitendra Singh', {'entities': [(0, 36, 'Label')]}), ('Shri Kiren Rijiju,Ministry of CultureMinistry of Tourism', {'entities': [(18, 56, 'Label')]}), ('Ministry of CultureMinistry of Tourism,Shri Kiren Rijiju', {'entities': [(0, 38, 'Label')]}), ('Shri Prahalad Singh Patel,Ministry of PowerMinistry of New and Renewable Energy', {'entities': [(26, 79, 'Label')]}), ('Ministry of PowerMinistry of New and Renewable Energy,Shri Prahalad Singh Patel', {'entities': [(0, 53, 'Label')]}), ('Shri Raj Kumar Singh,Ministry of Housing and Urban AffairsMinistry of Civil Aviation', {'entities': [(21, 84, 'Label')]}), ('Ministry of Housing and Urban AffairsMinistry of Civil Aviation,Shri Raj Kumar Singh', {'entities': [(0, 63, 'Label')]}), ('Shri Hardeep Singh Puri,Ministry of Shipping', {'entities': [(24, 44, 'Label')]}), ('Ministry of Shipping,Shri Hardeep Singh Puri', {'entities': [(0, 20, 'Label')]}), ('Shri Mansukh L. Mandaviya,Ministry of Defence', {'entities': [(26, 45, 'Label')]}), ('Ministry of Defence,Shri Mansukh L. Mandaviya', {'entities': [(0, 19, 'Label')]}), (",Prime Minister's OfficeMinistry of Personnel, Public Grievances and PensionsDepartment of Atomic EnergyDepartment of Space", {'entities': [(24, 123, 'Label')]}), ("Prime Minister's OfficeMinistry of Personnel, Public Grievances and PensionsDepartment of Atomic EnergyDepartment of Space,", {'entities': [(23, 122, 'Label')]}), ('Shri Shripad Yesso Naik,Ministry of Minority Affairs', {'entities': [(24, 52, 'Label')]}), ('Ministry of Minority Affairs,Shri Shripad Yesso Naik', {'entities': [(0, 28, 'Label')]}), ('Dr. Jitendra Singh,Ministry of Skill Development and Entrepreneurship', {'entities': [(19, 69, 'Label')]}), ('Ministry of Skill Development and Entrepreneurship,Dr. Jitendra Singh', {'entities': [(0, 50, 'Label')]}), ('Shri Kiren Rijiju,Ministry of Commerce and Industry', {'entities': [(18, 51, 'Label')]}), ('Ministry of Commerce and Industry,Shri Kiren Rijiju', {'entities': [(0, 33, 'Label')]}), ('Shri Raj Kumar Singh,Ministry of Chemicals and Fertilizers', {'entities': [(21, 58, 'Label')]}), ('Ministry of Chemicals and Fertilizers,Shri Raj Kumar Singh', {'entities': [(0, 37, 'Label')]}), ('Shri Hardeep Singh Puri,Ministry of Steel', {'entities': [(24, 41, 'Label')]}), ('Ministry of Steel,Shri Hardeep Singh Puri', {'entities': [(0, 17, 'Label')]}), ('Shri Mansukh L. Mandaviya,Ministry of Health and Family Welfare', {'entities': [(26, 63, 'Label')]}), ('Ministry of Health and Family Welfare,Shri Mansukh L. Mandaviya', {'entities': [(0, 37, 'Label')]}), ('Shri Faggansingh Kulaste,Ministry of Parliamentary AffairsMinistry of Heavy Industries and Public Enterprises', {'entities': [(25, 109, 'Label')]}), ('Ministry of Parliamentary AffairsMinistry of Heavy Industries and Public Enterprises,Shri Faggansingh Kulaste', {'entities': [(0, 84, 'Label')]}), ('Shri Ashwini Kumar Choubey,Ministry of Road Transport and Highways', {'entities': [(27, 66, 'Label')]}), ('Ministry of Road Transport and Highways,Shri Ashwini Kumar Choubey', {'entities': [(0, 39, 'Label')]}), ('Shri Arjun Ram Meghwal,Ministry of Social Justice and Empowerment', {'entities': [(23, 65, 'Label')]}), ('Ministry of Social Justice and Empowerment,Shri Arjun Ram Meghwal', {'entities': [(0, 42, 'Label')]}), ('General (Retd.) V.K. Singh,Ministry of Consumer Affairs, Food and Public Distribution', {'entities': [(27, 85, 'Label')]}), ('Ministry of Consumer Affairs, Food and Public Distribution,General (Retd.) V.K. Singh', {'entities': [(0, 58, 'Label')]}), ('Shri Krishan Pal,Ministry of Home Affairs', {'entities': [(17, 41, 'Label')]}), ('Ministry of Home Affairs,Shri Krishan Pal', {'entities': [(0, 24, 'Label')]}), ('Shri Danve Raosaheb Dadarao,Ministry of Agriculture & Farmers Welfare', {'entities': [(28, 69, 'Label')]}), ('Ministry of Agriculture & Farmers Welfare,Shri Danve Raosaheb Dadarao', {'entities': [(0, 41, 'Label')]}), ('Shri G. Kishan Reddy,Ministry of Social Justice and Empowerment', {'entities': [(21, 63, 'Label')]}), ('Ministry of Social Justice and Empowerment,Shri G. Kishan Reddy', {'entities': [(0, 42, 'Label')]}), ('Shri Parshottam Rupala,Ministry of Rural Development', {'entities': [(23, 52, 'Label')]}), ('Ministry of Rural Development,Shri Parshottam Rupala', {'entities': [(0, 29, 'Label')]}), ('Shri Ramdas Athawale,Ministry of Environment, Forest and Climate Change', {'entities': [(21, 71, 'Label')]}), ('Ministry of Environment, Forest and Climate Change,Shri Ramdas Athawale', {'entities': [(0, 50, 'Label')]}), ('Sadhvi Niranjan Jyoti,Ministry of Animal Husbandry, Dairying and Fisheries', {'entities': [(22, 74, 'Label')]}), ('Ministry of Animal Husbandry, Dairying and Fisheries,Sadhvi Niranjan Jyoti', {'entities': [(0, 52, 'Label')]}), ('Shri Babul Supriyo,Ministry of EducationMinistry of CommunicationsMinistry of Electronics and Information Technology', {'entities': [(19, 116, 'Label')]}), ('Ministry of EducationMinistry of CommunicationsMinistry of Electronics and Information Technology,Shri Babul Supriyo', {'entities': [(0, 97, 'Label')]}), ('Shri Sanjeev Kumar Balyan,Ministry of FinanceMinistry of Corporate Affairs', {'entities': [(26, 74, 'Label')]}), ('Ministry of FinanceMinistry of Corporate Affairs,Shri Sanjeev Kumar Balyan', {'entities': [(0, 48, 'Label')]}), ('Shri Dhotre Sanjay Shamrao,Ministry of Railways', {'entities': [(27, 47, 'Label')]}), ('Ministry of Railways,Shri Dhotre Sanjay Shamrao', {'entities': [(0, 20, 'Label')]}), ('Shri Anurag Singh Thakur,Ministry of Home Affairs', {'entities': [(25, 49, 'Label')]}), ('Ministry of Home Affairs,Shri Anurag Singh Thakur', {'entities': [(0, 24, 'Label')]}), ('Shri Angadi Suresh Channabasappa,Ministry of Jal ShaktiMinistry of Social Justice and Empowerment', {'entities': [(33, 97, 'Label')]}), ('Ministry of Jal ShaktiMinistry of Social Justice and Empowerment,Shri Angadi Suresh Channabasappa', {'entities': [(0, 64, 'Label')]}), ('Shri Nityanand Rai,Ministry of External AffairsMinistry of Parliamentary Affairs', {'entities': [(19, 80, 'Label')]}), ('Ministry of External AffairsMinistry of Parliamentary Affairs,Shri Nityanand Rai', {'entities': [(0, 61, 'Label')]}), ('Shri Rattan Lal Kataria,Ministry of Tribal Affairs', {'entities': [(24, 50, 'Label')]}), ('Ministry of Tribal Affairs,Shri Rattan Lal Kataria', {'entities': [(0, 26, 'Label')]}), ('Shri V. Muraleedharan,Ministry of Commerce and Industry', {'entities': [(22, 55, 'Label')]}), ('Ministry of Commerce and Industry,Shri V. Muraleedharan', {'entities': [(0, 33, 'Label')]}), ('Smt. Renuka Singh Saruta,Ministry of Food Processing Industries', {'entities': [(25, 63, 'Label')]}), ('Ministry of Food Processing Industries,Smt. Renuka Singh Saruta', {'entities': [(0, 38, 'Label')]}), ('Shri Som Parkash,Ministry of Micro, Small and Medium EnterprisesMinistry of Animal Husbandry, Dairying and Fisheries', {'entities': [(17, 116, 'Label')]}), ('Ministry of Micro, Small and Medium EnterprisesMinistry of Animal Husbandry, Dairying and Fisheries,Shri Som Parkash', {'entities': [(0, 99, 'Label')]}), ('Shri Rameswar Teli,Ministry of Agriculture & Farmers Welfare', {'entities': [(19, 60, 'Label')]}), ('Ministry of Agriculture & Farmers Welfare,Shri Rameswar Teli', {'entities': [(0, 41, 'Label')]}), ('Shri Pratap Chandra Sarangi,Ministry of Women and Child Development', {'entities': [(28, 67, 'Label')]}), ('Ministry of Women and Child Development,Shri Pratap Chandra Sarangi']

from spacy import displacy
import warnings
import spacy
import random
import time
import numpy as np
from spacy.util import minibatch, compounding
import os
from pathlib import Path
warnings.filterwarnings("ignore")
nlp = spacy.load('en')

test_sentences = [x[0] for x in TRAIN_DATA[0:15]] # extract the sentences from [sentence, entity]
for x in test_sentences:

    doc = nlp(x)
    #print(doc)
    displacy.render(doc, jupyter = True, style = "ent")
warnings.filterwarnings("default")

[x[0] for x in TRAIN_DATA[120:170]]

def get_names(t):
    doc = nlp(t)
    name=[]
    rest=[]
    #print("Entities", [(ent.text, ent.label_) for ent in doc.ents])
    for ent in doc.ents:
        if ent.label_=='PERSON':
            name.append(ent.text)

            return name
    return 0
get_names('Prime Minister of Europe')

output_dir = Path('/content/Model')
nlp.to_disk(output_dir)
print("Saved model to", output_dir)


text:
import os
import logging
import numpy as np
from PIL import Image
from PIL import ImageOps

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
tf.get_logger().setLevel(logging.ERROR)
from tensorflow.keras.utils import Sequence, to_categorical
from augmentation import augmentations

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

class DataGenerator(Sequence):
    def __init__(self,
                 data,
                 labels,
                 img_dim=(32, 32,3),
                 batch_size=32,
                 num_classes=10,
                 shuffle=True,
                 jsd=True
                ):

        self.data = data
        self.labels = labels
        self.img_dim = img_dim
        self.batch_size = batch_size
        self.num_classes = num_classes
        self.shuffle = shuffle
        self.jsd = jsd
        self.augmentations = augmentations
        self.on_epoch_end()

    def on_epoch_end(self):
        self.indices = np.arange(len(self.data))
        if self.shuffle:
            np.random.shuffle(self.indices)

    def apply_op(self, image, op, severity):
        image = np.clip(image * 255., 0, 255).astype(np.uint8)
        pil_img = Image.fromarray(image)  # Convert to PIL.Image
        pil_img = op(pil_img, severity)
        return np.asarray(pil_img).astype(np.float32) / 255.

    def augment_and_mix(self, image, severity=3, width=3, depth=-1, alpha=1.):
        """Perform AugMix augmentations and compute mixture.
        Args:
            image: Raw input image as ndarray shape (h, w, c)
            severity: Severity of underlying augmentation operators (1-10).
            width: Width of augmentation chain
            depth: Depth of augmentation chain. -1 or (1, 3)
            alpha: Probability coefficient for Beta and Dirichlet distributions.
        Returns:
            mixed: Augmented and mixed image.
        """
        ws = np.random.dirichlet([alpha] * width).astype(np.float32)
        m  = np.float32(np.random.beta(alpha, alpha))
        mix = np.zeros_like(image).astype(np.float32)

        for i in range(width):
            image_aug = image.copy()
            depth = depth if depth > 0 else np.random.randint(1, 4)
            for _ in range(depth):
                op = np.random.choice(self.augmentations)
                image_aug = self.apply_op(image_aug, op, severity)
                # Preprocessing commutes since all coefficients are convex
                mix += ws[i] * image_aug

        # mix the image and return
        mixed = (1 - m)*image + m*mix
        return mixed

    def __len__(self):
        return int(np.ceil(len(self.data) / self.batch_size))

    def __getitem__(self, idx):
        curr_batch = self.indices[idx*self.batch_size:(idx+1)*self.batch_size]
        batch_len = len(curr_batch)
        X_orig = np.zeros((batch_len, *self.img_dim), dtype=np.float32)
        y = np.zeros((batch_len, self.num_classes), dtype=np.float32)

        if self.jsd:
            X_aug1 = np.zeros_like(X_orig, dtype=np.float32)
            X_aug2 = np.zeros_like(X_orig, dtype=np.float32)

        for i, index in enumerate(curr_batch):
            img = self.data[index]
            X_orig[i] = img
            if self.jsd:
                X_aug1[i] = self.augment_and_mix(img)
                X_aug2[i] = self.augment_and_mix(img)
            y[i] = self.labels[index]

        if not self.jsd:
            return X_orig, y
        else:
            return [X_orig, X_aug1, X_aug2], y