import cv2
import itertools
import os

import numpy as np
np.set_printoptions(precision=2)

import openface

class ImageParser(object):
    def __init__(self):
      fileDir = os.path.dirname(os.path.realpath(__file__))
      modelDir = os.path.join(fileDir, '/root/openface/', 'models')
      dlibModelDir = os.path.join(modelDir, 'dlib')
      openfaceModelDir = os.path.join(modelDir, 'openface')

      imgDim = 96
      self.align = openface.AlignDlib(os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))

      self.catHeadImg = cv2.imread('cats/cat_head.png', cv2.IMREAD_UNCHANGED)
      self.catMouthImg = cv2.imread('cats/cat_mouth.png', cv2.IMREAD_UNCHANGED)
      self.catWidth = self.catHeadImg.shape[0]
      self.catHeight = self.catHeadImg.shape[1]
      self.sf = 1.8
      self.yOffset = 1.2

    def process(self, img):
      bgrImg = img.copy()
      if bgrImg is None:
          raise Exception("Unable to load image: {}".format(imgPath))
      rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)

      bbs = self.align.getAllFaceBoundingBoxes(rgbImg)
      np_bbs = np.array([[b.left(), b.top(), b.right(), b.bottom()] for b in bbs])
      final_bbs = self.non_max_suppression_fast(np_bbs, 0.3)
      sorted_bbs = sorted(zip(final_bbs, bbs), key=lambda x: x[0][2]-x[0][0])
      for (bb, dlibBB) in sorted_bbs:
        landmarks = self.align.findLandmarks(rgbImg, dlibBB)

        bbWidth = (bb[2]-bb[0])
        resizeWidth = int(round(bbWidth * self.sf))
        resizeHeight = int(round(self.sf * self.catHeight * (bbWidth / float(self.catWidth))))
        resizedCatHeadImg = cv2.resize(self.catHeadImg, (resizeWidth, resizeHeight))
        resizedCatMouthImg = cv2.resize(self.catMouthImg, (resizeWidth, resizeHeight))
        drawX = int(round((bb[0] + bb[2])/2-(resizeWidth/2)))
        drawY = int(round((bb[1] + bb[3])/2-(resizeHeight/2) * self.yOffset))

        self.overlay_image_alpha(bgrImg,
                        resizedCatHeadImg[:, :, 0:3],
                        (drawX, drawY),
                        resizedCatHeadImg[:, :, 3] / 255.0)

        yParting = self.get_parting(landmarks)

        self.overlay_image_alpha(bgrImg,
                        resizedCatMouthImg[:, :, 0:3],
                        (drawX, drawY + yParting),
                        resizedCatMouthImg[:, :, 3] / 255.0)

      # cv2.rectangle(bgrImg, (bb.left(), bb.top()), (bb.right(), bb.bottom()), (0,255,0), 3)
      return bgrImg

    def get_parting(self, landmarks):
      diff1 = landmarks[67][1]-landmarks[61][1]
      diff2 = landmarks[66][1]-landmarks[62][1]
      diff3 = landmarks[65][1]-landmarks[63][1]
      parting = (diff1 + diff2 + diff3) / 3
      return parting

    def overlay_image_alpha(self, img, img_overlay, pos, alpha_mask):
        """Overlay img_overlay on top of img at the position specified by
        pos and blend using alpha_mask.

        Alpha mask must contain values within the range [0, 1] and be the
        same size as img_overlay.
        """

        x, y = pos

        # Image ranges
        y1, y2 = max(0, y), min(img.shape[0], y + img_overlay.shape[0])
        x1, x2 = max(0, x), min(img.shape[1], x + img_overlay.shape[1])

        # Overlay ranges
        y1o, y2o = max(0, -y), min(img_overlay.shape[0], img.shape[0] - y)
        x1o, x2o = max(0, -x), min(img_overlay.shape[1], img.shape[1] - x)

        # Exit if nothing to do
        if y1 >= y2 or x1 >= x2 or y1o >= y2o or x1o >= x2o:
            return

        channels = img.shape[2]

        alpha = alpha_mask[y1o:y2o, x1o:x2o]
        alpha_inv = 1.0 - alpha

        for c in range(channels):
            img[y1:y2, x1:x2, c] = (alpha * img_overlay[y1o:y2o, x1o:x2o, c] +
                                    alpha_inv * img[y1:y2, x1:x2, c])

    # Malisiewicz et al.
    def non_max_suppression_fast(self, boxes, overlapThresh):
      # if there are no boxes, return an empty list
      if len(boxes) == 0:
        return []

      # if the bounding boxes integers, convert them to floats --
      # this is important since we'll be doing a bunch of divisions
      if boxes.dtype.kind == "i":
        boxes = boxes.astype("float")

      # initialize the list of picked indexes
      pick = []

      # grab the coordinates of the bounding boxes
      x1 = boxes[:,0]
      y1 = boxes[:,1]
      x2 = boxes[:,2]
      y2 = boxes[:,3]

      # compute the area of the bounding boxes and sort the bounding
      # boxes by the bottom-right y-coordinate of the bounding box
      area = (x2 - x1 + 1) * (y2 - y1 + 1)
      idxs = np.argsort(y2)

      # keep looping while some indexes still remain in the indexes
      # list
      while len(idxs) > 0:
        # grab the last index in the indexes list and add the
        # index value to the list of picked indexes
        last = len(idxs) - 1
        i = idxs[last]
        pick.append(i)

        # find the largest (x, y) coordinates for the start of
        # the bounding box and the smallest (x, y) coordinates
        # for the end of the bounding box
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])

        # compute the width and height of the bounding box
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)

        # compute the ratio of overlap
        overlap = (w * h) / area[idxs[:last]]

        # delete all indexes from the index list that have
        idxs = np.delete(idxs, np.concatenate(([last],
          np.where(overlap > overlapThresh)[0])))

      # return only the bounding boxes that were picked using the
      # integer data type
      return boxes[pick].astype("int")


# train a model for text classification with tensorflow / keras
from __future__ import absolute_import, division, print_function

import re
import json
import pathlib

import string
import numpy as np
import pandas as pd  # pip install pandas

# nltk
import nltk  # pip install --user -U nltk
# nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer

# sklearn
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib

def load_data(filename):
    data = []
    with open(str(pathlib.Path(__file__).parent.absolute()) + "/" + filename, 'r') as f:
        imported = json.load(f)
        data = pd.DataFrame(imported['data'])
        data.drop(['id', 'caseTitle', 'lbName', 'ltName', 'ltAlias'], axis=1, inplace=True)

    return data

### Text Normalizing function. Part of the following function was taken from this link.
### https://medium.com/@sabber/classifying-yelp-review-comments-using-lstm-and-word-embeddings-part-1-eb2275e4066b
def clean_text(text):
    """
        text: a string

        return: modified initial string
    """
    ## Remove puncuation
    text = text.translate(string.punctuation)

    ## Convert words to lower case and split them
    text = text.lower().split()

    ## Remove stop words
    stops = set(stopwords.words("german"))
    text = [w for w in text if not w in stops and len(w) >= 3]

    text = " ".join(text)
    ## Clean the text
    text = re.sub(r"[^A-Za-z0-9^,!.\/'+-=äöüß]", " ", text)
    text = re.sub(r"\'s", " ", text)
    text = re.sub(r",", " ", text)
    text = re.sub(r"\.", " ", text)
    text = re.sub(r"!", " ! ", text)
    text = re.sub(r"\/", " ", text)
    text = re.sub(r"\^", " ^ ", text)
    text = re.sub(r"\+", " + ", text)
    text = re.sub(r"\-", " - ", text)
    text = re.sub(r"\=", " = ", text)
    text = re.sub(r"'", " ", text)
    text = re.sub(r"(\d+)(k)", r"\g<1>000", text)
    text = re.sub(r":", " : ", text)
    text = re.sub(r" e g ", " eg ", text)
    text = re.sub(r" b g ", " bg ", text)
    text = re.sub(r" u s ", " american ", text)
    text = re.sub(r"\0s", "0", text)
    text = re.sub(r" 9 11 ", "911", text)
    text = re.sub(r"e-mail", "email", text)
    text = re.sub(r"j k", "jk", text)
    text = re.sub(r"\s{2,}", " ", text)

    ## Stemming
    text = text.split()
    stemmer = SnowballStemmer('german')
    stemmed_words = [stemmer.stem(word) for word in text]
    text = " ".join(stemmed_words)

    return text

# load data, limit to TOP 10 categories and remove null-Values
df = load_data("data.json")
top10lb = list(df['lbAlias'].value_counts().head(10).index)
df = df[df['lbAlias'].isin(top10lb)]

# print(top10lb)
# print(filteredDf.head(20))

df = df.dropna()
df = df[df.caseDesc.apply(lambda x: x != "")]
df = df[df.lbAlias.apply(lambda x: x != "")]

# apply text cleaning function to df['text']
df['text'] = df['caseDesc'].map(lambda x: clean_text(x))
tags = pd.get_dummies(df['lbAlias']).columns.tolist()

# print(df['lbAlias'].value_counts())
# print(df.head(10))

X = df.text
y = df.lbAlias
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state = 42)

# naive bayes
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.metrics import classification_report
"""
nb = Pipeline([('vect', CountVectorizer()),
               ('tfidf', TfidfTransformer()),
               ('clf', MultinomialNB()),
              ])
nb.fit(X_train, y_train)

y_pred = nb.predict(X_test)

print('accuracy %s' % accuracy_score(y_pred, y_test))
print(classification_report(y_test, y_pred, labels=tags, zero_division=0)) """

# support vector machine (SVM)
""" from sklearn.linear_model import SGDClassifier

sgd = Pipeline([('vect', CountVectorizer()),
                ('tfidf', TfidfTransformer()),
                ('clf', SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, random_state=42, max_iter=5, tol=None)),
               ])
sgd.fit(X_train, y_train)

y_pred = sgd.predict(X_test)

print('accuracy %s' % accuracy_score(y_pred, y_test))
print(classification_report(y_test, y_pred, labels=tags, zero_division=0)) """

# logistic regression
# https://towardsdatascience.com/multi-class-text-classification-model-comparison-and-selection-5eb066197568
from sklearn.linear_model import LogisticRegression
from imblearn.pipeline import Pipeline as Pipeline_IMB   # pip install -U imbalanced-learn
from imblearn.over_sampling import SMOTE, SMOTENC
from imblearn.metrics import classification_report_imbalanced
'''
logreg = Pipeline([('vect', CountVectorizer()),
                ('tfidf', TfidfTransformer()),
                ('clf', LogisticRegression(n_jobs=1, C=1e5, max_iter=500)),
               ])
'''

