# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from pathlib import Path
import shutil

import numpy as np

import paddle

import paddle.nn as nn

import tvm
import tvm.testing
import tvm.topi.testing
from tvm import relay
from tvm.contrib import graph_executor

PADDLE_TEST_DATA_ROOT_PATH = Path(Path("~").expanduser(), ".tvm_test_data", "paddle")
PADDLE_TEST_DATA_ROOT_PATH.mkdir(parents=True, exist_ok=True)

def assert_shapes_match(tru, est):
    if tru.shape != est.shape:
        msg = "Paddle Output shapes {} and TVM shapes {} don't match"
        raise AssertionError(msg.format(tru.shape, est.shape))
    if tru.dtype != est.dtype:
        msg = "Paddle Output dtype {} and TVM dtype {} don't match"
        raise AssertionError(msg.format(tru.dtype, est.dtype))

def get_paddle_model(func, input_spec):
    global PADDLE_TEST_DATA_ROOT_PATH
    model_path = Path(PADDLE_TEST_DATA_ROOT_PATH, "model")
    paddle.jit.save(func, str(model_path), input_spec=input_spec)
    baseline_model = paddle.jit.load(str(model_path))

    shutil.rmtree(str(PADDLE_TEST_DATA_ROOT_PATH))
    return baseline_model

def get_tvm_output_with_vm(mod, params, target, device, input_data):
    """Generic function to execute and get tvm output with vm executor"""

    ex = relay.create_executor("vm", mod=mod, device=device, target=target)
    params.update(input_data)
    result = ex.evaluate()(**params)
    if isinstance(result, tvm.runtime.NDArray):
        return [
            result.numpy(),
        ]
    return [r.numpy() for r in result]

def get_tvm_output(mod, params, target, device, input_data, compiled_names, num):
    """Generic function to execute and get tvm output"""

    lib = relay.build(mod, target=target, params=params)
    gmod = graph_executor.GraphModule(lib["default"](device))
    for name in compiled_names:
        gmod.set_input(name, input_data[name])
    gmod.run()
    outputs = []
    for i in range(num):
        outputs.append(gmod.get_output(i).numpy())
    return outputs

def verify_model(func, input_data, rtol=1e-5, atol=1e-5, input_shape=None):
    if not isinstance(input_data, (tuple, list)):
        input_data = [input_data]

    input_spec = []
    input_names = []
    input_shape_dict = {}
    compiled_input = {}
    for idx, data in enumerate(input_data):
        input_name = "input{}".format(idx)
        if input_shape:
            shape = input_shape[idx]
        else:
            shape = data.shape
        input_shape_dict[input_name] = shape
        input_spec.append(paddle.static.InputSpec(dtype=data.dtype, shape=shape, name=input_name))
        input_names.append(input_name)
        if isinstance(data, np.ndarray):
            compiled_input[input_name] = data
        else:
            compiled_input[input_name] = data.numpy()

    baseline_model = get_paddle_model(func, input_spec)
    baseline_outputs = baseline_model(*[input[:] for input in input_data])

    # get paddle outputs
    if isinstance(baseline_outputs, (tuple, list)):
        baseline_outputs = tuple(out.numpy() for out in baseline_outputs)
    else:
        baseline_outputs = (baseline_outputs.numpy(),)

    mod, params = relay.frontend.from_paddle(baseline_model, input_shape_dict)
    parms_num = min(len(input_names), len(mod["main"].params))
    compiled_names = []
    for arg in mod["main"].params:
        assert arg.name_hint in input_names or arg.name_hint in params
        if arg.name_hint in input_names:
            compiled_names.append(arg.name_hint)

    with tvm.transform.PassContext(opt_level=3):
        for target, dev in tvm.testing.enabled_targets():
            if input_shape:
                tvm_output = get_tvm_output_with_vm(mod, params, target, dev, compiled_input)
            else:
                tvm_output = get_tvm_output(
                    mod, params, target, dev, compiled_input, compiled_names, len(baseline_outputs)
                )

            for baseline_output, compiled_output in zip(baseline_outputs, tvm_output):
                assert_shapes_match(baseline_output, compiled_output)
                tvm.testing.assert_allclose(baseline_output, compiled_output, rtol=rtol, atol=atol)

@tvm.testing.uses_gpu
def test_forward_math():
    class MathOp(nn.Layer):
        def __init__(self, op_name):
            super(MathOp, self).__init__()
            for candidate in (paddle, paddle.nn.functional):
                self.func = getattr(candidate, op_name, None)
                if self.func:
                    break

        @paddle.jit.to_static
        def forward(self, inputs):
            return self.func(inputs)

    input_data = paddle.rand([1, 2, 5, 5], dtype="float32")
    op_list = [
        "abs",
        "acos",
        "asin",
        "atan",
        "ceil",
        "cos",
        "cosh",
        "erf",
        "exp",
        "floor",
        "log",
        "log2",
        "log10",
        "log1p",
        "numel",
        "relu",
        "round",
        "rsqrt",
        "sigmoid",
        "sign",
        "rsqrt",
        "sin",
        "sinh",
        "sqrt",
        "tan",
        "tanh",
    ]
    for op_name in op_list:
        verify_model(MathOp(op_name), input_data)

@tvm.testing.uses_gpu
def test_forward_add_subtract():
    input_shape = [10]

    @paddle.jit.to_static
    def add_subtract(inputs):
        return paddle.subtract(paddle.add(inputs, inputs), inputs)

    @paddle.jit.to_static
    def add_subtract2(inputs):
        return inputs + 1 - 2

    @paddle.jit.to_static
    def add_subtract3(inputs1, inputs2):
        ones = paddle.ones([10], dtype="float32")
        return inputs1 + ones - inputs2

    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(add_subtract, input_data)
    verify_model(add_subtract2, input_data)
    input_data2 = paddle.rand(input_shape, dtype="float32")
    verify_model(add_subtract3, [input_data, input_data2])

