import abc
import pandas as pd
from six import iteritems
from toolz import merge

from .base import PipelineLoader
from .frame import DataFrameLoader
from .utils import previous_event_frame, next_event_frame
from zipline.pipeline.common import TS_FIELD_NAME
from zipline.utils.numpy_utils import NaTD

WRONG_COLS_ERROR = "Expected columns {expected_columns} for sid {sid} but " \
                   "got columns {resulting_columns}."

WRONG_SINGLE_COL_DATA_FORMAT_ERROR = ("Data for sid {sid} is expected to have "
                                      "1 column and to be in a DataFrame, "
                                      "Series, or DatetimeIndex.")

WRONG_MANY_COL_DATA_FORMAT_ERROR = ("Data for sid {sid} is expected to have "
                                    "more than 1 column and to be in a "
                                    "DataFrame.")

SERIES_NO_DTINDEX_ERROR = ("Got Series for sid {sid}, but index was not "
                           "DatetimeIndex.")

DTINDEX_NOT_INFER_TS_ERROR = ("Got DatetimeIndex for sid {sid}.\n"
                              "Pass `infer_timestamps=True` to use the first "
                              "date in `all_dates` as implicit timestamp.")

DF_NO_TS_NOT_INFER_TS_ERROR = ("Got DataFrame without a '{"
                               "timestamp_column_name}' column for sid {sid}."
                               "\nPass `infer_timestamps=True` to use the "
                               "first date in `all_dates` as implicit "
                               "timestamp.")

class EventsLoader(PipelineLoader):
    """
    Abstract loader.

    Does not currently support adjustments to the dates of known events.

    Parameters
    ----------
    all_dates : pd.DatetimeIndex
        Index of dates for which we can serve queries.
    events_by_sid : dict[int -> pd.DataFrame or pd.Series or pd.DatetimeIndex]
        Dict mapping sids to objects representing dates on which earnings
        occurred.

        If a dict value is a Series, it's interpreted as a mapping from the
        date on which we learned an announcement was coming to the date on
        which the announcement was made.

        If a dict value is a DatetimeIndex, it's interpreted as just containing
        the dates that announcements were made, and we assume we knew about the
        announcement on all prior dates.  This mode is only supported if
        ``infer_timestamp`` is explicitly passed as a truthy value.
        Dict mapping sids to DataFrames, Series, or DatetimeIndexes.

        If the value is a DataFrame, it then represents dates on which events
        occurred along with other associated values. If the DataFrame
        contains a "timestamp" column, that column is interpreted as the date
        on which we learned about the event. If the DataFrames do not contain a
         "timestamp" column, we assume we knew about the event on all prior
         dates.  This mode is only supported if ``infer_timestamp`` is
         explicitly passed as a truthy value.

    infer_timestamps : bool, optional
        Whether to allow omitting the "timestamp" column.
    dataset : DataSet
        The DataSet object for which this loader loads data.

    """

    @abc.abstractproperty
    def expected_cols(self):
        raise NotImplemented('expected_cols')

    def __init__(self,
                 all_dates,
                 events_by_sid,
                 infer_timestamps=False,
                 dataset=None):
        self.all_dates = all_dates
        # Do not modify the original in place, since it may be used for other
        #  purposes.
        self.events_by_sid = (
            events_by_sid.copy()
        )
        dates = self.all_dates.values

        for k, v in iteritems(events_by_sid):
            # Already a DataFrame
            if isinstance(v, pd.DataFrame):
                if TS_FIELD_NAME not in v.columns:
                    if not infer_timestamps:
                        raise ValueError(
                            DF_NO_TS_NOT_INFER_TS_ERROR.format(
                                timestamp_column_name=TS_FIELD_NAME,
                                sid=k
                            )
                        )
                    self.events_by_sid[k] = v = v.copy()
                    v.index = [dates[0]] * len(v)
                else:
                    self.events_by_sid[k] = v.set_index(TS_FIELD_NAME)
                # Once data is in a DF, make sure columns are correct.
                cols_except_ts = (set(v.columns) -
                                  {TS_FIELD_NAME})

                # Check that all columns other than timestamp are as expected.
                if cols_except_ts != self.expected_cols:
                    raise ValueError(
                        WRONG_COLS_ERROR.format(
                            expected_columns=list(self.expected_cols),
                            sid=k,
                            resulting_columns=v.columns.values
                        )
                    )
            # Not a DataFrame and we only expect 1 column
            elif len(self.expected_cols) == 1:
                # First, must convert to DataFrame.
                if isinstance(v, pd.Series):
                    if not isinstance(v.index, pd.DatetimeIndex):
                        raise ValueError(
                            SERIES_NO_DTINDEX_ERROR.format(sid=k)
                        )
                    self.events_by_sid[k] = pd.DataFrame({
                        list(self.expected_cols)[0]: v})
                elif isinstance(v, pd.DatetimeIndex):
                    if not infer_timestamps:
                        raise ValueError(
                            DTINDEX_NOT_INFER_TS_ERROR.format(sid=k)
                        )
                    self.events_by_sid[k] = pd.DataFrame({
                        list(self.expected_cols)[0]: v
                    }, index=[dates[0]] * len(v))
                else:
                    # We expect 1 column, but we got something other than a
                    # Series, DatetimeIndex, or DataFrame.
                    raise ValueError(
                        WRONG_SINGLE_COL_DATA_FORMAT_ERROR.format(sid=k)
                    )
            else:
                # We expected multiple columns, but we got something other
                # than a DataFrame.
                raise ValueError(
                    WRONG_MANY_COL_DATA_FORMAT_ERROR.format(sid=k)
                )

        self.dataset = dataset

    def get_loader(self, column):
        if column in self.dataset.columns:
            return getattr(self, "%s_loader" % column.name)
        raise ValueError("Don't know how to load column '%s'." % column)

    def load_adjusted_array(self, columns, dates, assets, mask):
        return merge(
            self.get_loader(column).load_adjusted_array(
                [column], dates, assets, mask
            )
            for column in columns
        )

    def _next_event_date_loader(self, next_date_field, event_date_field_name):
        return DataFrameLoader(
            next_date_field,
            next_event_frame(
                self.events_by_sid,
                self.all_dates,
                next_date_field.missing_value,
                next_date_field.dtype,
                event_date_field_name,
                event_date_field_name
            ),
            adjustments=None,
        )

    def _next_event_value_loader(self,
                                 next_value_field,
                                 event_date_field_name,
                                 value_field_name):
        return DataFrameLoader(
            next_value_field,
            next_event_frame(
                self.events_by_sid,
                self.all_dates,
                next_value_field.missing_value,
                next_value_field.dtype,
                event_date_field_name,
                value_field_name
            ),
            adjustments=None,
        )

    def _previous_event_date_loader(self,
                                    prev_date_field,
                                    event_date_field_name):
        return DataFrameLoader(
            prev_date_field,
            previous_event_frame(
                self.events_by_sid,
                self.all_dates,
                NaTD,
                'datetime64[ns]',
                event_date_field_name,
                event_date_field_name
            ),
            adjustments=None,
        )

    def _previous_event_value_loader(self,
                                     previous_value_field,
                                     event_date_field_name,
                                     value_field_name):
        return DataFrameLoader(
            previous_value_field,
            previous_event_frame(
                self.events_by_sid,
                self.all_dates,
                previous_value_field.missing_value,
                previous_value_field.dtype,
                event_date_field_name,
                value_field_name
            ),
            adjustments=None,
        )


import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,5,11)
y=x**2

print(x)
print("y is",y)

plt.plot(x,y,'r')
plt.xlabel('x-label')
plt.ylabel('y-label')
plt.show()


from flask import Flask, jsonify, request
import json

app = Flask(__name__)

# após a barra do caminho, digitar uma id ou blalboa.com.br/app.ro
@app.route('/<int:id>')
def pessoas(id):
    return jsonify({'id' : id, 'nome' : 'Weslley', 'profissao' : 'Estudante'})

# @app.route('/soma/<int:valor1>/<int:valor2>/<int:valor3>/')
# def soma(valor1, valor2, valor3):
#     return jsonify({'soma' : valor1+valor2+valor3})

@app.route('/soma', methods = ['POST', 'GET'])
def soma():
    if request.method == 'POST':
        dados = json.loads(request.data)
        total = sum(dados['valores'])
    elif request.method == 'GET':
        total = 10 + 10
    return {'soma' : total}

if __name__ == "__main__":
    app.run(debug= True)

Python com Flask e REST API/primeira_api/app.py


# -*- coding: utf-8 -*-
from collections import OrderedDict
from io import BytesIO

import numpy as np
import pytest

import megengine as mge
import megengine.functional as F
from megengine import Parameter, Tensor, tensor
from megengine.device import get_device_count
from megengine.module import (
    BatchNorm1d,
    BatchNorm2d,
    Conv1d,
    Conv2d,
    Dropout,
    GroupNorm,
    InstanceNorm,
    Linear,
    MaxPool2d,
    Module,
    Sequential,
    Softmax,
)
from megengine.module.module import _access_structure
from megengine.quantization.quantize import quantize, quantize_qat
from megengine.traced_module import TracedModule, trace_module
from megengine.utils.module_utils import get_expand_structure, set_expand_structure

class MLP(Module):
    def __init__(self):
        super().__init__()
        self.dense0 = Linear(28, 50)
        self.dense1 = Linear(50, 20)

    def forward(self, x):
        x = self.dense0(x)
        x = F.relu(x)
        x = self.dense1(x)
        return x

class MyModule(Module):
    class InnerModule(Module):
        def __init__(self):
            super().__init__()
            self.bn = BatchNorm2d(4)

        def forward(self, x):
            return self.bn(x)

    def __init__(self):
        super().__init__()
        self.i = self.InnerModule()
        self.bn = BatchNorm2d(4)
        self.param = Parameter(np.ones(1, dtype=np.float32))
        self.buff = Tensor(np.ones(1, dtype=np.float32))

    def forward(self, x):
        x = self.i(x)
        x = self.bn(x)
        return x

@pytest.mark.parametrize("test_traced_module", [True, False])
def test_module_api(test_traced_module):
    m = MyModule()
    if test_traced_module:
        buff = m.buff
        param = m.param
        m = trace_module(m, Tensor(np.random.random((1, 4, 16, 16))))
        assert "buff" not in m.__dict__
        assert "param" not in m.__dict__
        m.buff = buff
        m.param = param

    assert list(m.children()) == [m.bn, m.i]
    assert list(m.named_children()) == [("bn", m.bn), ("i", m.i)]
    assert list(m.modules()) == [m, m.bn, m.i, m.i.bn]
    assert list(m.named_modules()) == [
        ("", m),
        ("bn", m.bn),
        ("i", m.i),
        ("i.bn", m.i.bn),
    ]
    assert list(m.named_modules(prefix="x")) == [
        ("x", m),
        ("x.bn", m.bn),
        ("x.i", m.i),
        ("x.i.bn", m.i.bn),
    ]
    assert list(m.buffers()) == [
        m.bn.running_mean,
        m.bn.running_var,
        m.buff,
        m.i.bn.running_mean,
        m.i.bn.running_var,
    ]
    assert list(m.buffers(recursive=False)) == [m.buff]
    assert list(m.named_buffers()) == [
        ("bn.running_mean", m.bn.running_mean),
        ("bn.running_var", m.bn.running_var),
        ("buff", m.buff),
        ("i.bn.running_mean", m.i.bn.running_mean),
        ("i.bn.running_var", m.i.bn.running_var),
    ]
    assert list(m.parameters()) == [
        m.bn.bias,
        m.bn.weight,
        m.i.bn.bias,
        m.i.bn.weight,
        m.param,
    ]
    assert list(m.named_parameters()) == [
        ("bn.bias", m.bn.bias),
        ("bn.weight", m.bn.weight),
        ("i.bn.bias", m.i.bn.bias),
        ("i.bn.weight", m.i.bn.weight),
        ("param", m.param),
    ]
    assert list(m.tensors()) == [
        m.bn.bias,
        m.bn.running_mean,
        m.bn.running_var,
        m.bn.weight,
        m.buff,
        m.i.bn.bias,
        m.i.bn.running_mean,
        m.i.bn.running_var,
        m.i.bn.weight,
        m.param,
    ]
    assert list(m.named_tensors()) == [
        ("bn.bias", m.bn.bias),
        ("bn.running_mean", m.bn.running_mean),
        ("bn.running_var", m.bn.running_var),
        ("bn.weight", m.bn.weight),
        ("buff", m.buff),
        ("i.bn.bias", m.i.bn.bias),
        ("i.bn.running_mean", m.i.bn.running_mean),
        ("i.bn.running_var", m.i.bn.running_var),
        ("i.bn.weight", m.i.bn.weight),
        ("param", m.param),
    ]
    m.eval()
    assert (
        m.training == False
        and m.bn.training == False
        and m.i.training == False
        and m.i.bn.training == False
    )
    m.bn.train()
    assert m.training == False and m.bn.training == True and m.i.bn.training == False
    m.eval()
    m.i.train()
    assert (
        m.training == False
        and m.bn.training == False
        and m.i.training == True
        and m.i.bn.training == True
    )
    m.eval()
    m.train()
    assert m.training == True and m.bn.training == True and m.i.bn.training == True

    def fn(m):
        m.training = False

    m.apply(fn)
    assert m.bn.training == False and m.i.bn.training == False