count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)

tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

print("Before oversampling: ", X_train_tfidf.shape)

sm = SMOTE(random_state=4)
train_text_res, train_y_res = sm.fit_sample(X_train_tfidf, y_train)

print("After oversampling: ", train_text_res.shape)

# logreg = LogisticRegression(n_jobs=1, C=1e5, max_iter=500)
logreg = LogisticRegression(solver='saga', multi_class='auto', max_iter=500, class_weight='balanced', n_jobs=-1, random_state=4)

# train model
logreg.fit(train_text_res, train_y_res)

# test model
X_new_counts = count_vect.transform(X_test)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)
y_pred = logreg.predict(X_new_tfidf)

print('accuracy %s' % accuracy_score(y_pred, y_test))
# print(classification_report(y_test, y_pred, labels=tags, zero_division=0))
print(classification_report_imbalanced(y_test, y_pred))

# save model to file
joblib.dump(logreg, str(pathlib.Path(__file__).parent.absolute()) + '/legalcase_logreg_model.pkl')
print("Saved model to disk")

# make validation predictions here
validation_data = load_data("legalcase_validation.json")

# filter by known legal branches
validation_data = validation_data[validation_data['lbAlias'].isin(top10lb)]

# sanitize text
validation_data['caseDesc'] = validation_data['caseDesc'].map(lambda x: clean_text(x))

result = []
for index, row in validation_data.iterrows():
    X_new_counts = count_vect.transform([row['caseDesc']])
    X_new_tfidf = tfidf_transformer.transform(X_new_counts)
    predictions = logreg.predict(X_new_tfidf)
    result.append([predictions[0], row['lbAlias'], (predictions[0] == row['lbAlias'])])

rPd = pd.DataFrame(result, columns=['predicted', 'given', 'result'])
print("\n# of correct predictions: {}".format(len(rPd[(rPd.result == True)])))
print("# of incorrect predictions: {}\n".format(len(rPd[(rPd.result == False)])))
with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', None):  # more options can be specified also
    print(rPd)


from setuptools.extension import Library, Extension
from setuptools import setup
from distutils.sysconfig import get_config_var

# "cpython-36m-x86_64-linux-gnu"
SOABI = get_config_var("SOABI")
# e.g. "linux"
MACHDEP = get_config_var("MACHDEP")
# e.g. "x86_64"
AR = get_config_var("AR").split("-")[0]
# e.g. "3.6"
VERSION = get_config_var("VERSION")

ACTUAL_LIB_DIR = "build/lib.%s-%s-%s" % (MACHDEP, AR, VERSION)

lib0 = Library("lib0",
               sources=["lib0/lib0.cpp"])

ext0 = Extension("_ext0",
                 sources=["ext0/ext0.cpp"],
                 include_dirs=["lib0/"],
                 libraries=["lib0." + SOABI],
                 library_dirs=[ACTUAL_LIB_DIR]
                 )

setup(
    name='exampe_setuptools',
    version='1.2',
    author='Michal Orsak',
    author_email='michal.o.socials@gmail.com',
    ext_modules=[lib0, ext0],
)


import cv2
import numpy as np
import pickle

face_cascade = cv2.CascadeClassifier('C:\Python\Python37\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer.yml')

labels = {}
with open("labels.pickle", 'rb') as f:
    og_labels = pickle.load(f)
    labels = {v:k for k,v in og_labels.items()}

cap = cv2.VideoCapture(0)