@tvm.testing.uses_gpu
def test_forward_addmm():
    @paddle.jit.to_static
    def addmm(input, x, y, alpha=1, beta=1):
        return paddle.addmm(input, x, y, alpha, beta)

    input_shape = [10, 10]
    x_shape = [10, 3]
    y_shape = [3, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    x_data = paddle.rand(x_shape, dtype="float32")
    y_data = paddle.rand(y_shape, dtype="float32")
    verify_model(addmm, input_data=[input_data, x_data, y_data])

@tvm.testing.uses_gpu
def test_forward_argmax():
    input_shape = [1, 3, 10, 10]

    class ArgMax(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return paddle.argmax(inputs)

    class ArgMax1(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return inputs.argmax(axis=1)

    class ArgMax2(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return inputs.argmax(axis=1, keepdim=False)

    class ArgMax3(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return inputs.argmax(axis=2, keepdim=True)

    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(ArgMax(), input_data=input_data)
    verify_model(ArgMax1(), input_data=input_data)
    verify_model(ArgMax2(), input_data=input_data)
    verify_model(ArgMax3(), input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_argmin():
    input_shape = [1, 3, 10, 10]

    class ArgMin(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return paddle.argmin(inputs)

    class ArgMin1(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return inputs.argmin(axis=1)

    class ArgMin2(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return inputs.argmin(axis=1, keepdim=False)

    class ArgMin3(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return inputs.argmin(axis=2, keepdim=True)

    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(ArgMin(), input_data=input_data)
    verify_model(ArgMin1(), input_data=input_data)
    verify_model(ArgMin2(), input_data=input_data)
    verify_model(ArgMin3(), input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_argsort():
    @paddle.jit.to_static
    def argsort(inputs):
        return paddle.argsort(inputs)

    @paddle.jit.to_static
    def argsort2(inputs):
        return paddle.argsort(inputs, axis=0, descending=True)

    input_shape = [2, 3, 5]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(argsort, input_data)
    input_data2 = np.random.randint(100, size=input_shape)
    verify_model(argsort2, input_data2)

@tvm.testing.uses_gpu
def test_forward_assign():
    class Assign(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return paddle.assign(inputs)

    input_shape = [2, 3]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(Assign(), [input_data])
    input_data2 = np.random.randint(100, size=input_shape)
    verify_model(Assign(), [input_data2], input_shape=[[-1, -1]])

@tvm.testing.uses_gpu
def test_forward_batch_norm():
    class BatchNorm1D(nn.Layer):
        def __init__(self):
            super(BatchNorm1D, self).__init__()
            self.batch_norm = nn.BatchNorm1D(2)

        @paddle.jit.to_static
        def forward(self, input_data):
            return self.batch_norm(input_data)

    class BatchNorm2D(nn.Layer):
        def __init__(self):
            super(BatchNorm2D, self).__init__()
            self.batch_norm = nn.BatchNorm2D(2)

        @paddle.jit.to_static
        def forward(self, input_data):
            return self.batch_norm(input_data)

    class BatchNorm3D(nn.Layer):
        def __init__(self):
            super(BatchNorm3D, self).__init__()
            self.batch_norm = nn.BatchNorm3D(2)

        @paddle.jit.to_static
        def forward(self, input_data):
            return self.batch_norm(input_data)

    input_data = paddle.rand((2, 2, 3), dtype="float32")
    verify_model(BatchNorm1D(), input_data=input_data)
    input_data = paddle.rand((2, 2, 2, 3), dtype="float32")
    verify_model(BatchNorm2D(), input_data=input_data)
    input_data = paddle.rand((2, 2, 2, 2, 3), dtype="float32")
    verify_model(BatchNorm3D(), input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_cast():
    @paddle.jit.to_static
    def cast1(inputs, dtype="uint8"):
        return paddle.cast(inputs, dtype)

    @paddle.jit.to_static
    def cast2(inputs, dtype="int64"):
        return inputs.cast(dtype)

    input_shape = [2, 3]
    input_data = paddle.rand(input_shape, dtype="float32") * 100
    verify_model(cast1, [input_data])
    verify_model(cast2, [input_data])

@tvm.testing.uses_gpu
def test_forward_clip():
    @paddle.jit.to_static
    def clip(inputs):
        return paddle.clip(inputs, min=3, max=5)

    @paddle.jit.to_static
    def clip2(inputs, max_value):
        return paddle.clip(inputs, max=max_value)

    @paddle.jit.to_static
    def clip3(inputs, min_value):
        return paddle.clip(inputs, min=min_value)

    @paddle.jit.to_static
    def clip4(inputs, min_value, max_value):
        return paddle.clip(inputs, min=min_value, max=max_value)

    verify_model(clip, paddle.to_tensor([[1, 2], [4, 6]], dtype="int32"))
    x = np.array([[1.2, 3.5], [4.5, 6.4]])
    x1 = paddle.to_tensor(x, dtype="float32")
    min_value = paddle.to_tensor(np.array([2.1]), dtype="float32")
    max_value = paddle.to_tensor(np.array([4.5]), dtype="float32")
    verify_model(clip2, [x1, max_value])
    verify_model(clip3, [x1, min_value])
    verify_model(clip4, [x1, min_value, max_value])

@tvm.testing.uses_gpu
def test_forward_concat_unsqueeze():
    @paddle.jit.to_static
    def concat_unsqueeze1(inputs):
        return paddle.concat([inputs[:, 0].unsqueeze(1), inputs[:, 1].unsqueeze(1)], axis=1)

    @paddle.jit.to_static
    def concat_unsqueeze2(inputs):
        a = (inputs[:, :, 0] + 2) * 7
        b = (inputs[:, :, 1] + 3) * 11
        c = (inputs[:, :, 2] + 5) * 13
        return paddle.concat([paddle.unsqueeze(t, axis=2) for t in [a, b, c]], axis=2)

    input_shape = [1, 3, 10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(concat_unsqueeze1, input_data=input_data)
    verify_model(concat_unsqueeze2, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_crop():
    @paddle.jit.to_static
    def crop1(inputs):
        return paddle.crop(inputs, shape=[2, 2])

    @paddle.jit.to_static
    def crop2(inputs, shape):
        return paddle.crop(inputs, shape=shape, offsets=[0, 1])

    @paddle.jit.to_static
    def crop3(inputs):
        offsets = paddle.to_tensor(np.array([1, 0]).astype("int32"))
        return paddle.crop(inputs, shape=[3, 3], offsets=offsets)

    @paddle.jit.to_static
    def crop4(inputs, shape, offsets):
        return paddle.crop(inputs, shape=shape, offsets=offsets)

    input_shape = [10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(crop1, input_data=[input_data])
    shape = paddle.to_tensor(np.array([3, 3], "int32"))
    verify_model(crop2, [input_data, shape], input_shape=[[-1, -1], [2]])
    verify_model(crop3, input_data=[input_data])
    offsets = paddle.to_tensor(np.array([1, 1]).astype("int32"))
    verify_model(crop4, input_data=[input_data, shape, offsets], input_shape=[[-1, -1], [2], [2]])

@tvm.testing.uses_gpu
def test_forward_cumsum():
    class Cumsum1(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return paddle.cumsum(inputs)

    class Cumsum2(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return paddle.cumsum(inputs, axis=0)

    class Cumsum3(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            return paddle.cumsum(inputs, axis=1)

    input_data = paddle.randint(0, 100, (10, 10), dtype=paddle.int32)
    verify_model(Cumsum1(), input_data)
    verify_model(Cumsum1(), [input_data.astype(paddle.int64)])
    verify_model(Cumsum2(), input_data)
    verify_model(Cumsum3(), input_data)

@tvm.testing.uses_gpu
def test_forward_conv():
    class Conv2D1(nn.Layer):
        def __init__(self):
            super(Conv2D1, self).__init__()
            self.conv = nn.Conv2D(3, 6, 7, bias_attr=True)
            self.softmax = nn.Softmax()

        @paddle.jit.to_static
        def forward(self, inputs):
            return self.softmax(self.conv(inputs))

    class Conv2D2(nn.Layer):
        def __init__(self):
            super(Conv2D2, self).__init__()
            self.conv = nn.Conv2D(3, 6, 7, groups=3, bias_attr=False)
            self.softmax = nn.Softmax()

        @paddle.jit.to_static
        def forward(self, inputs):
            return self.softmax(self.conv(inputs))

    class Conv2D3(nn.Layer):
        def __init__(self):
            super(Conv2D3, self).__init__()
            self.conv = nn.Conv2D(3, 6, 7, groups=3, bias_attr=False, padding="SAME")

        @paddle.jit.to_static
        def forward(self, inputs):
            return self.conv(inputs)

    class Conv2D4(nn.Layer):
        def __init__(self):
            super(Conv2D4, self).__init__()
            self.conv = nn.Conv2D(
                3, 6, 7, groups=3, bias_attr=False, padding=[1, 2, 0, 1], stride=2, dilation=2
            )

        @paddle.jit.to_static
        def forward(self, inputs):
            return self.conv(inputs)

    conv2d_input_shape = [1, 3, 112, 112]
    conv2d_input_data = paddle.rand(conv2d_input_shape, dtype="float32")
    verify_model(Conv2D1(), input_data=conv2d_input_data)
    verify_model(Conv2D2(), input_data=conv2d_input_data)
    verify_model(Conv2D3(), input_data=conv2d_input_data)
    verify_model(Conv2D4(), input_data=conv2d_input_data)
    verify_model(Conv2D1(), conv2d_input_data, input_shape=[[-1, 3, 112, 112]])

@tvm.testing.uses_gpu
def test_forward_dist():
    @paddle.jit.to_static
    def dist(x, y):
        return paddle.dist(x, y, p=2)

    @paddle.jit.to_static
    def dist2(x, y):
        return paddle.dist(x, y, p=20)

    @paddle.jit.to_static
    def dist3(x, y):
        return paddle.dist(x, y, p=float("-inf"))

    @paddle.jit.to_static
    def dist4(x, y):
        return paddle.dist(x, y, p=float("inf"))

    x_shape = [10, 3]
    y_shape = [10, 1]
    x_data = paddle.rand(x_shape, dtype="float32")
    y_data = paddle.rand(y_shape, dtype="float32")
    verify_model(dist, input_data=[x_data, y_data])
    verify_model(dist2, input_data=[x_data, y_data])
    verify_model(dist3, input_data=[x_data, y_data])
    verify_model(dist4, input_data=[x_data, y_data])

@tvm.testing.uses_gpu
def test_forward_dot():
    @paddle.jit.to_static
    def dot(x, y):
        return paddle.dot(x, y)

    x_shape = [10, 3]
    y_shape = [10, 3]
    x_data = paddle.rand(x_shape, dtype="float32")
    y_data = paddle.rand(y_shape, dtype="float32")
    verify_model(dot, input_data=[x_data, y_data])

@tvm.testing.uses_gpu
def test_forward_dropout():
    @paddle.jit.to_static
    def dropout(inputs):
        return nn.functional.dropout(inputs)

    input_shape = [1, 3, 10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(dropout, input_data=input_data[0, 0])
    verify_model(dropout, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_expand():
    @paddle.jit.to_static
    def expand1(inputs):
        return paddle.expand(inputs, shape=[2, 3])

    @paddle.jit.to_static
    def expand2(inputs, shape):
        return paddle.expand(inputs, shape=shape)

    x_shape = [3]
    x_data = paddle.rand(x_shape, dtype="float32")
    verify_model(expand1, input_data=[x_data])
    shape = paddle.to_tensor(np.array([2, 3]).astype("int32"))
    verify_model(expand2, [x_data, shape], input_shape=[[3], [2]])

@tvm.testing.uses_gpu
def test_forward_expand_as():
    @paddle.jit.to_static
    def expand_as(x, y):
        z = paddle.expand_as(x, y)
        z += y
        return z

    data_x = paddle.to_tensor([1, 2, 3], dtype="int32")
    data_y = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype="float32")
    verify_model(expand_as, [data_x, data_y])

@tvm.testing.uses_gpu
def test_forward_ones():
    @paddle.jit.to_static
    def ones1(inputs):
        ones = paddle.ones([1, 3, 10, 10])
        out = inputs + ones
        return out

    @paddle.jit.to_static
    def ones2(inputs):
        shape = paddle.to_tensor([1, 3, 10, 10], dtype="int32")
        ones = paddle.ones(shape)
        out = inputs + ones
        return out

    input_shape = [1, 3, 10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(ones1, input_data=input_data)
    verify_model(ones2, input_data=input_data)

def test_forward_elemwise():
    class ElemwiseOp(nn.Layer):
        def __init__(self, op_name):
            super(ElemwiseOp, self).__init__()
            self.op_name_ = op_name
            for candidate in (paddle, paddle.nn.functional):
                self.func = getattr(candidate, op_name, None)
                if self.func:
                    break

        @paddle.jit.to_static
        def forward(self, input1, input2):
            y = self.func(input1, input2)
            if "equal" in self.op_name_ or "than" in self.op_name_:
                y = paddle.cast(y, "int32")
            return y

    op_list = [
        "floor_divide",
        "floor_mod",
        "maximum",
        "minimum",
        "equal",
        "greater_equal",
        "greater_than",
        "less_equal",
        "less_than",
        "not_equal",
    ]
    input_shape = [10, 10]
    input_shape_2 = [
        10,
    ]
    x_data = paddle.rand(input_shape, dtype="float32")
    y_data = paddle.rand(input_shape_2, dtype="float32")
    x_data_2 = paddle.randint(1, 100, input_shape_2, dtype="int32")
    y_data_2 = paddle.randint(1, 100, input_shape, dtype="int32")
    for op_name in op_list:
        if op_name not in ["floor_divide"]:
            verify_model(ElemwiseOp(op_name), [x_data, y_data])
        verify_model(ElemwiseOp(op_name), [x_data_2, y_data_2])

@tvm.testing.uses_gpu
def test_forward_gelu():
    @paddle.jit.to_static
    def gelu(inputs):
        return nn.functional.gelu(inputs)

    input_shape = [1, 3, 10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(gelu, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_activation():
    class Activation(nn.Layer):
        def __init__(self, op_name):
            super(Activation, self).__init__()
            self.op_name_ = op_name
            for candidate in (paddle.nn.functional, paddle):
                self.func = getattr(candidate, op_name, None)
                if self.func:
                    break

        @paddle.jit.to_static
        def forward(self, inputs):
            return self.func(inputs)

    input_shape = [1, 3, 10, 10]
    input_data = paddle.normal(shape=input_shape) * 10.0
    input_data_2 = paddle.normal(shape=input_shape).astype("float64") * 10.0
    op_list = [
        "elu",
        "hardshrink",
        "hardsigmoid",
        "hardswish",
        "hardtanh",
        "log_sigmoid",
        "log_softmax",
        "selu",
        "sigmoid",
        "softsign",
    ]
    for op_name in op_list:
        verify_model(Activation(op_name), input_data=input_data)
        verify_model(Activation(op_name), input_data=input_data_2)

@tvm.testing.uses_gpu
def test_forward_isfinite():
    @paddle.jit.to_static
    def isfinite(inputs):
        return paddle.cast(paddle.isfinite(inputs), "int32")

    input_shape = [5, 5]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(isfinite, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_isinf():
    @paddle.jit.to_static
    def isinf(inputs):
        return paddle.cast(paddle.isinf(inputs), "int32")

    input_shape = [5, 5]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(isinf, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_isnan():
    @paddle.jit.to_static
    def isnan(inputs):
        return paddle.cast(paddle.isnan(inputs), "int32")

    input_shape = [5, 5]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(isnan, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_layer_norm():
    @paddle.jit.to_static
    def layer_norm(inputs, weight, bias):
        return nn.functional.layer_norm(inputs, inputs.shape[-1], weight=weight, bias=bias)

    class LayerNorm(nn.Layer):
        def __init__(self):
            super(LayerNorm, self).__init__()
            data_shape = [10]
            self.layer_norm = nn.LayerNorm(data_shape)

        @paddle.jit.to_static
        def forward(self, inputs):
            return self.layer_norm(inputs)

    input_shape = [1, 3, 10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    weight = paddle.rand([10], dtype="float32")
    bias = paddle.rand([10], dtype="float32")
    verify_model(layer_norm, input_data=[input_data, weight, bias])
    verify_model(LayerNorm(), input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_leaky_relu():
    @paddle.jit.to_static
    def leaky_relu(inputs):
        return nn.functional.leaky_relu(inputs)

    input_shape = [1, 3, 10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(leaky_relu, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_logical_op():
    class LogicalOp(nn.Layer):
        def __init__(self, op_name, out=False):
            super(LogicalOp, self).__init__()
            self.out = out
            for candidate in (paddle, paddle.nn.functional):
                self.func = getattr(candidate, op_name, None)
                if self.func:
                    break

        @paddle.jit.to_static
        def forward(self, x, y):
            if self.out:
                out = paddle.to_tensor([True, True, True])
                z = self.func(x, y, out=out)
            else:
                z = self.func(x, y)
            return paddle.cast(z, "int32")

    class LogicalOp_not(LogicalOp):
        @paddle.jit.to_static
        def forward(self, x):
            if self.out:
                out = paddle.to_tensor([True, True, True])
                z = self.func(x, out=out)
            else:
                z = self.func(x)
            return paddle.cast(z, "int32")

    op_list = [
        "logical_or",
        "logical_xor",
        "logical_and",
    ]
    x = paddle.to_tensor([True])
    y = paddle.to_tensor([True, False, True, False])
    for op_name in op_list:
        verify_model(LogicalOp(op_name, False), [x, y])
        verify_model(LogicalOp(op_name, True), [x, y])
    verify_model(LogicalOp_not("logical_not", False), [y])
    verify_model(LogicalOp_not("logical_not", True), [y])

@tvm.testing.uses_gpu
def test_forward_multiply():
    @paddle.jit.to_static
    def multiply1(inputs):
        return inputs * inputs

    @paddle.jit.to_static
    def multiply2(inputs):
        return inputs * 1.0 / 2.0

    @paddle.jit.to_static
    def multiply3(inputs, inputs2):
        ones = paddle.ones([10], dtype="float32")
        return inputs * ones / inputs2

    input_shape = [10]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(multiply1, input_data=input_data)
    verify_model(multiply2, input_data=input_data)
    input_data2 = paddle.rand(input_shape, dtype="float32")
    verify_model(multiply3, input_data=[input_data, input_data2])

@tvm.testing.uses_gpu
def test_forward_matmul():
    class MatMul1(nn.Layer):
        def forward(self, input1, input2):
            return paddle.matmul(input1, input2)

    # matrix x vector
    input_data1 = paddle.randn((3, 4), dtype="float32")
    input_data2 = paddle.randn((4,), dtype="float32")
    verify_model(MatMul1(), input_data=[input_data1, input_data2])

    # matrix x matrix
    input_data1 = paddle.randn((5, 4), dtype="float32")
    input_data2 = paddle.randn((4, 5), dtype="float32")
    verify_model(MatMul1(), input_data=[input_data1, input_data2])

    # batched matrix x batched matrix
    input_data1 = paddle.randn((10, 3, 4), dtype="float32")
    input_data2 = paddle.randn((10, 4, 5), dtype="float32")
    verify_model(MatMul1(), input_data=[input_data1, input_data2])

    # batched matrix x broadcasted matrix
    input_data1 = paddle.randn((10, 3, 4), dtype="float32")
    input_data2 = paddle.randn((4, 5), dtype="float32")
    verify_model(MatMul1(), input_data=[input_data1, input_data2])

@tvm.testing.uses_gpu
def test_forward_meshgrid():
    @paddle.jit.to_static
    def t(x, y, z):
        return paddle.meshgrid(x, y, z)

    x = paddle.randint(low=0, high=100, shape=[2])
    y = paddle.randint(low=0, high=100, shape=[3])
    z = paddle.randint(low=0, high=100, shape=[5])
    verify_model(t, [x, y, z])

def test_forward_mm():
    class Mm(nn.Layer):
        def forward(self, input1, input2):
            return paddle.mm(input1, input2)

    # matrix x vector
    input_data1 = paddle.randn((3, 4), dtype="float32")
    input_data2 = paddle.randn((4,), dtype="float32")
    verify_model(Mm(), input_data=[input_data1, input_data2])

    # matrix x matrix
    input_data1 = paddle.randn((5, 4), dtype="float32")
    input_data2 = paddle.randn((4, 5), dtype="float32")
    verify_model(Mm(), input_data=[input_data1, input_data2])

    # batched matrix x batched matrix
    input_data1 = paddle.randn((10, 3, 4), dtype="float32")
    input_data2 = paddle.randn((10, 4, 5), dtype="float32")
    verify_model(Mm(), input_data=[input_data1, input_data2])

    # batched matrix x broadcasted matrix
    input_data1 = paddle.randn((10, 3, 4), dtype="float32")
    input_data2 = paddle.randn((4, 5), dtype="float32")
    verify_model(Mm(), input_data=[input_data1, input_data2])

@tvm.testing.uses_gpu
def test_forward_mv():
    class Mv(nn.Layer):
        def forward(self, input1, input2):
            return paddle.mv(input1, input2)

    # matrix x vector
    input_data1 = paddle.randn((3, 4), dtype="float32")
    input_data2 = paddle.randn((4,), dtype="float32")
    verify_model(Mv(), input_data=[input_data1, input_data2])

@tvm.testing.uses_gpu
def test_forward_pool2d():
    @paddle.jit.to_static
    def pool2d1(inputs):
        return nn.functional.avg_pool2d(inputs, kernel_size=2, stride=2, padding=0)

    @paddle.jit.to_static
    def pool2d2(inputs):
        return nn.functional.adaptive_avg_pool2d(inputs, output_size=[3, 3])

    @paddle.jit.to_static
    def pool2d3(inputs):
        output = nn.functional.max_pool2d(inputs, kernel_size=2, stride=2, padding=0)
        return output

    @paddle.jit.to_static
    def pool2d4(inputs):
        output, max_indices = nn.functional.max_pool2d(
            inputs, kernel_size=2, stride=2, padding=0, return_mask=True
        )
        return output

    input_data = paddle.uniform(shape=[1, 2, 32, 32], dtype="float32", min=-1, max=1)
    verify_model(pool2d1, input_data, input_shape=[[-1, 2, 32, 32]])
    verify_model(pool2d2, input_data=input_data)
    input_data1 = paddle.uniform(shape=[1, 2, 1, 50], dtype="float32", min=-1, max=1)
    verify_model(pool2d3, input_data=input_data1)

@tvm.testing.uses_gpu
def test_forward_rank():
    class Rank(nn.Layer):
        @paddle.jit.to_static
        def forward(self, inputs):
            rank = paddle.rank(inputs)
            rank = paddle.unsqueeze(rank, axis=0)
            output = inputs + rank
            return output

    input_shape = [1, 2, 1, 3, 1]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(Rank(), input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_reshape():
    @paddle.jit.to_static
    def reshape1(inputs, new_shape):
        return paddle.reshape(inputs, new_shape)

    @paddle.jit.to_static
    def reshape2(inputs):
        return inputs.reshape([-1])

    @paddle.jit.to_static
    def reshape3(inputs):
        data_shape = inputs.shape
        return inputs.reshape([data_shape[1], data_shape[2], data_shape[0]])

    @paddle.jit.to_static
    def reshape4(inputs, x):
        new_shape = paddle.shape(x)
        return paddle.reshape(inputs, [new_shape[2], 2, -1])

    input_shape = [2, 1, 10, 1, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    input_data2 = paddle.randn([2, 1, 10, 10])
    new_shape = paddle.shape(input_data2)
    verify_model(reshape1, [input_data, new_shape], input_shape=[[2, 1, 10, 1, 10], [4]])
    verify_model(reshape2, input_data=input_data)
    verify_model(reshape3, input_data=paddle.randn((2, 3, 4)))
    verify_model(reshape4, input_data=[input_data, input_data2])

@tvm.testing.uses_gpu
def test_forward_scale():
    @paddle.jit.to_static
    def scale1(inputs):
        return paddle.scale(inputs, scale=2.0, bias=1.0)

    @paddle.jit.to_static
    def scale2(inputs):
        return paddle.scale(inputs, scale=3, bias=2.1, act="gelu")

    input_data = paddle.randn(shape=[2, 3], dtype="float32")
    verify_model(
        scale1,
        input_data=[
            input_data,
        ],
    )
    verify_model(scale2, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_scatter():
    @paddle.jit.to_static
    def scatter(x, index, updates):
        return paddle.scatter(x, index, updates, overwrite=True)

    @paddle.jit.to_static
    def scatter2(x, index, updates):
        return paddle.scatter(x, index, updates, overwrite=False)

    x = paddle.rand([10, 8, 5], dtype="float32")
    index = paddle.to_tensor(
        [
            2,
            1,
            0,
            6,
        ]
    )
    updates = paddle.rand([4, 8, 5], dtype="float32")
    verify_model(scatter, [x, index, updates], input_shape=[[-1, 8, 5], [4], [4, 8, 5]])
    verify_model(scatter2, [x, index, updates])

def test_forward_scatter_nd():
    @paddle.jit.to_static
    def scatter_nd(index, updates):
        shape = [3, 5, 9, 10]
        return paddle.scatter_nd(index, updates, shape)

    @paddle.jit.to_static
    def scatter_nd_add(x, index, updates):
        return paddle.scatter_nd_add(x, index, updates)

    index_data = np.array([[1, 1], [0, 1], [1, 3]]).astype(np.int64)
    index = paddle.to_tensor(index_data)
    updates = paddle.rand(shape=[3, 9, 10], dtype="float32")
    verify_model(scatter_nd, [index, updates])
    x = paddle.rand(shape=[3, 5, 4, 9, 10], dtype="float32")
    updates = paddle.rand(shape=[3, 2, 9, 10], dtype="float32")
    index = paddle.randint(0, 3, shape=[3, 2, 3])
    verify_model(scatter_nd_add, [x, index, updates])

@tvm.testing.uses_gpu
def test_forward_slice():
    @paddle.jit.to_static
    def slice1(inputs, end):
        return inputs[:, :, :, :end]

    @paddle.jit.to_static
    def slice2(inputs):
        return inputs[0, :, :-3, :]

    @paddle.jit.to_static
    def slice3(inputs):
        return inputs[0::2, 0::2] + inputs[1::2, 1::2]

    @paddle.jit.to_static
    def slice4(inputs):
        x0 = paddle.to_tensor([2]) - paddle.to_tensor([1])
        x1 = paddle.to_tensor([3]) + paddle.to_tensor([1])
        return inputs[:, x0:, 1:x1, :]

    @paddle.jit.to_static
    def slice5(inputs):
        x0 = paddle.to_tensor([3])
        return inputs[:, 1::1, 2::x0, 4:10]

    input_shape = [1, 3, 10, 10]
    input_data = paddle.rand(input_shape, dtype="float32")
    end = paddle.to_tensor(np.array([3]))
    verify_model(slice1, [input_data, end], input_shape=[[1, 3, 10, 10], [1]])
    verify_model(slice2, input_data=input_data)
    verify_model(slice3, input_data=paddle.randn((4, 4)))
    verify_model(slice4, input_data=input_data)
    verify_model(slice5, input_data=input_data)

@tvm.testing.uses_gpu
def test_forward_sort():
    @paddle.jit.to_static
    def sort(inputs):
        return paddle.sort(inputs)

    @paddle.jit.to_static
    def sort2(inputs):
        return paddle.sort(inputs, axis=0, descending=True)

    input_shape = [2, 3, 5]
    input_data = paddle.rand(input_shape, dtype="float32")
    verify_model(sort, input_data)
    input_data2 = np.random.randint(100, size=input_shape)
    verify_model(sort2, input_data2)

@tvm.testing.uses_gpu
def test_forward_subtract():
    class Subtract(nn.Layer):
        @paddle.jit.to_static
        def forward(self, x, y):
            return paddle.subtract(x, y)

    input_data1 = paddle.to_tensor([2, np.nan, 5], dtype="float32")
    input_data2 = paddle.to_tensor([1, 4, np.nan], dtype="float32")
    verify_model(Subtract(), input_data=[input_data1, input_data2])

    input_data1 = paddle.randint(0, 10, (3, 4), dtype="int32")
    input_data2 = paddle.randint(0, 10, (4,), dtype="int32")
    verify_model(Subtract(), input_data=[input_data1, input_data2])

    input_data1 = paddle.randint(0, 10, (10, 3, 4), dtype="int64")
    input_data2 = paddle.randint(0, 10, (3, 4), dtype="int64")
    verify_model(Subtract(), input_data=[input_data1, input_data2])

if __name__ == "__main__":
    test_forward_add_subtract()
    test_forward_addmm()
    test_forward_argmax()
    test_forward_argmin()
    test_forward_argsort()
    test_forward_assign()
    test_forward_batch_norm()
    test_forward_cast()
    test_forward_clip()
    test_forward_concat_unsqueeze()
    test_forward_conv()
    test_forward_crop()
    test_forward_cumsum()
    test_forward_dist()
    test_forward_dot()
    test_forward_dropout()
    test_forward_elemwise()
    test_forward_expand()
    test_forward_expand_as()
    test_forward_ones()
    test_forward_gelu()
    test_forward_math()
    test_forward_activation()
    test_forward_isinf()
    test_forward_layer_norm()
    test_forward_leaky_relu()
    test_forward_logical_op()
    test_forward_lstm()
    test_forward_gru()
    test_forward_matmul()
    test_forward_meshgrid()
    test_forward_mm()
    test_forward_mv()
    test_forward_multiply()
    test_forward_pool2d()
    test_forward_rank()
    test_forward_reshape()
    test_forward_scale()
    test_forward_scatter()
    test_forward_scatter_nd()
    test_forward_slice()
    test_forward_sort()
    test_forward_subtract()
    test_forward_math()


# we square the r instead of using absolute values to punish outliers
# Increasing it to cubed or ^4 would punish more

# Creating a regression line - aka line of best fit
# Y = mx + b
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style

style.use('fivethirtyeight')

# defined as numpy arrays
xs = np.array([1,2,3,4,5,6], dtype=np.float64)
ys = np.array([5,4,6,5,6,7], dtype=np.float64)

def best_fit_slope_and_intercept(xs,ys):
    m = (((mean(xs) * mean(ys)) - mean(xs*ys)) / ( (mean(xs) * mean(xs)) - mean(xs**2)    ) )
    b = mean(ys) - m*mean(xs)
    return m,b;

def squared_error(ys_orig, ys_line):
    return sum((ys_line-ys_orig)**2)

def coefficient_of_determination(ys_orig,ys_line):
    y_mean_line = [mean(ys_orig) for y in ys_orig]
    squared_error_regr = squared_error(ys_orig, ys_line)
    squared_error_y_mean = squared_error(ys_orig, y_mean_line)
    return 1 - (squared_error_regr / squared_error_y_mean)

m,b = best_fit_slope_and_intercept(xs,ys)

#Creating line via for loop
regression_line = [(m*x)+b for x in xs]

#print(m,b)

predict_x = 8
predict_y = (m*predict_x + b)

r_squared = coefficient_of_determination(ys, regression_line)
print(r_squared)

#Create plots
plt.scatter(xs,ys)
plt.scatter(predict_x,predict_y)
plt.plot(xs, regression_line)
plt.show()


import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
from math import sqrt
from sklearn.datasets import load_breast_cancer
from UCIDataSet import UCIDataSet
import random

if __name__=='__main__':

   datatype=5 #数据集类型
   isHill_Valley=False
   pathdir='D:\VSCode\SVMtest\mydataset'
   if datatype==1:
      cancer=load_breast_cancer() #用于二分类任务的乳腺癌数据集
      X = cancer.data
      y = cancer.target
   elif datatype==2:
      # 构造列标签名字
      column_names = ['Sample code number','Clump Thickness', 'Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion', 'Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
      # 读取数据  (如果不指定标签名，会默认把第一行数据当成标签名)
      data = pd.read_csv("D:\VSCode\SVMtest\mydataset\\breast-cancer-wisconsin.data", names=column_names)

      # 缺失值进行处理  (原始数据中的?表示缺失值)
      data = data.replace(to_replace='?', value=np.nan)
      data = data.dropna()  # 删除有缺少值的行
      X = np.array(data[column_names[1:10]]) # 第一列是id不需要，最后一列是目标值
      y = np.array(data[column_names[10]])
   elif datatype==3:
        pathname=['SPECTF.train','SPECTF.test']

        path = pathdir + '\\' + pathname[0]
        raw_dataset = np.loadtxt(path, delimiter = ',')
        train_data = raw_dataset[:,np.arange(1,raw_dataset.shape[1])] #特征
        train_label = raw_dataset[:,0] #标签

        path = pathdir + '\\' + pathname[1]
        raw_dataset = np.loadtxt(path, delimiter = ',')
        test_data = raw_dataset[:,np.arange(1,raw_dataset.shape[1])] #特征
        test_label = raw_dataset[:,0] #标签
   elif datatype==4:
        isnoise=False
        isHill_Valley=True
        pathname=[]
        if isnoise and isHill_Valley:
            pathname=['Hill_Valley_with_noise_Training.data','Hill_Valley_with_noise_Testing.data']
        elif isHill_Valley:
            pathname=['Hill_Valley_without_noise_Training.data','Hill_Valley_without_noise_Testing.data']
        else:
            pathname=[]
        if isHill_Valley:
            ucidstrain = UCIDataSet(pathdir=pathdir,pathname=pathname[0])
            xtrain,ytrain = ucidstrain.getdata_and_target()
            ucidstest = UCIDataSet(pathdir=pathdir,pathname=pathname[1])
            xtest,ytest = ucidstest.getdata_and_target()
        else:
            ucids = UCIDataSet(pathdir=pathdir,pathname=pathname)
            X,y = ucidstest.getdata_and_target()
   elif datatype==5:
        column_names=['fixed acidity','volatile acidity','citric acid','residual sugar','chlorides','free sulfur dioxide','total sulfur dioxide','density','pH','sulphates','alcohol','quality']
        data1 = pd.read_csv("D:\VSCode\SVMtest\mydataset\winequality-red.csv",names=column_names,delimiter = ';')
        data2 = pd.read_csv("D:\VSCode\SVMtest\mydataset\winequality-white.csv",names=column_names,delimiter = ';')
        X1 = np.array(data1[column_names[0:11]]) # 最后一列是目标值
        y1 = np.array(data1[column_names[11]])
        X2 = np.array(data2[column_names[0:11]]) # 最后一列是目标值
        y2 = np.array(data2[column_names[11]])
        sampleind1=random.sample(list(np.arange(0,X1.shape[0])), 250)
        sampleind2=random.sample(list(np.arange(0,X2.shape[0])), 750)
        X1 = X1[sampleind1]
        y1 = y1[sampleind1]
        X2 = X2[sampleind2]
        y2 = y2[sampleind2]
        print(X1.shape)
        print(X2.shape)
        X=np.concatenate((X1,X2))
        y=np.append(y1,y2)
        for i in range(y.shape[0]):
            if y[i]>5:
                y[i]=1
            else:
                y[i]=0
   elif datatype==6:
      column_names = ['M_ip','S_dip','E_kip','S_ip','M_DM-SNRc','S_dDM-SNRc','E_kDM-SNRc','S_DM-SNRc','Class']
      data = pd.read_csv("D:\VSCode\SVMtest\mydataset\HTRU_2.csv",names=column_names)
      X = np.array(data[column_names[0:8]]) # 第一列是id不需要，最后一列是目标值
      y = np.array(data[column_names[8]])
      dataind=np.arange(0,X.shape[0])
      dataind=list(dataind)
      sampleind=random.sample(dataind, 2000)
      X = X[sampleind]
      y = y[sampleind]

   if isHill_Valley:
      train_data1,test_data1,train_label1,test_label1=train_test_split(xtrain,ytrain,test_size=0.2,random_state=0,stratify=ytrain)
      train_data2,test_data2,train_label2,test_label2=train_test_split(xtest,ytest,test_size=0.2,random_state=0,stratify=ytest)
      train_data,train_label=train_data1,train_label1
      test_data,test_label=test_data2,test_label2
   elif datatype!=3:
      train_data,test_data,train_label,test_label=train_test_split(X,y,test_size=0.25,random_state=0,stratify=y)

   # 进行标准化处理   因为目标结果经过sigmoid函数转换成了[0,1]之间的概率，所以目标值不需要进行标准化。
   std = StandardScaler()
   train_data = std.fit_transform(train_data)
   test_data = std.transform(test_data)

   Lnorm = ['l1','l2']
   c=[0.1,0.2,0.5,1,2,5,10]
   # 逻辑回归预测
   for norm in Lnorm:
      print('*'*16)
      print('正则化范数：',norm)
      print('*'*16)
      print('-'*16)
      for i in c:
         print('正则化项超参数：',i)
         # 默认使用L2正则化避免过拟合，C=1.0表示正则力度(超参数，可以调参调优)
         lgr = LogisticRegression(C=i,penalty=norm)
         lgr.fit(train_data, train_label)

         # 进行预测
         y_predict = lgr.predict(test_data)
         predict = np.array(y_predict==test_label)
         acc = sum(predict==1)/len(predict) #准确率
         print("准确率:",acc)

         SD_sample=np.std(predict) # 标准差
         SE_sample=SD_sample/sqrt(len(predict)) # 标准误差
         print("标准误差:",SE_sample)
      print('-'*16)
      # print("准确率：", lgr.score(x_test, y_test))  # 0.964912280702
      # print("召回率：", classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"]))


# 20210710################################################
# 水くれミント用RaspberryPiのクライアント
# AWS IoT Core へ MQTT で接続し指定時間置きにメッセージ送信
###########################################################
import argparse
import json
import logging
import os
import signal
import sys
import time
import traceback
from datetime import datetime

from awscrt import io, mqtt
from awsiot import iotshadow, mqtt_connection_builder
from grovepi import analogRead

# 定数定義 ############################################
BASE_TOPIC = "data/"

# Shadowプロパティ初期値
DEFAULT_WAIT_TIME =1800
DEFAULT_STATE_TIME =''
DEFAULT_MOISTER =0

# Shadow用キー名
SHADOW_MOISTUER_KEY="moistuer"
SHADOW_SUTATE_TIME_KEY="state_time"
SHADOW_WAIT_TIME_KEY = "wait_time"

KEEP_ALIVE = 300    # タイムアウトまで
SENSER =0           # GrobePiアナログセンサー接続位置

# 変数 ############################################
# 接続用object
mqtt_connection = None
shadow_client = None
device_name = None

# Shadowプロパティ情報保持用
wait_time = DEFAULT_WAIT_TIME
state_time = DEFAULT_STATE_TIME
moistuer = DEFAULT_MOISTER

# ログ用
logger = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logging.basicConfig()

# 引数のチェックと取得、設定
def arg_check():
    """
        起動時の引数とヘルプを定義
        python3 device_main.py --device_name nonchan-20210704 --endpoint a6jwuuv50jxiy-ats.iot.ap-northeast-1.amazonaws.com
    """
    logging.debug("start: arg_check")
    parser = argparse.ArgumentParser()
    # 必須　AWS IOTのモノの名前
    parser.add_argument("--device_name", required=True,
                        help="[Must], AWS IoT Core Thing Name")
    # 必須　AWS IOTのエンドポイント
    parser.add_argument("--endpoint", required=True,
                        help="[Must], AWS IoT endpoint URI")
    # 任意　ルート証明書（ルート認証局の公開鍵）
    parser.add_argument("--root_ca", required=False,
                        help="AWS IoT Core root ca file name with path")
    # 任意　証明書
    parser.add_argument("--cert", required=False,
                        help="device cert file name with path")
    # 任意　証明書
    parser.add_argument("--private", required=False,
                        help="private cert key file name with path")
    # 任意　ログレベル
    parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel],
                        default=io.LogLevel.NoLogs.name, help='Logging level')

    # パラメータ取得
    args = parser.parse_args()

    log_level = getattr(io.LogLevel, args.verbosity, "error")
    io.init_logging(log_level, 'stderr')
    loglevel_map = [
        logging.INFO, logging.INFO, logging.INFO,
        logging.INFO, logging.INFO, logging.DEBUG,
        logging.DEBUG]
    logger.setLevel(loglevel_map[log_level])
    logging.basicConfig()

    # デフォルト証明書探す
    cert_list = find_certs_file()
    # 引数であればそっちの証明書優先
    if args.root_ca is not None:
        cert_list[0] = args.root_ca
    if args.private is not None:
        cert_list[1] = args.private
    if args.cert is not None:
        cert_list[2] = args.cert

    logging.debug(cert_list)
    # 証明書ファイルの存在チェック
    file_exist_check(cert_list)

    # 接続情報返却
    init_dict = {
        "device_name": args.device_name,
        "endpoint": args.endpoint,
        "certs": cert_list
    }
    return init_dict

# ファイルの存在チェック
def file_exist_check(cert_list):
    for file in cert_list:
        if not os.path.exists(file):
            # if file not found, raise
            logger.error("cert file not found:%s", file)
            raise RuntimeError("file_not_exists")

# このファイルと同じ階層のcertsディレクトリの証明書探す。
def find_certs_file():
    """
     ./certs

    Returns
    ----------
    file_list: Array
        0: Root CA Cert, 1: private key, 2: certificate
    """

    certs_dir = "./certs"
    file_list = ["AmazonRootCA1.pem", "private.pem", "certificate.crt"]
    for _, _, names in os.walk(certs_dir):
        for file in names:
            if "AmazonRootCA1.pem" in file:
                file_list[0] = certs_dir + "/" + file
            elif "private" in file:
                file_list[1] = certs_dir + "/" + file
            elif "certificate" in file:
                file_list[2] = certs_dir + "/" + file

    return file_list

# shadowの変更指示があった時
def on_shadow_delta_updated(delta):
    """
    callback for shadow delta update
    https://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/device-shadow-mqtt.html#update-delta-pub-sub-topic

    Parameters
    ----------
    delta: iotshadow.ShadowDeltaUpdatedEvent
    """
    global wait_time, state_time, moistuer
    try:
        # 待ち時間の変更
        if delta.state and (SHADOW_WAIT_TIME_KEY in delta.state):
            wait_val = DEFAULT_WAIT_TIME if delta.state[SHADOW_WAIT_TIME_KEY] is None else delta.state[SHADOW_WAIT_TIME_KEY]
            wait_time = wait_val
        # センサー取得指示
        if delta.state and (SHADOW_SUTATE_TIME_KEY in delta.state):
            state_val = DEFAULT_STATE_TIME if delta.state[SHADOW_SUTATE_TIME_KEY] is None else delta.state[SHADOW_SUTATE_TIME_KEY]
            moistuer = analogRead(SENSER)
            state_time = state_val

        change_shadow_value(wait_time,state_time,moistuer)

    except Exception as e:
        exit_sample(e)

def on_get_shadow_accepted(response):
    """
    callback for get shadow accepted
    https://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/device-shadow-mqtt.html#get-accepted-pub-sub-topic

    Parameters
    ----------
    response: iotshadow.GetShadowResponse
    """
    global wait_time
    global state_time
    global moistuer
    try:
        logger.info("Finished getting initial shadow state.")

        if response.state:
            wait_val = response.state.desired.get(SHADOW_WAIT_TIME_KEY)
            state_val = response.state.desired.get(SHADOW_SUTATE_TIME_KEY)

            if response.state.delta:
                if wait_val or state_val:
                    wait_time = wait_val if wait_val else wait_time
                    state_time = state_val if state_val else state_time
                    change_shadow_value(wait_time,state_time,moistuer)
                    return
            elif response.state.desired:
                if state_val or wait_val:
                    wait_time = wait_val if wait_val else wait_time
                    state_time = state_val if state_val else state_time
                    if not response.state.reported:
                        change_shadow_value(wait_time,state_time,moistuer)

            elif response.state.reported:
                if wait_val:
                    wait_time = wait_val
                if state_val:
                    state_time = state_val

        unsubscribe_get_shadow_events()
    except Exception as e:
        exit_sample(e)

def on_get_shadow_rejected(error):
    """
    callback for get shadow rejected
    https://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/device-shadow-mqtt.html#get-rejected-pub-sub-topic

    Parameters
    ----------
    error: iotshadow.ErrorResponse
    """
    if error.code == 404:
        logger.info("Thing has no shadow document. Creating with defaults...")
        unsubscribe_get_shadow_events()
        change_shadow_value(DEFAULT_WAIT_TIME,DEFAULT_STATE_TIME,DEFAULT_MOISTER)
    else:
        exit_sample("Get request was rejected. code:{} message:'{}'".format(
            error.code, error.message))

def on_publish_update_shadow(future):
    """
    callback for publish shadow update
    https://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/device-shadow-mqtt.html#update-pub-sub-topic

    Parameters
    ----------
    future: Future
    """
    try:
        future.result()
        logger.info("Update request published.")
    except Exception as e:
        logger.error("Failed to publish update request.")
        exit_sample(e)

# Update shadow reported state
def change_shadow_value(wait,state,moistuer):

    logger.info("Updating reported shadow to...")
    new_state = iotshadow.ShadowState(
        reported={SHADOW_WAIT_TIME_KEY: wait,SHADOW_SUTATE_TIME_KEY: state,SHADOW_MOISTUER_KEY: moistuer}
    )
    request = iotshadow.UpdateShadowRequest(
        thing_name=device_name,
        state=new_state
    )
    future = shadow_client.publish_update_shadow(request, mqtt.QoS.AT_LEAST_ONCE)
    future.add_done_callback(on_publish_update_shadow)

# Un subscribe Shadow get events
def unsubscribe_get_shadow_events():

    logger.info("un subscribe from get shadow events")
    shadow_client.unsubscribe("$aws/things/{}/shadow/get/accepted".format(device_name))
    shadow_client.unsubscribe("$aws/things/{}/shadow/get/rejected".format(device_name))

# メイン処理
def device_main():
    """
    main loop for dummy device
    """
    global device_name, mqtt_connection, shadow_client,moistuer,wait_time

    # 引数整理、接続情報取得
    init_info = arg_check()
    # 接続情報を設定
    device_name = init_info['device_name']
    iot_endpoint = init_info['endpoint']
    rootca_file = init_info['certs'][0]
    private_key_file = init_info['certs'][1]
    certificate_file = init_info['certs'][2]

    # log出力
    logger.info("device_name: %s", device_name)
    logger.info("endpoint: %s", iot_endpoint)
    logger.info("rootca cert: %s", rootca_file)
    logger.info("private key: %s", private_key_file)
    logger.info("certificate: %s", certificate_file)

    # ソケット通信の為のおまじない
    event_loop_group = io.EventLoopGroup(1)
    host_resolver = io.DefaultHostResolver(event_loop_group)
    # ソケット通信アクティビティを処理する共通ランタイムオブジェクト
    client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

    # MQTT プロトコルを使用して AWS IoT Core との接続を確立
    mqtt_connection = mqtt_connection_builder.mtls_from_path(
        endpoint=iot_endpoint,
        cert_filepath=certificate_file,
        pri_key_filepath=private_key_file,
        client_bootstrap=client_bootstrap,
        ca_filepath=rootca_file,
        client_id=device_name,
        clean_session=False,
        keep_alive_secs=KEEP_ALIVE)

    # コネクション確率
    connected_future = mqtt_connection.connect()
    # shadowクライアント作成
    shadow_client = iotshadow.IotShadowClient(mqtt_connection)
    connected_future.result()

    print("Check latest Shadow status")
    # 受信
    get_accepted_subscribed_future, _ = shadow_client.subscribe_to_get_shadow_accepted(
        request=iotshadow.GetShadowSubscriptionRequest(device_name),
        qos=mqtt.QoS.AT_LEAST_ONCE,
        callback=on_get_shadow_accepted)

    # 受信が成功するのを待つ
    get_accepted_subscribed_future.result()

    # 送信
    publish_get_future = shadow_client.publish_get_shadow(
        request=iotshadow.GetShadowRequest(device_name),
        qos=mqtt.QoS.AT_LEAST_ONCE)

    # 送信成功することを確認します
    publish_get_future.result()

    # Shadowの状態と指示の差分チェック
    logger.info("Subscribing to Shadow Delta events...")
    delta_subscribed_future, _ = shadow_client.subscribe_to_shadow_delta_updated_events(
        request=iotshadow.ShadowDeltaUpdatedSubscriptionRequest(device_name),
        qos=mqtt.QoS.AT_LEAST_ONCE,
        callback=on_shadow_delta_updated)

    delta_subscribed_future.result()

    # Start sending dummy data
    topic = BASE_TOPIC + device_name
    logging.info("topic: %s", topic)

    while True:
        now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
        # センサーで値取得
        moistuer = analogRead(SENSER)
        payload = {"DEVICE_NAME": device_name, "TIMESTAMP": now, "MOISTUER": int(moistuer)}
        logger.debug("  payload: %s", payload)
        # MQTTでIotにパブリッシュ
        mqtt_connection.publish(
            topic=topic,
            payload=json.dumps(payload),
            qos=mqtt.QoS.AT_LEAST_ONCE)
        # 指定時間毎に送信するため
        time.sleep(int(wait_time))

# Contlorl+Cで停止するための設定
def exit_sample(msg_or_exception):
    if isinstance(msg_or_exception, Exception):
        logger.error("Exiting sample due to exception.")
        traceback.print_exception(msg_or_exception.__class__, msg_or_exception, sys.exc_info()[2])
    else:
        logger.info("Exiting: %s", msg_or_exception)

    if not mqtt_connection:
        logger.info("Disconnecting...")
        mqtt_connection.disconnect()
    sys.exit(0)

# Contlorl+Cで停止するための設定
def exit_handler(_signal, frame):

    exit_sample(" Key abort")

if __name__ == "__main__":
    # Contlorl+Cで停止するための設定
    signal.signal(signal.SIGINT, exit_handler)
    # メイン処理
    device_main()


# -*- coding:utf-8 -*-
from flask import Flask,render_template,send_file, redirect, url_for, abort, flash, request,\
    current_app, session
from flask_login import login_required, current_user
from .forms import CommentForm,EditProfileForm,PostForm
from user import user
from modles import Post,Comment,User,db,User_info,job_info,Job_hire
from datetime import datetime

@user.route('/', methods = ['GET', 'POST'])
@user.route('/index',methods = ['GET', 'POST'])
@user.route('/index/<int:page>', methods = ['GET', 'POST'])
def index(page = 1):
    jobinfo = job_info.query.paginate(page, 10,False)
    if request.method == 'POST':
        keyword = request.form['keyword']
        jobinfo= jobinfo.query.whoosh_search(keyword).paginate(page, 10,False)
    return render_template('index.html',jobinfo=jobinfo )

@user.route('/hire_job/<string:job_id>',methods = ('GET', 'POST'))
@login_required
def hire_job(job_id):
    info =job_info.query.filter_by(id=job_id).first()
    if request.method == 'POST':
        jobId = request.form['job_id']
        usr_id = session['user_id']
        companyName = request.form['companyName']
        positionName =request.form['positionName']
        createTime = request.form['createTime']
        jobs_insert = Job_hire(usr_id=usr_id,jobs_id=jobId,companyName=companyName,\
                               positionName=positionName,createTime=createTime)
        test_info = Job_hire.query.filter_by(usr_id=usr_id,jobs_id=jobId).first()
        if test_info is not None:
            return redirect(url_for('user.hire_job',info=info,job_id=jobId))
        db.session.add(jobs_insert)
        db.session.commit()
        return redirect(url_for('user.index'))
    return render_template('hire_job.html',info=info,job_id=job_id )

@user.route('/contact')
@login_required
def contact():
    return render_template('contact.html')

@user.route('/work')
def work():
    return render_template('work.html')

@user.route('/post')
@user.route('/post/<int:page>')
@login_required
def post(page = 1):
    if current_user :
        posts = Post.query.order_by(
            Post.publish_date.desc()
        ).paginate(page, 10)
        # print(posts.items)
        recent = db.session.query(Post).order_by(
            Post.publish_date.desc()
        ).limit(5).all()
        return render_template('about.html',posts = posts,recent=recent)
    else:
        return redirect(url_for('au'))

@user.route('/post_doc/<string:post_id>', methods=('GET', 'POST'))
@login_required
def post_doc(post_id):
    form = CommentForm()
    user_id = session['user_id']
    print(user_id)

    if request.method == 'POST':
        post = Post.query.get_or_404(post_id) #查询当前页的内容
        comment_text = form.body.data
        date_time = datetime.now()
        user_id = session['user_id']
        new_comment = Comment(text=comment_text,date_time=date_time,post_id=post_id,user_id=user_id)
        db.session.add(new_comment)
        db.session.commit()

    post = Post.query.get_or_404(post_id) #查询当前页的内容
    comments = post.comments.order_by(Comment.date.desc()).all()#获取当前页的评论
    recent = db.session.query(Post).order_by(
        Post.publish_date.desc()
    ).limit(5).all()

    return render_template('post_doc.html', post=post,comments=comments,
                           recent=recent,postid=post.id,form=form)

@user.route('/user_profile/<string:username>',)
@login_required
def user_profile(username):
    user_id = session['user_id']
    print(user_id)
    # user_data = User.query.filter_by(username=username).first_or_404()
    # posts = user_data.posts.order_by(Post.publish_date.desc()).all()
    info =User_info.query.filter_by(info_id=user_id).first()
    recent = db.session.query(Post).order_by(
            Post.publish_date.desc()
        ).limit(5).all()

    return render_template('user_profile.html',
                           user=user, recent=recent,info=info)

@user.route('/edit_post', methods=('GET', 'POST'))
@login_required
def edit_post():
    username = current_user.username
    user_data = User.query.filter_by(username=username).first_or_404()
    posts = user_data.posts.order_by(Post.publish_date.desc()).paginate(1, 10)

    if request.method == 'POST':
        post_id = request.form['post_id']
        Post.query.filter_by(id=post_id).delete()
        db.session.commit()
        return redirect(url_for('user.edit_post',posts=posts))

    return render_template('user_profile_post.html',posts=posts)

@user.route('/edit_profile', methods=['GET', 'POST'])
@login_required
def edit_profile():
    form = EditProfileForm()
    user_id = session['user_id']
    info =User_info.query.filter_by(info_id=user_id).first()
    if request.method == 'POST':
        name = form.name.data
        email= form.email.data
        phone= form.phone.data
        ethn = form.ethn.data
        location = form.location.data
        education = form.education.data
        about_me= form.about_me.data
        school = form.school.data
        school_text = form.school_text.data
        company = form.company.data
        company_text = form.company_text.data

        info_id = session['user_id']
        info = User_info.query.filter_by(info_id=user_id).first()
        print(info)
        if info is None:
            usr_info = User_info(name=name,email=email,phone=phone,
                                 ethn=ethn,location=location,education=education,
                                 about_me=about_me,school=school,school_text=school_text,
                                 company=company,company_text=company_text,info_id=info_id)
            print(usr_info)
            db.session.add(usr_info)
            db.session.commit()
            return redirect(url_for('user.user_profile',username=current_user.username))
        else:
            user_id = session['user_id']
            info =User_info.query.filter_by(info_id=user_id).first_or_404()
            info_new = {}
            info_new['name'] = name
            info_new['email'] = email
            info_new['phone'] = form.phone.data
            info_new['ethn'] = ethn
            info_new['location'] = location
            info_new['education'] = education
            info_new['about_me'] = about_me
            info_new['school'] =school
            info_new['school_text'] = school_text
            info_new['company'] = company
            info_new['company_text'] = company_text
            form.school_text.data = info.school_text
            db.session.query(User_info).update(info_new)
            db.session.commit()
            print(info_new)
            print(current_user.username)
            return redirect(url_for('user.user_profile',username=current_user.username))

    return render_template('user_profile_info.html',forms=form,info=info)

@user.route('/new', methods=('GET', 'POST'))
def new_post():
    forms = PostForm()
    post_id = session['user_id']
    post_data =Post.query.filter_by(id=post_id).first()

    if request.method == 'POST':
        title = forms.title.data
        desc = forms.desc.data
        text = forms.text.data
        pub_time = datetime.now()

        post_id = session['user_id']
        post_data =Post.query.filter_by(id=post_id).first()
        print(post_data)
        if post_data is None:
            posts = Post(title=title,desc=desc,text=text,publish_date=pub_time,user_id=post_id)
            db.session.add(posts)
            db.session.commit()
            return redirect(url_for('user.edit_post'))
        else:
            new_post = {}
            new_post['title'] = title
            new_post['desc'] = desc
            new_post['publish_date'] = pub_time
            new_post['text'] = text
            new_post['user_id'] = post_id
            db.session.query(Post).update(new_post)
            db.session.commit()
            return redirect(url_for('user.edit_post'))
    return render_template('new_post.html',foms=forms,posts=post_data)

@user.route('/edit/<post_id>', methods=['GET', 'POST'])
@login_required
# @admin_permission.require(http_exception=403)
def edit(post_id):
    forms = PostForm()
    post = Post.query.get_or_404(post_id) #查询当前页的内容
    if request.method == 'post':
        title = forms.title.data
        desc = forms.desc.data
        text = forms.text.data
        new_post = {}
        new_post['title'] = title
        new_post['desc'] = desc
        new_post['text'] = text
        db.session.query(Post).update(new_post)
        db.session.commit()
        return redirect(url_for('user.edit_post'))
    return render_template('edit_post.html',posts=post,forms=forms,post_id=post_id)

@user.route('/user_job_hire',methods = ('GET', 'POST'))
@login_required
def user_job_hire():
    username = current_user.username
    user_data = User.query.filter_by(username=username).first_or_404()
    job_data = user_data.job_hire.paginate(1, 30)
    if request.method == 'POST':
        jobId = request.form['job_id']
        print(jobId)
        Job_hire.query.filter_by(id=jobId).delete()
        db.session.commit()
        return redirect(url_for('user.user_job_hire',data=job_data))
    return render_template('user_job_hire.html',data=job_data)

@user.route('/show_user_profile/<string:id>',methods = ('GET', 'POST'))
@login_required
def show_user_profile(id):
    user_data = User.query.filter_by(id=id).first_or_404()
    posts = user_data.posts.order_by(Post.publish_date.desc()).paginate(1, 20)

    info =User_info.query.filter_by(info_id=id).first()
    job_data = user_data.job_hire.paginate(1, 30)
    return render_template('show_user_profile.html',post_data = posts,userInfo=info,job_data=job_data,user_data=user_data)


#posterior probability for phase phi if photon arrival times from a periodic source are given.

import numpy as np
import matplotlib.pyplot as plt
import math
from pynverse import inversefunc

def lc(t): #lightcurve
    return (5./3.)*( 0.1 + np.sin(math.pi*(t+phi0))*np.sin(math.pi*(t+phi0)) ) #normalized
    '''s = (t+phi0)%1.0; x=0.0
    if (s < 0.6  and s >= 0.4):
        x = 5.0
    return x   '''
def cpdf(t):
    return (5./3.)*( 3.*t/5. - (np.sin(2.*math.pi*(t+phi0))-np.sin(2.*math.pi*phi0))/(4.*math.pi) ) #normalized CPDF to generate arrival times
    '''
    sz = np.size(t); x=np.zeros(sz); s = (t+phi0)%1.0
    print('here')
    if (s >= 0.4 and s < 0.6):
        x = (s-0.4)*5.
    if s >= 0.6:
        x = 1.0
    return x'''

# generate photon arrival times, assuming that the probability is proportional to lum, assume period is 1 and phi0=0.25
global phi0
phi0=0.0
num_ph=400
inv_cpdf = inversefunc(cpdf)
yunif = np.random.uniform(0.0,1.0,num_ph)
Tarr = inv_cpdf(yunif)

num=300
phi = np.linspace(0,1,num)
'''
lnP = np.zeros(num)
for i in range(num_ph):
    lnP = lnP + np.log(lc(Tarr[i] + phi))
'''
T_plus_phi = np.outer(Tarr,np.ones(num)) + np.outer(np.ones(num_ph),phi)
P = lc(T_plus_phi); #P_phi = np.prod(P,axis=0)
lnP = np.sum( np.log(P), axis=0 )
plt.figure()
plt.plot(phi, lnP)#, 'o',phi, np.log(P_phi))
plt.show()
plt.grid()


# -*- coding: utf-8 -*-
"""
Created on Mon May 31 15:14:44 2021

@author: Wasif Alam
"""

import pandas as pd
import numpy as np
import json
from docx import Document
def check_parent(file,arr,row,col):
    """this function will take children of json file, arr, row index and col index
    and return boolean value i.e it's checks whether the parent of a particular cell
    exists in that particular column"""
    for chk in range(row):
        #print(chk,file['header'][0],arr[chk][col][0])
        if file['header'][0]==arr[chk][col][0]:
            return True
        if file['header'][0]=="None" and arr[chk][col][0]==0:
            return True
    return False

def make_python_table(data):
    """this function makes a blueprint of table, i.e it's stores the table in
    the list"""
    rc=get_rows_col(data)
    #arr=[[[0]*2]*rc[1]]*rc[0]
    """creating list of dimension row*col*2
    at [row][col][0] contains id
    at [row][col][1] contains the text"""
    arr = [0] * rc[0]
    for i in range(rc[0]):
        arr[i] = [0] * rc[1]
        for z in range(rc[1]):
            arr[i][z]=[0]*2
    #filling first row of list
    d2=data['hasChildren'][0]
    j=0
    for child in d2['hasChildren']:
        cs=int(child['column_Span'])+j
        while j<cs:
            arr[0][j][0]=child['id']
            arr[0][j][1]=child['text']
            j+=1
    #filling all rows in the list
    d=data['hasChildren']
    row=1
    while row<rc[0]:
        col=0
        jcol=0
        while col<rc[1]:
            if check_parent(d[row]['hasChildren'][jcol],arr,row,col):
                col_span=int(d[row]['hasChildren'][jcol]['column_Span'])+col
                while col<col_span:
                    arr[row][col][0]=d[row]['hasChildren'][jcol]['id']
                    arr[row][col][1]=d[row]['hasChildren'][jcol]['text']
                    col+=1
                jcol+=1
                if jcol== len(d[row]['hasChildren']):
                    col=rc[1]
            else:
                col+=1

        row+=1
    """putting empty string where id of the cell is None"""
    for r1 in arr:
        for c1 in r1:
            if c1[0]==0:
                c1[1]=""
    return arr

def get_rows_col(data):
    """this function return dimension of the table"""
    r=len(data['hasChildren'])
    print(r)
    d1=data['hasChildren'][0]
    cnt=0
    for j in d1['hasChildren']:
        cnt=cnt+int(j['column_Span'])
    print(cnt)
    return [r,cnt]

def creating_table(data,document):
    rc=get_rows_col(data)
    arr=make_python_table(data)
    """creating table"""
    table = document.add_table(rows=1, cols=rc[1])
    table_row=table.rows[0].cells
    """copying text from arr to table"""
    for j_table in range(rc[1]):
        table_row[j_table].text=str(arr[0][j_table][1])
    i_table=1
    while i_table<rc[0]:
        table_row = table.add_row().cells
        for j_table in range(rc[1]):
            table_row[j_table].text=str(arr[i_table][j_table][1])
        i_table+=1
    """Now Merging the cell which has same id"""
    for i in range(rc[0]):
        j=1
        A=table.cell(i,j-1)
        while j<rc[1]:
            if arr[i][j-1][0]==arr[i][j][0]:
                a=table.cell(i,j)
                A=A.merge(a)
                A.text=str(arr[i][j][1])
            else:
                A=table.cell(i,j)
            j+=1
    table.style= 'TableGrid'

def creating_doc(a):
    """creating doc"""
    document=Document()
    document.add_heading('The TABLE',0)
    for filename in a:
        f= open(filename,)
        # returns JSON object as a dictionary
        data= json.load(f)
        creating_table(data, document)
        #closing file
        f.close()
        document.add_paragraph(' ')
    """output file name"""
    document.save('merge11.docx')

"""input json file"""
a=['New.json','tough.json','input3.json']
#f = open('New.json',)
# returns JSON object as a dictionary
#data = json.load(f)

# Iterating through the json
# list
#for i in data['people']:
    #print(i)

# Closing file
#f.close()
#rc=get_rows_col(data)
creating_doc(a)


from geotrek.common.mixins import CustomColumnsMixin
from django.db.models import Q
from django.conf import settings
from django.contrib.gis.db.models.functions import Transform
from rest_framework import permissions as rest_permissions
from geotrek.authent.decorators import same_structure_required
from geotrek.common.views import DocumentPublic, MarkupPublic
from geotrek.outdoor.filters import SiteFilterSet, CourseFilterSet
from geotrek.outdoor.forms import SiteForm, CourseForm
from geotrek.outdoor.models import Site, Course
from geotrek.outdoor.serializers import SiteSerializer, SiteGeojsonSerializer, CourseSerializer, CourseGeojsonSerializer
from mapentity.views import (MapEntityLayer, MapEntityList, MapEntityJsonList,
                             MapEntityDetail, MapEntityCreate, MapEntityUpdate,
                             MapEntityDelete, MapEntityViewSet, MapEntityFormat)

class SiteLayer(MapEntityLayer):
    properties = ['name']
    filterform = SiteFilterSet
    queryset = Site.objects.all()

class SiteList(CustomColumnsMixin, MapEntityList):
    mandatory_columns = ['id', 'name']
    default_extra_columns = ['super_practices', 'date_update']
    filterform = SiteFilterSet
    queryset = Site.objects.all()

class SiteJsonList(MapEntityJsonList, SiteList):
    pass

class SiteDetail(MapEntityDetail):
    queryset = Site.objects.all()

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['can_edit'] = self.get_object().same_structure(self.request.user)
        return context

class SiteCreate(MapEntityCreate):
    model = Site
    form_class = SiteForm

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['site'] = self.request.GET.get('site')
        return kwargs

class SiteUpdate(MapEntityUpdate):
    queryset = Site.objects.all()
    form_class = SiteForm

    @same_structure_required('outdoor:site_detail')
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

class SiteDelete(MapEntityDelete):
    model = Site

    @same_structure_required('outdoor:site_detail')
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

class SiteViewSet(MapEntityViewSet):
    model = Site
    serializer_class = SiteSerializer
    geojson_serializer_class = SiteGeojsonSerializer
    permission_classes = [rest_permissions.DjangoModelPermissionsOrAnonReadOnly]

    def get_queryset(self):
        qs = Site.objects.filter(published=True)
        if 'source' in self.request.GET:
            qs = qs.filter(source__name__in=self.request.GET['source'].split(','))
        if 'portal' in self.request.GET:
            qs = qs.filter(Q(portal__name=self.request.GET['portal']) | Q(portal=None))
        return qs.annotate(api_geom=Transform("geom", settings.API_SRID))

class SiteDocumentPublicMixin:
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        content = self.get_object()

        context['headerimage_ratio'] = settings.EXPORT_HEADER_IMAGE_SIZE['site']
        context['object'] = context['content'] = content

        return context

class SiteDocumentPublic(SiteDocumentPublicMixin, DocumentPublic):
    pass

class SiteMarkupPublic(SiteDocumentPublicMixin, MarkupPublic):
    pass

class SiteFormatList(MapEntityFormat, SiteList):
    mandatory_columns = ['id']
    default_extra_columns = [
        'structure', 'name', 'practice', 'description',
        'description_teaser', 'ambiance', 'advice', 'period', 'labels', 'themes',
        'portal', 'source', 'information_desks', 'web_links', 'eid',
        'orientation', 'wind', 'ratings_min', 'ratings_max', 'managers',
    ]

class CourseLayer(MapEntityLayer):
    properties = ['name']
    filterform = CourseFilterSet
    queryset = Course.objects.all()

class CourseList(CustomColumnsMixin, MapEntityList):
    mandatory_columns = ['id', 'name']
    default_extra_columns = ['site', 'date_update']
    filterform = CourseFilterSet
    queryset = Course.objects.all()

class CourseJsonList(MapEntityJsonList, CourseList):
    pass

class CourseDetail(MapEntityDetail):
    queryset = Course.objects.all()

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['can_edit'] = self.get_object().same_structure(self.request.user)
        return context

class CourseCreate(MapEntityCreate):
    model = Course
    form_class = CourseForm

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['site'] = self.request.GET.get('site')
        return kwargs

class CourseUpdate(MapEntityUpdate):
    queryset = Course.objects.all()
    form_class = CourseForm

    @same_structure_required('outdoor:course_detail')
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

class CourseDelete(MapEntityDelete):
    model = Course

    @same_structure_required('outdoor:course_detail')
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

class CourseViewSet(MapEntityViewSet):
    model = Course
    serializer_class = CourseSerializer
    geojson_serializer_class = CourseGeojsonSerializer
    permission_classes = [rest_permissions.DjangoModelPermissionsOrAnonReadOnly]

    def get_queryset(self):
        qs = Course.objects.filter(published=True)
        if 'source' in self.request.GET:
            qs = qs.filter(site__source__name__in=self.request.GET['source'].split(','))
        if 'portal' in self.request.GET:
            qs = qs.filter(Q(site__portal__name=self.request.GET['portal']) | Q(site__portal=None))
        return qs.annotate(api_geom=Transform("geom", settings.API_SRID))

class CourseDocumentPublicMixin:
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        content = self.get_object()

        context['headerimage_ratio'] = settings.EXPORT_HEADER_IMAGE_SIZE['course']
        context['object'] = context['content'] = content

        return context

class CourseDocumentPublic(CourseDocumentPublicMixin, DocumentPublic):
    pass

class CourseMarkupPublic(CourseDocumentPublicMixin, MarkupPublic):
    pass

class CourseFormatList(MapEntityFormat, CourseList):
    mandatory_columns = ['id']
    default_extra_columns = [
        'structure', 'name', 'site', 'description',
        'advice', 'equipment', 'eid', 'height', 'length', 'ratings'
    ]


#This file illustrates some basic usage for the c2raytools package
#The script reads some data files and prints and plots some statistics

import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import pylab as pl
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator, MultipleLocator
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

mpl.rcParams['axes.linewidth'] = 1.5
mpl.rcParams['xtick.labelsize'] = 14
mpl.rcParams['ytick.labelsize'] = 14
mpl.rcParams['axes.labelsize'] = 17

#Some path names. Modify these as needed
one_filename = '../244Mpc_f2_0_250_pyt/results/dT_lc_b5nu4_244Mpc_0_250.dat'
#two_filename =	'../244Mpc_f2_8.2S_250_pyt/results/dT_rsd_b5nu4_244Mpc_8.2S_250.dat'
#three_filename = '../244Mpc_f2_8.2pS_250_pyt/results/dT_rsd_b5nu4_244Mpc_8.2pS_250.dat'
#four_filename = '../244Mpc_f2_gS_250_pyt/results/dT_rsd_b5nu4_244Mpc_gS_250.dat'
five_filename = '../244Mpc_f2_0_250_pyt/results/xfrac_244Mpc_f2_0_250.dat'
#six_filename =	'../244Mpc_f2_8.2S_250_pyt/results/xfrac_244Mpc_f2_8.2S_250.dat'
#seven_filename = '../244Mpc_f2_8.2pS_250_pyt/results/xfrac_244Mpc_f2_8.2pS_250.dat'
#eight_filename = '../244Mpc_f2_gS_250_pyt/results/xfrac_244Mpc_f2_gS_250.dat'

#Read in dTx files files (z, nu, dT_mean, dTconv_mean, dTconv_rms, dTconv_skew)
f1 = np.loadtxt(one_filename)
#f2 = np.loadtxt(two_filename)
#f3 = np.loadtxt(three_filename)
#f4 = np.loadtxt(four_filename)
f5 = np.loadtxt(five_filename)
#f6 = np.loadtxt(six_filename)
#f7 = np.loadtxt(seven_filename)
#f8 = np.loadtxt(eight_filename)

pl.figure()
pl.plot(f1[:,1],f1[:,3],'r-',label='L1',lw=1.5)
#pl.plot(f2[:,1],f2[:,3],'b:',label='L2',lw=2)
#pl.plot(f3[:,1],f3[:,3],'g--',label='L3',lw=2)
#pl.plot(f4[:,1],f4[:,3],'m-.',label='L4',lw=2)
pl.ylim([0,9])
pl.xlim([210,70])
#pl.title('rms source comparison')
pl.xlabel('$\\nu_{\\rm obs}\; \\rm{[MHz]}$')
pl.ylabel('$<\delta T_{\\rm b}>^{1/2}\; \\rm{[mK]}$')
pl.minorticks_on()
leg = pl.legend(loc='upper left',prop={'size':11})
leg.draw_frame(False)
pl.savefig('./eps/dTrms_244_rsd_lc_sources.eps')
pl.savefig('./png/dTrms_244_rsd_lc_sources.png')

pl.clf()
pl.plot(f1[:,1],f1[:,3],'r-',lw=1.5,label='L1')
#pl.plot(f2[:,1],f2[:,7],'b:',lw=2,label='L2')
#pl.plot(f3[:,1],f3[:,7],'g--',lw=2,label='L3')
#pl.plot(f4[:,1],f4[:,7],'m-.',lw=2,label='L4')
#pl.plot(f1[:,1],f1[:,7],'r',label='no LMACHs')
#pl.plot(f2[:,1],f2[:,7],'b',label='supp LMACHs')
#pl.plot(f3[:,1],f3[:,7],'g',label='psupp LMACHs')
#pl.plot(f4[:,1],f4[:,7],'m',label='gsupp LMACHs')
pl.ylim([0,5])
pl.xlim([210,70])
#pl.title('rms Source comparison with 5" beam, 0.4 MHz')
pl.xlabel('$\\nu_{\\rm obs}\; \\rm{[MHz]}$')
pl.ylabel('$<\delta T_{\\rm b}>^{1/2} \\rm{[mK]}$')
pl.minorticks_on()
leg = pl.legend(loc='upper left',prop={'size':11})
leg.draw_frame(False)
pl.savefig('./eps/dTrms_244_rsd_lc_b5nu4_sources.eps')
pl.savefig('./png/dTrms_244_rsd_lc_b5nu4_sources.png')
'''
pl.clf()
pl.plot(f1[:,1],f1[:,2],'r-',lw=1.5,label='L1')
pl.plot(f2[:,1],f2[:,2],'b:',lw=2,label='L2')
pl.plot(f3[:,1],f3[:,2],'g--',lw=2,label='L3')
pl.plot(f4[:,1],f4[:,2],'m-.',lw=2,label='L4')
pl.ylim([0,40])
pl.xlim([210,70])
#pl.title('mean dT source comparison')
pl.xlabel('$\\nu_{\\rm obs}\; \\rm{[MHz]}$')
pl.ylabel('$\\overline{\delta T}_{\\rm b}\; \\rm{[mK]}$')
leg = pl.legend(loc='upper left',prop={'size':11})
leg.draw_frame(False)
pl.savefig('./eps/dTmean_244_rsd_lc_sources.eps')
pl.savefig('./png/dTmean_244_rsd_lc_sources.png')

pl.clf()
pl.plot(f5[:,2],f1[:,3],'r-',lw=1.5,label='L1')
pl.plot(f6[:,2],f2[:,3],'b:',lw=2,label='L2')
pl.plot(f7[:,2],f3[:,3],'g--',lw=2,label='L3')
pl.plot(f8[:,2],f4[:,3],'m-.',lw=2,label='L4')
#pl.plot(f5[:,2],f1[:,3],'r',lw=2,label='no LMACHs')
#pl.plot(f6[:,2],f2[:,3],'b',lw=2,label='supp LMACHs')
#pl.plot(f7[:,2],f3[:,3],'g',lw=2,label='psupp LMACHs')
#pl.plot(f8[:,2],f4[:,3],'m',lw=2,label='gsupp LMACHs')
pl.ylim([0,9])
pl.xlim([1.05,-0.05])
#pl.title('rms comparison vs. ionized fraction')
pl.xlabel('$x_{\\rm m}$')
pl.ylabel('$<\delta T_{\\rm b}>^{1/2}\; \\rm{[mK]}$')
pl.minorticks_on()
leg = pl.legend(loc='upper left',prop={'size':11})
leg.draw_frame(False)
pl.savefig('./eps/dTrmsxi_244_rsd_lc_sources.eps')
pl.savefig('./png/dTrmsxi_244_rsd_lc_sources.png')

pl.clf()
pl.plot(f5[:,2],f1[:,7],'r-',lw=1.5,label='L1')
pl.plot(f6[:,2],f2[:,7],'b:',lw=2,label='L2')
pl.plot(f7[:,2],f3[:,7],'g--',lw=2,label='L3')
pl.plot(f8[:,2],f4[:,7],'m-.',lw=2,label='L4')
#pl.plot(f5[:,2],f1[:,7],'r',label='no LMACHs')
#pl.plot(f6[:,2],f2[:,7],'b',label='supp LMACHs')
#pl.plot(f7[:,2],f3[:,7],'g',label='psupp LMACHs')
#pl.plot(f8[:,2],f4[:,7],'m',label='gsupp LMACHs')
pl.ylim([0,5])
pl.xlim([1.05,-0.05])
#pl.title('rms source comparison with 5" beam, 0.4 MHz')
pl.xlabel('$x_{\\rm m}$')
pl.ylabel('$<\delta T_{\\rm b}>^{1/2}\; \\rm{[mK]}$')
pl.minorticks_on()
leg = pl.legend(loc='upper left',prop={'size':11})
leg.draw_frame(False)
pl.savefig('./eps/dTrmsxi_244_rsd_lc_b5nu4_sources.eps')
pl.savefig('./png/dTrmsxi_244_rsd_lc_b5nu4_sources.png')

pl.clf()
fg = gridspec.GridSpec(3,1)
fg.update(hspace=0)
up = pl.subplot(fg[0, :])
up.plot(f1[:,1],f1[:,3],'r-',label='L1',lw=1.5)
up.plot(f2[:,1],f2[:,3],'b:',label='L2',lw=2)
up.plot(f3[:,1],f3[:,3],'g--',label='L3',lw=2)
up.plot(f4[:,1],f4[:,3],'m-.',label='L4',lw=2)
up.set_ylabel('$\\overline{\delta T}_{\\rm b}\; \\rm{[mK]}$')
up.set_ylim([0,39])
up.set_xlim([210,70])
up.minorticks_on()
up_yax = up.axes.get_yaxis()
up_yax.set_major_locator(MaxNLocator(integer=True))
low = pl.subplot(fg[1:, :])
low.plot(f1[:,1],f1[:,7],'r-',label='L1',lw=1.5)
low.plot(f2[:,1],f2[:,7],'b:',label='L2',lw=2)
low.plot(f3[:,1],f3[:,7],'g--',label='L3',lw=2)
low.plot(f4[:,1],f4[:,7],'m-.',label='L4',lw=2)
low.set_ylim([0,5])
low.set_xlim([210,70])
leg = up.legend(loc='lower right',prop={'size':12})
leg.draw_frame(False)
low.set_ylabel('$<\delta T_{\\rm b}>^{1/2}\; \\rm{[mK]}$')
low.set_xlabel('$\\nu_{\\rm obs}\; \\rm{[MHz]}$')
low.minorticks_on()

pl.setp(up.get_xticklabels(), visible=False)
pl.minorticks_on()
pl.savefig('./eps/dTmeanrms_244_rsd_lc_b5nu4_sources.eps')
pl.savefig('./png/dTmeanrms_244_rsd_lc_b5nu4_sources.png')

pl.clf()
fg = gridspec.GridSpec(1,1)
fg.update(hspace=0)
fig = pl.subplot(fg[0, :])
fig.plot(f1[:,1],f1[:,7],'r-',lw=1.5,label='L1')
fig.plot(f2[:,1],f2[:,7],'b:',lw=2,label='L2')
fig.plot(f3[:,1],f3[:,7],'g--',lw=2,label='L3')
fig.plot(f4[:,1],f4[:,7],'m-.',lw=2,label='L4')
fig.set_ylim([0,5])
fig.set_xlim([210,70])
fig.set_xlabel('$\\nu_{\\rm obs}\; \\rm{[MHz]}$')
fig.set_ylabel('$<\delta T_{\\rm b}>^{1/2} \\rm{[mK]}$')
fig.minorticks_on()
leg = fig.legend(loc='upper left',prop={'size':11})
leg.draw_frame(False)
mpl.rcParams['xtick.labelsize'] = 11
mpl.rcParams['ytick.labelsize'] = 11
a = inset_axes(fig.axes, width="30%", height="30%",loc=4) #width="30%", height="30%",add_axes([7, 4, 1, 1])
a.plot(f1[:,1],f1[:,2],'r-',lw=1.1)
a.plot(f2[:,1],f2[:,2],'b:',lw=1.5)
a.plot(f3[:,1],f3[:,2],'g--',lw=1.5)
a.plot(f4[:,1],f4[:,2],'m-.',lw=1.5)
a.set_ylim([0,39])
a.set_xlim([209,69])
a.get_xaxis().set_visible(False)
pl.savefig('./eps/dTinset_244_rsd_lc_b5nu4_sources.eps')
pl.savefig('./png/dTinset_244_rsd_lc_b5nu4_sources.png')
'''


""" Solve for new parameters

Description:

    Submodule to solve for the new parameter set using the thermodynamic
perturbation technique outlined in Matysiak Clementi 2004. Solutions are
found with the Truncated Singular Value Decomposition (TSVD) algorithm

References:

(1) Matysiak, S.; Clementi, C. Optimal Combination of Theory and Experiment for
the Characterization of the Protein Folding Landscape of S6: How Far Can a
Minimalist Model Go? J. Mol. Biol. 2004, 343, 235-248.
"""

import matplotlib.pyplot as plt
import numpy as np

def find_solutions(model,method):

    target_feature = np.loadtxt("target_feature.dat")
    target_feature_err = np.loadtxt("target_feature_err.dat")
    sim_feature = np.loadtxt("sim_feature.dat")
    sim_feature_err = np.loadtxt("sim_feature_err.dat")
    Jacobian = np.loadtxt("Jacobian.dat")
    Jacobian_err = np.loadtxt("Jacobian_err.dat")
    J = Jacobian

    epsilons = model.contact_epsilons
    norm_eps = np.linalg.norm(epsilons)

    ## Normalize the target step. MAYBE NOT NECESSARY
    df = target_feature - sim_feature
    df /= np.linalg.norm(df)

    u,s,v = np.linalg.svd(J)
    temp  = list(0.5*(s[:-1] + s[1:])) + [0.0]
    temp.reverse()
    cutoffs = np.array(temp)

    nrm_soln = []
    nrm_resd = []
    condition_number = []
    solutions = []
    Taus = []
    for i in range(len(cutoffs)):
        S = np.zeros(J.shape)
        tau = cutoffs[i]
        s_use = s[s > tau]
        S[np.arange(len(s_use)),np.arange(len(s_use))] = 1./s_use
        x_soln = np.dot(v.T,np.dot(S.T,np.dot(u.T,df)))

        residual = np.dot(J,x_soln) - df

        nrm_soln.append(np.linalg.norm(x_soln))
        nrm_resd.append(np.linalg.norm(residual))
        solutions.append(x_soln)
        Taus.append(tau)

        J_use = np.dot(v.T,np.dot(S.T,u.T))
        J_inv_use = np.pinv(J_use)
        cond_num = np.linalg.norm(J_use)*np.linalg.norm(J_inv_use)
        condition_number.append(cond_num)

    save_solution_data(solutions,Taus,nrm_soln,nrm_resd,norm_eps,condition_number,s)

def save_solution_data(solutions,Taus,nrm_soln,nrm_resd,norm_eps,condition_number,s):
    """ Save and plot data for choosing solution and damping parameter

    Description:

        A solution should be chosen that
    """

    ## Save data
    for i in range(len(solutions)):
        np.savetxt("xp_%d.dat" % i,solutions[i])

    np.savetxt("taus.dat",Taus)
    np.savetxt("solution_norms.dat",nrm_soln)
    np.savetxt("residual_norms.dat",nrm_resd)
    np.savetxt("perturbation_norms.dat",np.array(nrm_soln)/norm_eps)
    np.savetxt("singular_values.dat",s)
    np.savetxt("condition_num.dat",condition_number)

    plot_Lcurve(nrm_resd,nrm_soln,Taus)
    plt.savefig("Lcurve.png")
    plt.savefig("Lcurve.pdf")

    plot_Lcurve_curvature(nrm_resd,nrm_soln,Taus)
    plt.savefig("Lcurve_curvature.png")
    plt.savefig("Lcurve_curvature.pdf")

    plot_condition_number(Taus,condition_number)
    plt.savefig("condition_vs_tau.png")
    plt.savefig("condition_vs_tau.pdf")

    plot_solutions(Taus,solutions)
    plt.savefig("solutions.png")
    plt.savefig("solutions.pdf")

def plot_solutions(Taus,solutions):

    solutions = np.array(solutions)

    plt.figure()
    plt.plot(Taus,solutions)
    plt.title("solution values")
    plt.xlabel("$\\tau$",fontsize=16)
    plt.ylabel("$\\delta\\epsilon_{\\tau}$",fontsize=16)
    plt.ylim(-1,5)

def plot_Lcurve(nrm_resd,nrm_soln,Taus,skip=30):
    x = np.log10(nrm_resd)
    y = np.log10(nrm_soln)
    y2 = np.log10(Taus)

    ## Plot the L-curve
    fig, ax1 =  plt.subplots()
    ax2 = ax1.twinx()
    line1, = ax1.plot(x,y,'b')
    line2, = ax2.plot(x,y2,'r')
    ax1.set_xlabel("$\\log{||J\\delta\\epsilon_{\\tau} - \\delta f||}$",fontsize=16)
    ax1.set_ylabel("$\\log{||\\delta\\epsilon_{\\tau}}||}$",fontsize=16)
    ax2.set_ylabel("$\\log{\\tau}$",fontsize=16)
    ticks = ax1.get_xticks()
    ax2.set_xticks(ticks)
    ax2.grid(True)
    plt.title("L-curve for choosing damping parameter $\\tau$")

    ax1.yaxis.label.set_color(line1.get_color())
    ax2.yaxis.label.set_color(line2.get_color())

    ax1.tick_params(axis='y', colors=line1.get_color())
    ax2.tick_params(axis='y', colors=line2.get_color())

def plot_Lcurve_curvature(nrm_resd,nrm_soln,Taus):
    x = np.log10(nrm_resd)
    y = np.log10(nrm_soln)
    y2 = np.log10(Taus)
    ## Calculate the second derivative of the L-curve.
    x2 = np.array([ 0.5*(x[i] + x[i+1]) for i in range(len(x)-1)])
    x3 = np.array([ 0.5*(x2[i] + x2[i+1]) for i in range(len(x2)-1)])

    dy = np.diff(y)
    dx = np.diff(x)
    dx2 = np.diff(x2)
    dydx = dy/dx
    ddy = np.diff(dydx)
    ddyddx = ddy/dx2

    ## Plot the second derivative of the L-curve to help the user select the
    ## optimum damping parameter, tau_choice. tau_choice should be
    ## the tau where the L-curve curvature is the most positive.
    fig, ax1 =  plt.subplots()
    ax2 = ax1.twinx()
    line1, = ax1.plot(x3,ddyddx,'b')
    ax1.xaxis.grid(True)
    ax1.yaxis.grid(True)
    line2, = ax2.plot(x,y2,'r')
    ax1.set_xlabel("$\\log{||J\\delta\\epsilon_{\\tau} - \\delta f||}$",fontsize=16)
    ax1.set_ylabel("$\\frac{d^2}{dx^2}\\log{||\\delta\\epsilon_{\\tau}}||}$",fontsize=16)
    ax2.set_ylabel("$\\log{\\tau}$",fontsize=16)
    plt.title("$\\tau_{choice}$ should be $\\tau$ of max positive curvature")

    ax1.yaxis.label.set_color(line1.get_color())
    ax2.yaxis.label.set_color(line2.get_color())
    ax1.tick_params(axis='y', colors=line1.get_color())
    ax2.tick_params(axis='y', colors=line2.get_color())

def plot_condition_number(Taus,condition_number):
    y2 = np.log10(Taus)
    ## Plot condition number versus damping/regularization parameter tau.
    fig, ax1 =  plt.subplots()
    ax2 = ax1.twinx()
    line1, = ax1.plot(y2,condition_number,'b')
    ax1.xaxis.grid(True)
    ax1.yaxis.grid(True)
    line2, = ax2.plot(y2,np.log(condition_number),'r')
    ax1.set_xlabel("$\\log{\\tau}$",fontsize=16)
    ax1.set_ylabel("condition number",fontsize=14)
    ax2.set_ylabel("$\\kappa = \\log{cond}$",fontsize=16)
    plt.title("Condition number versus damping parameter")

    ax1.yaxis.label.set_color(line1.get_color())
    ax2.yaxis.label.set_color(line2.get_color())
    ax1.tick_params(axis='y', colors=line1.get_color())
    ax2.tick_params(axis='y', colors=line2.get_color())

if __name__ == '__main__':
    import os
    import argparse
    import model_builder as mdb

    parser = argparse.ArgumentParser(description='.')
    parser.add_argument('--name', type=str, required=True, help='Name of protein to plot.')
    parser.add_argument('--iteration', type=int, required=True, help='Iteration to plot.')
    args = parser.parse_args()

    name = args.name
    iteration = args.iteration

    model = mdb.check_inputs.load_model("%s" % name,dry_run=True)
    model.Mut_iteration = iteration
    model.contact_epsilons = np.ones(model.n_contacts,float)
    method = "ddG_MC2004"

    cwd = os.getcwd()
    if not os.path.exists("%s/Mut_%d/test" % (name,iteration)):
        os.mkdir("%s/Mut_%d/test" % (name,iteration))
    os.chdir("%s/Mut_%d/test" % (name,iteration))

    find_solutions(model,method)

    os.chdir(cwd)


import tensorflow as tf
import tensorflow_datasets as tfds
from flask import Flask, jsonify, request

app = Flask(__name__)
padding_size = 1000
model = tf.keras.models.load_model(r'C:\Users\saish\PycharmProjects\sentiment analysis deployment\venv\sentiment_analysis.hdf5')
text_encoder = tfds.features.text.TokenTextEncoder.load_from_file(r"C:\Users\saish\PycharmProjects\sentiment analysis deployment\venv\sa_encoder.vocab")

print('Model and Vocabulary loaded.........')

def pad_to_size(vec, size):
  zeros = [0]* (size- len(vec))
  vec.extend(zeros)
  return vec

def predict_fn(pred_text, pad_size):
  encoded_pred_text = text_encoder.encode(pred_text)
  encoded_pred_text = pad_to_size(encoded_pred_text, pad_size)
  encoded_pred_text = tf.cast(encoded_pred_text, tf.int64)
  predictions = model.predict(tf.expand_dims(encoded_pred_text, 0))
  return(predictions.tolist())

@app.route('/seclassifier', methods=['POST'])
def predict_sentiment():
    text = request.get_json()['text']
    print(text)
    predictions = predict_fn(text, padding_size)
    sentiment = 'positive' if float(''.join(map(str, predictions[0])))>0 else 'negative'
    return jsonify({'predictions': predictions, 'sentiment': sentiment})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='5000')


from django import forms
from .models import customerenter

class Customerform(forms.ModelForm):
	class Meta:
		model = customerenter
		fields =[
		"first_name",
		"last_name",
		"checkin_date",
		"checkout_date",
		"idname",
		"id_num"

		]
		help_texts = {
			'checkin_date': ('yyyy-mm-dd'),
			'checkout_date': ('yyyy-mm-dd'),

		}


from multiprocessing import Process, Lock, Queue
from time import sleep
import ConfigParser
import copy
import requests
import json

class servo_sig(object):
	def __init__(self, conf_path):
		config = ConfigParser.ConfigParser()
		config.read(conf_path)

		self.dev_id = int(config.get("servoConfig", "devID"))

		self.mgmt_sleep = int(config.get("servoConfig", "mgmtSleep"))
		self.op_sleep = int(config.get("servoConfig", "opSleep"))
		self.blaster = open('/dev/servoblaster', 'w')

		self.des_queue_x = Queue()
		self.des_queue_y = Queue()
		self.cur_queue_x = Queue()
		self.cur_queue_y = Queue()

		lock = Lock()
		self.servo_x = servo(config, 0, self.blaster, lock, self.des_queue_x, self.cur_queue_x)
		self.servo_y = servo(config, 1, self.blaster, lock, self.des_queue_y, self.cur_queue_y)

		self.step = int(config.get("servoConfig", "step"))
		self.server_addr = config.get("servoConfig", "server")

		self.process = Process(target = self.signal_channel, args=())
		self.process.start()

	def signal_channel(self):
		dev_url = "http://" + self.server_addr + "/dev/" + str(self.dev_id)
		headers = {'Content-Type': 'application/json'}

		servo_positions = [0, 0]

		while True:
			# TODO check conn
			ans = requests.get(dev_url)
			ans = ans.json()

			if ans['manage']:  # management mode
				# TODO: Shutdown all the operational video
				sleep(self.mgmt_sleep)

				complete_code = 1
				response = {}

				if ('servo_active' in ans and ans['servo_active']):  # move servo
					if (not ans['servo_turn_mode']):  # inc
						if(not ans['servo_inc_xy']):  # x=0, y=1
							self.des_queue_x.put(servo_positions[0] + self.step)
						else:
							self.des_queue_y.put(servo_positions[1] + self.step)
					else:  # pos
						self.des_queue_x.put(ans['pos_x'])
						self.des_queue_y.put(ans['pos_y'])

					# sleep(2) # wait for turn servo

					sleep(1)
					if (not self.cur_queue_x.empty()):
						servo_positions[0] = self.cur_queue_x.get()
					if (not self.cur_queue_y.empty()):
						servo_positions[1] = self.cur_queue_y.get()

					if (servo_positions[0] == int(ans['pos_x']) and
					    servo_positions[1] == int(ans['pos_y'])):
						response["servo_turned"] = True
					else:
						response["servo_turned"] = False

					response["pos_x"] = servo_positions[0]
					response["pos_y"] = servo_positions[1]

				if ('picture' in ans and ans['picture']):  # take picture
					filename = dev + str(self.dev_id) + ".jpg"
					os.popen("raspistill -t 10 -o " + filename + " &")
					sleep(1)
					try:
						to_upload = open(filename)
					except IOError:
						# print "cannot open"
						response["picture_taken"] = False
					else:
						# TODO check conn
						resp = requests.post(dev_url + "/picture",
								     files = {filename: open(filename, 'rb')})

						if (resp['success']):
							response["picture_taken"] = True
						else:
							response["picture_taken"] = False

				# TODO take video and upload
				if ('video' in ans and ans['video']):
					pass

				# TODO update script
				if ('update' in ans and ans['update']):
					pass
				elif ('commit' in ans and ans['commit']):
					pass

				# Response completion of jobs
				# TODO check conn
				resp = requests.post(dev_url, data=json.dumps(response), headers=headers)
			else:
				sleep(self.op_sleep)

	def distroy_channel(self):
		self.process.terminate()
		self.servo_x.disable_servo()
		self.servo_y.disable_servo()

class servo(object):
	def __init__(self, config, sID, blaster, lock, des_queue, cur_queue):
		self.cur_pos = 0
		self.blaster = blaster
		self.servo_str = "P1-"+config.get("servoConfig", "servo"+str(sID)+"Pin")+"="
		self.sID = sID

		volt_str = config.get("servoConfig", "servo"+str(sID) + "volt")
		volt_str = volt_str.split(",")
		self.servo_voltage = [int(volt_str[0]), int(volt_str[1])]
		self.ratio = float(self.servo_voltage[1] - self.servo_voltage[0])/180

		self.lock = lock
		self.sleepTime = 0.1

		self.des_queue = des_queue
		self.cur_queue = cur_queue

		# init
		lock.acquire()
		self.blaster.write(self.servo_str + str(90 * self.ratio + self.servo_voltage[0])+'\n')
		self.blaster.flush()
		lock.release()

		sleep(2)

		self.process = Process(target = self.move, args=())
		self.process.start()

	def move(self):
		cur_pos = 0
		while True:
			sleep(self.sleepTime)
			if (self.des_queue.empty()):
				pass
			else:
				des_pos = self.des_queue.get()

				if (des_pos == cur_pos or des_pos > 90 or des_pos < -90):  # posistion range [-90, 90]
					pass
				else:
					volt = float(des_pos + 90) * self.ratio + self.servo_voltage[0]
					volt = max(self.servo_voltage[0], volt)
					volt = min(self.servo_voltage[1], volt)
					# print "voltage : " + str(volt)
					self.lock.acquire()
					self.blaster.write(self.servo_str + str(volt) + '\n')
					self.blaster.flush()
					self.lock.release()
					cur_pos = des_pos
					# print "servo" + str(self.sID) + " cur: " + str(cur_pos)
					# print "servo" + str(self.sID) + " dst: " + str(des_pos)
					self.cur_queue.put(cur_pos)

	def disable_servo(self):
		self.process.terminate()

if __name__ == "__main__":
	ss = servo_sig("./config.ini")
	raw_input("press enter to continue")
	ss.distroy_channel()


#-*-coding:utf-8-*-
from django import template
register = template.Library()

@register.filter
def gradeInformation(value):
    value= int(value)
    if value>=90:
        return u"优秀"
    elif value>=80:
        return u"良好"
    elif value>=60:
        return u"合格"
    else:
        return u"不合格"