@pytest.mark.parametrize("test_traced_module", [True, False])
def test_module_api_reuse_submodule(test_traced_module):
    m = MyModule()
    if test_traced_module:
        m = trace_module(m, Tensor(np.random.random((1, 4, 16, 16))))
    m.h = m.i  # pylint: disable=attribute-defined-outside-init
    assert list(m.modules()) == [m, m.bn, m.i, m.i.bn]
    assert list(m.named_modules()) == [
        ("", m),
        ("bn", m.bn),
        ("h", m.i),
        ("h.bn", m.i.bn),
    ]

@pytest.mark.parametrize("test_traced_module", [True, False])
def test_module_api_iterable_stability(test_traced_module):
    m = MyModule()
    if test_traced_module:
        m = trace_module(m, Tensor(np.random.random((1, 4, 16, 16))))
    l = list(m.modules())
    for _ in range(100):
        assert list(m.modules()) == l

@pytest.mark.parametrize("test_traced_module", [True, False])
def test_module_api_hooks(test_traced_module):
    net = MyModule()
    if test_traced_module:
        net = trace_module(net, Tensor(np.zeros((1, 4, 1, 1))))
    pre_hook_num = 0
    post_hook_num = 0
    hooks = []

    def pre_hook(_, inputs):
        nonlocal pre_hook_num
        pre_hook_num += 1
        modified_inputs = tuple(inp + 1 for inp in inputs)
        return modified_inputs

    def post_hook(_, __, outputs):
        nonlocal post_hook_num
        post_hook_num += 1
        outputs += 1
        return outputs

    net.apply(lambda module: hooks.append(module.register_forward_pre_hook(pre_hook)))
    net.apply(lambda module: hooks.append(module.register_forward_hook(post_hook)))

    shape = (1, 4, 1, 1)
    x = tensor(np.zeros(shape, dtype=np.float32))
    y = net(x)

    assert pre_hook_num == 4
    assert post_hook_num == 4
    mean1 = Parameter(np.zeros(shape), dtype=np.float32)
    bn1 = F.batch_norm(
        x + 3, mean1, Parameter(np.ones(shape), dtype=np.float32), training=True
    )
    np.testing.assert_allclose(
        net.i.bn.running_mean.numpy(), mean1.numpy(),
    )
    mean2 = Parameter(np.zeros(shape), dtype=np.float32)
    bn2 = F.batch_norm(
        bn1 + 3, mean2, Parameter(np.ones(shape), dtype=np.float32), training=True
    )
    np.testing.assert_allclose(
        net.bn.running_mean.numpy(), mean2.numpy(),
    )
    np.testing.assert_allclose((bn2 + 2).numpy(), y.numpy())

    assert len(hooks) == 8
    for handler in hooks:
        handler.remove()
    y = net(x)
    assert pre_hook_num == 4
    assert post_hook_num == 4

class MyModule2(Module):
    class InnerModule(Module):
        def __init__(self):
            super().__init__()
            self.bn = BatchNorm2d(4)
            self.test_bool_key = {True: 1, False: 0}

        def forward(self, x):
            x = self.bn(x)

    def __init__(self):
        super().__init__()
        self.bn = BatchNorm2d(4)
        self.a = [
            BatchNorm2d(4),
            {"x": BatchNorm2d(4), "y": [BatchNorm2d(4), self.InnerModule()], "z": 0},
            (self.InnerModule(),),
        ]

    def forward(self, x):
        return x

def test_expand_structure():
    m = MyModule2()
    rst = [
        ("", m),
        ("a.0", m.a[0]),
        ("a.1.x", m.a[1]["x"]),
        ("a.1.y.0", m.a[1]["y"][0]),
        ("a.1.y.1", m.a[1]["y"][1]),
        ("a.1.y.1.bn", m.a[1]["y"][1].bn),
        ("a.2.0", m.a[2][0]),
        ("a.2.0.bn", m.a[2][0].bn),
        ("bn", m.bn),
    ]
    assert list(m.named_modules()) == rst

    for item in rst[1:]:
        assert get_expand_structure(m, item[0]) == item[1]

    for item in reversed(rst[1:]):
        if _access_structure(m, item[0], lambda p, k, o: isinstance(p, tuple)):
            continue
        set_expand_structure(m, item[0], "TEST_VALUE")
        assert get_expand_structure(m, item[0]) == "TEST_VALUE"

def test_flatten_others():
    def be_others(obj):
        return not isinstance(obj, (Tensor, Module))

    m = MyModule2()
    assert len(list(m._flatten(with_key=True, predicate=be_others))) == 0

def test_flatten_with_parent():
    m = MyModule2()
    assert list(m.named_modules(with_parent=True)) == [
        ("", m, None),
        ("a.0", m.a[0], m),
        ("a.1.x", m.a[1]["x"], m),
        ("a.1.y.0", m.a[1]["y"][0], m),
        ("a.1.y.1", m.a[1]["y"][1], m),
        ("a.1.y.1.bn", m.a[1]["y"][1].bn, m.a[1]["y"][1]),
        ("a.2.0", m.a[2][0], m),
        ("a.2.0.bn", m.a[2][0].bn, m.a[2][0]),
        ("bn", m.bn, m),
    ]
    assert list(m.modules(with_parent=True)) == [
        (m, None),
        (m.a[0], m),
        (m.a[1]["x"], m),
        (m.a[1]["y"][0], m),
        (m.a[1]["y"][1], m),
        (m.a[1]["y"][1].bn, m.a[1]["y"][1]),
        (m.a[2][0], m),
        (m.a[2][0].bn, m.a[2][0]),
        (m.bn, m),
    ]

class MyModule3(Module):
    class InnerModule(Module):
        def __init__(self):
            super().__init__()
            self.bn = BatchNorm2d(4)

        def forward(self, x):
            x = self.bn(x)

    def __init__(self):
        super().__init__()
        self.bn = BatchNorm2d(4)
        self.seq = Sequential(BatchNorm2d(4), self.InnerModule(),)

    def forward(self, x):
        return x

def test_module_api_with_sequential():
    m = MyModule3()
    assert list(m.named_modules()) == [
        ("", m),
        ("bn", m.bn),
        ("seq", m.seq),
        ("seq.0", m.seq[0]),
        ("seq.1", m.seq[1]),
        ("seq.1.bn", m.seq[1].bn),
    ]

def test_sequential_named_children():
    modules = OrderedDict()
    modules["name0"] = Linear(20, 10)
    modules["name1"] = Linear(10, 5)
    modules["name2"] = Linear(5, 1)
    m = Sequential(modules)
    l = list(m.named_children())
    assert l[0][0] == "name0"
    assert l[1][0] == "name1"
    assert l[2][0] == "name2"

def test_state_dict():
    data_shape = (2, 28)
    data = tensor(np.random.random(data_shape))
    mlp = MLP()
    pred0 = mlp(data)

    with BytesIO() as fout:
        mge.save(mlp.state_dict(), fout)
        fout.seek(0)
        state_dict = mge.load(fout)
        state_dict["extra"] = None
        mlp1 = MLP()
        mlp1.load_state_dict(state_dict, strict=False)
        pred1 = mlp1(data)
        np.testing.assert_allclose(pred0.numpy(), pred1.numpy(), atol=5e-6)
        with pytest.raises(KeyError):
            mlp1.load_state_dict(state_dict)
        del state_dict["extra"]
        del state_dict["dense0.bias"]
        with pytest.raises(KeyError):
            mlp1.load_state_dict(state_dict)

class AssertModule(Module):
    def __init__(self):
        super().__init__()
        self.error_tensor_key = {True: tensor([]), False: 0}

    def forward(self, x):
        return x

def test_assert_message():
    with pytest.raises(
        AssertionError, match="keys for Tensor and Module must be str, error key: True"
    ):
        m = AssertModule()
        list(m._flatten())

class Simple(Module):
    def __init__(self):
        super().__init__()
        self.conv0 = Conv2d(1, 1, kernel_size=3, bias=False)
        self.conv1 = Conv2d(1, 1, kernel_size=3, bias=False)
        self.conv1.weight = self.conv0.weight

    def forward(self, inputs):
        x = self.conv0(inputs)
        y = self.conv1(inputs)
        return x + y

@pytest.mark.parametrize("test_traced_module", [True, False])
def test_shared_param(test_traced_module):
    net = Simple()
    if test_traced_module:
        net = trace_module(net, tensor(np.random.random((1, 1, 8, 8))))
    assert net.conv0.weight is net.conv1.weight
    data = tensor(np.random.random((1, 1, 8, 8)).astype(np.float32))
    np.testing.assert_allclose(net.conv0(data).numpy(), net.conv1(data).numpy())
    with BytesIO() as f:
        mge.save(net, f)
        f.seek(0)
        net1 = mge.load(f)
    assert net1.conv0.weight is net1.conv1.weight
    np.testing.assert_allclose(net1.conv0(data).numpy(), net1.conv1(data).numpy())

    with BytesIO() as f:
        mge.save(net.conv0, f)
        f.seek(0)
        conv0 = mge.load(f)

    with BytesIO() as f:
        mge.save(net.conv1, f)
        f.seek(0)
        conv1 = mge.load(f)

    assert conv0.weight is not conv1.weight
    np.testing.assert_allclose(conv0(data).numpy(), conv1(data).numpy())

class Simple2(Module):
    def __init__(self):
        super().__init__()
        self.conv1 = Conv1d(1, 1, kernel_size=3, bias=False)
        self.conv0 = Conv1d(1, 1, kernel_size=3, bias=False)
        self.conv1.weight = self.conv0.weight

    def forward(self, inputs):
        pass

def test_shared_param_1d():
    net = Simple2()
    assert net.conv0.weight is net.conv1.weight
    data = tensor(np.random.random((1, 1, 8)).astype(np.float32))
    np.testing.assert_allclose(net.conv0(data).numpy(), net.conv1(data).numpy())
    with BytesIO() as f:
        mge.save(net, f)
        f.seek(0)
        net1 = mge.load(f)
    assert net1.conv0.weight is net1.conv1.weight
    np.testing.assert_allclose(net1.conv0(data).numpy(), net1.conv1(data).numpy())

    with BytesIO() as f:
        mge.save(net.conv0, f)
        f.seek(0)
        conv0 = mge.load(f)

    with BytesIO() as f:
        mge.save(net.conv1, f)
        f.seek(0)
        conv1 = mge.load(f)

    assert conv0.weight is not conv1.weight
    np.testing.assert_allclose(conv0(data).numpy(), conv1(data).numpy())