while(cap.isOpened()):
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    for (x, y, w, h) in faces:
        roi_gray = gray[y: y+h, x: x+w]
        roi_color = gray[y: y + h, x: x + w]

        id_, conf = recognizer.predict(roi_gray)
        if conf >= 45: # and conf <= 85:
            print(id_, labels[id_])
            cv2.putText(frame, labels[id_], (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, [255,255,0], 2)

        cv2.rectangle(frame, (x, y), (x + w, y + h), [0, 255, 0], 3)

    cv2.imshow("Frames", frame)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


# #####
# PYGAL
# #####

from IPython.display import display, HTML
html_pygal="""
<!DOCTYPE html>
<html>
  <head>
  <script type="text/javascript" src="http://kozea.github.com/pygal.js/latest/pygal-tooltips.min.js"></script>
    <!-- ... -->
  </head>
  <body>
    <figure>
      <!-- Pygal render() result: -->
      {a}
      <!-- End of Pygal render() result: -->
    </figure>
  </body>
</html>
"""
fig_pygal="""
<figure>
  <!-- Pygal render() result: -->
  {a}
  <!-- End of Pygal render() result: -->
</figure>
"""
def show(chart):
    plot_html = html_pygal.format(a=chart.render(is_unicode=True))
    display(HTML(plot_html))
def to_html(chart):
    plot_html = html_pygal.format(a=chart.render(is_unicode=True))
    return plot_html
def to_html_fig(chart):
    plot_html_fig = fig_pygal.format(a=chart.render(is_unicode=True))
    return plot_html_fig
def to_svg(chart):
    return chart.render(is_unicode=True)


from datetime import datetime
from pathlib import Path

def create_model_monitor_dir(rewards_dir, model_name, env_name, policy_name):
    now_str = datetime.now().strftime("%d-%m-%Y_%H-%M")
    path = Path(rewards_dir, env_name, f"{model_name}-{policy_name}_{now_str}")
    path.mkdir(parents=True, exist_ok=True)
    return path

def create_model_save_dir(models_dir, model_name, env_name, policy):
    # cast policy to str, as we might use our own policy implementation
    now_str = datetime.now().strftime("%d-%m-%Y_%H-%M")
    path = Path(models_dir, env_name, f"{model_name}-{str(policy)}-{now_str}")
    path.mkdir(parents=True, exist_ok=True)
    return path

def add_metrics_dict_to_csv(metrics, csv_filepath, is_first_row=False):
    if is_first_row:
        # Create file if doesn't exist - i.e. on first write
        fp = csv_filepath.open("w")
    else:
        fp = csv_filepath.open("a")

    # write header row
    if is_first_row:
        headers = ",".join(metrics.keys())
        fp.write(headers)
        fp.write("\n")

    # write data row
    fp.write(",".join(map(str, metrics.values())))
    fp.write("\n")


"""Algorithms for spectral clustering"""

# Author: Gael Varoquaux <gael.varoquaux@normalesup.org>
#         Brian Cheung
#         Wei LI <kuantkid@gmail.com>
#         Andrew Knyazev <Andrew.Knyazev@ucdenver.edu>
# License: BSD 3 clause

import warnings
from numbers import Integral, Real

import numpy as np
from scipy.linalg import LinAlgError, qr, svd
from scipy.sparse import csc_matrix

from ..base import BaseEstimator, ClusterMixin, _fit_context
from ..manifold import spectral_embedding
from ..metrics.pairwise import KERNEL_PARAMS, pairwise_kernels
from ..neighbors import NearestNeighbors, kneighbors_graph
from ..utils import as_float_array, check_random_state
from ..utils._param_validation import Interval, StrOptions, validate_params
from ._kmeans import k_means

def cluster_qr(vectors):
    """Find the discrete partition closest to the eigenvector embedding.

        This implementation was proposed in [1]_.

    .. versionadded:: 1.1

        Parameters
        ----------
        vectors : array-like, shape: (n_samples, n_clusters)
            The embedding space of the samples.

        Returns
        -------
        labels : array of integers, shape: n_samples
            The cluster labels of vectors.

        References
        ----------
        .. [1] :doi:`Simple, direct, and efficient multi-way spectral clustering, 2019
            Anil Damle, Victor Minden, Lexing Ying
            <10.1093/imaiai/iay008>`

    """

    k = vectors.shape[1]
    _, _, piv = qr(vectors.T, pivoting=True)
    ut, _, v = svd(vectors[piv[:k], :].T)
    vectors = abs(np.dot(vectors, np.dot(ut, v.conj())))
    return vectors.argmax(axis=1)

def discretize(
    vectors, *, copy=True, max_svd_restarts=30, n_iter_max=20, random_state=None
):
    """Search for a partition matrix which is closest to the eigenvector embedding.

    This implementation was proposed in [1]_.

    Parameters
    ----------
    vectors : array-like of shape (n_samples, n_clusters)
        The embedding space of the samples.

    copy : bool, default=True
        Whether to copy vectors, or perform in-place normalization.

    max_svd_restarts : int, default=30
        Maximum number of attempts to restart SVD if convergence fails

    n_iter_max : int, default=30
        Maximum number of iterations to attempt in rotation and partition
        matrix search if machine precision convergence is not reached

    random_state : int, RandomState instance, default=None
        Determines random number generation for rotation matrix initialization.
        Use an int to make the randomness deterministic.
        See :term:`Glossary <random_state>`.

    Returns
    -------
    labels : array of integers, shape: n_samples
        The labels of the clusters.

    References
    ----------

    .. [1] `Multiclass spectral clustering, 2003
           Stella X. Yu, Jianbo Shi
           <https://people.eecs.berkeley.edu/~jordan/courses/281B-spring04/readings/yu-shi.pdf>`_

    Notes
    -----

    The eigenvector embedding is used to iteratively search for the
    closest discrete partition.  First, the eigenvector embedding is
    normalized to the space of partition matrices. An optimal discrete
    partition matrix closest to this normalized embedding multiplied by
    an initial rotation is calculated.  Fixing this discrete partition
    matrix, an optimal rotation matrix is calculated.  These two
    calculations are performed until convergence.  The discrete partition
    matrix is returned as the clustering solution.  Used in spectral
    clustering, this method tends to be faster and more robust to random
    initialization than k-means.

    """

    random_state = check_random_state(random_state)

    vectors = as_float_array(vectors, copy=copy)

    eps = np.finfo(float).eps
    n_samples, n_components = vectors.shape

    # Normalize the eigenvectors to an equal length of a vector of ones.
    # Reorient the eigenvectors to point in the negative direction with respect
    # to the first element.  This may have to do with constraining the
    # eigenvectors to lie in a specific quadrant to make the discretization
    # search easier.
    norm_ones = np.sqrt(n_samples)
    for i in range(vectors.shape[1]):
        vectors[:, i] = (vectors[:, i] / np.linalg.norm(vectors[:, i])) * norm_ones
        if vectors[0, i] != 0:
            vectors[:, i] = -1 * vectors[:, i] * np.sign(vectors[0, i])

    # Normalize the rows of the eigenvectors.  Samples should lie on the unit
    # hypersphere centered at the origin.  This transforms the samples in the
    # embedding space to the space of partition matrices.
    vectors = vectors / np.sqrt((vectors**2).sum(axis=1))[:, np.newaxis]

    svd_restarts = 0
    has_converged = False

    # If there is an exception we try to randomize and rerun SVD again
    # do this max_svd_restarts times.
    while (svd_restarts < max_svd_restarts) and not has_converged:
        # Initialize first column of rotation matrix with a row of the
        # eigenvectors
        rotation = np.zeros((n_components, n_components))
        rotation[:, 0] = vectors[random_state.randint(n_samples), :].T

        # To initialize the rest of the rotation matrix, find the rows
        # of the eigenvectors that are as orthogonal to each other as
        # possible
        c = np.zeros(n_samples)
        for j in range(1, n_components):
            # Accumulate c to ensure row is as orthogonal as possible to
            # previous picks as well as current one
            c += np.abs(np.dot(vectors, rotation[:, j - 1]))
            rotation[:, j] = vectors[c.argmin(), :].T

        last_objective_value = 0.0
        n_iter = 0

        while not has_converged:
            n_iter += 1

            t_discrete = np.dot(vectors, rotation)

            labels = t_discrete.argmax(axis=1)
            vectors_discrete = csc_matrix(
                (np.ones(len(labels)), (np.arange(0, n_samples), labels)),
                shape=(n_samples, n_components),
            )

            t_svd = vectors_discrete.T * vectors

            try:
                U, S, Vh = np.linalg.svd(t_svd)
            except LinAlgError:
                svd_restarts += 1
                print("SVD did not converge, randomizing and trying again")
                break

            ncut_value = 2.0 * (n_samples - S.sum())
            if (abs(ncut_value - last_objective_value) < eps) or (n_iter > n_iter_max):
                has_converged = True
            else:
                # otherwise calculate rotation and continue
                last_objective_value = ncut_value
                rotation = np.dot(Vh.T, U.T)

    if not has_converged:
        raise LinAlgError("SVD did not converge")
    return labels

@validate_params(
    {"affinity": ["array-like", "sparse matrix"]},
    prefer_skip_nested_validation=False,
)
def spectral_clustering(
    affinity,
    *,
    n_clusters=8,
    n_components=None,
    eigen_solver=None,
    random_state=None,
    n_init=10,
    eigen_tol="auto",
    assign_labels="kmeans",
    verbose=False,
):
    """Apply clustering to a projection of the normalized Laplacian.

    In practice Spectral Clustering is very useful when the structure of
    the individual clusters is highly non-convex or more generally when
    a measure of the center and spread of the cluster is not a suitable
    description of the complete cluster. For instance, when clusters are
    nested circles on the 2D plane.

    If affinity is the adjacency matrix of a graph, this method can be
    used to find normalized graph cuts [1]_, [2]_.

    Read more in the :ref:`User Guide <spectral_clustering>`.

    Parameters
    ----------
    affinity : {array-like, sparse matrix} of shape (n_samples, n_samples)
        The affinity matrix describing the relationship of the samples to
        embed. **Must be symmetric**.

        Possible examples:
          - adjacency matrix of a graph,
          - heat kernel of the pairwise distance matrix of the samples,
          - symmetric k-nearest neighbours connectivity matrix of the samples.

    n_clusters : int, default=None
        Number of clusters to extract.

    n_components : int, default=n_clusters
        Number of eigenvectors to use for the spectral embedding.

    eigen_solver : {None, 'arpack', 'lobpcg', or 'amg'}
        The eigenvalue decomposition method. If None then ``'arpack'`` is used.
        See [4]_ for more details regarding ``'lobpcg'``.
        Eigensolver ``'amg'`` runs ``'lobpcg'`` with optional
        Algebraic MultiGrid preconditioning and requires pyamg to be installed.
        It can be faster on very large sparse problems [6]_ and [7]_.

    random_state : int, RandomState instance, default=None
        A pseudo random number generator used for the initialization
        of the lobpcg eigenvectors decomposition when `eigen_solver ==
        'amg'`, and for the K-Means initialization. Use an int to make
        the results deterministic across calls (See
        :term:`Glossary <random_state>`).

        .. note::
            When using `eigen_solver == 'amg'`,
            it is necessary to also fix the global numpy seed with
            `np.random.seed(int)` to get deterministic results. See
            https://github.com/pyamg/pyamg/issues/139 for further
            information.

    n_init : int, default=10
        Number of time the k-means algorithm will be run with different
        centroid seeds. The final results will be the best output of n_init
        consecutive runs in terms of inertia. Only used if
        ``assign_labels='kmeans'``.

    eigen_tol : float, default="auto"
        Stopping criterion for eigendecomposition of the Laplacian matrix.
        If `eigen_tol="auto"` then the passed tolerance will depend on the
        `eigen_solver`:

        - If `eigen_solver="arpack"`, then `eigen_tol=0.0`;
        - If `eigen_solver="lobpcg"` or `eigen_solver="amg"`, then
          `eigen_tol=None` which configures the underlying `lobpcg` solver to
          automatically resolve the value according to their heuristics. See,
          :func:`scipy.sparse.linalg.lobpcg` for details.

        Note that when using `eigen_solver="lobpcg"` or `eigen_solver="amg"`
        values of `tol<1e-5` may lead to convergence issues and should be
        avoided.

        .. versionadded:: 1.2
           Added 'auto' option.

    assign_labels : {'kmeans', 'discretize', 'cluster_qr'}, default='kmeans'
        The strategy to use to assign labels in the embedding
        space.  There are three ways to assign labels after the Laplacian
        embedding.  k-means can be applied and is a popular choice. But it can
        also be sensitive to initialization. Discretization is another
        approach which is less sensitive to random initialization [3]_.
        The cluster_qr method [5]_ directly extracts clusters from eigenvectors
        in spectral clustering. In contrast to k-means and discretization, cluster_qr
        has no tuning parameters and is not an iterative method, yet may outperform
        k-means and discretization in terms of both quality and speed.

        .. versionchanged:: 1.1
           Added new labeling method 'cluster_qr'.

    verbose : bool, default=False
        Verbosity mode.

        .. versionadded:: 0.24

    Returns
    -------
    labels : array of integers, shape: n_samples
        The labels of the clusters.

    Notes
    -----
    The graph should contain only one connected component, elsewhere
    the results make little sense.

    This algorithm solves the normalized cut for `k=2`: it is a
    normalized spectral clustering.

    References
    ----------

    .. [1] :doi:`Normalized cuts and image segmentation, 2000
           Jianbo Shi, Jitendra Malik
           <10.1109/34.868688>`

    .. [2] :doi:`A Tutorial on Spectral Clustering, 2007
           Ulrike von Luxburg
           <10.1007/s11222-007-9033-z>`

    .. [3] `Multiclass spectral clustering, 2003
           Stella X. Yu, Jianbo Shi
           <https://people.eecs.berkeley.edu/~jordan/courses/281B-spring04/readings/yu-shi.pdf>`_

    .. [4] :doi:`Toward the Optimal Preconditioned Eigensolver:
           Locally Optimal Block Preconditioned Conjugate Gradient Method, 2001
           A. V. Knyazev
           SIAM Journal on Scientific Computing 23, no. 2, pp. 517-541.
           <10.1137/S1064827500366124>`

    .. [5] :doi:`Simple, direct, and efficient multi-way spectral clustering, 2019
           Anil Damle, Victor Minden, Lexing Ying
           <10.1093/imaiai/iay008>`

    .. [6] :doi:`Multiscale Spectral Image Segmentation Multiscale preconditioning
           for computing eigenvalues of graph Laplacians in image segmentation, 2006
           Andrew Knyazev
           <10.13140/RG.2.2.35280.02565>`

    .. [7] :doi:`Preconditioned spectral clustering for stochastic block partition
           streaming graph challenge (Preliminary version at arXiv.)
           David Zhuzhunashvili, Andrew Knyazev
           <10.1109/HPEC.2017.8091045>`
    """

    clusterer = SpectralClustering(
        n_clusters=n_clusters,
        n_components=n_components,
        eigen_solver=eigen_solver,
        random_state=random_state,
        n_init=n_init,
        affinity="precomputed",
        eigen_tol=eigen_tol,
        assign_labels=assign_labels,
        verbose=verbose,
    ).fit(affinity)

    return clusterer.labels_

class SpectralClustering(ClusterMixin, BaseEstimator):
    """Apply clustering to a projection of the normalized Laplacian.

    In practice Spectral Clustering is very useful when the structure of
    the individual clusters is highly non-convex, or more generally when
    a measure of the center and spread of the cluster is not a suitable
    description of the complete cluster, such as when clusters are
    nested circles on the 2D plane.

    If the affinity matrix is the adjacency matrix of a graph, this method
    can be used to find normalized graph cuts [1]_, [2]_.

    When calling ``fit``, an affinity matrix is constructed using either
    a kernel function such the Gaussian (aka RBF) kernel with Euclidean
    distance ``d(X, X)``::

            np.exp(-gamma * d(X,X) ** 2)

    or a k-nearest neighbors connectivity matrix.

    Alternatively, a user-provided affinity matrix can be specified by
    setting ``affinity='precomputed'``.

    Read more in the :ref:`User Guide <spectral_clustering>`.

    Parameters
    ----------
    n_clusters : int, default=8
        The dimension of the projection subspace.

    eigen_solver : {'arpack', 'lobpcg', 'amg'}, default=None
        The eigenvalue decomposition strategy to use. AMG requires pyamg
        to be installed. It can be faster on very large, sparse problems,
        but may also lead to instabilities. If None, then ``'arpack'`` is
        used. See [4]_ for more details regarding `'lobpcg'`.

    n_components : int, default=None
        Number of eigenvectors to use for the spectral embedding. If None,
        defaults to `n_clusters`.

    random_state : int, RandomState instance, default=None
        A pseudo random number generator used for the initialization
        of the lobpcg eigenvectors decomposition when `eigen_solver ==
        'amg'`, and for the K-Means initialization. Use an int to make
        the results deterministic across calls (See
        :term:`Glossary <random_state>`).

        .. note::
            When using `eigen_solver == 'amg'`,
            it is necessary to also fix the global numpy seed with
            `np.random.seed(int)` to get deterministic results. See
            https://github.com/pyamg/pyamg/issues/139 for further
            information.

    n_init : int, default=10
        Number of time the k-means algorithm will be run with different
        centroid seeds. The final results will be the best output of n_init
        consecutive runs in terms of inertia. Only used if
        ``assign_labels='kmeans'``.

    gamma : float, default=1.0
        Kernel coefficient for rbf, poly, sigmoid, laplacian and chi2 kernels.
        Ignored for ``affinity='nearest_neighbors'``.

    affinity : str or callable, default='rbf'
        How to construct the affinity matrix.
         - 'nearest_neighbors': construct the affinity matrix by computing a
           graph of nearest neighbors.
         - 'rbf': construct the affinity matrix using a radial basis function
           (RBF) kernel.
         - 'precomputed': interpret ``X`` as a precomputed affinity matrix,
           where larger values indicate greater similarity between instances.
         - 'precomputed_nearest_neighbors': interpret ``X`` as a sparse graph
           of precomputed distances, and construct a binary affinity matrix
           from the ``n_neighbors`` nearest neighbors of each instance.
         - one of the kernels supported by
           :func:`~sklearn.metrics.pairwise.pairwise_kernels`.

        Only kernels that produce similarity scores (non-negative values that
        increase with similarity) should be used. This property is not checked
        by the clustering algorithm.

    n_neighbors : int, default=10
        Number of neighbors to use when constructing the affinity matrix using
        the nearest neighbors method. Ignored for ``affinity='rbf'``.

    eigen_tol : float, default="auto"
        Stopping criterion for eigen decomposition of the Laplacian matrix.
        If `eigen_tol="auto"` then the passed tolerance will depend on the
        `eigen_solver`:

        - If `eigen_solver="arpack"`, then `eigen_tol=0.0`;
        - If `eigen_solver="lobpcg"` or `eigen_solver="amg"`, then
          `eigen_tol=None` which configures the underlying `lobpcg` solver to
          automatically resolve the value according to their heuristics. See,
          :func:`scipy.sparse.linalg.lobpcg` for details.

        Note that when using `eigen_solver="lobpcg"` or `eigen_solver="amg"`
        values of `tol<1e-5` may lead to convergence issues and should be
        avoided.

        .. versionadded:: 1.2
           Added 'auto' option.

    assign_labels : {'kmeans', 'discretize', 'cluster_qr'}, default='kmeans'
        The strategy for assigning labels in the embedding space. There are two
        ways to assign labels after the Laplacian embedding. k-means is a
        popular choice, but it can be sensitive to initialization.
        Discretization is another approach which is less sensitive to random
        initialization [3]_.
        The cluster_qr method [5]_ directly extract clusters from eigenvectors
        in spectral clustering. In contrast to k-means and discretization, cluster_qr
        has no tuning parameters and runs no iterations, yet may outperform
        k-means and discretization in terms of both quality and speed.

        .. versionchanged:: 1.1
           Added new labeling method 'cluster_qr'.

    degree : float, default=3
        Degree of the polynomial kernel. Ignored by other kernels.

    coef0 : float, default=1
        Zero coefficient for polynomial and sigmoid kernels.
        Ignored by other kernels.

    kernel_params : dict of str to any, default=None
        Parameters (keyword arguments) and values for kernel passed as
        callable object. Ignored by other kernels.

    n_jobs : int, default=None
        The number of parallel jobs to run when `affinity='nearest_neighbors'`
        or `affinity='precomputed_nearest_neighbors'`. The neighbors search
        will be done in parallel.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    verbose : bool, default=False
        Verbosity mode.

        .. versionadded:: 0.24

    Attributes
    ----------
    affinity_matrix_ : array-like of shape (n_samples, n_samples)
        Affinity matrix used for clustering. Available only after calling
        ``fit``.

    labels_ : ndarray of shape (n_samples,)
        Labels of each point

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    sklearn.cluster.KMeans : K-Means clustering.
    sklearn.cluster.DBSCAN : Density-Based Spatial Clustering of
        Applications with Noise.

    Notes
    -----
    A distance matrix for which 0 indicates identical elements and high values
    indicate very dissimilar elements can be transformed into an affinity /
    similarity matrix that is well-suited for the algorithm by
    applying the Gaussian (aka RBF, heat) kernel::

        np.exp(- dist_matrix ** 2 / (2. * delta ** 2))

    where ``delta`` is a free parameter representing the width of the Gaussian
    kernel.

    An alternative is to take a symmetric version of the k-nearest neighbors
    connectivity matrix of the points.

    If the pyamg package is installed, it is used: this greatly
    speeds up computation.

    References
    ----------
    .. [1] :doi:`Normalized cuts and image segmentation, 2000
           Jianbo Shi, Jitendra Malik
           <10.1109/34.868688>`

    .. [2] :doi:`A Tutorial on Spectral Clustering, 2007
           Ulrike von Luxburg
           <10.1007/s11222-007-9033-z>`

    .. [3] `Multiclass spectral clustering, 2003
           Stella X. Yu, Jianbo Shi
           <https://people.eecs.berkeley.edu/~jordan/courses/281B-spring04/readings/yu-shi.pdf>`_

    .. [4] :doi:`Toward the Optimal Preconditioned Eigensolver:
           Locally Optimal Block Preconditioned Conjugate Gradient Method, 2001
           A. V. Knyazev
           SIAM Journal on Scientific Computing 23, no. 2, pp. 517-541.
           <10.1137/S1064827500366124>`

    .. [5] :doi:`Simple, direct, and efficient multi-way spectral clustering, 2019
           Anil Damle, Victor Minden, Lexing Ying
           <10.1093/imaiai/iay008>`

    Examples
    --------
    >>> from sklearn.cluster import SpectralClustering
    >>> import numpy as np
    >>> X = np.array([[1, 1], [2, 1], [1, 0],
    ...               [4, 7], [3, 5], [3, 6]])
    >>> clustering = SpectralClustering(n_clusters=2,
    ...         assign_labels='discretize',
    ...         random_state=0).fit(X)
    >>> clustering.labels_
    array([1, 1, 1, 0, 0, 0])
    >>> clustering
    SpectralClustering(assign_labels='discretize', n_clusters=2,
        random_state=0)
    """

    _parameter_constraints: dict = {
        "n_clusters": [Interval(Integral, 1, None, closed="left")],
        "eigen_solver": [StrOptions({"arpack", "lobpcg", "amg"}), None],
        "n_components": [Interval(Integral, 1, None, closed="left"), None],
        "random_state": ["random_state"],
        "n_init": [Interval(Integral, 1, None, closed="left")],
        "gamma": [Interval(Real, 0, None, closed="left")],
        "affinity": [
            callable,
            StrOptions(
                set(KERNEL_PARAMS)
                | {"nearest_neighbors", "precomputed", "precomputed_nearest_neighbors"}
            ),
        ],
        "n_neighbors": [Interval(Integral, 1, None, closed="left")],
        "eigen_tol": [
            Interval(Real, 0.0, None, closed="left"),
            StrOptions({"auto"}),
        ],
        "assign_labels": [StrOptions({"kmeans", "discretize", "cluster_qr"})],
        "degree": [Interval(Integral, 0, None, closed="left")],
        "coef0": [Interval(Real, None, None, closed="neither")],
        "kernel_params": [dict, None],
        "n_jobs": [Integral, None],
        "verbose": ["verbose"],
    }

    def __init__(
        self,
        n_clusters=8,
        *,
        eigen_solver=None,
        n_components=None,
        random_state=None,
        n_init=10,
        gamma=1.0,
        affinity="rbf",
        n_neighbors=10,
        eigen_tol="auto",
        assign_labels="kmeans",
        degree=3,
        coef0=1,
        kernel_params=None,
        n_jobs=None,
        verbose=False,
    ):
        self.n_clusters = n_clusters
        self.eigen_solver = eigen_solver
        self.n_components = n_components
        self.random_state = random_state
        self.n_init = n_init
        self.gamma = gamma
        self.affinity = affinity
        self.n_neighbors = n_neighbors
        self.eigen_tol = eigen_tol
        self.assign_labels = assign_labels
        self.degree = degree
        self.coef0 = coef0
        self.kernel_params = kernel_params
        self.n_jobs = n_jobs
        self.verbose = verbose

    @_fit_context(prefer_skip_nested_validation=True)
    def fit(self, X, y=None):
        """Perform spectral clustering from features, or affinity matrix.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features) or \
                (n_samples, n_samples)
            Training instances to cluster, similarities / affinities between
            instances if ``affinity='precomputed'``, or distances between
            instances if ``affinity='precomputed_nearest_neighbors``. If a
            sparse matrix is provided in a format other than ``csr_matrix``,
            ``csc_matrix``, or ``coo_matrix``, it will be converted into a
            sparse ``csr_matrix``.

        y : Ignored
            Not used, present here for API consistency by convention.

        Returns
        -------
        self : object
            A fitted instance of the estimator.
        """
        X = self._validate_data(
            X,
            accept_sparse=["csr", "csc", "coo"],
            dtype=np.float64,
            ensure_min_samples=2,
        )
        allow_squared = self.affinity in [
            "precomputed",
            "precomputed_nearest_neighbors",
        ]
        if X.shape[0] == X.shape[1] and not allow_squared:
            warnings.warn(
                "The spectral clustering API has changed. ``fit``"
                "now constructs an affinity matrix from data. To use"
                " a custom affinity matrix, "
                "set ``affinity=precomputed``."
            )

        if self.affinity == "nearest_neighbors":
            connectivity = kneighbors_graph(
                X, n_neighbors=self.n_neighbors, include_self=True, n_jobs=self.n_jobs
            )
            self.affinity_matrix_ = 0.5 * (connectivity + connectivity.T)
        elif self.affinity == "precomputed_nearest_neighbors":
            estimator = NearestNeighbors(
                n_neighbors=self.n_neighbors, n_jobs=self.n_jobs, metric="precomputed"
            ).fit(X)
            connectivity = estimator.kneighbors_graph(X=X, mode="connectivity")
            self.affinity_matrix_ = 0.5 * (connectivity + connectivity.T)
        elif self.affinity == "precomputed":
            self.affinity_matrix_ = X
        else:
            params = self.kernel_params
            if params is None:
                params = {}
            if not callable(self.affinity):
                params["gamma"] = self.gamma
                params["degree"] = self.degree
                params["coef0"] = self.coef0
            self.affinity_matrix_ = pairwise_kernels(
                X, metric=self.affinity, filter_params=True, **params
            )

        random_state = check_random_state(self.random_state)
        n_components = (
            self.n_clusters if self.n_components is None else self.n_components
        )
        # We now obtain the real valued solution matrix to the
        # relaxed Ncut problem, solving the eigenvalue problem
        # L_sym x = lambda x  and recovering u = D^-1/2 x.
        # The first eigenvector is constant only for fully connected graphs
        # and should be kept for spectral clustering (drop_first = False)
        # See spectral_embedding documentation.
        maps = spectral_embedding(
            self.affinity_matrix_,
            n_components=n_components,
            eigen_solver=self.eigen_solver,
            random_state=random_state,
            eigen_tol=self.eigen_tol,
            drop_first=False,
        )
        if self.verbose:
            print(f"Computing label assignment using {self.assign_labels}")

        if self.assign_labels == "kmeans":
            _, self.labels_, _ = k_means(
                maps,
                self.n_clusters,
                random_state=random_state,
                n_init=self.n_init,
                verbose=self.verbose,
            )
        elif self.assign_labels == "cluster_qr":
            self.labels_ = cluster_qr(maps)
        else:
            self.labels_ = discretize(maps, random_state=random_state)

        return self

    def fit_predict(self, X, y=None):
        """Perform spectral clustering on `X` and return cluster labels.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features) or \
                (n_samples, n_samples)
            Training instances to cluster, similarities / affinities between
            instances if ``affinity='precomputed'``, or distances between
            instances if ``affinity='precomputed_nearest_neighbors``. If a
            sparse matrix is provided in a format other than ``csr_matrix``,
            ``csc_matrix``, or ``coo_matrix``, it will be converted into a
            sparse ``csr_matrix``.

        y : Ignored
            Not used, present here for API consistency by convention.

        Returns
        -------
        labels : ndarray of shape (n_samples,)
            Cluster labels.
        """
        return super().fit_predict(X, y)

    def _more_tags(self):
        return {
            "pairwise": self.affinity in [
                "precomputed",
                "precomputed_nearest_neighbors",
            ]
        }


import yaml
import sys
from typing import List
import re

from blueprint.schema import module
from blueprint.schema import param

from blueprint.lib import dag
from blueprint.lib import event
from blueprint.validate import blueprint_validator

from blueprint.lib.logger import logr
# import logging
# logr = logging.getLogger(__name__)

def eprint(*args, **kwargs):
    logr.error(*args)
    print(*args, file=sys.stderr, **kwargs)
#========================================================================

BlueprintType = "blueprint"

class Blueprint(dict):
    yaml_tag = u'!Blueprint'
    def __init__(self,
                    name: str                       = "__init__",
                    description: str                = None,
                    inputs: List[param.Input]       = None,
                    outputs: List[param.Output]     = None,
                    settings: List[param.Setting]   = None,
                    modules: List[module.Module]    = None ):
        """Blueprint schema.

        :param name: Name of the blueprint
        :param description: Blueprint description
        :param inputs: List of input parameters (type param.Input)
        :param outputs: List of output parameters (type param.Output)
        :param settings: List of envitonment settings (type param.Setting)
        :param modules: List of modules in the blueprint (type module.Module)
        """

        self.schema_version = "1.0.0"
        self.type = BlueprintType
        self.name = name
        self.modules = []
        if(description != None):
            self.description = description
        else:
            self.description = None
        if(inputs != None):
            self.set_inputs(inputs)
        else:
            self.inputs = None
        if(outputs != None):
            self.set_outputs(outputs)
        else:
            self.outputs = None
        if(settings != None):
            self.set_settings(settings)
        else:
            self.settings = None
        if(modules != None):
            self.set_modules(modules)
        else:
            self.modules = None

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.name = "__init__"

    def __str__(self):
        txt = "Blueprint("
        txt += "name:" + (self.name if hasattr(self, 'name') else 'None') + ", "
        txt += "inputs:" + str(self.inputs if hasattr(self, 'inputs') else 'None') + ", "
        txt += "outputs:" + str(self.outputs if hasattr(self, 'outputs') else 'None') + ", "
        txt += "settings:" + str(self.settings if hasattr(self, 'settings') else 'None') + ", "
        txt += "modules:" + str(self.modules if hasattr(self, 'modules') else 'None')
        txt += ")"
        return txt

    def __repr__(self):
        return self.__str__()

    def noop(self, *args, **kw):
        pass

    yaml.emitter.Emitter.process_tag = noop

    def __eq__(self, other):
        if other == None:
            return False

        self_name = "" if self.name == None else self.name
        self_schema_version = "" if self.schema_version == None else self.schema_version
        self_type = "" if not hasattr(self, 'type') or self.type == None else self.type
        self_description = "" if not hasattr(self, 'description') or self.description == None else self.description
        self_inputs = "" if not hasattr(self, 'inputs') or self.inputs == None else str(self.inputs)
        self_outputs = "" if not hasattr(self, 'outputs') or self.outputs == None else str(self.outputs)
        self_settings = "" if not hasattr(self, 'settings') or self.settings == None else str(self.settings)
        self_modules = "" if not hasattr(self, 'modules') or self.modules == None else str(self.modules)

        other_name = "" if other.name == None else other.name
        other_schema_version = "" if other.schema_version == None else other.schema_version
        other_type = "" if not hasattr(other, 'type') or other.type == None else other.type
        other_description = "" if not hasattr(other, 'description') or other.description == None else other.description
        other_inputs = "" if not hasattr(other, 'inputs') or other.inputs == None else str(other.inputs)
        other_outputs = "" if not hasattr(other, 'outputs') or other.outputs == None else str(other.outputs)
        other_settings = "" if not hasattr(other, 'settings') or other.settings == None else str(other.settings)
        other_modules = "" if not hasattr(other, 'modules') or other.modules == None else str(other.modules)

        return (self_name == other_name) and (self_type == other_type) and (self_schema_version == other_schema_version) \
            and (self_description == other_description) and (self_modules == other_modules) \
            and (self_inputs == other_inputs) and (self_outputs == other_outputs) and (self_settings == other_settings)

    def __hash__(self):
        self_name = "" if self.name == None else self.name
        self_schema_version = "" if self.schema_version == None else self.schema_version
        self_type = "" if not hasattr(self, 'type') or self.type == None else self.type
        self_description = "" if not hasattr(self, 'description') or self.description == None else self.description
        self_inputs = "" if not hasattr(self, 'inputs') or self.inputs == None else str(self.inputs)
        self_outputs = "" if not hasattr(self, 'outputs') or self.outputs == None else str(self.outputs)
        self_settings = "" if not hasattr(self, 'settings') or self.settings == None else str(self.settings)
        self_modules = "" if not hasattr(self, 'modules') or self.modules == None else str(self.modules)

        return hash((self_name, self_schema_version, self_type, self_description, self_inputs, self_outputs, self_settings, self_modules))

    def remove_null_entries(self):
        if self.name == None:
            del self.name
        if hasattr(self, 'type') and self.type == None:
            del self.type
        if hasattr(self, 'schema_version') and self.schema_version == None:
            del self.schema_version
        if hasattr(self, 'description') and self.description == None:
            del self.description

        if hasattr(self, 'inputs'):
            if self.inputs == None or (self.inputs != None and len(self.inputs) == 0):
                del self.inputs
            else:
                for p in self.inputs:
                    p.remove_null_entries()

        if hasattr(self, 'outputs'):
            if self.outputs == None or (self.outputs != None and len(self.outputs) == 0):
                del self.outputs
            else:
                for p in self.outputs:
                    p.remove_null_entries()

        if hasattr(self, 'settings'):
            if self.settings == None or (self.settings != None and len(self.settings) == 0):
                del self.settings
            else:
                for p in self.settings:
                    p.remove_null_entries()

        if hasattr(self, 'modules'):
            if self.modules == None or (self.modules != None and len(self.modules) == 0):
                del self.modules
            else:
                for m in self.modules:
                    m.remove_null_entries()

    def to_yaml(self, stream = None):
        self.remove_null_entries()
        errors = self.validate()
        # eprint(errors)

        return (yaml.safe_load(self.to_yaml_str()), errors)

    def _process_comment(self, s):
        out_str = ""
        pattern = 'comment:'
        if pattern not in s:
            return s
        re_loc = re.finditer(pattern=pattern, string=s)
        loc = [ind.start() for ind in re_loc]
        start = 0
        for i in loc:
            out_str += s[start:i]
            j = s.find("'", i)
            out_str += s[i:j+1]
            j += 1

            while True:
                k = s.find("'", j)
                n = s.find("\n", j)
                if k < n:
                    out_str += s[j:k+1]
                    start = k+1
                    break
                else:
                    out_str += s[j:n+1]
                    k = n+1
                    lenn = 0
                    while True:
                        if s[k] != ' ':
                            break
                        k += 1
                        lenn += 1
                    k = 0
                    while k < lenn-2:
                        out_str += ' '
                        k += 1
                    out_str += '#'
                    out_str += ' '
                    j=n+1+lenn
                    start = j
        return out_str

    def to_yaml_str(self, do_validate = True, stream = None) -> str:
        self.remove_null_entries()
        errors = []
        if do_validate:
            errors = self.validate()
            # if len(errors) > 0:
            #     eprint(errors)
        yaml_str = yaml.dump(self, sort_keys=False)
        yaml_str = yaml_str.replace("comment: ", "# comment: ")
        yaml_str = self._process_comment(yaml_str)
        return (yaml_str, errors)

    def generate_input_file(self, stream = None):
        inputs_data = {}
        for p in self.inputs:
            if p.type != None:
                if p.type.lower() == "string" :
                    inputs_data[p.name] = "sample string for " + p.name
                elif p.type.lower() == "boolean" :
                    inputs_data[p.name] = False
                elif p.type.lower() == "number" or p.type.tolower() == "integer" :
                    inputs_data[p.name] = 0
                elif p.type.lower() == "date" or p.type.tolower() == "date-time" :
                    inputs_data[p.name] = "2000-12-30 19:21:03.478039"
                else:
                    inputs_data[p.name] = ""
            else:
                inputs_data[p.name] = ""

        return yaml.dump(inputs_data, sort_keys=False)

    @classmethod
    def from_yaml_str(cls, yaml_str):
        yaml_data = yaml.safe_load(yaml_str)
        bp = Blueprint.from_yaml_data(yaml_data)
        return cls(bp.name, bp.description,
                    bp.inputs, bp.outputs, bp.settings,
                    bp.modules)

    @classmethod
    def from_yaml_data(cls, yaml_data):
        value_ref = dict()
        name = yaml_data['name']
        try:
            description = yaml_data['description']
        except KeyError:
            description = None

        try:
            inputs=[]
            for p in yaml_data['inputs']:
                in_param = param.Input.from_yaml(p)
                value_ref.update({"$blueprint." + in_param.name : "$blueprint.inputs." + in_param.name})
                inputs.append(in_param)
        except (KeyError, UnboundLocalError, TypeError):
            inputs = None

        try:
            outputs=[]
            for p in yaml_data['outputs']:
                out_param = param.Output.from_yaml(p)
                value_ref.update({"$blueprint." + out_param.name : "$blueprint.outputs." + out_param.name})
                outputs.append(out_param)

        except (KeyError, UnboundLocalError, TypeError):
            outputs = None

        try:
            settings=[]
            for p in yaml_data['settings']:
                env_param = param.Setting.from_yaml(p)
                value_ref.update({"$blueprint." + env_param.name : "$blueprint.settings." + env_param.name})
                settings.append(env_param)

        except (KeyError, UnboundLocalError, TypeError):
            settings = None

        try:
            modules=[]
            for d in yaml_data['modules']:
                mod = module.Module.from_yaml(d)
                mod.repair_module(value_ref)
                modules.append(mod)

        except (KeyError, UnboundLocalError, TypeError):
            modules = None

        return cls(name, description,
                    inputs, outputs, settings, modules)

    def validate(self):
        bp_validator = blueprint_validator.BlueprintModel(self)
        return bp_validator.validate()

    def input_ref(self, key): # -> (str, event.ValidationEvent):
        if hasattr(self, "inputs") and self.inputs != None:
            for p in self.inputs:
                if p.name == key:
                    return ("$blueprint.inputs." + key, None)
        return (None, event.ValidationEvent(event.BPWarning, 'Blueprint input parameter not found', self))

    def output_ref(self, key): # -> (str, event.ValidationEvent):
        if hasattr(self, "outputs") and self.outputs != None:
            for p in self.outputs:
                if p.name == key:
                    return ("$blueprint.outputs." + key, None)
        return (None, event.ValidationEvent(event.BPWarning, 'Blueprint output parameter not found', self))

    def setting_ref(self, key): # -> (str, event.ValidationEvent):
        if hasattr(self, "settings") and self.settings != None:
            for p in self.settings:
                if p.name == key:
                    return ("$blueprint.settings." + key, None)
        return (None, event.ValidationEvent(event.BPWarning, 'Blueprint settings parameter not found', self))

    def input_param(self, key): # -> (param.Input, event.ValidationEvent):
        if hasattr(self, "inputs") and self.inputs != None:
            for p in self.inputs:
                if p.name == key:
                    return (p, None)
        return (None, event.ValidationEvent(event.BPWarning, 'Blueprint input parameter not found', self))

    def output_param(self, key): # -> (param.Output, event.ValidationEvent):
        if hasattr(self, "outputs") and self.outputs != None:
            for p in self.outputs:
                if p.name == key:
                    return (p, None)
        return (None, event.ValidationEvent(event.BPWarning, 'Blueprint output parameter not found', self))

    def setting_param(self, key): # -> (param.Setting, event.ValidationEvent):
        if hasattr(self, "settings") and self.settings != None:
            for p in self.settings:
                if p.name == key:
                    return (p, None)
        return (None, event.ValidationEvent(event.BPWarning, 'Blueprint settings parameter not found', self))

    def module_ref(self, mod_name, key): # -> (str, event.ValidationEvent):
        if hasattr(self, "modules") and self.modules != None:
            (m, err) = self.get_module(mod_name)
            if err == None:
                (mod_ref, err) = m.module_ref(key)
                if err == None:
                    return (mod_ref, None)
                else:
                    return (None, err)
            else:
                return (None, err)
        return (None, event.ValidationEvent(event.BPWarning, 'Invalid modules in blueprint', self))

    def module_input_ref(self, mod_name, key): # -> (str, event.ValidationEvent):
        (m, err) = self.get_module(mod_name)
        if err == None:
            (mod_ref, err) = m.input_ref(key)
            if err == None:
                return (mod_ref, None)
            else:
                return (None, event.ValidationEvent(event.BPWarning, 'Invalid module input, in the blueprint', self, None, chain = err))
        else:
            return (None, event.ValidationEvent(event.BPWarning, 'Invalid module input, in the blueprint', self, None, chain = err))

    def module_output_ref(self, mod_name, key): # -> (str, event.ValidationEvent):
        (m, err) = self.get_module(mod_name)
        if err == None:
            (mod_ref, err) = m.output_ref(key)
            if err == None:
                return (mod_ref, None)
            else:
                return (None, event.ValidationEvent(event.BPWarning, 'Invalid module output in blueprint', self, None, chain=err))
        else:
            return (None, event.ValidationEvent(event.BPWarning, 'Invalid module output in blueprint', self, None, chain=err))

    def module_setting_ref(self, mod_name, key): # -> (str, event.ValidationEvent):
        (m, err) = self.get_module(mod_name)
        if err == None:
            (mod_ref, err) = m.setting_ref(key)
            if err == None:
                return (mod_ref, None)
            else:
                return (None, event.ValidationEvent(event.BPWarning, 'Invalid module setting in blueprint', self, None, chain=err))
        else:
            return (None, event.ValidationEvent(event.BPWarning, 'Invalid module setting in blueprint', self, None, chain=err))

    def list_input_param_names(self) -> List[str]:
        if hasattr(self, "inputs") and self.inputs != None:
            param_names = []
            for p in self.inputs:
                param_names.append(p.name)
            return param_names
        return None

    def set_inputs(self,
                    input_params: List[param.Input]) -> List[event.ValidationEvent]:
        errors = []
        if(input_params == None):
            self.inputs = None
            return errors
        for param in input_params:
            errors += param.validate()

        # Set the input params, even if there are errors in them
        self.inputs = []
        self.inputs.extend(list(input_params))
        return errors

    def add_input(self,
                    input_param: param.Input) -> List[event.ValidationEvent]:
        if self.inputs == None:
            self.inputs = []
        errors = input_param.validate()

        # Add input param, even if there are errors in them
        self.inputs.append(input_param)
        return errors

    def update_input(self,
                    input_param: param.Input) -> List[event.ValidationEvent]:
        if self.inputs == None:
            self.inputs = []
        errors = input_param.validate()

        # Find and update the input param, even if there are errors in the input_param
        for param in self.inputs:
            if param.name == input_param.name:
                param.type          = input_param.type      if hasattr(input_param, 'type') else None
                param.description   = input_param.description if hasattr(input_param, 'description') else None
                param.value         = input_param.value     if hasattr(input_param, 'value') else None
                param.default       = input_param.default   if hasattr(input_param, 'default') else None
                param.optional      = input_param.optional  if hasattr(input_param, 'optional') else None

        return errors

    def add_inputs(self,
                    input_params: List[param.Input]) -> List[event.ValidationEvent]:
        if self.inputs == None:
            self.inputs = []
        errors = []
        for param in input_params:
            errors += param.validate()

        self.inputs.extend(list(input_params))
        return errors

    def set_input_value(self,
                    param_name: str,
                    param_value: str,
                    param_type:str = "string") -> List[event.ValidationEvent]:
        errors = []
        param_copy = []
        for p in self.inputs:
            if p.name == param_name:
                p.value = param_value
                p.type = param_type
            e = p.validate()
            if len(e) == 0:
                param_copy.append(p)
            errors += e
        self.inputs = param_copy
        return errors

    def list_output_param_names(self) -> List[str]:
        if hasattr(self, "outputs") and self.outputs != None:
            param_names = []
            for p in self.outputs:
                param_names.append(p.name)
            return param_names
        return None

    def set_outputs(self, output_params) -> List[event.ValidationEvent]:
        errors = []
        if(output_params == None):
            self.outputs = None
            return errors
        for param in output_params:
            errors += param.validate()

        self.outputs = []
        self.outputs.extend(output_params)
        return errors

    def add_output(self, output_param) -> List[event.ValidationEvent]:
        if self.outputs == None:
            self.outputs = []
        errors = output_param.validate()

        self.outputs.append(output_param)
        return errors

    def add_outputs(self, output_params) -> List[event.ValidationEvent]:
        errors = []
        if self.outputs == None:
            self.outputs = []
        for param in output_params:
            errors += param.validate()

        self.outputs.extend(list(output_params))
        return errors

    def update_output(self,
                    output_param: param.Output) -> List[event.ValidationEvent]:
        if self.outputs == None:
            self.outputs = []
        errors = output_param.validate()

        # Find and update the output param, even if there are errors in the output_param
        for param in self.outputs:
            if param.name == output_param.name:
                param.type          = output_param.type     if hasattr(output_param, 'type') else None
                param.description   = output_param.description if hasattr(output_param, 'description') else None
                param.value         = output_param.value    if hasattr(output_param, 'value') else None

        return errors

    def set_output_value(self, param_name, param_value) -> List[event.ValidationEvent]:
        errors = []
        param_copy = []
        for p in self.outputs:
            if p.name == param_name:
                p.value = param_value
            e = p.validate()
            if len(e) == 0:
                param_copy.append(p)
            errors += e
        self.outputs = param_copy
        return errors

    def list_setting_param_names(self) -> List[str]:
        if hasattr(self, "inputs") and self.inputs != None:
            param_names = []
            for p in self.settings:
                param_names.append(p.name)
            return param_names
        return None

    def set_settings(self, setting_params) -> List[event.ValidationEvent]:
        errors = []
        if(setting_params == None):
            self.settings = None
            return
        for param in setting_params:
            errors += param.validate()

        self.settings = []
        self.settings.extend(setting_params)
        return errors

    def add_setting(self, setting_param) -> List[event.ValidationEvent]:
        if self.settings == None:
            self.settings = []
        errors = setting_param.validate()

        self.settings.append(setting_param)
        return errors

    def add_settings(self, setting_params) -> List[event.ValidationEvent]:
        errors = []
        if self.settings == None:
            self.settings = []
        for param in setting_params:
            errors += param.validate()

        self.settings.extend(list(setting_params))
        return errors

    def update_setting(self,
                    setting_param: param.Setting) -> List[event.ValidationEvent]:
        if self.settings == None:
            self.settings = []
        errors = setting_param.validate()

        # Find and update the output param, even if there are errors in the output_param
        for param in self.settings:
            if param.name == setting_param.name:
                param.type          = setting_param.type        if hasattr(setting_param, 'type') else None
                param.description   = setting_param.description if hasattr(setting_param, 'description') else None
                param.value         = setting_param.value       if hasattr(setting_param, 'value') else None
                param.default       = setting_param.default     if hasattr(setting_param, 'default') else None

        return errors

    def set_setting_value(self, param_name, param_value) -> List[event.ValidationEvent]:
        errors = []
        param_copy = []
        for p in self.settings:
            if p.name == param_name:
                p.value = param_value
            e = p.validate()
            if len(e) == 0:
                param_copy.append(p)
            errors += e
        self.settings = param_copy
        return errors

    def get_modules(self) -> List[module.Module]:
        return self.modules

    def get_module(self, mod_name): # -> (module.Module, event.ValidationEvent):
        if hasattr(self, 'modules') and self.modules != None:
            for m in self.modules:
                if mod_name == m.name:
                    return (m, None)
        return (None, event.ValidationEvent(event.BPWarning, 'Invalid module name: ' + str(mod_name), self))

    def set_modules(self, mods) -> List[event.ValidationEvent]:
        if(mods == None):
            self.modules = None
            return
        self.modules = []
        errors = []
        for mod in mods:
            e = mod.validate()
            if len(e) == 0:
                self.modules.append(mod)
            else:
                errors += e
        return errors

    def add_module(self, mod) -> List[event.ValidationEvent]:
        errors = []
        errors = mod.validate()

        if self.modules == None:
            self.modules = []
        self.modules.append(mod)

        return errors

    def add_modules(self, mods) -> List[event.ValidationEvent]:
        if(mods == None):
            self.modules = None
            return
        if self.modules == None:
            self.modules = []
        errors = []
        for mod in mods:
            e = mod.validate()
            if len(e) == 0:
                self.modules.append(mod)
            else:
                errors += e
        return errors

    def update_module(self, mod) -> List[event.ValidationEvent]:
        errors = []
        if mod != None:
            if self.modules == None:
                self.modules = []
            try:
                (m, err) = self.get_module(mod.name)
                if err == None:
                    m.merge(mod)
                else:
                    errors += err
                return errors
            except:
                self.modules.append(mod)
        return errors

    def set_module_inputs(self, mod_name, inputs) -> List[event.ValidationEvent]:
        (m, err) = self.get_module(mod_name)
        if err == None:
            err = m.set_inputs(inputs)
        return err

#========================================================================

    def find_replace_in_module(self, param_ref, value) -> List[event.ValidationEvent]:
        errors = []
        if self.modules != None:
            for mod in self.modules:
                param_ref_alias = param_ref.replace("$blueprint.inputs.", "$blueprint.")
                param_name = mod.find_input_ref(param_ref)
                if param_name == None:
                    param_name = mod.find_input_ref(param_ref_alias)
                if param_name != None:
                    mod.set_input_value(param_name, value)
                else:
                    param_name = mod.find_setting_ref(param_ref)
                    if param_name == None:
                        param_name = mod.find_setting_ref(param_ref_alias)
                    if param_name != None:
                        mod.set_setting_value(param_name, value)

                param_ref_alias = param_ref.replace("$blueprint.settings.", "$blueprint.")
                param_name = mod.find_setting_ref(param_ref)
                if param_name == None:
                    param_name = mod.find_setting_ref(param_ref_alias)
                if param_name != None:
                    mod.set_setting_value(param_name, value)
                else:
                    param_name = mod.find_input_ref(param_ref)
                    if param_name == None:
                        param_name = mod.find_input_ref(param_ref_alias)
                    if param_name != None:
                        mod.set_input_value(param_name, value)

        return errors

    def propagate_blueprint_input_data(self) -> List[event.ValidationEvent]:
        errors = []
        for p in self.inputs:
            if p.value != None:
                (p_ref, err) = self.input_ref(p.name)
                if err != None:
                    self.find_replace_in_module(p_ref, p.value)

        for p in self.settings:
            if p.value != None:
                (p_ref, err) = self.setting_ref(p.name)
                if err != None:
                    self.find_replace_in_module(p_ref, p.value)

        return errors

    def propagate_module_data(self, module_data) -> List[event.ValidationEvent]:
        # module_data -> dict
        errors = []
        if self.modules != None:
            for mod in self.modules:
                for p in mod.inputs:
                    if p.value != None and p.value in module_data.keys():
                        mod.set_input_value(p.name, module_data[p.value])

                for p in mod.settings:
                    if p.value != None and p.value in module_data.keys():
                        mod.set_setting_value(p.name, module_data[p.value])

        if self.outputs != None:
            for p in self.outputs:
                if p.value != None:
                    if p.value != None and p.value in module_data.keys():
                        self.set_output_value(p.name, module_data[p.value])

        return errors

#======================================================================

    def build_dag(self) -> dag.BlueprintGraph:
        g = dag.BlueprintGraph()
        if hasattr(self, "modules") and self.modules != None:
            for m in self.modules:
                iref = m.input_value_refs()
                for i in iref:
                    if i.startswith("$module.") :
                        n = i[8:i.find(".", 8)]
                        if n != m.name:
                            g.addEdge(m.name, n)
                    elif i.startswith("$blueprint.") :
                        g.addEdge(m.name, "blueprint")

                oref = m.output_refs()
                for o in oref:
                    if o.startswith("$module.") :
                        n = o[8:o.find(".", 8)]
                        if n != m.name:
                            g.addEdge(n, m.name)
                    elif o.startswith("$blueprint.") :
                        g.addEdge("blueprint", m.name)

                g.addEdge("root", m.name)

        return g

    def is_dag_empty(self, bp_graph: dag.BlueprintGraph) -> bool:
        return bp_graph.isEmpty()

    def dag_next_node(self, bp_graph: dag.BlueprintGraph):
        if bp_graph.isEmpty():
            return None

        while not bp_graph.isEmpty():
            node_name = bp_graph.getAnIndependentNode()
            if node_name == "root" or node_name == "blueprint":
                bp_graph.popNode(node_name)
            else:
                return node_name

        return None

#======================================================================


import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F

import torch.utils.data
import math
device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu")
class LayerNorm(nn.Module):
    """
    layer norm
    """
    def __init__(self,feature_dim,eps=1e-6):
        super(LayerNorm,self).__init__()
        self.gamma = nn.Parameter(torch.ones((feature_dim,)))
        self.register_parameter("gamma",self.gamma)
        self.beta = nn.Parameter(torch.zeros((feature_dim,)))
        self.register_parameter("beta",self.beta)
        self.eps = eps

    def forward(self,x):
        #x:[batch_size,embed_dim]
        mean = x.mean(-1,keepdim=True)
        std = x.std(-1,keepdim=True)
        return self.gamma*(x-mean)/(std+self.eps)+self.beta

def get_activation_function(activation,context_str):
    if activation == "leakyrelu":
        return nn.LeakyReLU(negative_slope=0.2)
    elif activation == "relu":
        return nn.ReLU()
    elif activation == "sigmoid":
        return nn.Sigmoid()
    elif activation == "tanh":
        return nn.Tanh()
    else:
        raise Exception("{} activation not recognized.".format(context_str+' '+activation))

class SingleFeedForwardNN(nn.Module):
    """
    single layer fully connected feed forward neural network
    """
    def __init__(self,input_dim,
                    output_dim,
                    dropout_rate=None,
                    activation="sigmoid",
                    use_layernormalize=False,
                    skip_connection=False,
                    context_str=""):
        super(SingleFeedForwardNN,self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim

        if dropout_rate is not None:
            self.dropout = nn.Dropout(p=dropout_rate)
        else:
            self.dropout = None

        self.act = get_activation_function(activation,context_str)

        if use_layernormalize:
            self.layernorm = nn.LayerNorm(self.output_dim)
        else:
            self.layernorm = None

        if self.input_dim == self.output_dim:
            self.skip_connection = skip_connection
        else:
            self.skip_connection = False

        self.linear = nn.Linear(self.input_dim,self.output_dim)
        nn.init.xavier_uniform(self.linear.weight)

    def forward(self,input_tensor):
        assert input_tensor.size()[-1] == self.input_dim
        #Linear layer

        input_tensor = input_tensor.to(device)
        # print(device,input_tensor.is_cuda)
        output = self.linear(input_tensor)
        #non-linerity
        output = self.act(output)

        if self.dropout is not None:
            output = self.dropout(output)

        if self.skip_connection:
            output = output + input_tensor

        if self.layernorm is not None:
            output = self.layernorm(output)

        return output

class MultiLayerFeedForwardNN(nn.Module):
    """
    N fully connected feed forward NN
    """
    def __init__(self,input_dim,
                    output_dim,
                    num_hidden_layers=0,
                    dropout_rate=None,
                    hidden_dim=-1,
                    activation="sigmoid",
                    use_layernormalize=False,
                    skip_connection=False,
                    context_str=None):
        super(MultiLayerFeedForwardNN,self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.num_hidden_layers = num_hidden_layers
        self.dropout_rate = dropout_rate
        self.hidden_dim = hidden_dim
        self.activation = activation
        self.use_layernormalize = use_layernormalize
        self.skip_connection = skip_connection
        self.context_str = context_str

        self.layers = nn.ModuleList()
        if self.num_hidden_layers <= 0:
            self.layers.append(SingleFeedForwardNN(input_dim=self.input_dim,
                                                   output_dim=self.output_dim,
                                                   dropout_rate=self.dropout_rate,
                                                   activation=self.activation,
                                                   use_layernormalize=False,
                                                   skip_connection=False,
                                                   context_str=self.context_str))
        else:
            self.layers.append(SingleFeedForwardNN(input_dim=self.input_dim,
                                                   output_dim=self.hidden_dim,
                                                   dropout_rate=self.dropout_rate,
                                                   activation=self.activation,
                                                   use_layernormalize=self.use_layernormalize,
                                                   skip_connection=self.skip_connection,
                                                   context_str=self.context_str))
            for i in range(self.num_hidden_layers-1):
                self.layers.append(SingleFeedForwardNN(input_dim=self.hidden_dim,
                                                   output_dim=self.hidden_dim,
                                                   dropout_rate=self.dropout_rate,
                                                   activation=self.activation,
                                                   use_layernormalize=self.use_layernormalize,
                                                   skip_connection=self.skip_connection,
                                                   context_str=self.context_str))

            self.layers.append(SingleFeedForwardNN(input_dim=self.hidden_dim,
                                                   output_dim=self.output_dim,
                                                   dropout_rate=self.dropout_rate,
                                                   activation=self.activation,
                                                   use_layernormalize=False,
                                                   skip_connection=False,
                                                   context_str=self.context_str))

    def forward(self,input_tensor):
        assert input_tensor.size()[-1] == self.input_dim
        input_tensor = input_tensor.to(device)
        # print(input_tensor.is_cuda)
        output = input_tensor
        for i in range(len(self.layers)):
            output = self.layers[i](output)
        return output

class ResLayer(nn.Module):
    def __init__(self,linear_size):
        super(ResLayer,self).__init__()
        self.l_size = linear_size
        self.nonlin1 = nn.ReLU(inplace=True)
        self.nonlin2 = nn.ReLU(inplace=True)
        self.dropout1 = nn.Dropout()
        self.w1 = nn.Linear(self.l_size,self.l_size)
        self.w2 = nn.Linear(self.l_size,self.l_size)

    def forward(self,x):
        y = self.w1(x)
        y = self.nonlin1(y)
        y = self.dropout1(y)
        y = self.w2(y)
        y = self.nonlin2(y)
        out = x+y
        return out

class FCNet(nn.Module):
    def __init__(self,num_inputs,num_filts,num_hidden_layers):
        super(FCNet,self).__init__()
        self.num_hidden_layers = num_hidden_layers
        self.feats = nn.Sequential()
        self.feats.add_module("ln_1",nn.Linear(num_inputs,num_filts))
        self.feats.add_module("relu_1",nn.ReLU(inplace=True))
        for i in range(num_hidden_layers):
            self.feats.add_module("resnet_{}".format(i+1),ResLayer(num_filts))

    def forward(self,x):
        loc_emb = self.feats(x)
        return loc_emb


import argparse
import os
import copy
import math
import time
import random
import pickle
import torch
import numpy as np
from prettytable import PrettyTable
from multiprocessing import Pool
from tianshou.data import ReplayBuffer, Batch
from envs.expert_env import ExpertEnv
from utils.common import get_config_from_yml, generate_environment_samples, read_log_file, generate_environment_test_samples

def collect_demonstrations(expert_env):
    sample_count = expert_env.sample_count()
    demonstrations = {}
    for _ in range(sample_count):
        state = expert_env.reset()
        video, user, trace, qoe_weight = expert_env.current_video, expert_env.current_user, \
            expert_env.current_trace, expert_env.current_qoe_weight
        qoe_weight = tuple(qoe_weight.astype(np.int32).tolist())

        states, actions = [], []
        done = False
        while not done:
            action = expert_env.choose_action()
            states.append(copy.deepcopy(state))
            actions.append(action)
            state, reward, done, _ = expert_env.step(action)

        length = len(states)
        demonstration = ReplayBuffer(length)
        for i in range(length):
            demonstration.add(Batch(obs=states[i], act=actions[i], rew=0, done=(i == length - 1),
                                    obs_next=i + 1, info={}))
        demonstrations[video, user, trace, qoe_weight] = demonstration
        print(f'Demonstration of video-{video}, user-{user}, trace-{trace}, qoe-weight-{qoe_weight} done!')
    return demonstrations

def create_demonstrations(args, config, qoe_weights, models_dir, demos_dir, cache_path, mode='train'):
    log_path = os.path.join(models_dir, f'{mode}_log.csv')
    demo_path = os.path.join(demos_dir, f'{mode}_demonstrations.pkl')
    if os.path.exists(log_path):
        os.remove(log_path)

    videos = config.video_split[args.train_dataset][mode]
    users = config.user_split[args.train_dataset][mode]
    traces = config.network_split[args.network_dataset][mode]
    all_samples = generate_environment_samples(videos, users, traces, qoe_weights, seed=args.seed)
    print('Total samples:', len(all_samples))

    proc_num = args.proc_num
    samples_per_proc = math.ceil(len(all_samples) / proc_num)
    expert_envs = []
    for i in range(proc_num):
        samples = []
        for j in range(i * samples_per_proc, min((i + 1) * samples_per_proc, len(all_samples))):
            samples.append(all_samples[j])
        expert_envs.append(ExpertEnv(config, args.train_dataset, args.network_dataset, qoe_weights, samples,
                                     demos_dir, cache_path, log_path, config.startup_download, args.horizon,
                                     args.refresh_cache if i == 0 else False, mode, args.seed))

    start_time = time.time()
    pool = Pool(proc_num)
    results = []
    for i in range(proc_num):
        results.append(pool.apply_async(collect_demonstrations, (expert_envs[i],)))
    pool.close()
    pool.join()

    total_demonstrations = {}
    for result in results:
        total_demonstrations.update(result.get())
    end_time = time.time()

    pickle.dump(total_demonstrations, open(demo_path, 'wb'))
    print(f'Create {len(all_samples)} demonstrations, saved at {demo_path}, cost {round((end_time - start_time) / 3600, 2)}h')

def test(args, config, qoe_weights, results_dir, demos_dir, cache_path):
    log_path = os.path.join(results_dir, 'results.csv')
    if os.path.exists(log_path):
        os.remove(log_path)

    videos = config.video_split[args.test_dataset]['test']
    users = config.user_split[args.test_dataset]['test']
    traces = config.network_split[args.network_dataset]['test']
    all_samples = generate_environment_test_samples(videos, users, traces, qoe_weights)

    expert_env = ExpertEnv(config, args.test_dataset, args.network_dataset, qoe_weights, all_samples,
                           demos_dir, cache_path, log_path, config.startup_download, args.horizon,
                           args.refresh_cache, 'test', args.seed)
    sample_count = expert_env.sample_count()
    for i in range(sample_count):
        state = expert_env.reset()
        video, user, trace, qoe_weight = expert_env.current_video, expert_env.current_user, \
            expert_env.current_trace, expert_env.current_qoe_weight

        done = False
        while not done:
            action = expert_env.choose_action()
            state, reward, done, _ = expert_env.step(action)
        print(f'video-{video}, user-{user}, trace-{trace}, qoe-weight-{qoe_weight} done!')

    read_log_file(log_path)

def run(args, config):
    # seed
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)
    random.seed(args.seed)

    if args.qoe_train_ids is None:
        args.qoe_train_ids = list(range(len(config.qoe_split['train'])))
    split = 'train' if args.test_on_seen else 'test'
    if args.qoe_test_ids is None:
        args.qoe_test_ids = list(range(len(config.qoe_split[split])))

    models_dir = os.path.join(config.bs_models_dir, args.model, args.train_dataset + '_' + args.network_dataset, 'qoe' + '_'.join(map(str, args.qoe_train_ids)))
    demos_dir = os.path.join(config.bs_models_dir, args.model, args.train_dataset + '_' + args.network_dataset, 'qoe' + '_'.join(map(str, args.qoe_train_ids)))
    if args.test_on_seen:
        results_dir = os.path.join(config.bs_results_dir, args.model,  args.test_dataset + '_' + args.network_dataset, 'seen_qoe' + '_'.join(map(str, args.qoe_test_ids)))
    else:
        results_dir = os.path.join(config.bs_results_dir, args.model,  args.test_dataset + '_' + args.network_dataset, 'unseen_qoe' + '_'.join(map(str, args.qoe_test_ids)))
    train_cache_path = os.path.join(config.bs_models_dir, args.model, f'{args.train_dataset}_cache.pkl')
    test_cache_path = os.path.join(config.bs_models_dir, args.model, f'{args.test_dataset}_cache.pkl')
    if not os.path.exists(models_dir):
        os.makedirs(models_dir)
    if not os.path.exists(demos_dir):
        os.makedirs(demos_dir)
    if not os.path.exists(results_dir):
        os.makedirs(results_dir)

    if args.train:
        qoe_weights = [config.qoe_split['train'][i] for i in args.qoe_train_ids]
        print("Training QoE weights:", qoe_weights)
        create_demonstrations(args, config, qoe_weights, models_dir, demos_dir, train_cache_path, 'train')
    if args.valid:
        qoe_weights = [config.qoe_split['valid'][i] for i in args.qoe_train_ids]
        print('Validating QoE weights:', qoe_weights)
        create_demonstrations(args, config, qoe_weights, models_dir, demos_dir, train_cache_path, 'valid')
    if args.test:
        qoe_weights = [config.qoe_split[split][i] for i in args.qoe_test_ids]
        print('Testing QoE weights:', qoe_weights)
        test(args, config, qoe_weights, results_dir, demos_dir, test_cache_path)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--logdir', type=str, default='log_tensorboard')
    parser.add_argument('--model', type=str, default='expert')
    parser.add_argument("--train", action='store_true')
    parser.add_argument("--valid", action='store_true')
    parser.add_argument("--test", action='store_true')
    parser.add_argument("--test-on-seen", action='store_true')
    parser.add_argument('--train-dataset', type=str, default='Wu2017')
    parser.add_argument('--test-dataset', type=str, default='Wu2017')
    parser.add_argument('--network-dataset', type=str, default='4G')
    parser.add_argument('--qoe-train-ids', type=int, nargs='*')  # train on one or more qoe
    parser.add_argument('--qoe-test-ids', type=int, nargs='*')  # test on one or more qoe
    parser.add_argument('--proc-num', type=int, help='Process number for multiprocessing')
    parser.add_argument('--horizon', type=int, help='The horizon for expert to look ahead', default=4)
    parser.add_argument('--refresh-cache', action='store_true', help='Refresh cache')
    parser.add_argument('--seed', type=int, default=1)
    args = parser.parse_known_args()[0]

    # Train/Valid
    # python run_expert.py --train-dataset Jin2022 --train --valid --horizon 4 --proc-num 8

    # Test
    # python run_expert.py --test-dataset Jin2022 --test --horizon 2 --qoe-test-ids 3 --test-on-seen

    print(args)

    config = get_config_from_yml()
    run(args, config)