@pytest.mark.parametrize("test_traced_module", [True, False])
def test_pickle_module(test_traced_module):
    data_shape = (2, 28)
    data = tensor(np.random.random(data_shape))
    mlp = MLP()
    pred_gt = mlp(data)
    if test_traced_module:
        mlp = trace_module(mlp, data)
    # pickle before forward
    with BytesIO() as fout:
        mge.save(mlp, fout)
        fout.seek(0)
        mlp1 = mge.load(fout)
        if test_traced_module:
            assert type(mlp1) == TracedModule
        pred0 = mlp1(data)

    pred1 = mlp(data)

    # pickle after forward
    with BytesIO() as fout:
        mge.save(mlp, fout)
        fout.seek(0)
        mlp1 = mge.load(fout)
        if test_traced_module:
            assert type(mlp1) == TracedModule
        pred2 = mlp1(data)

    np.testing.assert_allclose(pred_gt.numpy(), pred1.numpy(), atol=5e-6)
    np.testing.assert_allclose(pred0.numpy(), pred1.numpy(), atol=5e-6)
    np.testing.assert_allclose(pred0.numpy(), pred2.numpy(), atol=5e-6)

def test_repr_basic():
    # test whether __repr__ can output correct information
    class ConvModel(Module):
        def __init__(self):
            super().__init__()
            self.conv1 = Conv2d(3, 128, 3, padding=1, bias=False)
            self.conv2 = Conv2d(3, 128, 3, dilation=2, bias=False)
            self.bn1 = BatchNorm1d(128)
            self.bn2 = BatchNorm2d(128)
            self.pooling = MaxPool2d(kernel_size=2, padding=0)
            modules = OrderedDict()
            modules["depthwise"] = Conv2d(256, 256, 3, 1, 0, groups=256, bias=False,)
            modules["pointwise"] = Conv2d(
                256, 256, kernel_size=1, stride=1, padding=0, bias=True,
            )
            self.submodule1 = Sequential(modules)
            self.list1 = [Dropout(drop_prob=0.1), [Softmax(axis=100)]]
            self.tuple1 = (
                Dropout(drop_prob=0.1),
                (Softmax(axis=100), Dropout(drop_prob=0.2)),
            )
            self.dict1 = {"Dropout": Dropout(drop_prob=0.1)}
            self.fc1 = Linear(512, 1024)

        def forward(self, inputs):
            pass

    ground_truth = (
        "ConvModel(\n"
        "  (conv1): Conv2d(3, 128, kernel_size=(3, 3), padding=(1, 1), bias=False)\n"
        "  (conv2): Conv2d(3, 128, kernel_size=(3, 3), dilation=(2, 2), bias=False)\n"
        "  (bn1): BatchNorm1d(128, eps=1e-05, momentum=0.9, affine=True, track_running_stats=True)\n"
        "  (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.9, affine=True, track_running_stats=True)\n"
        "  (pooling): MaxPool2d(kernel_size=2, stride=2, padding=0)\n"
        "  (submodule1): Sequential(\n"
        "    (depthwise): Conv2d(256, 256, kernel_size=(3, 3), groups=256, bias=False)\n"
        "    (pointwise): Conv2d(256, 256, kernel_size=(1, 1))\n"
        "  )\n"
        "  (list1.0): Dropout(drop_prob=0.1)\n"
        "  (list1.1.0): Softmax(axis=100)\n"
        "  (tuple1.0): Dropout(drop_prob=0.1)\n"
        "  (tuple1.1.0): Softmax(axis=100)\n"
        "  (tuple1.1.1): Dropout(drop_prob=0.2)\n"
        "  (dict1.Dropout): Dropout(drop_prob=0.1)\n"
        "  (fc1): Linear(in_features=512, out_features=1024, bias=True)\n"
        ")"
    )
    net = ConvModel()
    output = net.__repr__()
    assert output == ground_truth

def test_repr_module_reassign():
    # test whether __repr__ can deal with module reassign
    class ConvModel1(Module):
        def __init__(self):
            super().__init__()
            self.conv1 = Conv2d(3, 128, 3, bias=False)
            self.conv2 = Conv2d(3, 128, 3, padding=1, bias=False)
            self.conv1 = Conv2d(3, 256, 3, dilation=2, bias=False)

        def forward(self, inputs):
            pass

    ground_truth = (
        "ConvModel1(\n"
        "  (conv1): Conv2d(3, 256, kernel_size=(3, 3), dilation=(2, 2), bias=False)\n"
        "  (conv2): Conv2d(3, 128, kernel_size=(3, 3), padding=(1, 1), bias=False)\n"
        ")"
    )
    net = ConvModel1()
    output = net.__repr__()
    assert output == ground_truth

def test_repr_module_rereference():
    # test whether __repr__ can deal with module re-reference
    class ConvModel2(Module):
        def __init__(self):
            super().__init__()
            self.conv1 = Conv2d(3, 128, 3, bias=False)
            self.conv2 = self.conv1
            self.conv3 = self.conv1

        def forward(self, inputs):
            pass

    ground_truth = (
        "ConvModel2(\n"
        "  (conv1): Conv2d(3, 128, kernel_size=(3, 3), bias=False)\n"
        "  (conv2): Conv2d(3, 128, kernel_size=(3, 3), bias=False)\n"
        "  (conv3): Conv2d(3, 128, kernel_size=(3, 3), bias=False)\n"
        ")"
    )
    net = ConvModel2()
    output = net.__repr__()
    assert output == ground_truth

def test_repr_module_delete():
    # test whether __repr__ can deal with module delete
    class ConvModel3(Module):
        def __init__(self):
            super().__init__()
            self.conv1 = Conv2d(3, 128, 3, bias=False)
            self.softmax = Softmax(100)

        def forward(self, inputs):
            pass

    ground_truth = (
        "ConvModel3(\n"
        "  (conv1): Conv2d(3, 128, kernel_size=(3, 3), bias=False)\n"
        ")"
    )
    net = ConvModel3()
    del net.softmax
    output = net.__repr__()
    assert output == ground_truth

def test_repr_module_reset_attr():
    class ResetAttrModule(Module):
        def __init__(self, flag):
            super().__init__()
            if flag:
                self.a = None
                self.a = Linear(3, 5)
            else:
                self.a = Linear(3, 5)
                self.a = None

        def forward(self, x):
            if self.a:
                x = self.a(x)
            return x

    ground_truth = [
        (
            "ResetAttrModule(\n"
            "  (a): Linear(in_features=3, out_features=5, bias=True)\n"
            ")"
        ),
        ("ResetAttrModule()"),
    ]

    m0 = ResetAttrModule(True)
    m1 = ResetAttrModule(False)
    output = [m0.__repr__(), m1.__repr__()]
    assert output == ground_truth

def test_module_compatible():
    class Empty(Module):
        def forward(self):
            pass

    empty_module = Empty()
    old_attributes = set(
        [
            "_modules",
            "name",
            "training",
            "quantize_disabled",
            "_forward_pre_hooks",
            "_forward_hooks",
            "_name",
            "_short_name",
        ]
    )
    current_attributes = set(empty_module.__dict__.keys())
    assert (
        old_attributes == current_attributes
    ), "Add or delete attributes in Module class may break compatibility of pickle serialization"

@pytest.mark.skip(reason="pytest aborted")
@pytest.mark.parametrize("affine", [True, False])
def test_grou_norm(affine):
    num_groups = 256
    num_channels = 256
    weight_np = np.random.uniform(-0.5, 0.5, (num_channels))
    bias_np = np.random.uniform(-0.5, 0.5, (num_channels))

    class OriginGroupNormFunc(Module):
        def __init__(self, eps=1e-5, affine=True, **kwargs):
            super().__init__(**kwargs)
            assert num_channels % num_groups == 0
            self.num_groups = num_groups
            self.num_channels = num_channels
            self.eps = eps
            self.affine = affine
            if self.affine:
                self.weight = Parameter(weight_np)
                self.bias = Parameter(bias_np)
            else:
                self.weight = None
                self.bias = None

        def forward(self, x):
            N, C, H, W = x.shape
            x = x.reshape(N, self.num_groups, -1)
            mean = x.mean(axis=2, keepdims=True)
            var = (x * x).mean(axis=2, keepdims=True) - mean * mean
            x = (x - mean) / F.sqrt(var + self.eps)
            x = x.reshape(N, C, H, W)
            if self.affine:
                x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(
                    1, -1, 1, 1
                )
            return x

    inp = np.random.uniform(-0.5, 0.5, (2, num_channels, 10, 16)).astype("float32")
    mge_inp = Tensor(inp)
    mge_m = GroupNorm(num_groups, num_channels, affine=affine)
    mge_m.weight = Parameter(weight_np)
    mge_m.bias = Parameter(bias_np)
    ori_inp = Tensor(inp)
    ori_m = OriginGroupNormFunc(affine=affine)

    mge_gm = mge.autodiff.GradManager().attach((*mge_m.parameters(), mge_inp))
    ori_gm = mge.autodiff.GradManager().attach((*ori_m.parameters(), ori_inp))
    dy = Tensor(np.random.uniform(-0.5, 0.5, inp.shape))
    for i in range(2):
        with mge_gm:
            mge_output = mge_m(mge_inp)

            mge_gm.backward(mge_output, dy)

        with ori_gm:
            ori_output = ori_m(ori_inp)

            ori_gm.backward(ori_output, dy)

        np.testing.assert_allclose(mge_output.numpy(), ori_output.numpy(), atol=1e-05)
        np.testing.assert_allclose(
            ori_inp.grad.numpy(), mge_inp.grad.numpy(), atol=1e-05
        )
        if affine == True:
            np.testing.assert_allclose(
                mge_m.weight.grad.numpy(), ori_m.weight.grad.numpy(), atol=1e-05
            )
            np.testing.assert_allclose(
                mge_m.bias.grad.numpy(), ori_m.bias.grad.numpy(), atol=1e-05
            )

@pytest.mark.parametrize("affine", [True, False])
def test_instance_norm(affine):
    num_channels = 4
    weight_np = np.random.uniform(-0.5, 0.5, (num_channels))
    bias_np = np.random.uniform(-0.5, 0.5, (num_channels))

    class OriginInstanceNormFunc(Module):
        def __init__(self, eps=1e-5, affine=True, **kwargs):
            super().__init__(**kwargs)
            self.num_channels = num_channels
            self.eps = eps
            self.affine = affine
            if self.affine:
                self.weight = Parameter(weight_np)
                self.bias = Parameter(bias_np)
            else:
                self.weight = None
                self.bias = None

        def forward(self, x):
            N, C, H, W = x.shape
            x = x.reshape(N, self.num_channels, -1)
            mean = x.mean(axis=2, keepdims=True)
            var = (x * x).mean(axis=2, keepdims=True) - mean * mean
            x = (x - mean) / F.sqrt(var + self.eps)
            x = x.reshape(N, C, H, W)
            if self.affine:
                x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(
                    1, -1, 1, 1
                )
            return x

    inp = np.random.uniform(-0.5, 0.5, (2, num_channels, 10, 16)).astype("float32")
    mge_inp = Tensor(inp)
    mge_m = InstanceNorm(num_channels, affine=affine)
    mge_m.weight = Parameter(weight_np)
    mge_m.bias = Parameter(bias_np)

    ori_inp = Tensor(inp)
    ori_m = OriginInstanceNormFunc(affine=affine)

    mge_im = mge.autodiff.GradManager().attach((*mge_m.parameters(), mge_inp))
    ori_im = mge.autodiff.GradManager().attach((*ori_m.parameters(), ori_inp))
    dy = Tensor(np.random.uniform(-0.5, 0.5, inp.shape))

    for i in range(2):
        with mge_im:
            mge_output = mge_m(mge_inp)

            mge_im.backward(mge_output, dy)

        with ori_im:
            ori_output = ori_m(ori_inp)

            ori_im.backward(ori_output, dy)

        np.testing.assert_allclose(mge_output.numpy(), ori_output.numpy(), atol=1e-05)
        np.testing.assert_allclose(
            ori_inp.grad.numpy(), mge_inp.grad.numpy(), atol=1e-04
        )
        if affine == True:
            np.testing.assert_allclose(
                mge_m.weight.grad.numpy(), ori_m.weight.grad.numpy(), atol=1e-04
            )
            np.testing.assert_allclose(
                mge_m.bias.grad.numpy(), ori_m.bias.grad.numpy(), atol=1e-04
            )


#__________________________________________________________TITLES_________________________________________________________#

import tkinter as tk
from tkinter import ttk, filedialog
from PIL import ImageTk, Image
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import main
import pandas
from tkinter import *
from tkinter.ttk import *
import matplotlib.ticker as ticker

root = tk.Tk()
root.title("GPA Calculator")
root.config(bg='#CACBD1')
#root.attributes('-fullscreen', True)
root.geometry('1280x800')
root.resizable(width=0, height=0)

#data to be stored for every .RUN file
index: int = 0
sec_data = None
grp_data = None
pages: list = None

# ROGER WILLIAMS LABEL
title_label = ttk.Label(root, text="   Roger Williams", font=("Times New Roman", 20), foreground="white", background="#1E3261")
title_label.grid(row=1, column=0, columnspan=5, pady=(10, 0), sticky="nsew")

# UNIVERSITY LABEL
univ_label = ttk.Label(root, text="  University", font=("Times New Roman", 27), foreground="#60ADF0", background="#1E3261")
univ_label.grid(row=2, column=0, columnspan=5, pady=(0, 45), sticky="nsew")

#__________________________________________________________TEXTBOXES______________________________________________________#

# TOP SPACER BOX
top_space = tk.Entry(root, width=500, bg="#1E3261", fg="#1E3261", bd=0)
top_space = top_space.grid(row=0, rowspan=2, columnspan=5, pady=(0,30) , column=0, sticky="nesw")

# FILE DIRECTORY BOX
dir_box = tk.Entry(root, width=80, font=("Arial", 14), bg="white", fg="#a3a3a3", bd=0)
dir_box.insert(0, " Enter file path")
dir_box.bind("<FocusIn>", lambda event: dir_box.delete(0, tk.END))
dir_box.grid(row=3, column=0, padx=20, pady=10, sticky="w")

# FILE CONTENTS BOX
file_box = tk.Text(root, height=6, font=("Arial", 14), bg="white", fg="#a3a3a3", bd=0)
file_box.insert(tk.END, ' File contents will be displayed here.\n\n If entering manually, use the following format (include quotations):\n\n "Last","First","Student ID","Grade"')
file_box.bind("<FocusIn>", lambda event: file_box.delete(1.0, tk.END))
file_box.grid(row=5, column=0, padx=20, pady=10, sticky="w", columnspan=2)

# CALCULATION BOX
calc_box = tk.Text(root, height=17, font=("Arial", 14), bg="white", fg="#a3a3a3", bd=0)
calc_box.bind("<FocusIn>", lambda event: calc_box.delete(1.0, tk.END))
calc_box.grid(row=6, column=0, padx=20, pady=10, sticky="nsew", columnspan=2)

# BOTTOM SPACER BOX
bot_space = tk.Entry(root, width=100, bg="#CACBD1", fg="#CACBD1", bd=0)
bot_space = bot_space.grid(row=7, column=0, padx=5, pady=5, sticky="ew")

# GRAPH BOX
graph_box = tk.Entry(root, width=100, bg="white", fg="white", bd=0)
graph_box.grid(row=3, rowspan=4, column=3, columnspan=2, padx=20, pady=10, sticky="nse")

#________________________________________________________FUNCTIONS________________________________________________________#

# Function to handle file selection from directory browser
def select_file():
    file_path = filedialog.askopenfilename()
    if file_path:
        dir_box.delete(0, tk.END)
        dir_box.insert(0, file_path)
        with open(file_path, "r") as file:
            file_contents = file.read()
            file_box.delete(1.0, tk.END)
            file_box.insert(tk.END, file_contents)

def generate_graph():
    global index, pages, grp_data, sec_data

    fig, ax = plt.subplots(figsize=(6, 4))

    grades = ['A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'F', 'I', 'P', 'NP', 'W']
    num_students = [0, 3, 5, 10, 12, 15, 20, 18, 10, 8, 4, 0, 2, 6, 4, 1]

    ax.bar(grades, num_students)

    # Add labels and title
    ax.grid(axis='y')
    ax.set_xlabel('Grades')
    ax.set_ylabel('Number of Students')
    ax.set_title('Distribution of Grades')
    ax.set_yticks(range(0, 51, 2))

    # Add bar chart to window
    canvas = FigureCanvasTkAgg(fig, master=graph_box)
    canvas.draw()
    canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)

def clear_text():
    dir_box.delete(0, tk.END)
    dir_box.insert(0, " Enter file path")
    file_box.delete(1.0, tk.END)
    file_box.insert(tk.END, ' File contents will be displayed here.\n\n If entering manually, use the following format (include quotations):\n\n "Last","First","Student ID","Grade"')
    calc_box.delete(1.0, tk.END)
    calc_box.insert(tk.END, "")

def clear_canvas():
    global graph_canvas, graph_fig, graph_ax
    # Clear the old graph if it exists
    if graph_fig is not None:
        graph_ax.clear()
        graph_fig = None
        graph_canvas.draw()
        graph_canvas.get_tk_widget().destroy()

# Function to exit program
def exit_program():
    root.destroy()

#__________________________________________________________BUTTONS________________________________________________________#

# LEFT BUTTON
left_button = tk.Button(root, width = 10, text="<<", bd=0, fg='white', bg='#1E3261')
left_button.grid(row = 3, columnspan=2, column = 3, pady=10, padx=250, sticky='w')

# BROWSE BUTTON
browse_button = ttk.Button(root, text="Browse", width=10)
browse_button.grid(row=4, column=0, pady=10, padx=20, sticky="w")

# CALCULATE BUTTON
calc_button = ttk.Button(root, text="Calculate", width=10)
calc_button.grid(row=4, column=0, pady=10, padx=20, sticky="n")

# CLEAR BUTTON
clear_button = ttk.Button(root, text="Clear", width=10)
clear_button.grid(row=4, column=0, pady=10, padx=20, sticky="e")

# RIGHT BUTTON
right_button = tk.Button(root, text=">>", width = 10, bd=0, fg='white', bg='#1E3261')
right_button.grid(row = 3, columnspan=2, column = 4, pady=10, padx=210, sticky='e')

# EXIT BUTTON
exit_button = ttk.Button(root, text="Exit", command=exit_program, width=10)
exit_button.grid(row=1, column=4, sticky="ne", padx=7, pady=7)

#___________________________________________________________FUNCTIONS________________________________________________________#

def displayData():
    global index, pages, grp_data, sec_data

    if pages == None:
        return

    generate_graph()
    datastr = ""
    if pages[index].endswith("GRP"):
        data2 = grp_data[pages[index]]

        datastr += "\n" + "Group:\t" + str(pages[index])
        datastr += "\n" + "Sections and Z-test:"

        ztestdata = sorted(data2["ztests"].items(), key=lambda x:x[1])
        for item in ztestdata:
            datastr += "\n\t" + str(item[0]) + ":\t " + str(item[1])
            if item[1] >= 2.0:
                datastr += "SIGNIFICANTLY HIGH"
            elif item[1] <= -2.0:
                datastr += "SIGNIFICANTLY LOW"
        datastr += "\n"

        datastr += "\n" + "Mean:\t" + str(data2["mean"])
        datastr += "\n" + "Standard Deviation:\t" + str(data2["stddev"])
        datastr += "\n" + "Number of Students:\t" + str(data2["numstudents"])
        datastr += "\n" + "Grade Counts:"

        gradeCounts = list(data2["gradecounts"].keys())
        gradeCounts.sort()
        for item in gradeCounts:
            datastr += "\n\t" + str(item) + ":\t" + str(data2["gradecounts"][item])

    elif pages[index].endswith("SEC"):
        data2 = sec_data[pages[index]]

        datastr += "\n" + "Section:\t" + str(pages[index])
        datastr += "\n" + "Credit Hours:\t" + str(data2["creditHours"]) + "\n"
        datastr += "\n" + "Mean:\t" + str(data2["mean"])
        datastr += "\n" + "Standard Deviation:\t" + str(data2["stddev"])
        datastr += "\n" + "Number of Students:\t" + str(data2["numstudents"])
        datastr += "\n" + "Grade Counts:"
        gradeCounts = list(data2["gradecounts"].keys())
        gradeCounts.sort()
        for item in gradeCounts:
            datastr += "\n\t" + str(item) + ":\t" + str(data2["gradecounts"][item])
        datastr += "\n" + "Students:"

        students: pandas.DataFrame = data2["data"]
        datastr += "\n\t%15s %15s %7s %6s %8s" % (
            students.columns[0], students.columns[1], students.columns[2], students.columns[3], students.columns[4])
        for i in range(len(students.index)):
            datastr += "\n\t%s %s %s %s %s" % (
                str(students.iloc[i][0]).strip().ljust(20, " "),
                str(students.iloc[i][1]).strip().ljust(20, " "),
                str(students.iloc[i][2]).strip().ljust(7, " "),
                str(students.iloc[i][3]).strip().ljust(6, " "),
                str(students.iloc[i][4]).strip().ljust(8, " ")
            )

        #datastr += data2["data"].to_string(col_space = 15)
    calc_box.delete("1.0", tk.END)
    calc_box.insert(tk.END, datastr)

def shiftIndexRight():
    global index, pages

    if pages == None:
        return

    if len(pages)-1 == index:
        index = 0
    else:
        index += 1

    # Clear graph from canvas
    for widget in graph_box.winfo_children():
        widget.destroy()

    displayData()

def shiftIndexLeft():
    global index, pages

    if pages == None:
        return

    if index == 0:
        index = len(pages)-1
    else:
        index -= 1

    # Clear graph from canvas
    for widget in graph_box.winfo_children():
        widget.destroy()

    displayData()

def getData():
    global sec_data, grp_data, pages, index

    path = dir_box.get()

    if not path.endswith(".RUN"):
        calc_box.delete("1.0", tk.END)
        calc_box.insert(tk.END, "Not a valid RUN file")
    else:
        data = None
        try:
            data = main.fetch(dir_box.get())
        except Exception:
            calc_box.delete("1.0", tk.END)
            calc_box.insert(tk.END, "Error reading RUN file")
            return
        sec_data = data[0]
        grp_data = data[1]
        pages = list(grp_data.keys())
        pages.extend(list(sec_data.keys()))
        index = 0
        displayData()

#___________________________________________________________ATTACH________________________________________________________#

# Attach select_file function to browse button
browse_button.config(command=select_file)

# Attach clear_text function to clear button
clear_button.config(command=clear_text)

# Attach calc_text function to calc button
calc_button.config(command= getData)

# Attach clear_canvas function to arrow buttons
left_button.config(command = (shiftIndexLeft))
right_button.config(command = shiftIndexRight)

#__________________________________________________________WEIGHTS________________________________________________________#

# Set grid weights to allow for resizing
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=2)
root.rowconfigure(3, weight=1)

root.mainloop()


import tensorflow as tf
import os
from utils.reprocessing import generator_enqueue
from utils.reprocessing import PAD_ID,GO_ID, EOS_ID, UNK_ID

class Variational_autoencoder_Seq2Seq(object):

    def __init__(self, layers_size=100, VAE_layers_size=100, learning_rate=0.01,
                 vocab_size=1000,
                 init_embedding=None, embedding_size=100,
                 lr_decay=0.9, scope='seq2seq', max_grad_norm=5, queue_capacity=1000,
                 max_length=50, min_length=3, num_samples=1024,
                 type_model='Train', tracking='tracking', dtype=tf.float32):
        self.layers_size = layers_size
        self.learning_rate = learning_rate
        self.VAE_layers_size = VAE_layers_size
        self.vocab_size = vocab_size
        self.max_length = max_length
        self.lr_decay = lr_decay
        self.scope = scope
        self.embedding_size = embedding_size
        self.min_length = min_length
        self.max_grad_norm = max_grad_norm
        self.init_embedding = init_embedding
        self.tracking = tracking
        self.num_samples = num_samples
        self.dtype = dtype
        self.type_model = type_model
        self.graph = None

    def _cell(self, num_units, keep_prob=1.):
        cell = tf.contrib.rnn.BasicLSTMCell(num_units)
        return cell

    def _clip_gradients(self, grads_and_vars, embedding_norm = 0.1):
        """In addition to standard gradient clipping, also clips embedding
        gradients to a specified value."""
        clipped_gradients = []
        variables = []
        for gradient, variable in grads_and_vars:
            if "embedding" in variable.name:
                tmp = tf.clip_by_norm(
                    gradient.values, embedding_norm)
                gradient = tf.IndexedSlices(tmp, gradient.indices, gradient.dense_shape)
            clipped_gradients.append(gradient)
            variables.append(variable)
        return list(zip(clipped_gradients, variables))

    def _optimizer(self, loss):
        """Create the optimizer node of the graph."""
        self.lr_var = tf.Variable(self.learning_rate, trainable=False)
        tvars = tf.trainable_variables()
        grads, _ = tf.clip_by_global_norm(tf.gradients(loss, tvars),
                                          self.max_grad_norm)
#         grads_and_vars = self._clip_gradients(list(zip(grads, tvars)))
        grads_and_vars = zip(grads, tvars)

        optimizer = tf.train.AdamOptimizer(
            learning_rate=self.lr_var, epsilon=1e-08)
        _train_op = optimizer.apply_gradients(
            grads_and_vars, global_step=self.global_step)
        return _train_op

    def _sample_posterior(self, x):
        x = tf.concat(x, axis=-1)
        latent_dim = self.VAE_layers_size
        with tf.variable_scope('variantional_autoencoder'):
            #             epsilon = tf.constant(1e-8)
            self.z_mu = tf.layers.dense(
                inputs=x, units=latent_dim, name='z_mu')
            self.z_var = tf.layers.dense(inputs=x, units=latent_dim, activation=tf.nn.softplus,
                                         bias_initializer=tf.constant_initializer(
                                             -2.5),
                                         name='z_log_sigma') + 1e-8

            epsilon = tf.random_normal(shape=tf.shape(self.z_mu))
            z = self.z_mu + tf.sqrt(self.z_var) * epsilon

        self.variable_summaries(z)
        concat = tf.layers.dense(
            inputs=z, units=2 * self.layers_size, name='project_state')
        state = tf.contrib.rnn.LSTMStateTuple(*tf.split(concat, 2, 1))

        kl_div = -0.5 * tf.reduce_sum(1.0 + tf.log(self.z_var) - tf.square(self.z_mu) - self.z_var,
                                      axis=1)
        self.kl_div = tf.reduce_mean(kl_div)

        tf.summary.scalar('KL_divergence_loss', self.kl_div)

        return state

    def _encoder(self, inputs):
        encoder_cell = self._cell(self.layers_size)
        encoder_outputs, encoder_state = tf.nn.dynamic_rnn(
            encoder_cell, inputs, dtype=self.dtype)

        return encoder_outputs, encoder_state

    def _decoder(self, inputs, state, is_train):
        decoder_cell = self._cell(self.layers_size, self.keep_prob)
        with tf.variable_scope('decoder'):
            self.sequence_length = tf.cast(
                tf.reduce_sum(self.weight, axis=1), tf.int32)

            train_helper = tf.contrib.seq2seq.TrainingHelper(
                inputs, self.sequence_length)
            inference_helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(
                self.embedding,
                tf.fill(
                    [self.batch_size], GO_ID),
                tf.constant(EOS_ID))

            project_layer = layers_core.Dense(self.embedding.get_shape()[
                                              0], name='output_project')
            def _create_decoder(helper):
                return tf.contrib.seq2seq.BasicDecoder(
                    cell=decoder_cell,
                    helper=helper,
                    initial_state=state,
                    output_layer=project_layer)  # initial state of decoder
            train_decoder = _create_decoder(train_helper)
            inference_decoder = _create_decoder(inference_helper)

        return tf.cond(is_train,
                       lambda: tf.contrib.seq2seq.dynamic_decode(
                           train_decoder, maximum_iterations=self.max_length),
                       lambda: tf.contrib.seq2seq.dynamic_decode(inference_decoder, maximum_iterations=self.max_length))

    def variable_summaries(self, var):
        """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
        with tf.name_scope('summaries'):
            mean = tf.reduce_mean(var)
            tf.summary.scalar('mean', mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
            tf.summary.scalar('stddev', stddev)
            tf.summary.scalar('max', tf.reduce_max(var))
            tf.summary.scalar('min', tf.reduce_min(var))
            tf.summary.histogram('histogram', var)

    def annealing_schedule(self, t, pivot):
        return tf.nn.sigmoid((t - pivot) / pivot * 10)

    def build_model(self, data_inputs, validate_inputs=None):
        with tf.Graph().as_default() as self.graph:
            # Placeholder
            self.global_step = tf.Variable(0, trainable=False)
            self.batch_size = tf.placeholder(
                tf.int32, shape=(), name='batch_size')
            self.annealing_pivot = tf.placeholder(
                tf.float32, shape=(), name='annealing_pivot')
            self.keep_prob = tf.placeholder(
                tf.float32, shape=(), name='keep_prob')
            self.is_train = tf.placeholder(tf.bool, shape=(), name='is_train')
            self.word_keeping = tf.placeholder(
                dtype=tf.float32, shape=(), name='word_keeping')
            self.l2_epsilon = tf.placeholder(
                dtype=tf.float32, shape=(), name='l2_epsilon')

            self.scale_kl_div = tf.placeholder(
                dtype=tf.float32, shape=(), name='scale_kl_div')

            self.coord = tf.train.Coordinator()
            self.generator_enqueue = generator_enqueue(
                self.coord, data_inputs, validate_inputs)
            encode_queue, decode_queue, weight_queue = self.generator_enqueue.get_queue(
                self.batch_size)

            self.encode_input = tf.placeholder_with_default(
                encode_queue, shape=[None, None], name='encode_input')
            self.decode_input = tf.placeholder_with_default(
                decode_queue, shape=[None, None], name='decode_input')
            self.weight = tf.placeholder_with_default(
                weight_queue, shape=[None, None], name='weigth')

            ###EMBEDDING###
            if self.init_embedding is not None:
                self.embedding = tf.get_variable("embedding",
                                                 initializer=self.init_embedding,
                                                 trainable=True)
            else:
                self.embedding = tf.get_variable("embedding",
                                                 [self.vocab_size, self.embedding_size])

            emb_encode = tf.nn.embedding_lookup(
                self.embedding, self.encode_input)

            # Word dropout

            random_tensor = self.word_keeping + \
                tf.random_uniform(shape=tf.shape(self.decode_input))
            binary_tensor = tf.floor(random_tensor)
            dropped_decode = tf.where(tf.greater(binary_tensor, 0),
                                      self.decode_input, tf.fill(tf.shape(self.decode_input), UNK_ID))
            emb_decode = tf.nn.embedding_lookup(self.embedding, dropped_decode)
            ###

            ###ENCODER###
            encoder_outputs, encoder_state = self._encoder(emb_encode)
            ###

            ###SAMPLING###
            sample = self._sample_posterior(encoder_state)
            ###

            ###DECODER###
            self.outputs, final_state, final_sequence_length = self._decoder(
                emb_decode, sample, self.is_train)
            ###

            # BUILD LOSS
            _logits = self.outputs.rnn_output

            tf.summary.histogram('output', self.outputs.sample_id)
            paddings = [[0, 0], [0, self.max_length -
                                 tf.shape(_logits)[1]], [0, 0]]
            logits = tf.pad(_logits, paddings, "CONSTANT")

            _target = self.decode_input[:, 1:]  # remove the start_token
            paddings = [[0, 0], [0, self.max_length - tf.shape(_target)[1]]]
            target = tf.pad(_target, paddings, "CONSTANT")

            paddings = [
                [0, 0], [0, self.max_length - tf.shape(self.weight)[1]]]
            padded_weight = tf.pad(self.weight, paddings, "CONSTANT")

            self.reconstruct_loss = tf.contrib.seq2seq.sequence_loss(
                logits, target, padded_weight)

            tf.summary.scalar('reconstruct_loss', self.reconstruct_loss)

            annealing_weight = self.annealing_schedule(
                tf.cast(self.global_step, tf.float32), self.annealing_pivot)
            # TOTAL LOSS
            self.total_loss = self.reconstruct_loss + annealing_weight * self.kl_div
            tf.summary.scalar('total_loss', self.total_loss)

            self.op = self._optimizer(self.total_loss)

            self.merged = tf.summary.merge_all()
            self.train_writer = tf.summary.FileWriter(self.tracking)
            self.init = tf.global_variables_initializer()
            self.saver = tf.train.Saver()
        return self

    def save(self, sess, model_dir=""):
        self.saver.save(sess, os.path.join(
            model_dir, 'dynamic_attention_decoder'), global_step=self.global_step)

    def load(self, sess, model_dir=""):
        ckpt = tf.train.get_checkpoint_state(model_dir)
        if ckpt and ckpt.model_checkpoint_path:
            print('success')
            self.saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            print('fail')

    def validate(self, sess, inputs, batch_size=1, feed_dict={}):
        losses = 0
        for i in range(len(inputs) // batch_size):
            def pad(x):
                max_len = len(max(x, key=len))
                x = map(lambda y: (max_len - len(x)) * [0], x)
                return x
            batch_inputs = inputs[i * batch_size: (i + 1) * batch_size]
            weight_input = map(lambda x: [1.] * len(x), batch_inputs)
            decode_input = map(lambda x: [GO_ID] + x + [EOS_ID], batch_inputs)
            feed_dict[self.encode_input] = pad(batch_inputs)
            feed_dict[self.decode_input] = pad(decode_input)
            feed_dict[self.weight] = pad(weight_input)

            loss, = sess.run([self.total_loss], feed_dict=feed_dict)
            losses += loss
        return losses

    def train(self, sess):
        thread = threading.Thread(target=self.generator_enqueue.run, args=(
            sess, self.max_length, self.min_length))
        thread.daemon = True
        thread.start()
        self.coord.register_thread(thread)

    def stop_train(self, sess):
        self.coord.request_stop()


from app.accounts.services import get_verifiers
from app.positions.services import check_position_duplicate
from app.positions.services import create_position, create_section_division
from app.positions.services import delete_position_record, delete_secdiv_record
from app.positions.services import get_section_divisions_and_positions, get_verifier
from app.positions.services import update_secdiv_record, update_position_record
from flask import Blueprint, render_template, request
from json import dumps

positionsbp = Blueprint('positions', __name__, template_folder='templates')

@positionsbp.route('/positions')
def positions():
    section_divisions = get_section_divisions_and_positions()
    verifiers = get_verifiers()

    return render_template('positions.html', section_divisions=section_divisions, verifiers=verifiers)

@positionsbp.route('/positions/new-position', methods=['POST'])
def new_position():
    data = {
        'title': request.form['position-title'],
        'section_division': request.form['section-division']
    }

    create_position(data)

    return dumps({'status': 'OK'})

@positionsbp.route('/positions/new-section-division', methods=['POST'])
def new_section_division():
    data = {
        'name': request.form['name'],
        'verifier': request.form['verifier']
    }

    create_section_division(data)

    return dumps({'status': 'OK'})

@positionsbp.route('/positions/get-verifier', methods=['GET'])
def get_sec_div_verifier():
    sec_div = request.args.get('section_division')
    verifier = get_verifier(sec_div)

    data = {
        'verifier': verifier
    }

    return dumps({'status': 'OK', 'data': data})

@positionsbp.route('/positions/delete-secdiv')
def delete_secdiv():
    sec_div = request.args.get('sec_div')
    delete_secdiv_record(sec_div)

    return dumps({'status': 'OK'})

@positionsbp.route('/positions/update-secdiv', methods=['POST'])
def update_secdiv():
    current = request.form['current_name']
    new = request.form['name']
    verifier = request.form['verifier']

    update_secdiv_record(current, new, verifier)

    return dumps({'status': 'OK'})

@positionsbp.route('/positions/update-pos', methods=['POST'])
def update_position():
    current = request.form['cur_pos']
    cur_secdiv = request.form['cur_secdiv']
    new = request.form['position']
    secdiv = request.form['secdiv']

    if current != new or cur_secdiv != secdiv:
        duplicate = check_position_duplicate(new, secdiv)

        if duplicate:
            return dumps({'status': 'FAILED: DUPLICATE ENTRY'})

    update_position_record(current, new, secdiv)
    return dumps({'status': 'OK'})

@positionsbp.route('/positions/delete-position')
def delete_position():
    pos = request.args.get('pos')
    sec_div = request.args.get('secdiv')

    delete_position_record(sec_div, pos)

    return dumps({'status': 'OK'})


import math

import matplotlib.pylab as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sympy import symbols, solve

print("Question 2")
my_id = "09170218"  # defining U
U = [int(u) for u in my_id]
X = [x for x in range(1, 9)]

plt.plot(X, U)
plt.xlabel("X")
plt.ylabel("U(x)")
plt.title("U_X graph")
plt.show()

"""
The mechanical energy (E = U + K) to maximize amplitude:
   we set E=5 (U(5) = 0) ==> Xl = 4.7 and Xr = 6
   ATTENTION: (4.7, 2) this point is approximated by my eyes.
   and we get three points to fit a quadratic function to them (4.7, 2), (5, 0) and (6, 2)
"""

X = np.matrix([4.7, 5, 6]).reshape((3, 1))
U = np.matrix([2, 0, 2]).reshape((3, 1))
a = PolynomialFeatures(degree=2)
a.fit(X, U)
model = make_pipeline(a, LinearRegression())
model.fit(X, U)

alpha = model.steps[1][1].coef_[0][2]
beta = model.steps[1][1].coef_[0][1]
phi = model.steps[1][1].intercept_[0]

print("alpha: ", alpha, " beta: ", beta, " landa: ", phi)

def U(x):
    return alpha * x ** 2 + beta * x + phi

"""
At equilibrium point we have dU(x)/dx = 0
"""

equilibrium_point = symbols('equilibrium_point')
equation = 2 * alpha * equilibrium_point + beta  # dU(x)/dx = 0
find_equilibrium_point = solve([equation])
print("find_equilibrium_point: ", find_equilibrium_point.get(equilibrium_point))

"""
To estimate the frequency of the harmonic oscillations  we do the following:
    we have to convert the quadratic function into this form
    U = 1/2 * k * (x-x0)^2 opening up this equation we get
    alpha = 1/2 * k * x**2
    beta = -1/2 * k * x0 * x
    phi = 1/2 * k * x0**2
"""
k = 2 * alpha
mass = 1
print("k: ", k)
frequency_of_the_harmonic_oscillations = 1 / (2 * math.pi) * math.sqrt(k / mass)
print("frequency_of_the_harmonic_oscillations: ", frequency_of_the_harmonic_oscillations)

print('\n\n\n')
###########  Question 3 ###########################
print("Question 3")
h = 10
M = 10
"""
celli = [x_coordinate, y_coordinate, mass_of_the_cell]
N = [0,1,2,3,4,5,6,7,8,9]
mass of i are all equal if i= N - {4,5} = M/16
mass of i are all equal if i= {4,5} = M/4
"""
cell0 = [h / 8, 7 * h / 8, M / 16]
cell1 = [3 * h / 8, 7 * h / 8, M / 16]
cell2 = [5 * h / 8, 7 * h / 8, M / 16]
cell3 = [7 * h / 8, 7 * h / 8, M / 16]
cell4 = [h / 4, h / 2, M / 4]
cell5 = [3 * h / 4, h / 2, M / 4]
cell6 = [h / 8, h / 8, M / 16]
cell7 = [3 * h / 8, h / 8, M / 16]
cell8 = [5 * h / 8, h / 8, M / 16]
cell9 = [7 * h / 8, h / 8, M / 16]

"""
cell8 is the one that must be removed owing to my id ==>
"""
coordinates_of_all_cells = [cell0, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell9]

x_of_center_of_mass = 0
center_of_mass = 0
for cell in coordinates_of_all_cells:
    x_of_center_of_mass += cell[0] * cell[2]
    center_of_mass += cell[2]
x_of_center_of_mass = x_of_center_of_mass / center_of_mass

y_of_center_of_mass = 0
center_of_mass = 0
for cell in coordinates_of_all_cells:
    y_of_center_of_mass += cell[1] * cell[2]
    center_of_mass += cell[2]
y_of_center_of_mass = y_of_center_of_mass / center_of_mass

"""Now I want to show all the points (except cell8)"""

all_the_x = [cell[0] for cell in coordinates_of_all_cells]
all_the_y = [cell[1] for cell in coordinates_of_all_cells]
center_of_mass_coordinates = [x_of_center_of_mass, y_of_center_of_mass]

plt.scatter(all_the_x, all_the_y)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("X_Y graph")

plt.scatter(center_of_mass_coordinates[0], center_of_mass_coordinates[1], edgecolors='r')
plt.show()

print('\n\n\n')
################# Question 4 ################################
print("Question 4")

"""mi = [ x_coordinate_of_mi, y_coordinate_of_mi, mass_of_mi ] """
m1 = [0, 9, 1]
m2 = [1, 7, 2]
m3 = [0, 2, 2]
m4 = [1, 8, 1]

all_the_bodies = [m1, m2, m3, m4]
all_the_bodies_x = [body[0] for body in all_the_bodies]
all_the_bodies_y = [body[1] for body in all_the_bodies]

# part 1: will show all the points later on

# part 2:

center_of_mass_x = 0
center_of_mass = 0
for body in all_the_bodies:
    center_of_mass_x += body[0] * body[2]
    center_of_mass += body[2]
center_of_mass_x = center_of_mass_x / center_of_mass

center_of_mass_y = 0
center_of_mass = 0
for body in all_the_bodies:
    center_of_mass_y += body[1] * body[2]
    center_of_mass += body[2]
center_of_mass_y = center_of_mass_y / center_of_mass

center_of_mass_coordinates = [center_of_mass_x,
                              center_of_mass_y]  # center of mass of the original system

plt.scatter(all_the_bodies_x, all_the_bodies_y)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("X_Y graph")
plt.scatter(center_of_mass_coordinates[0], center_of_mass_coordinates[1], edgecolors='r')
plt.show()
print("center_of_mass_coordinates: ", center_of_mass_coordinates)

# part 3

def distance_between_2_points(m1, m2):
    return math.sqrt((m1[0] - m2[1]) ** 2 + (m1[2] - m1[2]) ** 2)

L1 = distance_between_2_points(m1, m2)
L2 = distance_between_2_points(m2, m3)
L3 = distance_between_2_points(m3, m4)

print('L1: ', L1)
print('L2: ', L2)
print('L3: ', L3)

"""
We have obtained that theta is angle between v1 and x-axis and is 63.43 so
    x and y components of v1 are obtained as follows:
"""
theta = 63.43
v1 = L1
v1x = abs(math.cos(theta) * v1)
v1y = abs(math.sin(theta) * v1)
v1 = [-v1x, v1y]  # negative sign is for the fact that we are on the left side of X-axis
new_position_of_m1_after_1_second = [m1[0] + v1[0], m1[1] + v1[1]]
print("new_position_of_m1_after_1_second: ", new_position_of_m1_after_1_second)

"""
We have obtained that alpha is angle between v2 and x-axis and is 80.53 so
    x and y components of v1 are obtained as follows:
"""
alpha = 80.53
v2 = L3
v2x = abs(math.cos(alpha) * v2)
v2y = abs(math.sin(alpha) * v2)
v2 = [v2x, v2y]  # positive sign is for the fact that we are on the right side of X-axis
new_position_of_m4_after_1_second = [m4[0] + v2[0], m4[1] + v2[1]]
print("new_position_of_m4_after_1_second: ", new_position_of_m4_after_1_second)

L2_center_of_mass = 0  # center of mass of the rod L2 1 second after the explosions
"""
There is no external force acting on our system ==> explosion does not change the center of mass
    ==> center_of_mass_coordinates do not change.
The logic is the following:
    we use both new_position_of_m1_after_1_second and new_position_of_m4_after_1_second to calculate
    L2_center_of_mass.
"""
total_mass = m1[2] + m2[2] + m3[2] + m4[2]

L2_center_of_mass_x = symbols('L2_center_of_mass_x')
equation_for_x = ((L2_center_of_mass_x * (m2[2] + m3[2]) + new_position_of_m1_after_1_second[0] *
                   m1[2] + new_position_of_m4_after_1_second[0] * m4[2]) / total_mass) - \
                 center_of_mass_coordinates[0]

L2_center_of_mass_y = symbols('L2_center_of_mass_y')
equation_for_y = ((L2_center_of_mass_y * (m2[2] + m3[2]) + new_position_of_m1_after_1_second[1] *
                   m1[2] + new_position_of_m4_after_1_second[1] * m4[2]) / total_mass) - \
                 center_of_mass_coordinates[1]

L2_center_of_mass_x = solve([equation_for_x]).get(L2_center_of_mass_x)
L2_center_of_mass_y = solve([equation_for_y]).get(L2_center_of_mass_y)

print("L2_center_of_mass_x after explosion: ", L2_center_of_mass_x)
print("L2_center_of_mass_y after explosion: ", L2_center_of_mass_y)

plt.scatter([L2_center_of_mass_x], [L2_center_of_mass_y], edgecolors='r')
plt.xlabel("X")
plt.ylabel("Y")
plt.title("X_Y Graph After Explosion")
plt.show()


#!/usr/bin/env python3
import math

import numpy
import roslib
import rospy
from geometry_msgs.msg import (
    Point,
    Pose,
    PoseWithCovariance,
    Quaternion,
    Twist,
    TwistWithCovariance,
    Vector3,
    WrenchStamped,
)
from interactive_markers.interactive_marker_server import InteractiveMarkerServer
from mil_msgs.msg import PoseTwist, PoseTwistStamped
from mil_msgs.orientation_helpers import xyz_array, xyzw_array
from nav_msgs.msg import Odometry
from std_msgs.msg import ColorRGBA, Header
from tf import transformations
from visualization_msgs.msg import (
    InteractiveMarker,
    InteractiveMarkerControl,
    InteractiveMarkerFeedback,
    Marker,
)

def makeBoxControl() -> InteractiveMarkerControl:
    return InteractiveMarkerControl(
        always_visible=True,
        markers=[
            Marker(
                type=Marker.CUBE,
                scale=Vector3(0.45, 0.45, 0.45),
                color=ColorRGBA(0.5, 0.5, 0.5, 1),
            ),
        ],
    )

def make6DofMarker(
    name: str,
    description: str,
    allow_rotation: bool = False,
) -> InteractiveMarker:
    int_marker = InteractiveMarker()
    int_marker.name = name
    int_marker.description = description
    int_marker.header.frame_id = "map"
    int_marker.scale = 1

    # insert a box
    int_marker.controls.append(makeBoxControl())

    for direction in "xyz":
        control = InteractiveMarkerControl()
        control.orientation.w = 1
        control.orientation.x = direction == "x"
        control.orientation.y = direction == "y"
        control.orientation.z = direction == "z"
        control.name = "move_" + direction
        control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
        int_marker.controls.append(control)

        if allow_rotation:
            control = InteractiveMarkerControl()
            control.orientation.w = 1
            control.orientation.x = direction == "x"
            control.orientation.y = direction == "y"
            control.orientation.z = direction == "z"
            control.name = "rotate_" + direction
            control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
            int_marker.controls.append(control)

    return int_marker

def makeMovingMarker(name: str, description: str) -> InteractiveMarker:
    int_marker = InteractiveMarker()
    int_marker.name = name
    int_marker.description = description
    int_marker.header.frame_id = "map"
    int_marker.scale = 1

    # insert a box
    int_marker.controls.append(makeBoxControl())

    return int_marker

if __name__ == "__main__":
    roslib.load_manifest("interactive_markers")
    roslib.load_manifest("rise_6dof")

    rospy.init_node("test_rviz")
    server = InteractiveMarkerServer("test_rviz")

    wrench = [(0, 0, 0), (0, 0, 0)]

    def set_wrench(new_wrench):
        assert new_wrench.header.frame_id == "/base_link"
        new_wrench = new_wrench.wrench
        wrench[:] = [
            (new_wrench.force.x, new_wrench.force.y, new_wrench.force.z),
            (new_wrench.torque.x, new_wrench.torque.y, new_wrench.torque.z),
        ]

    wrench_sub = rospy.Subscriber("/output", WrenchStamped, set_wrench)

    desired_pub = rospy.Publisher("/desired", PoseTwistStamped)
    desired_posetwist = PoseTwistStamped(
        header=Header(frame_id="map", stamp=rospy.Time.now()),
        posetwist=PoseTwist(
            pose=Pose(position=Point(0, 0, 0), orientation=Quaternion(0, 0, 0, 1)),
            twist=Twist(linear=Vector3(0, 0, 0), angular=Vector3(0, 0, 0)),
        ),
    )

    def updateDesired(feedback=None):
        global desired_posetwist
        if (
            feedback is not None
            and feedback.event_type == InteractiveMarkerFeedback.POSE_UPDATE
        ):
            # compute twist from this and last pose update
            if desired_posetwist is not None and rospy.Duration.from_sec(
                0,
            ) < rospy.Time.now() - desired_posetwist.header.stamp < rospy.Duration.from_sec(
                0.05,
            ):
                dt = (rospy.Time.now() - desired_posetwist.header.stamp).to_sec()
                vel = (
                    xyz_array(feedback.pose.position)
                    - xyz_array(desired_posetwist.posetwist.pose.position)
                ) / dt

                def quat_to_scaledaxis(q):
                    q = q / numpy.linalg.norm(q)
                    if q[3] < 0:
                        q *= -1
                    return 2 / numpy.sinc(q[3] / math.pi) * numpy.array(q[:3])

                angvel = (
                    quat_to_scaledaxis(
                        transformations.quaternion_multiply(
                            xyzw_array(feedback.pose.orientation),
                            transformations.quaternion_conjugate(
                                xyzw_array(
                                    desired_posetwist.posetwist.pose.orientation,
                                ),
                            ),
                        ),
                    )
                    / dt
                )
                twist = Twist(linear=Vector3(*vel), angular=Vector3(*angvel))
            else:
                twist = Twist(linear=Vector3(0, 0, 0), angular=Vector3(0, 0, 0))
            desired_posetwist = PoseTwistStamped(
                header=Header(frame_id="map", stamp=rospy.Time.now()),
                posetwist=PoseTwist(
                    pose=feedback.pose,
                    twist=twist,
                ),
            )
        if rospy.Time.now() - desired_posetwist.header.stamp > rospy.Duration.from_sec(
            0.05,
        ):
            desired_posetwist.posetwist.twist = Twist(
                linear=Vector3(0, 0, 0),
                angular=Vector3(0, 0, 0),
            )
        if desired_posetwist is not None:
            desired_pub.publish(desired_posetwist)

    server.insert(make6DofMarker("desired", "\nDesired", True), updateDesired)
    rospy.Timer(rospy.Duration(0.01), lambda msg: updateDesired(None))

    current_pub = rospy.Publisher("/current", Odometry)
    server.insert(makeMovingMarker("current", "Current\n"))

    current_pos = numpy.zeros(3)
    current_vel = numpy.zeros(3)
    current_orientation = numpy.array([0, 0, 0, 1])
    current_angvel = numpy.zeros(3)
    vel_m = 10
    angvel_I = 10
    dt = 0.01

    def updateCurrent(msg):
        global current_pos, current_vel, current_orientation, current_angvel

        world_from_body = transformations.quaternion_matrix(current_orientation)[:3, :3]

        # print wrench
        wrench_world = world_from_body.dot(wrench[0]), world_from_body.dot(wrench[1])

        current_vel += dt * (wrench_world[0] - 10 * current_vel) / vel_m
        current_angvel += dt * (wrench_world[1] - 5 * current_angvel) / angvel_I

        current_pos += dt * current_vel

        def quat_exp(quat):
            x, y, z = quat
            return transformations.quaternion_about_axis(
                numpy.linalg.norm((x, y, z)),
                (x, y, z),
            )

        current_orientation = transformations.quaternion_multiply(
            quat_exp(current_angvel * dt),
            current_orientation,
        )

        pose = Pose(
            Point(x=current_pos[0], y=current_pos[1], z=current_pos[2]),
            Quaternion(
                x=current_orientation[0],
                y=current_orientation[1],
                z=current_orientation[2],
                w=current_orientation[3],
            ),
        )

        server.setPose("current", pose)
        server.applyChanges()

        current_pub.publish(
            Odometry(
                header=Header(frame_id="map"),
                pose=PoseWithCovariance(pose=pose),
                twist=TwistWithCovariance(
                    twist=Twist(
                        linear=Vector3(*current_vel),
                        angular=Vector3(*current_angvel),
                    ),
                ),
            ),
        )

    rospy.Timer(rospy.Duration(dt), updateCurrent)

    server.applyChanges()
    rospy.spin()


import logging
import numpy as num
from scipy import signal

from matplotlib import cm, pyplot as plt

from pyrocko.guts import Tuple, Float, Int, StringChoice, Bool
from pyrocko.plot import mpl_margins, mpl_graph_color, mpl_init

from grond.plot.config import PlotConfig
from grond.plot.collection import PlotItem

logger = logging.getLogger('grond.problem.plot')

guts_prefix = 'grond'

def fixlim(lo, hi):
    if lo == hi:
        return lo - 1.0, hi + 1.0
    else:
        return lo, hi

class SequencePlot(PlotConfig):
    '''
    Draws all parameter values evaluated during the optimisation

    The sequence of all the parameter values is either a function of the
    optimisation in progress or of the misfit from high to low. This plot can
    be used to check on convergence or see if model parameters push the given
    bounds. The color always shows the relative misfit. Relatively high misfits
    are in cold blue colors and relatively low misfits in red. The last panel
    gives the corresponding misfit values.
    '''

    name = 'sequence'
    size_cm = Tuple.T(2, Float.T(), default=(14., 6.))
    misfit_cutoff = Float.T(optional=True)
    ibootstrap = Int.T(optional=True)
    sort_by = StringChoice.T(
        choices=['iteration', 'misfit'],
        default='iteration')
    subplot_layout = Tuple.T(2, Int.T(), default=(1, 1))
    marker_size = Float.T(default=1.5)
    show_reference = Bool.T(default=True)

    def make(self, environ):
        cm = environ.get_plot_collection_manager()
        history = environ.get_history()
        optimiser = environ.get_optimiser()

        mpl_init(fontsize=self.font_size)
        cm.create_group_mpl(
            self,
            self.draw_figures(history, optimiser),
            title=u'Sequence Plots',
            section='optimiser',
            description=u'''
Sequence plots for all parameters of the optimisation.

The sequence of all the parameter values is either a function of the
optimisation in progress or of the misfit from high to low. This plot can be
used to check on convergence or to see if model parameters push the given
bounds.

The color always shows the relative misfit. Relatively high misfits are in
cold blue colors and relatively low misfits in red. The last panel gives the
corresponding misfit values.
''',
            feather_icon='fast-forward')

    def draw_figures(self, history, optimiser):
        misfit_cutoff = self.misfit_cutoff
        sort_by = self.sort_by

        problem = history.problem
        models = history.models

        npar = problem.nparameters
        ndep = problem.ndependants
        fontsize = self.font_size
        nfx, nfy = self.subplot_layout

        imodels = num.arange(history.nmodels)
        bounds = problem.get_combined_bounds()

        xref = problem.get_reference_model()

        gms = history.get_primary_chain_misfits()
        gms_softclip = num.where(gms > 1.0, 0.2 * num.log10(gms) + 1.0, gms)

        isort = num.argsort(gms)[::-1]

        if sort_by == 'iteration':
            imodels = imodels[isort]
        elif sort_by == 'misfit':
            imodels = num.arange(imodels.size)
        else:
            assert False

        gms = gms[isort]
        gms_softclip = gms_softclip[isort]
        models = models[isort, :]

        iorder = num.empty_like(isort)
        iorder = num.arange(iorder.size)

        if misfit_cutoff is None:
            ibest = num.ones(gms.size, dtype=num.bool)
        else:
            ibest = gms < misfit_cutoff

        def config_axes(axes, nfx, nfy, impl, iplot, nplots):
            if (impl - 1) % nfx != nfx - 1:
                axes.get_yaxis().tick_left()

            if (impl - 1) >= (nfx * (nfy - 1)) or iplot >= nplots - nfx:
                axes.set_xlabel('Iteration')
                if not (impl - 1) // nfx == 0:
                    axes.get_xaxis().tick_bottom()
            elif (impl - 1) // nfx == 0:
                axes.get_xaxis().tick_top()
                axes.set_xticklabels([])
            else:
                axes.get_xaxis().set_visible(False)

        # nfz = (npar + ndep + 1 - 1) / (nfx*nfy) + 1
        cmap = cm.YlOrRd
        cmap = cm.jet
        msize = self.marker_size
        axes = None
        fig = None
        item_fig = None
        nfigs = 0
        alpha = 0.5
        for ipar in range(npar):
            impl = ipar % (nfx * nfy) + 1

            if impl == 1:
                if item_fig:
                    yield item_fig
                    nfigs += 1

                fig = plt.figure(figsize=self.size_inch)
                labelpos = mpl_margins(
                    fig, nw=nfx, nh=nfy,
                    left=7.,
                    right=2.,
                    top=1.,
                    bottom=5.,
                    wspace=7., hspace=2., units=fontsize)

                item = PlotItem(name='fig_%i' % (nfigs+1))
                item.attributes['parameters'] = []
                item_fig = (item, fig)

            par = problem.parameters[ipar]

            item_fig[0].attributes['parameters'].append(par.name)

            axes = fig.add_subplot(nfy, nfx, impl)
            labelpos(axes, 2.5, 2.0)

            axes.set_ylabel(par.get_label())
            axes.get_yaxis().set_major_locator(plt.MaxNLocator(4))

            config_axes(axes, nfx, nfy, impl, ipar, npar + ndep + 1)

            axes.set_ylim(*fixlim(*par.scaled(bounds[ipar])))
            axes.set_xlim(0, history.nmodels)

            axes.scatter(
                imodels[ibest], par.scaled(models[ibest, ipar]), s=msize,
                c=iorder[ibest], edgecolors='none', cmap=cmap, alpha=alpha,
                rasterized=True)

            if self.show_reference:
                axes.axhline(par.scaled(xref[ipar]), color='black', alpha=0.3)

        for idep in range(ndep):
            # ifz, ify, ifx = num.unravel_index(ipar, (nfz, nfy, nfx))
            impl = (npar + idep) % (nfx * nfy) + 1

            if impl == 1:
                if item_fig:
                    yield item_fig
                    nfigs += 1

                fig = plt.figure(figsize=self.size_inch)
                labelpos = mpl_margins(
                    fig, nw=nfx, nh=nfy,
                    left=7.,
                    right=2.,
                    top=1.,
                    bottom=5.,
                    wspace=7., hspace=2., units=fontsize)

                item = PlotItem(name='fig_%i' % (nfigs+1))
                item.attributes['parameters'] = []

                item_fig = (item, fig)

            par = problem.dependants[idep]
            item_fig[0].attributes['parameters'].append(par.name)

            axes = fig.add_subplot(nfy, nfx, impl)
            labelpos(axes, 2.5, 2.0)

            axes.set_ylabel(par.get_label())
            axes.get_yaxis().set_major_locator(plt.MaxNLocator(4))

            config_axes(axes, nfx, nfy, impl, npar + idep, npar + ndep + 1)

            axes.set_ylim(*fixlim(*par.scaled(bounds[npar + idep])))
            axes.set_xlim(0, history.nmodels)

            ys = problem.make_dependant(models[ibest, :], par.name)
            axes.scatter(
                imodels[ibest], par.scaled(ys), s=msize, c=iorder[ibest],
                edgecolors='none', cmap=cmap, alpha=alpha, rasterized=True)

            if self.show_reference:
                y = problem.make_dependant(xref, par.name)
                axes.axhline(par.scaled(y), color='black', alpha=0.3)

        impl = (npar + ndep) % (nfx * nfy) + 1
        if impl == 1:
            if item_fig:
                yield item_fig
                nfigs += 1

            fig = plt.figure(figsize=self.size_inch)
            labelpos = mpl_margins(
                fig, nw=nfx, nh=nfy,
                left=7.,
                right=2.,
                top=1.,
                bottom=5.,
                wspace=7., hspace=2., units=fontsize)

            item = PlotItem(name='fig_%i' % (nfigs+1))
            item.attributes['parameters'] = []

            item_fig = (item, fig)

        axes = fig.add_subplot(nfy, nfx, impl)
        labelpos(axes, 2.5, 2.0)

        config_axes(axes, nfx, nfy, impl, npar + ndep, npar + ndep + 1)

        axes.set_ylim(0., 1.5)
        axes.set_yticks([0., 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4])
        axes.set_yticklabels(
            ['0.0', '0.2', '0.4', '0.6', '0.8', '1', '10', '100'])

        axes.scatter(
            imodels[ibest], gms_softclip[ibest], c=iorder[ibest],
            s=msize, edgecolors='none', cmap=cmap, alpha=alpha)

        axes.axhspan(1.0, 1.5, color=(0.8, 0.8, 0.8), alpha=0.2)
        axes.axhline(1.0, color=(0.5, 0.5, 0.5), zorder=2)

        axes.set_xlim(0, history.nmodels)
        axes.set_xlabel('Iteration')

        axes.set_ylabel('Misfit')

        yield item_fig
        nfigs += 1

class ContributionsPlot(PlotConfig):
    ''' Relative contribution of single targets to the global misfit
    '''

    name = 'contributions'
    size_cm = Tuple.T(2, Float.T(), default=(21., 14.9))

    def make(self, environ):
        cm = environ.get_plot_collection_manager()
        history = environ.get_history()
        optimiser = environ.get_optimiser()
        dataset = environ.get_dataset()

        environ.setup_modelling()

        mpl_init(fontsize=self.font_size)
        cm.create_group_mpl(
            self,
            self.draw_figures(dataset, history, optimiser),
            title=u'Target Contributions',
            section='solution',
            feather_icon='thermometer',
            description=u'''
Contributions of the targets to the total misfit.

The relative contribution that each single target has in the global misfit
result is plotted relative and unscales as a function of global misfit
(descending).

The target contribution is shown in color-filled curves with the bottom curve
on the bottom and the best-fit target on top. This plot can be used to analyse
the balance of targets in the optimisations. For ideal configurations, the
target contributions are of similar size. If the contribution of a single
target is much larger than those of all others, the weighting should be
modified.
''')

    def draw_figures(self, dataset, history, optimiser):

        fontsize = self.font_size

        fig = plt.figure(figsize=self.size_inch)
        labelpos = mpl_margins(fig, nw=2, nh=2, w=7., h=5., wspace=2.,
                               hspace=5., units=fontsize)

        problem = history.problem
        if not problem:
            logger.warn('Problem not set.')
            return []

        models = history.models

        if models.size == 0:
            logger.warn('Empty models vector.')
            return []

        for target in problem.targets:
            target.set_dataset(dataset)

        imodels = num.arange(history.nmodels)

        gms = history.get_sorted_primary_misfits()[::-1]
        isort = history.get_sorted_misfits_idx(chain=0)[::-1]

        gms **= problem.norm_exponent
        gms_softclip = num.where(gms > 1.0, 0.1 * num.log10(gms) + 1.0, gms)

        gcms = problem.combine_misfits(
            history.misfits,
            extra_correlated_weights=optimiser.get_correlated_weights(problem),
            get_contributions=True)

        gcms = gcms[isort, :]
        nmisfits = gcms.shape[1]  # noqa

        ncontributions = sum([1 if t.plot_misfits_cumulative else t.nmisfits
                              for t in problem.targets])
        cum_gcms = num.zeros((history.nmodels, ncontributions))

        # Squash matrix and sum large targets.nmisifts, eg SatelliteTarget
        plot_target_labels = []
        idx = 0
        idx_cum = 0
        for itarget, target in enumerate(problem.targets):
            target_gcms = gcms[:, idx:idx+target.nmisfits]
            if target.plot_misfits_cumulative:
                cum_gcms[:, idx_cum] = target_gcms.sum(axis=1)
                plot_target_labels.append(target.string_id())
                idx_cum += 1
            else:
                cum_gcms[:, idx_cum:idx_cum+target.nmisfits] = target_gcms
                plot_target_labels.extend(target.misfits_string_ids())
                idx_cum += target.nmisfits
            idx += target.nmisfits

        jsort = num.argsort(cum_gcms[-1, :])[::-1]

        # ncols = 4
        # nrows = ((problem.ntargets + 1) - 1) / ncols + 1

        axes = fig.add_subplot(2, 2, 1)
        labelpos(axes, 2.5, 2.0)

        axes.set_ylabel('Relative contribution (smoothed)')
        axes.set_ylim(0.0, 1.0)

        axes2 = fig.add_subplot(2, 2, 3, sharex=axes)
        labelpos(axes2, 2.5, 2.0)

        axes2.set_xlabel(
            'Tested model, sorted descending by global misfit value')

        axes2.set_ylabel('Square of misfit')

        axes2.set_ylim(0., 1.5)
        axes2.axhspan(1.0, 1.5, color=(0.8, 0.8, 0.8))
        axes2.set_yticks(
            [0., 0.2, 0.4, 0.6, 0.8, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5])
        axes2.set_yticklabels(
            ['0.0', '0.2', '0.4', '0.6', '0.8', '1', '10', '100', '1000',
             '10000', '100000'])

        axes2.set_xlim(imodels[0], imodels[-1])

        rel_ms_sum = num.zeros(history.nmodels)
        rel_ms_smooth_sum = num.zeros(history.nmodels)
        ms_smooth_sum = num.zeros(history.nmodels)
        b = num.hanning(min(100, history.nmodels//5))
        b /= num.sum(b)
        a = [1]
        ii = 0

        for idx in jsort:
            target_label = plot_target_labels[idx]
            ms = cum_gcms[:, idx]

            ms = num.where(num.isfinite(ms), ms, 0.0)
            if num.all(ms == 0.0):
                continue

            rel_ms = ms / gms

            if b.shape[0] > 5:
                rel_ms_smooth = signal.filtfilt(b, a, rel_ms)
            else:
                rel_ms_smooth = rel_ms

            ms_smooth = rel_ms_smooth * gms_softclip

            rel_poly_y = num.concatenate(
                [rel_ms_smooth_sum[::-1], rel_ms_smooth_sum + rel_ms_smooth])
            poly_x = num.concatenate([imodels[::-1], imodels])

            add_args = {}
            if ii < 20:
                add_args['label'] = '%s (%.2g)' % (
                    target_label, num.mean(rel_ms[-1]))

            axes.fill(
                poly_x, rel_poly_y,
                alpha=0.5,
                color=mpl_graph_color(ii),
                **add_args)

            poly_y = num.concatenate(
                [ms_smooth_sum[::-1], ms_smooth_sum + ms_smooth])

            axes2.fill(poly_x, poly_y, alpha=0.5, color=mpl_graph_color(ii))

            rel_ms_sum += rel_ms

            # axes.plot(
            #    imodels, rel_ms_sum, color='black', alpha=0.1, zorder=-1)

            ms_smooth_sum += ms_smooth
            rel_ms_smooth_sum += rel_ms_smooth
            ii += 1

        axes.legend(
            title='Contributions (top twenty)',
            bbox_to_anchor=(1.05, 0.0, 1.0, 1.0),
            loc='upper left',
            ncol=1, borderaxespad=0., prop={'size': 9})

        axes2.plot(imodels, gms_softclip, color='black')
        axes2.axhline(1.0, color=(0.5, 0.5, 0.5))

        return [[PlotItem(name='main'), fig]]

class BootstrapPlot(PlotConfig):
    '''
    Sorted misfit (descending) of single bootstrap chains

    For each bootstrap configuration, all models are sorted according to their
    misfit value (red lines) and their global misfit value (black line). (They
    are sorted individually for each line). The best model of every bootstrap
    configuration (right end model of red lines) is marked as a cross in the
    global misfit configuration. The horizontal black lines indicate mean and
    +- standard deviation of the y-axis values of these crosses. If the
    bootstrap configurations converge to the same region in model-space, all
    crosses should be close to the right end of the plot. If this is not the
    case, some bootstrap configurations have converged to very different places
    in model-space. This would be an indicator that there might be
    inconsistencies in the observations (maybe due to faulty or noisy or
    misoriented data). Also the shape of the curve in general can give
    information. A well-behaved optimisation run has approximately linear
    functions in this plot. Only at the end they should have a higher downward
    gradient. This would be the place where the objective functions of the
    bootstrap start to disagree.
    '''

    name = 'bootstrap'
    size_cm = Tuple.T(2, Float.T(), default=(21., 14.9))
    show_ticks = Bool.T(default=False)

    def make(self, environ):
        cm = environ.get_plot_collection_manager()
        history = environ.get_history()
        optimiser = environ.get_optimiser()
        mpl_init(fontsize=self.font_size)
        cm.create_group_mpl(
            self,
            self.draw_figures(history, optimiser),
            title=u'Bootstrap Misfit',
            section='optimiser',
            feather_icon='trending-down',
            description=u'''
Sorted misfit (descending) of single bootstrap chains.

For each bootstrap configuration, all models are sorted according to their
misfit value (red lines) and their global misfit value (black line). (They are
sorted individually for each line). The best model of every bootstrap
configuration (right end model of red lines) is marked as a cross in the global
misfit configuration. The horizontal black lines indicate mean and +- standard
deviation of the y-axis values of these crosses.

If the bootstrap configurations converge to the same region in model-space, all
crosses should be close to the right end of the plot. If this is not the case,
some bootstrap configurations have converged to very different places in
model-space. This would indicate that there might be inconsistencies in the
observations (maybe due to faulty or noisy or misoriented data). Also the shape
of the curve in general can give information. A well-behaved optimisation run
has approximately linear functions in this plot. Only at the end they should
have a higher downward gradient. This would be the place where the objective
functions of the bootstrap start to disagree.
''')

    def draw_figures(self, history, optimiser):

        fig = plt.figure()

        imodels = num.arange(history.nmodels)
        gms = history.bootstrap_misfits[:, 0]

        gms_softclip = num.where(gms > 1.0,
                                 0.1 * num.log10(gms) + 1.0,
                                 gms)
        axes = fig.add_subplot(1, 1, 1)

        ibests = []
        for ibootstrap in range(history.nchains):
            # if ibootstrap ==0:
            #    global, no-bootstrapping misfits, chain
            #    gms = history.bootstrap_misfits[:, ibootstrap]
            #    gms_softclip = num.where(gms > 1.0,
            #                            0.1 * num.log10(gms) + 1.0,
            #                            gms)

            bms = history.bootstrap_misfits[:, ibootstrap]
            isort_bms = num.argsort(bms)[::-1]
            ibests.append(isort_bms[-1])

            bms_softclip = num.where(
                bms > 1.0, 0.1 * num.log10(bms) + 1.0, bms)
            axes.plot(imodels, bms_softclip[isort_bms], color='red', alpha=0.2)

        isort = num.argsort(gms)[::-1]
        iorder = num.empty(isort.size)
        iorder[isort] = imodels

        axes.plot(iorder[ibests], gms_softclip[ibests], 'x', color='black')

        m = num.median(gms_softclip[ibests])
        s = num.std(gms_softclip[ibests])
        axes.axhline(m + s, color='black', alpha=0.5)
        axes.axhline(m, color='black')
        axes.axhline(m - s, color='black', alpha=0.5)

        axes.plot(imodels, gms_softclip[isort], color='black')

        axes.set_xlim(imodels[0], imodels[-1])
        axes.set_xlabel(
            'Tested model, sorted descending by global misfit value')

        return [(PlotItem(name='main'), fig)]

def get_plot_classes():
    return [SequencePlot, ContributionsPlot, BootstrapPlot]