from typing import (
    Any,
    AsyncGenerator,
    Coroutine,
    Dict,
    Generator,
    Mapping,
    Optional,
    Union,
)

__all__ = ["BaseCallBuilder"]

class BaseCallBuilder:
    """Creates a new :class:`BaseCallBuilder` pointed to server defined by horizon_url.

    :param horizon_url: Horizon server URL.
    """

    def __init__(self, horizon_url: str) -> None:
        self.horizon_url: str = horizon_url
        self.params: Dict[str, str] = {}
        self.endpoint: str = ""
        self.prev_href: Optional[str] = None
        self.next_href: Optional[str] = None

    def call(self) -> Union[Dict[str, Any], Coroutine[Any, Any, Dict[str, Any]]]:
        """Triggers a HTTP request using this builder's current configuration.

        :return: If it is called synchronous, the response will be returned. If
            it is called asynchronously, it will return Coroutine.
        :raises:
            | :exc:`ConnectionError <stellar_sdk.exceptions.ConnectionError>`: if you have not successfully
                connected to the server.
            | :exc:`NotFoundError <stellar_sdk.exceptions.NotFoundError>`: if status_code == 404
            | :exc:`BadRequestError <stellar_sdk.exceptions.BadRequestError>`: if 400 <= status_code < 500
                and status_code != 404
            | :exc:`BadResponseError <stellar_sdk.exceptions.BadResponseError>`: if 500 <= status_code < 600
            | :exc:`UnknownRequestError <stellar_sdk.exceptions.UnknownRequestError>`: if an unknown error occurs,
                please submit an issue
        """
        raise NotImplementedError

    def stream(
        self,
    ) -> Union[
        AsyncGenerator[Dict[str, Any], None], Generator[Dict[str, Any], None, None]
    ]:
        """Creates an EventSource that listens for incoming messages from the server.

        See `Horizon Response Format <https://developers.stellar.org/api/introduction/response-format/>`__

        See `MDN EventSource <https://developer.mozilla.org/en-US/docs/Web/API/EventSource>`__

        :return: If it is called synchronous, it will return ``Generator``, If
            it is called asynchronously, it will return ``AsyncGenerator``.

        :raise: :exc:`StreamClientError <stellar_sdk.exceptions.StreamClientError>` - Failed to fetch stream resource.
        """
        raise NotImplementedError

    def next(self):
        raise NotImplementedError

    def prev(self):
        raise NotImplementedError

    def cursor(self, cursor: Union[int, str]):
        """Sets ``cursor`` parameter for the current call. Returns the CallBuilder object on which this method has been called.

        See `Pagination <https://developers.stellar.org/api/introduction/pagination/>`__

        :param cursor: A cursor is a value that points to a specific location in a collection of resources.
        :return: current CallBuilder instance
        """
        self._add_query_param("cursor", cursor)
        return self

    def limit(self, limit: int):
        """Sets ``limit`` parameter for the current call. Returns the CallBuilder object on which this method has been called.

        See `Pagination <https://developers.stellar.org/api/introduction/pagination/>`__

        :param limit: Number of records the server should return.
        :return:
        """
        self._add_query_param("limit", limit)
        return self

    def order(self, desc: bool = True):
        """Sets `order` parameter for the current call. Returns the CallBuilder object on which this method has been called.

        :param desc: Sort direction, ``True`` to get desc sort direction, the default setting is ``True``.
        :return: current CallBuilder instance
        """
        order = "asc"
        if desc:
            order = "desc"
        self._add_query_param("order", order)
        return self

    def _add_query_param(self, key: str, value: Union[str, float, int, bool, None]):
        if value is None:
            pass  # pragma: no cover
        elif value is True:
            self.params[key] = "true"
        elif value is False:
            self.params[key] = "false"
        else:
            self.params[key] = str(value)

    def _check_pageable(self, response: dict) -> None:
        links = response.get("_links")
        if not links:
            return
        prev_page = links.get("prev")
        next_page = links.get("next")
        if prev_page:
            self.prev_href = prev_page.get("href")
        if next_page:
            self.next_href = next_page.get("href")

    def _add_query_params(
        self, params: Mapping[str, Union[str, float, int, bool, None]]
    ) -> None:
        for k, v in params.items():
            self._add_query_param(k, v)


import os, shutil
from django.core.files.base import File
from django.conf import settings
from django.test import TestCase
from django.utils.encoding import force_bytes

from test_app.models import FileTesting, ImageTesting, DependencyTesting, RenameFileTesting

def add_base(path):
    return os.path.join(settings.BASE_PATH, path)

class FileBaseTestCase(TestCase):
    static_path = add_base("static")
    media_path = add_base("media")

    def setUp(self):
        self.tearDown()
        os.makedirs(self.media_path)
        shutil.copytree(self.static_path, os.path.join(self.media_path, "static"))

    def tearDown(self):
        if os.path.exists(self.media_path): shutil.rmtree(self.media_path)
        pass

class FileTestCase(FileBaseTestCase):

    def test_file_field(self):
        instance = FileTesting.objects.create()
        # test default static
        self.assertEqual(instance.field_1_foo.url, "/static/defaults/foo.txt")
        self.assertEqual(instance.bar.url, "/static/defaults/bar.txt")
        # test default FieldFile set and processed
        self.assertEqual(instance.field_1_foo.read(), force_bytes("FOO\n"))
        self.assertEqual(instance.bar.read(), force_bytes("BAR\n"))
        self.assertEqual(instance.field_2.read(), force_bytes("foo\n"))
        field_2_path = instance.field_2.path
        self.assertTrue(os.path.isfile(field_2_path))
        # test assignment of file
        foo_bar = File(open(add_base("media/static/defaults/foo-bar.txt"), 'r'))
        instance.field_1 = foo_bar
        instance.save()
        foo_bar.close()
        # make sure default file was not removed
        self.assertTrue(os.path.isfile(field_2_path))
        # check new content
        self.assertEqual(instance.field_1.read(), force_bytes("FOO BAR\n"))
        self.assertEqual(instance.field_1_foo.read(), force_bytes("FOO BAR\n"))
        instance.field_2.seek(0)
        self.assertEqual(instance.field_2.read(), force_bytes("foo\n"))
        # testing setting default value again
        instance.field_2 = None
        instance.save()
        # make sure previous file was removed
        self.assertFalse(os.path.isfile(field_2_path))
        self.assertEqual(instance.field_2.read(), force_bytes("foo bar\n"))
        # test deletion of file together with instance
        field_1_path = instance.field_1.path
        field_1_foo_path = instance.field_1_foo.path
        field_2_path = instance.field_2.path
        self.assertTrue(os.path.isfile(field_1_path))
        self.assertTrue(os.path.isfile(field_1_foo_path))
        self.assertTrue(os.path.isfile(field_2_path))
        instance.delete()
        self.assertFalse(os.path.isfile(field_1_path))
        self.assertFalse(os.path.isfile(field_1_foo_path))
        self.assertFalse(os.path.isfile(field_2_path))

    def test_file_cleanup_after_delete(self):
        instance = FileTesting.objects.create()
        foo_bar = File(open(add_base("media/static/defaults/foo-bar.txt"), 'r'))
        instance.field_3 = foo_bar
        instance.field_4 = foo_bar
        instance.save()
        foo_bar.close()
        field_3_path = instance.field_3.path
        field_4_path = instance.field_4.path
        self.assertTrue(os.path.isfile(field_3_path))
        self.assertTrue(os.path.isfile(field_4_path))
        instance.delete()
        # testing cleanup without dependencies
        self.assertFalse(os.path.isfile(field_3_path))
        # testing keep_orphans=True
        self.assertTrue(os.path.isfile(field_4_path))

    def test_file_cleanup_after_replace(self):
        instance = FileTesting.objects.create()
        foo_bar = File(open(add_base("media/static/defaults/foo-bar.txt"), 'r'))
        instance.field_3 = foo_bar
        instance.field_4 = foo_bar
        instance.save()
        foo_bar.close()
        field_3_path = instance.field_3.path
        field_4_path = instance.field_4.path
        self.assertTrue(os.path.isfile(field_3_path))
        self.assertTrue(os.path.isfile(field_4_path))
        foo = File(open(add_base("media/static/defaults/foo.txt"), 'r'))
        instance.field_3 = foo
        instance.field_4 = foo
        instance.save()
        foo.close()
        # testing cleanup without dependencies
        self.assertFalse(os.path.isfile(field_3_path))
        # testing keep_orphans=True
        self.assertTrue(os.path.isfile(field_4_path))

class ImageTestCase(FileBaseTestCase):

    def test_image_field_mimic_django(self):
        instance = ImageTesting.objects.create()
        lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'),
                          name="lenna_rect.jpg")
        instance.image_1 = lenna_rect
        instance.image_2 = lenna_rect
        instance.save()
        lenna_rect.close()
        # make sure width and heigth values are correct and same as django's
        self.assertEqual(instance.image_1_width, instance.image_2_width)
        self.assertEqual(instance.image_1_height, instance.image_2_height)
        self.assertEqual(instance.image_2_width, 400)
        self.assertEqual(instance.image_2_height, 225)
        # make sure values are saved properly
        instance = ImageTesting.objects.get(pk=instance.pk)
        self.assertEqual(instance.image_2_width, 400)
        self.assertEqual(instance.image_2_height, 225)
        # make sure image is still there and can properly retrieve dims
        self.assertEqual(instance.image_2.width, 400)
        self.assertEqual(instance.image_2.height, 225)
        self.assertEqual(instance.image_1.url, "/media/image_1/lenna_rect.jpg")
        self.assertEqual(instance.image_2.url, "/media/image_2/lenna_rect.jpg")
        # test image replacing
        lenna_square = File(open(add_base("media/static/images/lenna_square.png"), 'rb'))
        instance.image_2 = lenna_square
        self.assertTrue(os.path.isfile(add_base("media/image_2/lenna_rect.jpg")))
        instance.save()
        lenna_square.close()
        self.assertFalse(os.path.isfile(add_base("media/image_2/lenna_rect.jpg")))
        self.assertEqual(instance.image_2.width, 512)
        self.assertEqual(instance.image_2.height, 512)
        instance.image_2 = None
        instance.save()
        self.assertIsNone(instance.image_2_width)
        self.assertIsNone(instance.image_2_height)
        # remove django's ImageFieldFile manually
        instance.image_1.delete()
        instance.delete()
        self.assertFalse(os.path.isfile(add_base("media/image_2/lenna_square.png")))

    def test_wand_image_processor(self):
        instance = ImageTesting.objects.create()
        lenna_square = File(open(add_base("media/static/images/lenna_square.png"), 'rb'))
        instance.image_5 = lenna_square
        instance.save()
        # make sure conversion went through properly
        self.assertEquals(instance.image_5_jpeg.width, 150)
        self.assertEquals(instance.image_5_jpeg.height, 150)
        # save instance, so files get commited to storage
        path = instance.image_5.path
        path_jpeg = instance.image_5_jpeg.path
        # check to see that files got commited
        self.assertTrue(os.path.isfile(path))
        self.assertTrue(os.path.isfile(path_jpeg))

    def test_image_processor(self):
        instance = ImageTesting.objects.create()
        lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
        instance.image_3 = lenna_rect
        instance.save()
        # make sure conversion went through properly
        self.assertEquals(instance.image_3_png.width, 200)
        self.assertEquals(instance.image_3_png.height, 112)
        # save instance, so files get commited to storage
        path = instance.image_3.path
        path_png = instance.image_3_png.path
        # check to see that files got commited
        self.assertTrue(os.path.isfile(path))
        self.assertTrue(os.path.isfile(path_png))
        # make sure dependency gets reattached as expected
        instance = ImageTesting.objects.get(pk=instance.pk)
        self.assertEquals(instance.image_3_png.width, 200)
        self.assertEquals(instance.image_3_png.height, 112)
        self.assertTrue(os.path.isfile(path))
        self.assertTrue(os.path.isfile(path_png))
        # test problematic processor (JPEG2000 is missing a required library)
        instance.image_4 = lenna_rect
        instance.save()
        # check to see that files got commited
        # It is possible than `libjpeg` isn't installed which will cause the test to fail
        self.assertEquals(instance.image_4_jpeg2000.width, 400)
        self.assertEquals(instance.image_4_jpeg2000.height, 225)
        lenna_rect.close()
        # delete instance and check if everything is cleaned up
        instance.delete()
        self.assertFalse(os.path.isfile(path))
        self.assertFalse(os.path.isfile(path_png))

    def test_self_dependency(self):
        instance = DependencyTesting.objects.create()
        lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
        instance.image_1 = lenna_rect
        instance.save()
        lenna_rect.close()
        self.assertEqual(instance.image_1.width, 50)
        self.assertEqual(
            instance.image_1.url,
            "/media/test_app/dependencytesting/%s/image_1.bmp" % instance.pk)
        self.assertEqual(instance.image_1_gif.width, 50)
        self.assertEqual(
            instance.image_1_gif.url,
            "/media/test_app/dependencytesting/%s/image_1_gif.gif" % instance.pk)
        instance.delete()

    def test_value_restoration_1(self):
        lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
        text_file = File(open(add_base("media/static/defaults/foo.txt"), 'rb'))
        instance = DependencyTesting.objects.create()
        instance.image_1 = lenna_rect
        instance.save()
        lenna_rect.close()
        image_1 = instance.image_1
        image_1_gif = instance.image_1_gif
        instance.image_1 = text_file
        instance.save()
        text_file.close()
        self.assertIs(instance.image_1, image_1)
        self.assertIs(instance.image_1_gif, image_1_gif)
        instance.delete()

    def test_value_restoration_2(self):
        lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
        text_file = File(open(add_base("media/static/defaults/foo.txt"), 'rb'))
        instance = DependencyTesting.objects.create()
        instance.image_2 = lenna_rect
        instance.save()
        lenna_rect.close()
        image_3 = instance.image_3
        image_4 = instance.image_4
        # restores values since new file is a text file that cannot be processed
        instance.image_2 = text_file
        instance.save()
        text_file.close()
        self.assertEqual(instance.image_3, image_3)
        self.assertEqual(instance.image_4, image_4)
        self.assertEqual(instance.image_3.path, image_3.path)
        self.assertEqual(instance.image_4.path, image_4.path)
        instance.delete()

    def test_forward_dependency(self):
        instance = DependencyTesting.objects.create()
        lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
        instance.image_3 = lenna_rect
        instance.image_4 = lenna_rect
        instance.save()
        image_3_path = instance.image_3.path
        image_4_path = instance.image_4.path
        self.assertEqual(instance.image_3.width, 400)
        self.assertEqual(instance.image_4.width, 400)
        self.assertEqual(
            instance.image_3.url,
            "/media/test_app/dependencytesting/%s/image_3.jpg" % instance.pk)
        self.assertEqual(
            instance.image_4.url,
            "/media/test_app/dependencytesting/%s/image_4.jpg" % instance.pk)
        instance.image_2 = lenna_rect
        self.assertTrue(os.path.isfile(image_3_path))
        self.assertTrue(os.path.isfile(image_4_path))
        instance.save()
        lenna_rect.close()
        self.assertEqual(instance.image_3.width, 100)
        self.assertEqual(instance.image_4.width, 150)
        # forward dependencies on django's FileFields will also do the cleanup
        self.assertFalse(os.path.isfile(image_3_path))
        self.assertFalse(os.path.isfile(image_4_path))
        instance.delete()

    def test_dependency_error(self):
        instance = ImageTesting()
        image_1 = instance._meta.get_field('image_1')
        image_2 = instance._meta.get_field('image_2')
        self.assertRaises(AssertionError, image_2.manager.dependencies[0].set_field, image_1)

    def test_rename_file_testing(self):
        instance = RenameFileTesting()
        lenna = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
        instance.label = 'foo'
        instance.dynamic_name_file = lenna
        instance.save()
        self.assertEqual(instance.dynamic_name_file.url,
                         "/media/test_app/renamefiletesting/foo.jpg")
        foo_path = instance.dynamic_name_file.path
        self.assertTrue(os.path.isfile(foo_path))
        instance.label = "bar"
        instance.save()
        self.assertEqual(instance.dynamic_name_file.url,
                         "/media/test_app/renamefiletesting/bar.jpg")
        bar_path = instance.dynamic_name_file.path
        self.assertNotEqual(foo_path, bar_path)
        self.assertFalse(os.path.isfile(foo_path))
        self.assertTrue(os.path.isfile(bar_path))
        instance.delete()
        self.assertFalse(os.path.isfile(bar_path))


import torch
from torch.autograd import Variable
import torch.nn.functional as F
from settings import *
from torchvision import datasets, transforms
import torch.nn as nn
import numpy as np
import torch.optim as optim
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
SCALE = 255.0
IMAGE_SIZE = 28 * 28
K = 1
EPOCH = 100
FAKE_IMG_SIZE = 256

def get_data_mnist_loader(train=True):
    return torch.utils.data.DataLoader(
        datasets.MNIST(MNIST_DATA,
                       train=train,
                       download=True,
                       transform=transforms.Compose([
                           transforms.ToTensor(),
                           # transforms.Normalize((0.1307,), (0.3081,))
                       ])),

        batch_size=BATCH_SIZE,
        shuffle=True)

class GeneratorNet(nn.Module):
    def __init__(self, input_size=IMAGE_SIZE, output_size=IMAGE_SIZE, layers=(50, 50), scale=SCALE):
        super(GeneratorNet, self).__init__()

        # self.layer_dims = np.concatenate(([input_size], list(layers), [output_size]))
        # self.fcs = [nn.Linear(self.layer_dims[i], self.layer_dims[i + 1]) for i in range(len(self.layer_dims) - 1)]
        # self.scale = SCALE
        self.fc1 = nn.Linear(FAKE_IMG_SIZE, 256)
        self.fc2 = nn.Linear(256, 512)
        self.fc3 = nn.Linear(512, 1024)
        self.fc4 = nn.Linear(1024, IMAGE_SIZE)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        # for i in range(len(self.fcs) - 1):
        x = Variable(x)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, p=0.5)
        x = F.relu(self.fc2(x))
        x = F.dropout(x, p=0.5)
        x = F.relu(self.fc3(x))
        x = F.dropout(x, p=0.5)
        x = self.fc4(x)

        x = self.sigmoid(x)

        # x = torch.mul(x, SCALE)

        # x = x.view(-1, 1, 28, 28)
        return x

class DiscriminatorNet(nn.Module):
    def __init__(self, input_size=IMAGE_SIZE, output_size=1, layers_cnn=(32, 20), kernel=3, layers_dnn=(320, 50)):
        super(DiscriminatorNet, self).__init__()

        # self.conv_dims = np.concatenate(([input_size], list(layers_cnn)))
        # self.fc_dims = np.concatenate(([list(layers_dnn), [output_size]]))
        #
        # self.convs = [nn.Conv2d(i, i + 1, kernel_size=kernel) for i in range(len(self.fc_dims) - 1)]
        # self.fcs = [nn.Linear(self.fc_dims[i], self.fc_dims[i + 1]) for i in range(len(self.fc_dims) - 1)]
        # self.conv1 = nn.Conv2d(1, 5, kernel_size=kernel, padding=1)
        # self.conv2 = nn.Conv2d(5, 5, kernel_size=kernel, padding=1)
        #
        # self.fc1 = nn.Linear(5*IMAGE_SIZE, 1)
        # self.fc2 = nn.Linear(layers_dnn[1], output_size)

        self.fc1 = nn.Linear(IMAGE_SIZE, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 128)
        self.fc4 = nn.Linear(128, 1)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        # for conv in self.convs:
        #     x = F.relu(conv(x))
        #
        # x = x.view(-1, self.fc_dims[0])
        #
        # for i in range(len(self.fcs) - 1):
        #     x = F.relu(self.fcs[i](x))
        #
        # x = self.fcs[-1](x)
        # print(x.shape)
        # x = F.relu(self.conv1(x))
        #
        # x = F.relu(self.conv2(x))
        #
        # x = x.view(-1, 5*IMAGE_SIZE) # hack
        #
        # x = self.fc1(x)
        x = x.view(-1, IMAGE_SIZE)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = self.fc4(x)

        x = self.sigmoid(x)

        return x

PRINT = False

def get_next_batch(data_loader, epoch=EPOCH):
    for ep in range(epoch):
        for batch_idx, (data, _) in enumerate(data_loader):
            yield ep, data.cuda()
        global PRINT

        PRINT = True
    yield None

def train(train_loader):
    gen_model = GeneratorNet()
    gen_model.cuda()
    gen_optimizer = optim.Adam(gen_model.parameters(), lr=0.000002)

    disc_model = DiscriminatorNet()
    disc_model.cuda()
    disc_optimizer = optim.Adam(disc_model.parameters(), lr=0.0002)

    batch_data = get_next_batch(train_loader, EPOCH)

    train_loop = True
    while(train_loop):
        gen_model.eval()
        disc_model.train()
        for i in range(K):
            try:
                _, data = next(batch_data)
            except StopIteration:
                train_loop = False
                break
            # for batch_idx, (data, _) in enumerate(train_loader):
            data = Variable(data)
            disc_optimizer.zero_grad()
            noise_data = torch.rand(data.size(0), FAKE_IMG_SIZE).cuda()

            y_noise_data = Variable(torch.zeros(data.size(0)).cuda())
            y_data = Variable(torch.ones(data.size(0)).cuda())

            loss_d_data = torch.nn.BCELoss()
            loss_d_noise_data = torch.nn.BCELoss()

            g_d = gen_model(noise_data)
            d_nd = disc_model(g_d)
            d_d = disc_model(data)

            loss_d = loss_d_data(d_d, y_data) + loss_d_noise_data(d_nd, y_noise_data)

            loss_d.backward()
            disc_optimizer.step()
            # print('Discriminator loss: %f' % loss_d)
        try:
            ep, data = next(batch_data)
        except StopIteration:
            train_loop = False
            break

        disc_model.eval()
        gen_model.train()
        gen_optimizer.zero_grad()

        y_data = Variable(torch.ones(data.size(0)).cuda())

        noise_data = torch.rand(data.size(0), FAKE_IMG_SIZE).cuda()  # hacky

        g_d = gen_model(noise_data)
        d_nd = disc_model(g_d)

        loss_g = torch.nn.BCELoss()
        loss_g = loss_g(d_nd, y_data)
        loss_g.backward()
        gen_optimizer.step()
        # print()
        # print('Generator loss: %f' % loss_g)
        global PRINT
        if PRINT:
            PRINT = False
            print('Discriminator loss: %f' % loss_d)
            print('Generator loss: %f' % loss_g)
            plt.imshow(g_d.cpu().data.numpy()[-1].reshape((28,28)), cmap='gray')
            plt.savefig(os.path.join(PLOT_DIR, 'plot_' + str(ep)))

def main():
    train_loader = get_data_mnist_loader(train=True)
    train(train_loader)

if __name__ == '__main__':
    main()


import cv2
import os
import numpy as np

def loadImages(folder_name):
    files = os.listdir(folder_name)
    print("Loading images from ", folder_name)
    images = []
    for f in files:
        # print(f)
        image_path = folder_name + "/" + f
        image = cv2.imread(image_path)
        if image is not None:
            images.append(image)
        else:
            print("Error in loading image ", image)

    return images

def extractParamFromA(init_A, init_kc):
    alpha = init_A[0,0]
    gamma = init_A[0,1]
    beta = init_A[1,1]
    u0 = init_A[0,2]
    v0 = init_A[1,2]
    k1 = init_kc[0]
    k2 = init_kc[1]

    x0 = np.array([alpha, gamma, beta, u0, v0, k1, k2])
    return x0

def retrieveA(x0):
    alpha, gamma, beta, u0, v0, k1, k2 = x0
    A = np.array([[alpha, gamma, u0], [0, beta, v0], [0, 0, 1]]).reshape(3,3)
    kc = np.array([k1, k2]).reshape(2,1)
    return A, kc


from rasa_core.policies.keras_policy import KerasPolicy
import logging
logger = logging.getLogger(__name__)
class MomPolicy(KerasPolicy):
    def _build_model(self, num_features, num_actions, max_history_len):
        """Build a keras model and return a compiled model.
        :param max_history_len: The maximum number of historical turns used to
                                decide on next action"""
        from keras.layers import LSTM, Activation, Masking, Dense
        from keras.models import Sequential

        n_hidden = 32  # size of hidden layer in LSTM
        # Build Model
        batch_shape = (None, max_history_len, num_features)

        model = Sequential()
        model.add(Masking(-1, batch_input_shape=batch_shape))
        model.add(LSTM(n_hidden, batch_input_shape=batch_shape))
        model.add(Dense(input_dim=n_hidden, output_dim=num_actions))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy',
                      optimizer='adam',
                      metrics=['accuracy'])

        logger.debug(model.summary())
        return model


# -*- coding: utf-8 -*-
import requests

url = "http://www.dzzsb.com.cn:7777/cjxs.asp"

# form-data参数要写成如下格式，注意有None
data = {
    "zkzh": (None, 201381100362),
    "bmxh": (None, 13812020013251),
    "jym": (None, '孔丽玄'),
    "button2": (None, '查询2')
}

response = requests.session().post(url, data=data)
response.encoding='utf-8'
print(response.text)


"""
Hierarchical triple orbit simultaneously fit with RV, astrometry, and disk constraints for both tight inner binary and wide outer binary: inner: `parallax`, `P_A`, `a_A_ang`, `M_Ab`, `e_A`, `i_A`, `omega_Aa`, `Omega_Aa`,  outer: `P_B`, `a_B_ang`, `e_AB`, `i_AB`, `omega_A`, `Omega_A`, `gamma_AB`. `M_A` is derived from inner orbit and fed to outer orbit.  `gamma_A` is essentially the RV prediction of A, and is derived from outer orbit and fed to the inner orbit. This has 15 orbital parameters. Adding 4 RV offsets, 2 * 4 RV jitter terms, and 2 astrometric jitter terms makes it 30 parameters total.

We want to preserve the option of doing a joint fit but without the prior from the disk fit.

To do that, just switch the directory output to `stars-only` and comment out the disk portion of the model definition.
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams["savefig.dpi"] = 100
rcParams["figure.dpi"] = 100
rcParams["font.size"] = 16
rcParams["text.usetex"] = False
rcParams["font.family"] = ["sans-serif"]
rcParams["font.sans-serif"] = ["cmss10"]
rcParams["axes.unicode_minus"] = False

from astropy.time import Time
from astropy.io import ascii
from astropy import units as u
from astropy import constants
import astropy

# load the exoplanet part
import pymc3 as pm
import theano.tensor as tt
import arviz as az

# rcParams["plot.max_subplots"] = 60

import corner

import exoplanet as xo
from exoplanet.distributions import Angle

from src.constants import *

# change to "stars-only/" if you remove the astrometric constraint
# outdir = "disk/"
outdir = "stars-only/"

# calculate posteriors on MA using the disk samples
flatchain = np.load("run10/flatchain.npy")
mass_samples = flatchain[:, 0]
MA_mean = np.mean(mass_samples)
MA_std = np.std(mass_samples)
print(f"{MA_mean:.2f} +/- {MA_std:.2f}")

# for now, subtract from all dates for start of astrometric timeseries
jd0 = 2448630.485039346

def get_arrays(asciiTable, errDict=None, jitter=False):
    """
    Reformat ascii tables into pure numpy arrays of the right dimension.
    """

    output = []

    for star in ["Aa", "Ab"]:

        # get the RVs
        rv = asciiTable["RV_" + star]

        if type(rv) is astropy.table.column.MaskedColumn:
            mask = ~rv.mask  # values we want to keep when indexing
        else:
            mask = np.ones(len(rv), dtype="bool")

        rv = np.ascontiguousarray(rv[mask])
        date = np.ascontiguousarray(asciiTable["HJD"][mask]) + 2400000 - jd0

        if errDict is None:
            err = np.ascontiguousarray(asciiTable["sigma_" + star][mask])
        else:
            err = np.ones(len(date), dtype=np.float64) * errDict[star]

        if jitter:
            err = (
                np.ones(len(date), dtype=np.float64) * 0.1
            )  # [km/s] assume a small error, since we'll infer.

        assert len(date) == len(rv), "date - rv length mismatch"
        assert len(date) == len(err), "date - err length mismatch"

        tup = (date, rv, err)

        output.append(tup)

    return output

# Do this to infer w/ jitter
jitter = True

# load all the data
data_cfa = ascii.read("../close/data/cfa.dat")
# cfa errors are provided in table
cfa1, cfa2 = get_arrays(data_cfa, jitter=jitter)

data_keck = ascii.read("../close/data/keck.dat", format="tab", fill_values=[("X", 0)])
err_keck = {"Aa": 0.63, "Ab": 0.85, "B": 0.59}  # km/s
keck1, keck2 = get_arrays(data_keck, err_keck, jitter=jitter)

data_feros = ascii.read("../close/data/feros.dat")
err_feros = {"Aa": 2.61, "Ab": 3.59, "B": 2.60}  # km/s
feros1, feros2 = get_arrays(data_feros, err_feros, jitter=jitter)

data_dupont = ascii.read("../close/data/dupont.dat", fill_values=[("X", 0)])
err_dupont = {"Aa": 1.46, "Ab": 2.34, "B": 3.95}  # km/s
dupont1, dupont2 = get_arrays(data_dupont, err_dupont, jitter=jitter)

data = [data_cfa, data_keck, data_feros, data_dupont]

# specifically load the B velocities
mask = ~data_keck["RV_B"].mask

keck3 = (
    np.ascontiguousarray(data_keck["HJD"][mask]) + 2400000 - jd0,
    np.ascontiguousarray(data_keck["RV_B"][mask]),
    0.2 * np.ones(np.sum(mask), dtype=np.float64),
)

# load the astrometric points for the close orbit

# keep in mind that the primary and secondary stars *could* be switched
# separation is in milliarcseconds
int_data = ascii.read("../close/data/int_data.dat")

astro_jd = int_data["epoch"][0] - jd0
rho_data = int_data["sep"][0] * 1e-3  # arcsec
rho_err = int_data["sep_err"][0] * 1e-3  # arcsec
theta_data = int_data["pa"][0] * deg  # radians
theta_err = int_data["pa_err"][0] * deg  # radians

anthonioz = (astro_jd, rho_data, rho_err, theta_data, theta_err)

# load the astrometry for the wide orbit
data = ascii.read(
    "../wide/data/visual_data_besselian.csv", format="csv", fill_values=[("X", "0")]
)

# convert years
jds = Time(np.ascontiguousarray(data["epoch"]), format="byear").jd - jd0

data["rho_err"][data["rho_err"].mask == True] = 0.05
data["PA_err"][data["PA_err"].mask == True] = 5.0

# convert all masked frames to be raw np arrays, since theano has issues with astropy masked columns
rho_data = np.ascontiguousarray(data["rho"], dtype=float)  # arcsec
rho_err = np.ascontiguousarray(data["rho_err"], dtype=float)

# the position angle measurements come in degrees in the range [0, 360].
# we need to convert this to radians in the range [-pi, pi]
theta_data = np.ascontiguousarray(data["PA"] * deg, dtype=float)
theta_data[theta_data > np.pi] -= 2 * np.pi

theta_err = np.ascontiguousarray(data["PA_err"] * deg)  # radians

wds = (jds, rho_data, rho_err, theta_data, theta_err)

def calc_Mtot(a, P):
    """
    Calculate the total mass of the system using Kepler's third law.

    Args:
        a (au) semi-major axis
        P (days) period

    Returns:
        Mtot (M_sun) total mass of system (M_primary + M_secondary)
    """

    day_to_s = (1 * u.day).to(u.s).value
    au_to_m = (1 * u.au).to(u.m).value
    kg_to_M_sun = (1 * u.kg).to(u.M_sun).value

    return (
        4
        * np.pi ** 2
        * (a * au_to_m) ** 3
        / (constants.G.value * (P * day_to_s) ** 2)
        * kg_to_M_sun
    )

def calc_a(Mtot, P):
    """
    Calculate the semi-major axis using Kepler's third law

    Args:
        Mtot (Msun) total mass
        P (days) period

    Returns:
        a (au)
    """

    day_to_s = (1 * u.day).to(u.s).value
    au_to_m = (1 * u.au).to(u.m).value
    kg_to_M_sun = (1 * u.kg).to(u.M_sun).value

    return (
        ((Mtot / kg_to_M_sun) * constants.G.value * (P * day_to_s) ** 2)
        / (4 * np.pi ** 2)
    ) ** (1 / 3) / au_to_m

zeros = np.zeros_like(wds[0])

xs_phase = np.linspace(0, 1, num=500)
t_data = np.linspace(-yr, wds[0][-1] + yr)

with pm.Model() as model:

    # We'll include the parallax data as a prior on the parallax value
    mparallax = pm.Normal("mparallax", mu=27.31, sd=0.12)  # milliarcsec GAIA DR2
    parallax = pm.Deterministic("parallax", 1e-3 * mparallax)  # arcsec

    a_ang_inner = pm.Uniform("a_ang_inner", 0.1, 10.0, testval=4.66)  # milliarcsec

    # the semi-major axis in au
    a_inner = pm.Deterministic("a_inner", 1e-3 * a_ang_inner / parallax)  # au

    logP_inner = pm.Uniform(
        "logP_inner", lower=0, upper=np.log(50.0), testval=np.log(34.879)
    )  # days
    P_inner = pm.Deterministic("P_inner", tt.exp(logP_inner))

    e_inner = pm.Uniform("e_inner", lower=0, upper=1, testval=0.63)

    omega_inner = Angle("omega_inner", testval=1.42)  # omega_Aa
    Omega_inner = Angle("Omega_inner", testval=1.99)

    cos_incl_inner = pm.Uniform("cos_incl_inner", lower=-1.0, upper=1.0, testval=0.67)
    incl_inner = pm.Deterministic("incl_inner", tt.arccos(cos_incl_inner))

    MAb = pm.Normal("MAb", mu=0.29, sd=0.5)  # solar masses

    t_periastron_inner = pm.Uniform(
        "t_periastron_inner", lower=4050.0, upper=4100.0, testval=4073.0
    )  # + 2400000 + jd0

    orbit_inner = xo.orbits.KeplerianOrbit(
        a=a_inner * au_to_R_sun,
        period=P_inner,
        ecc=e_inner,
        t_periastron=t_periastron_inner,
        omega=omega_inner,
        Omega=Omega_inner,
        incl=incl_inner,
        m_planet=MAb,
    )

    # derived properties from inner orbit
    MA = pm.Deterministic("MA", orbit_inner.m_total)
    MAa = pm.Deterministic("MAa", MA - MAb)

    # we expect the period to be somewhere in the range of 25 years,
    # so we'll set a broad prior on logP
    logP_outer = pm.Normal("logP_outer", mu=6.71, sd=1.0)  # yrs
    P_outer = pm.Deterministic("P_outer", tt.exp(logP_outer) * yr)  # days

    omega_outer = Angle("omega_outer", testval=-0.939)  # - pi to pi
    Omega_outer = Angle("Omega_outer", testval=-0.491)  # - pi to pi

    phi_outer = Angle("phi_outer", testval=0.63)

    n = 2 * np.pi * tt.exp(-logP_outer) / yr  # radians per day

    t_periastron_outer = pm.Deterministic(
        "t_periastron_outer", (phi_outer + omega_outer) / n
    )

    cos_incl_outer = pm.Uniform("cos_incl_outer", lower=-1.0, upper=1.0, testval=-0.60)
    incl_outer = pm.Deterministic("incl_outer", tt.arccos(cos_incl_outer))
    e_outer = pm.Uniform("e_outer", lower=0.0, upper=1.0, testval=0.03)
    gamma_outer = pm.Uniform(
        "gamma_outer", lower=0, upper=15, testval=10.0
    )  # km/s on CfA RV scale

    # We would like to use MB as a parameter, but we have already defined Mtot via the
    # semi-major axis and period, and we have already defined MA from the inner orbit.
    # Mtot = pm.Deterministic("Mtot", calc_Mtot(a_outer, P_outer))
    # MB = pm.Deterministic("MB", Mtot - MA) # solar masses
    PosNormal = pm.Bound(pm.Normal, lower=0.0)
    MB = PosNormal("MB", mu=0.3, sd=0.3, testval=0.5)
    Mtot = pm.Deterministic("Mtot", MA + MB)

    # instead, MB becomes a parameter, and then a_outer is calculated from Mtot and P_outer
    a_outer = pm.Deterministic("a_outer", calc_a(Mtot, P_outer))
    a_ang_outer = pm.Deterministic("a_ang_outer", a_outer * parallax)

    orbit_outer = xo.orbits.KeplerianOrbit(
        a=a_outer * au_to_R_sun,
        period=P_outer,
        ecc=e_outer,
        t_periastron=t_periastron_outer,
        omega=omega_outer,
        Omega=Omega_outer,
        incl=incl_outer,
        m_planet=MB,
    )

    # parameters to shift the RV scale onto the CfA scale
    offset_keck = pm.Normal("offsetKeck", mu=-1.6, sd=5.0)  # km/s
    offset_feros = pm.Normal("offsetFeros", mu=1.8, sd=5.0)  # km/s
    offset_dupont = pm.Normal("offsetDupont", mu=-0.57, sd=5.0)  # km/s

    def get_gamma_A(t):
        """
        Helper function to determine gamma of the inner orbit.

        Args:
            t: time to query

        Returns:
            gamma_A (t) : [km/s]
        """

        gamma_A = conv * orbit_outer.get_star_velocity(t)[2]

        return gamma_A

    # get the radial velocity predictions for RV B
    rv3_keck = (
        conv * orbit_outer.get_planet_velocity(keck3[0])[2] + gamma_outer + offset_keck
    )

    # since we have 4 instruments, we need to predict 4 different dataseries
    def get_RVs(t1, t2, offset):
        """
        Helper function for RVs.

        Args:
            t1: times to query for star 1
            t2 : times to query for star 2
            offset: (km/s)

        Returns:
            (rv1, rv2) [km/s] evaluated at those times with offset applied
       """
        # get the RV predictions
        # get_star_velocity and get_planet_velocity return (v_x, v_y, v_z) tuples,
        # so we only need the v_z vector
        # but, note that since +Z points towards the observer,
        # we actually want v_radial = -v_Z (see conv)
        rv1 = (
            conv * orbit_inner.get_star_velocity(t1)[2]
            + gamma_outer
            + get_gamma_A(t1)
            + offset
        )
        rv2 = (
            conv * orbit_inner.get_planet_velocity(t2)[2]
            + gamma_outer
            + get_gamma_A(t2)
            + offset
        )

        return (rv1, rv2)

    # expects m/s
    # dates are the first entry in each tuple of (date, rv, err)
    rv1_cfa, rv2_cfa = get_RVs(cfa1[0], cfa2[0], 0.0)
    rv1_keck, rv2_keck = get_RVs(keck1[0], keck2[0], offset_keck)
    rv1_feros, rv2_feros = get_RVs(feros1[0], feros2[0], offset_feros)
    rv1_dupont, rv2_dupont = get_RVs(dupont1[0], dupont2[0], offset_dupont)

    logjit_cfa = pm.Uniform("logjittercfa", lower=-5.0, upper=np.log(4), testval=1.18)
    logjit_keck = pm.Uniform(
        "logjitterkeck", lower=-5.0, upper=np.log(4), testval=-0.07
    )
    logjit_feros = pm.Uniform(
        "logjitterferos", lower=-5.0, upper=np.log(4), testval=1.32
    )
    logjit_dupont = pm.Uniform(
        "logjitterdupont", lower=-5.0, upper=np.log(4), testval=0.66
    )
    jit_cfa = pm.Deterministic("jitCfa", tt.exp(logjit_cfa))
    jit_keck = pm.Deterministic("jitKeck", tt.exp(logjit_keck))
    jit_feros = pm.Deterministic("jitFeros", tt.exp(logjit_feros))
    jit_dupont = pm.Deterministic("jitDupont", tt.exp(logjit_dupont))

    # get the total errors
    def get_err(rv_err, logjitter):
        return tt.sqrt(rv_err ** 2 + tt.exp(2 * logjitter))

    # define the RV likelihoods
    pm.Normal(
        "cfaRV1Obs", mu=rv1_cfa, observed=cfa1[1], sd=get_err(cfa1[2], logjit_cfa)
    )
    pm.Normal(
        "cfaRV2Obs", mu=rv2_cfa, observed=cfa2[1], sd=get_err(cfa2[2], logjit_cfa)
    )

    pm.Normal(
        "keckRV1Obs", mu=rv1_keck, observed=keck1[1], sd=get_err(keck1[2], logjit_keck)
    )
    pm.Normal(
        "keckRV2Obs", mu=rv2_keck, observed=keck2[1], sd=get_err(keck2[2], logjit_keck)
    )
    pm.Normal(
        "keckRV3Obs", mu=rv3_keck, observed=keck3[1], sd=get_err(keck3[2], logjit_keck)
    )

    pm.Normal(
        "ferosRV1Obs",
        mu=rv1_feros,
        observed=feros1[1],
        sd=get_err(feros1[2], logjit_feros),
    )
    pm.Normal(
        "ferosRV2Obs",
        mu=rv2_feros,
        observed=feros2[1],
        sd=get_err(feros2[2], logjit_feros),
    )

    pm.Normal(
        "dupontRV1Obs",
        mu=rv1_dupont,
        observed=dupont1[1],
        sd=get_err(dupont1[2], logjit_dupont),
    )
    pm.Normal(
        "dupontRV2Obs",
        mu=rv2_dupont,
        observed=dupont2[1],
        sd=get_err(dupont2[2], logjit_dupont),
    )

    # get the astrometric predictions
    # since there is only one Anthonioz measurement, we won't use jitter
    rho_inner, theta_inner = orbit_inner.get_relative_angles(
        anthonioz[0], parallax
    )  # arcsec

    # astrometric likelihood functions
    pm.Normal("obs_rho_inner", mu=rho_inner, observed=anthonioz[1], sd=anthonioz[2])
    # wrap-safe
    theta_diff_inner = tt.arctan2(
        tt.sin(theta_inner - anthonioz[3]), tt.cos(theta_inner - anthonioz[3])
    )
    pm.Normal("obs_theta_inner", mu=theta_diff_inner, observed=0.0, sd=anthonioz[4])

    rho_outer, theta_outer = orbit_outer.get_relative_angles(wds[0], parallax)  # arcsec

    # add jitter terms to both separation and position angle
    log_rho_s = pm.Normal("logRhoS", mu=-5.14, sd=2.0)
    log_theta_s = pm.Normal("logThetaS", mu=-4.03, sd=2.0)

    rho_tot_err = tt.sqrt(wds[2] ** 2 + tt.exp(2 * log_rho_s))
    theta_tot_err = tt.sqrt(wds[4] ** 2 + tt.exp(2 * log_theta_s))

    pm.Normal("obs_rho_outer", mu=rho_outer, observed=wds[1], sd=rho_tot_err)
    theta_diff_outer = tt.arctan2(
        tt.sin(theta_outer - wds[3]), tt.cos(theta_outer - wds[3])
    )
    pm.Normal("obs_theta_outer", mu=theta_diff_outer, observed=zeros, sd=theta_tot_err)

    # disk dynamical likelihoods
    pm.Normal("obs_MA", mu=MA, observed=MA_mean, sd=MA_std)

# optimize to get a decent starting point
# with model:
#     map_sol0 = xo.optimize(vars=[t_periastron_inner, gamma_outer])
#     map_sol1 = xo.optimize(map_sol0, vars=[a_ang_inner, logP_inner, omega_inner, Omega_inner,
#                                            cos_incl_inner, e_inner, gamma_outer])
#     map_sol2 = xo.optimize(map_sol1, vars=[phi_outer, omega_outer,
#                                             Omega_outer, cos_incl_outer, e_outer])
#     map_sol3 = xo.optimize(map_sol2)

# now let's actually explore the posterior for real
# sampler = xo.PyMC3Sampler(finish=1000, chains=4)
with model:
    trace = pm.sample(
        draws=2000, tune=1000, step_kwargs=dict(target_accept=0.9), chains=4
    )

# save as CSV file
df = pm.trace_to_dataframe(trace)
df.to_csv(f"{outdir}current.csv")

vars = [
    "mparallax",
    "MA",
    "MAb",
    "a_ang_inner",
    "a_inner",
    "logP_inner",
    "e_inner",
    "omega_inner",
    "Omega_inner",
    "cos_incl_inner",
    "incl_inner",
    "t_periastron_inner",
    "logP_outer",
    "omega_outer",
    "Omega_outer",
    "phi_outer",
    "cos_incl_outer",
    "e_outer",
    "gamma_outer",
    "MB",
    "offsetKeck",
    "offsetFeros",
    "offsetDupont",
    "logRhoS",
    "logThetaS",
    "logjittercfa",
    "logjitterkeck",
    "logjitterferos",
    "logjitterdupont",
]

pm.summary(trace)
az.plot_trace(trace)  # , var_names=vars)
plt.savefig(f"{outdir}traceplot.png", dpi=120)


import tcod
import tcod.console
import tcod.event

from components import particle

# Constants
SCREEN_WIDTH = 160
SCREEN_HEIGHT = 90

GAME_TITLE = "Rogue Like Jagger"

FONT = 'assets/font_12x12.png'
FONT_OPTIONS_MASK = tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD

def init_tcod() -> tcod.console.Console:
    tcod.console_set_custom_font(FONT, FONT_OPTIONS_MASK)

    root_console = tcod.console_init_root(
        SCREEN_WIDTH,
        SCREEN_HEIGHT,
        GAME_TITLE,
        renderer=tcod.RENDERER_SDL2,
        fullscreen=False,
        vsync=True,
        order='F')

    tcod.sys_set_fps(30)
    tcod.console_set_fullscreen(True)
    return root_console

def game_loop(console):
    mist = {
        'fade': 0.0000000001,
        'col': (2, 4, 8),
        'length': 25,
        'height': 5,
        'vx': (-5, 5),
        'vy': (-4, -1),
        'life': 200
    }

    rain = {
        'fade': 0.0005,
        'col': (20, 20, 40),
        'vx': (-100, -80),
        'vy': (70, 100),
        'life': 90,
        'height': 2
    }

    lantern = {
        'vy': (-10, -5),
        'vx': (-10, 10),
        'fade': 0.000000002 ,
        'col': (30, 25, 10),
        'life': 140
    }

    emitter = particle.Emitter(console, mist, 1, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 23, 1)
    lantern = particle.Emitter(console, lantern, 1540 // 12, 816 // 12, 3, 1)
    rain = particle.Emitter(console, rain, 1, 0, SCREEN_WIDTH - 2, 1, rate=3)
    bee = open('assets/bee.txt').read()
    wiz = tcod.image_load('assets/wizard_idle_dark.bmp')
    # wiz.set_key_color((0, 0, 0))
    i = 20
    glow = 0
    while True:
        tcod.console_flush()
        console.clear()

        rain.create_particle()
        rain.draw()
        lantern.create_particle()
        lantern.draw()
        emitter.create_particle()
        emitter.draw()

        console.draw_frame(
            0, 0,
            SCREEN_WIDTH, SCREEN_HEIGHT,
            'Welcome to game :)',
            clear=False
        )

        wiz.blit(console, SCREEN_WIDTH - 32, SCREEN_HEIGHT // 2, tcod.BKGND_SCREEN, 1.5, 1.5, 0)
        console.print_box(3, 3, SCREEN_WIDTH - 64, SCREEN_HEIGHT - 3, bee[0:i], fg=(217, 130, 67))
        console.print(SCREEN_WIDTH - 2, 1, str(chr(30)), fg=(252, 149, 71))
        console.print(SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2, str(chr(31)), fg=(252, 149, 71))
        console.print(SCREEN_WIDTH - 8, 1, str(tcod.sys_get_fps()), fg=(230, 230, 230))

        glow = (glow + 0.1) % 6
        if i < len(bee):
            i += 1

        print(glow)
        # console.print(0,0,'lol hi!')
        for event in tcod.event.get():
            if event.type == "QUIT": exit()
            if event.type == "MOUSEMOTION":
                console.print(SCREEN_WIDTH - 40, 1, str(event.pixel))
            if event.type == "KEYDOWN":
                print(i)

def main():
    root_console = init_tcod()
    game_loop(root_console)
    print("Rogue like Jagger")

if __name__ == "__main__":
    main()


import xml.etree.ElementTree as ET
from lxml.etree import parse
import os
from PIL import Image, ImageDraw,ImageFont

def read_xml(xml_path):
    '''
    读取xml模板
    args:
        xml_path: xml文件路径
    '''
    try:
        tree = parse(xml_path)
        root = tree.getroot()
        return tree,root
    except:
        return None

def make_dirs(path):
    '''
    目录不存在就创建
    args:
        path: 要创建的目录
    '''
    if not os.path.exists(path):
        os.makedirs(path)

def get_color_map_list(num_classes):
    """
    Args:
        num_classes (int): number of class
    Returns:
        color_map (list): RGB color list
    """

    color_map = [(0,230,0),(255,255,0),(0,255,255),(255,0,255),(255,0,0)]
    return color_map

def get_color_map_list_(num_classes):
    """
    Args:
        num_classes (int): number of class
    Returns:
        color_map (list): RGB color list
    """
    color_map = num_classes * [0, 0, 0]
    for i in range(0, num_classes):
        j = 0
        lab = i
        while lab:
            color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
            color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
            color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
            j += 1
            lab >>= 3
    color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)]
    return color_map

def draw_box(im, np_boxes, labels, threshold=0.5):
    """
    Args:
        im (PIL.Image.Image): PIL image
        np_boxes (np.ndarray): shape:[N,6], N: number of box,
                               matix element:[class, score, x_min, y_min, x_max, y_max]
        labels (list): labels:['class1', ..., 'classn']
        threshold (float): threshold of box
    Returns:
        im (PIL.Image.Image): visualized image
    """
    draw_thickness = min(im.size) // 300
    draw = ImageDraw.Draw(im)
    clsid2color = {}
    color_list = get_color_map_list(len(labels))
    expect_boxes = (np_boxes[:, 1] > threshold) & (np_boxes[:, 0] > -1)
    np_boxes = np_boxes[expect_boxes, :]

    font_path = "F:/Datasets/matting/Image_Text/font/微软雅黑.ttf"
    font_size = 15
    font = ImageFont.truetype(font_path, font_size)

    for dt in np_boxes:
        clsid, bbox, score = int(dt[0]), dt[2:], dt[1]
        if clsid not in clsid2color:
            clsid2color[clsid] = color_list[clsid]
        color = tuple(clsid2color[clsid])

        if len(bbox) == 4:
            xmin, ymin, xmax, ymax = bbox
            # print('class_id:{:d}, confidence:{:.4f}, left_top:[{:.2f},{:.2f}],'
            #       'right_bottom:[{:.2f},{:.2f}]'.format(
            #           int(clsid), score, xmin, ymin, xmax, ymax))
            # draw bbox
            draw.line(
                [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
                 (xmin, ymin)],
                width=draw_thickness,
                fill=color)
        elif len(bbox) == 8:
            x1, y1, x2, y2, x3, y3, x4, y4 = bbox
            draw.line(
                [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)],
                width=2,
                fill=color)
            xmin = min(x1, x2, x3, x4)
            ymin = min(y1, y2, y3, y4)

        # draw label
        # text = "{} {:.4f}".format(labels[clsid], score)
        text = "{}".format(labels[clsid])
        tw, th = draw.textsize(text)
        tw = len(text)*int(font_size*2/3)
        th = font_size
        draw.rectangle(
            [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
        # draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
        draw.text((xmin + 1, ymin - th), text, fill=(0, 0, 0),font=font)
    return im

def cal_iou(boxa, boxb):
    x1, y1, w1, h1 = boxa
    x2, y2, w2, h2 = boxb
    if (x1 > x2 + w2):
        return 0
    if (y1 > y2 + h2):
        return 0
    if (x1 + w1 < x2):
        return 0
    if (y1 + h1 < y2):
        return 0
    colInt = abs(min(x1 + w1, x2 + w2) - max(x1, x2))
    rowInt = abs(min(y1 + h1, y2 + h2) - max(y1, y2))
    overlap_area = colInt * rowInt
    area1 = w1 * h1
    area2 = w2 * h2
    return overlap_area / (area1 + area2 - overlap_area)

def cal_overlap(boxa, boxb):
    x1, y1, w1, h1 = boxa
    x2, y2, w2, h2 = boxb
    if (x1 > x2 + w2):
        return 0
    if (y1 > y2 + h2):
        return 0
    if (x1 + w1 < x2):
        return 0
    if (y1 + h1 < y2):
        return 0
    colInt = abs(min(x1 + w1, x2 + w2) - max(x1, x2))
    rowInt = abs(min(y1 + h1, y2 + h2) - max(y1, y2))
    overlap_area = colInt * rowInt
    area1 = w1 * h1
    area2 = w2 * h2
    return overlap_area / (area1)


import os
import time
from cognitojwt import decode
from chalicelib.config import (COGNITO_CLIENT_ID, USER_POOL_ID, USER_POOL_REGION,
                                LIVE_ARN_RESOURCES, LOCAL_ARN_RESOURCES)

class CognitoAuthSingleton:
    _instance = None

    @staticmethod
    def get_instance():
        """
        Get the singleton instance of CognitoAuthSingleton class.

        Returns:
            CognitoAuthSingleton: The singleton instance.
        """
        if not CognitoAuthSingleton._instance:
            CognitoAuthSingleton._instance = CognitoAuthSingleton()
        return CognitoAuthSingleton._instance

    def __init__(self):
        """
        Initialize the CognitoAuthSingleton class.
        """
        if CognitoAuthSingleton._instance:
            raise Exception("This class is a singleton!")
        CognitoAuthSingleton._instance = self

    def authenticate_request(self, auth_request):
        """
        Authenticate the request using the provided authentication request.

        Args:
            auth_request (AuthRequest): The authentication request.

        Returns:
            dict: The policy document.

        Raises:
            Exception: If the token is expired or the issuer is invalid.
        """
        token = auth_request.token.replace('Bearer ', '')
        try:
            decoded_token = self._decode_token(token)
            self._validate_token(decoded_token)

            principal_id = decoded_token['sub']
            policy_document = self._generate_policy_document(principal_id, decoded_token)
            return policy_document

        except Exception as e:
            return {"message": str(e)}

    def _decode_token(self, token):
        """
        Decode the provided token.

        Args:
            token (str): The token to decode.

        Returns:
            dict: The decoded token.
        """
        return decode(
            token=token,
            region=USER_POOL_REGION,
            userpool_id=USER_POOL_ID,
            app_client_id=COGNITO_CLIENT_ID
        )

    def _validate_token(self, decoded_token):
        """
        Validate the decoded token.

        Args:
            decoded_token (dict): The decoded token.

        Raises:
            Exception: If the token is expired or the issuer is invalid.
        """
        if decoded_token['exp'] < time.time():
            raise Exception('Token has expired')

        expected_issuer = f'https://cognito-idp.{USER_POOL_REGION}.amazonaws.com/{USER_POOL_ID}'
        if decoded_token['iss'] != expected_issuer:
            raise Exception('Token issuer is invalid')

    def _generate_policy_document(self, principal_id, decoded_token):
        """
        Generate the policy document.

        Args:
            principal_id (str): The principal ID.
            decoded_token (dict): The decoded token.

        Returns:
            dict: The policy document.
        """
        resource_list = LIVE_ARN_RESOURCES + LOCAL_ARN_RESOURCES
        policy_document = {
            'principalId': principal_id,
            'context': decoded_token,
            'policyDocument': {
                'Version': '2012-10-17',
                'Statement': [
                    {
                        'Action': 'execute-api:Invoke',
                        'Effect': 'Allow',
                        'Resource': resource_list
                    }
                ]
            }
        }
        return policy_document


from __future__ import division

import logging as log
from enum import Enum

from nicelib import NiceLib, NiceObjectDef, load_lib

__all__ = ['K10CR1']

# Message Enums
#
class MessageType(Enum):
    GenericDevice = 0
    GenericPiezo = 1
    GenericMotor = 2
    GenericDCMotor = 3
    GenericSimpleMotor = 4
    RackDevice = 5
    Laser = 6
    TECCtlr = 7
    Quad = 8
    NanoTrak = 9
    Specialized = 10
    Solenoid = 11

class GenericDevice(Enum):
    SettingsInitialized = 0
    SettingsUpdated = 1
    Error = 2
    Close = 3

class GenericMotor(Enum):
    Homed = 0
    Moved = 1
    Stopped = 2
    LimitUpdated = 3

class GenericDCMotor(Enum):
    Error = 0
    Status = 1

MessageIDs = {
    MessageType.GenericDevice: GenericDevice,
    MessageType.GenericMotor: GenericMotor,
    MessageType.GenericDCMotor: GenericDCMotor
}

def _instrument(params):
    if 'kinesis_serial' not in params:
        raise InstrumentTypeError("K10CR1 requires 'kinesis_serial'")
    serial = params['kinesis_serial']
    kwds = {key: params[key] for key in ('gear_box_ratio', 'steps_per_rev', 'micro_steps_per_step',
                                         'offset') if key in params}
    return K10CR1(serial, **kwds)

def list_instruments():
    NiceKinesisISC.BuildDeviceList()
    serial_nums = NiceKinesisISC.GetDeviceListExt().strip(',').split(',')
    instruments = []
    for serial_num in serial_nums:
        params = _ParamDict("<K10CR1 '{}'>".format(serial_num))
        params['module'] = 'positioners.K10CR1'
        params['kinesis_serial'] = serial_num
        instruments.append(params)
    return instruments

class KinesisError(Exception):
    messages = {
        0: 'Success',
        1: 'The FTDI functions have not been initialized',
        2: 'The device could not be found. Make sure to call TLI_BuildDeviceList().',
        3: 'The device must be opened before it can be accessed',
        4: 'An I/O Error has occured in the FTDI chip',
        5: 'There are insufficient resources to run this application',
        6: 'An invalid parameter has been supplied to the device',
        7: 'The device is no longer present',
        8: 'The device detected does not match that expected',
        32: 'The device is already open',
        33: 'The device has stopped responding',
        34: 'This function has not been implemented',
        35: 'The device has reported a fault',
        36: 'The function could not be completed because the device is disconnected',
        41: 'The firmware has thrown an error',
        42: 'The device has failed to initialize',
        43: 'An invalid channel address was supplied',
        37: 'The device cannot perform this function until it has been Homed',
        38: 'The function cannot be performed as it would result in an illegal position',
        39: 'An invalid velocity parameter was supplied. The velocity must be greater than zero',
        44: 'This device does not support Homing. Check the Limit switch parameters are correct',
        45: 'An invalid jog mode was supplied for the jog function',
    }

    def __init__(self, code=None, msg=''):
        if code is not None and not msg:
            msg = '(0x{:X}) {}'.format(code, self.messages[code])
        super(KinesisError, self).__init__(msg)
        self.code = code

class NiceKinesisISC(NiceLib):
    """ This class provides a convenient low-level wrapper for the library
    Thorlabs.MotionControl.FilterFlipper.dll"""
    _info = load_lib('kinesis', __package__)

    #
    # Error wrapping functions
    #
    def _ret(ret):
        if ret != 0:
            raise KinesisError(ret)

    def _ret_success(ret, funcname):
        if not ret:
            raise KinesisError(msg="Call to function '{}' failed".format(funcname))

    #
    # Function signatures
    #
    _prefix = 'TLI_'

    BuildDeviceList = ()
    GetDeviceListSize = ({'ret': 'return'},)
    GetDeviceListExt = ('buf', 'len')
    GetDeviceListByTypeExt = ('buf', 'len', 'in')
    GetDeviceListByTypesExt = ('buf', 'len', 'in', 'in')
    GetDeviceInfo = ('in', 'out')

    # GetDeviceList = ('out')
    # GetDeviceListByType = ('out', 'in', dict(first_arg=False))
    # GetDeviceListByTypes = ('out', 'in', 'in', dict(first_arg=False))

    Device = NiceObjectDef(prefix='ISC_', attrs=dict(
        Open = ('in'),
        Close = ('in', {'ret': 'return'}),
        Identify = ('in', {'ret': 'ignore'}),
        GetHardwareInfo = ('in', 'buf', 'len', 'out', 'out', 'buf', 'len', 'out', 'out', 'out'),
        GetFirmwareVersion = ('in', {'ret': 'return'}),
        GetSoftwareVersion = ('in', {'ret': 'return'}),
        LoadSettings = ('in', {'ret': 'success'}),
        PersistSettings = ('in', {'ret': 'success'}),
        GetNumberPositions = ('in', {'ret': 'return'}),
        Home = ('in'),
        NeedsHoming = ('in', {'ret': 'return'}),
        MoveToPosition = ('in', 'in'),
        GetPosition = ('in', {'ret': 'return'}),
        RequestStatus = ('in'),
        RequestStatusBits = ('in'),
        GetStatusBits = ('in', {'ret': 'return'}),
        StartPolling = ('in', 'in', {'ret': 'success'},),
        PollingDuration = ('in', {'ret': 'return'}),
        StopPolling = ('in', {'ret': 'ignore'}),
        RequestSettings = ('in'),
        ClearMessageQueue = ('in', {'ret': 'ignore'}),
        RegisterMessageCallback = ('in', 'in', {'ret': 'ignore'}),
        MessageQueueSize = ('in', {'ret': 'return'}),
        GetNextMessage = ('in', 'out', 'out', 'out', {'ret': 'success'}),
        WaitForMessage = ('in', 'out', 'out', 'out', {'ret': 'success'}),
        GetMotorParamsExt = ('in', 'out', 'out', 'out'),
        SetJogStepSize = ('in', 'in'),
        GetJogVelParams = ('in', 'out', 'out'),
        GetBacklash = ('in', {'ret': 'return'}),
        SetBacklash = ('in', 'in'),
    ))

STATUS_MOVING_CW = 0x10
STATUS_MOVING_CCW = 0x20
STATUS_JOGGING_CW = 0x40
STATUS_JOGGING_CCW = 0x80

class K10CR1(Motion):
    """ Class for controlling Thorlabs K10CR1 integrated stepper rotation stages
    Takes the serial number of the device as a string as well as the gear box ratio,
    steps per revolution and microsteps per step as integers. It also takes the polling
    period as a pint quantity.
    The polling period, which is how often the device updates its status, is
    passed as a pint pint quantity with units of time and is optional argument,
    with a default of 200ms
    """
    _lib = NiceKinesisISC

    # Enums
    MessageType = MessageType
    GenericDevice = GenericDevice
    GenericMotor = GenericMotor
    GenericDCMotor = GenericDCMotor

    @check_units(polling_period='ms')
    def __init__(self, serial, gear_box_ratio=120, steps_per_rev=200, micro_steps_per_step=2048,
                 polling_period='200 ms', offset='0 deg'):
        """
        Parameters
        ----------
        serial: str
        polling_period: pint quantity with units of time
        """
        self.serial = serial
        self.offset = offset
        self._unit_scaling = (gear_box_ratio * micro_steps_per_step *
                              steps_per_rev / (360.0 * u.deg))
        self._open()
        self._start_polling(polling_period)
        self._wait_for_message(0, 0)  # Make sure status has been loaded before we return

    @property
    def _param_dict(self):
        param_dict = _ParamDict(self.__class__.__name__)
        param_dict['module'] = 'motion.kinesis'
        param_dict['kinesis_serial'] = self.serial
        param_dict['offset'] = str(self.offset)
        return param_dict

    def _open(self):
        NiceKinesisISC.BuildDeviceList()  # Necessary?
        self.dev = NiceKinesisISC.Device(self.serial)
        self.dev.Open()

    def close(self):
        self.dev.StopPolling()
        self.dev.Close()

    @check_units(polling_period='ms')
    def _start_polling(self, polling_period='200ms'):
        """Starts polling the device to update its status with the given period provided, rounded
        to the nearest millisecond
        Parameters
        ----------
        polling_period: pint quantity with units of time
        """
        self.polling_period = polling_period
        self.dev.StartPolling(self.polling_period.m_as('ms'))

    @check_units(angle='deg')
    def move_to(self, angle, wait=False):
        """Rotate the stage to the given angle
        Parameters
        ----------
        angle : Quantity
            Angle that the stage will rotate to. Takes the stage offset into account.
        """
        log.debug("Moving stage to {}".format(angle))
        log.debug("Current position is {}".format(self.position))
        self.dev.ClearMessageQueue()
        self.dev.MoveToPosition(self._to_dev_units(angle + self.offset))
        if wait:
            self.wait_for_move()

    def _wait_for_message(self, match_type, match_id):
        msg_type, msg_id, msg_data = self.dev.WaitForMessage()
        log.debug("Received kinesis message ({},{},{})".format(msg_type, msg_id, msg_data))
        while msg_type != match_type or msg_id != match_id:
            msg_type, msg_id, msg_data = self.dev.WaitForMessage()
            log.debug("Received kinesis message ({},{},{})".format(msg_type, msg_id, msg_data))

    def _check_for_message(self, match_type, match_id):
        """Check if a message of the given type and id is in the queue"""
        while True:
            try:
                msg_type, msg_id, msg_data = self.dev.GetNextMessage()
            except KinesisError:
                return False

            log.debug("Received kinesis message ({},{},{})".format(msg_type, msg_id, msg_data))
            if msg_type == match_type and msg_id == match_id:
                return True

    def wait_for_move(self):
        """Wait for the most recent move to complete"""
        self._wait_for_message(2, 1)

    def move_finished(self):
        """Check if the most recent move has finished"""
        return self._check_for_message(2, 1)

    def _to_real_units(self, dev_units):
        return (dev_units / self._unit_scaling).to('deg')

    @check_units(real_units='deg')
    def _to_dev_units(self, real_units):
        return int(round(float(real_units * self._unit_scaling)))

    def home(self, wait=False):
        """Home the stage
        Parameters
        ----------
        wait : bool, optional
            Wait until the stage has finished homing to return
        """
        self.dev.ClearMessageQueue()
        self.dev.Home()

        if wait:
            self.wait_for_home()

    def wait_for_home(self):
        """Wait for the most recent homing operation to complete"""
        self._wait_for_message(2, 0)

    def homing_finished(self):
        """Check if the most recent homing operation has finished"""
        return self._check_for_message(2, 0)

    @property
    def needs_homing(self):
        """True if the device needs to be homed before a move can be performed"""
        return bool(self.dev.NeedsHoming())

    @property
    def offset(self):
        return self._offset

    @offset.setter
    @check_units(offset='deg')
    def offset(self, offset):
        self._offset = offset

    @property
    def position(self):
        return self._to_real_units(self.dev.GetPosition()) - self.offset

    @property
    def is_homing(self):
        return bool(self.dev.GetStatusBits() & 0x00000200)

    @property
    def is_moving(self):
        return bool(self.dev.GetStatusBits() & 0x00000030)

    def get_next_message(self):
        msg_type, msg_id, msg_data = self.dev.GetNextMessage()
        type = MessageType(msg_type)
        id = MessageIDs[type](msg_id)
        return (type, id, msg_data)

    def get_messages(self):
        messages = []
        while True:
            try:
                messages.append(self.get_next_message())
            except Exception:
                break
        return messages


"""Abrir uma imagem colorida, transformar em tom de cinza,
visualizar imagem de entrada.
Apliquem uma limiarização (thresholding),
visualizem os resultados e salvem.
Obs: busquem compreender os resultados da técnica."""

import cv2

imagem = cv2.imread('flor.jpg')

imagem_cinza = cv2.cvtColor(imagem, cv2.COLOR_BGR2GRAY)

# Applicando o threshold
ret, threshold_imagem = cv2.threshold(imagem_cinza, 70, 255, cv2.THRESH_BINARY)

# Show the input image
cv2.imshow('Input grayscale image', imagem_cinza)

# Show the result of the threshold
cv2.imshow('Canny filter result', threshold_imagem)

cv2.waitKey(0)

# Save the results
cv2.imwrite('canny_filter_result.jpg', threshold_imagem)


"""
Determine bad pixels from a calibration file.
Usage:
  bad_pixels.py [options]

Options:
  --help                        Show this
  --calib_file=PATH             Calibration YAML file used to determine the
                                bad pixels together with --nsigma_gain and
                                --nsigma_elecnoise
                                [Default: none]
  --nsigma_gain=INT             Number of sigmas around the mean value
                                acceptable for the gain. If the gain value of a
                                pixel is outside of the acceptable range, this
                                pixel is considered bad.
                                [Default: 5]
  --nsigma_elecnoise=INT        Number of sigmas around the mean value
                                acceptable for the electronic noise. If the
                                electronic noise value of a pixel is outside
                                of the acceptable range, this pixel is
                                considered bad.
                                [Default: 5]
  --dark_histo=FILE             Histogram of the adc samples during dark run
                                used to determine the bad pixels together with
                                --nsigma_dark
                                [Default: none]
  --nsigma_dark=INT             Number of sigmas around the mean value
                                acceptable for raw adc values in dark. If the
                                value of a pixel is outside of the acceptable
                                range, this pixel is considered bad.
                                [Default: 8]
  --plot=FILE                   path to the output plot. Will show the
                                histograms of the values used to determine bad
                                pixels and highlight those.
                                If set to "show", the plot is displayed and not
                                saved. If set to "none", no plot is done.
                                [Default: show]
  --output=FILE                 Same calibration YAML file as the one used as
                                input plus the list of bad pixels. If "none"
                                no file is created. It is safe to use the same
                                file as the input.
                                [Default: none]

"""
import matplotlib.pyplot as plt
import numpy as np
from docopt import docopt
import yaml
from astropy.stats import SigmaClip
from histogram.histogram import Histogram1D

from digicampipe.utils.docopt import convert_int, convert_text

def get_bad_pixels(
        calib_file=None, nsigma_gain=5, nsigma_elecnoise=5,
        dark_histo=None, nsigma_dark=8,
        plot="none", output=None
):
    bad_pix = np.array([], dtype=int)
    if calib_file is not None:
        with open(calib_file) as file:
            calibration_parameters = yaml.load(file)
        gain = np.array(calibration_parameters['gain'])
        elecnoise = np.array(calibration_parameters['sigma_e'])
        nan_mask = np.logical_or(~np.isfinite(gain), ~np.isfinite(elecnoise))
        sigclip_gain = SigmaClip(sigma=nsigma_gain, iters=10,
                                 cenfunc=np.nanmean, stdfunc=np.nanstd)
        sigclip_elecnoise = SigmaClip(sigma=nsigma_elecnoise, iters=10,
                                      cenfunc=np.nanmean, stdfunc=np.nanstd)
        gain_mask = sigclip_gain(gain).mask
        elecnoise_mask = sigclip_elecnoise(elecnoise).mask
        bad_mask = np.logical_or(nan_mask,
                                 np.logical_or(gain_mask, elecnoise_mask))
        bad_pix = np.where(bad_mask)[0]
        if output is not None:
            calibration_parameters['bad_pixels'] = bad_pix
            with open(output, 'w') as file:
                yaml.dump(calibration_parameters, file)
        if plot is not None:
            fig, (ax_gain, ax_elecnoise) = plt.subplots(2, 1)
            gain_bins = np.linspace(np.nanmin(gain), np.nanmax(gain), 100)
            ax_gain.hist(gain[~bad_mask], gain_bins, color='b')
            ax_gain.hist(gain[bad_mask], gain_bins, color='r')
            ax_gain.set_xlabel('integral gain [LSB p.e.$^{-1}$]')
            elecnoise_bins = np.linspace(np.nanmin(elecnoise),
                                         np.nanmax(elecnoise), 100)
            ax_elecnoise.hist(elecnoise[~bad_mask], elecnoise_bins, color='b')
            ax_elecnoise.hist(elecnoise[bad_mask], elecnoise_bins, color='r')
            ax_elecnoise.set_xlabel('electronic noise [LSB]')
            plt.tight_layout()
            if plot != "show":
                plt.savefig(plot)
            else:
                plt.show()
            plt.close(fig)
    if dark_histo is not None:
        dark_histo = Histogram1D.load(dark_histo)
        baseline = dark_histo.mean(method='mid')
        sigclip_dark = SigmaClip(sigma=nsigma_dark, iters=10,
                                 cenfunc=np.nanmean, stdfunc=np.nanstd)
        dark_mask = sigclip_dark(baseline).mask
        bad_dark = np.where(dark_mask)[0]
        bad_pix = np.unique(np.concatenate((bad_pix, bad_dark)))
    return bad_pix

def entry():
    args = docopt(__doc__)
    calib_file = convert_text(args['--calib_file'])
    nsigma_gain = convert_int(args['--nsigma_gain'])
    nsigma_elecnoise = convert_int(args['--nsigma_elecnoise'])
    dark_file = convert_text(args['dark_histo'])
    nsigma_dark = convert_int(args['--nsigma_dark'])
    plot = convert_text(args['--plot'])
    output = convert_text(args['--output'])
    bad_pix = get_bad_pixels(calib_file, nsigma_gain, nsigma_elecnoise,
                             dark_file, nsigma_dark, plot, output)
    print('bad pixels found:', bad_pix)

if __name__ == '__main__':
    entry()


from urllib.parse import urlparse

import requests
from bs4 import BeautifulSoup

def extract_name(url):
    parsing_result = urlparse(url)
    return f'{parsing_result[0]}://{parsing_result[1]}'.lower()

def parse_site(url):
    page_data = {}
    try:
        r = requests.get(url.name)
    except requests.exceptions.RequestException:
        return page_data
    else:
        soup = BeautifulSoup(r.text, 'html.parser')
        page_data['status_code'] = r.status_code
        page_data['title'] = soup.select_one('title')
        page_data['h1'] = soup.select_one('h1')
        page_data['description'] = soup.select_one('meta[name="description"]')
    return page_data


from typing import Any, Dict, Iterable, List, Optional, Tuple, TypeVar, Union

import tensorflow as tf
import torch

from fastestimator.backend._binary_crossentropy import binary_crossentropy
from fastestimator.backend._categorical_crossentropy import categorical_crossentropy
from fastestimator.backend._sparse_categorical_crossentropy import sparse_categorical_crossentropy
from fastestimator.op.tensorop.loss.loss import LossOp
from fastestimator.util.traceability_util import traceable

Tensor = TypeVar('Tensor', tf.Tensor, torch.Tensor)

@traceable()
class CrossEntropy(LossOp):
    """Calculate Element-Wise CrossEntropy (binary, categorical or sparse categorical).

    Args:
        inputs: A tuple or list like: [y_pred, y_true].
        outputs: String key under which to store the computed loss value.
        mode: What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute
            regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument
            like "!infer" or "!train".
        ds_id: What dataset id(s) to execute this Op in. To execute regardless of ds_id, pass None. To execute in all
            ds_ids except for a particular one, you can pass an argument like "!ds1".
        from_logits: Whether y_pred is logits (without softmax).
        average_loss: Whether to average the element-wise loss after the Loss Op.
        form: What form of cross entropy should be performed ('binary', 'categorical', 'sparse', or None). None will
            automatically infer the correct form based on tensor shape: if the both y_pred and y_true are rank-2 tensors
            then 'categorical' will be used, if y_pred is rank-2 tensors but y_true is rank-1 tensor, then `sparse` will
            be chosen, otherwise `binary` will be applied.
        class_weights: Dictionary mapping class indices to a weight for weighting the loss function. Useful when you
            need to pay more attention to samples from an under-represented class.

    Raises:
        AssertionError: If `class_weights` or it's keys and values are of unacceptable data types.
    """
    def __init__(self,
                 inputs: Union[Tuple[str, str], List[str]],
                 outputs: str,
                 mode: Union[None, str, Iterable[str]] = "!infer",
                 ds_id: Union[None, str, Iterable[str]] = None,
                 from_logits: bool = False,
                 average_loss: bool = True,
                 form: Optional[str] = None,
                 class_weights: Optional[Dict[int, float]] = None):
        super().__init__(inputs=inputs, outputs=outputs, mode=mode, ds_id=ds_id, average_loss=average_loss)
        self.from_logits = from_logits
        self.form = form
        self.cross_entropy_fn = {
            "binary": binary_crossentropy,
            "categorical": categorical_crossentropy,
            "sparse": sparse_categorical_crossentropy
        }

        if class_weights:
            assert isinstance(class_weights, dict), \
                "class_weights should be a dictionary or have None value, got {}".format(type(class_weights))
            assert all(isinstance(key, int) for key in class_weights.keys()), \
                "Please ensure that the keys of the class_weight dictionary are of type: int"
            assert all(isinstance(value, float) for value in class_weights.values()), \
                "Please ensure that the values of the class_weight dictionary are of type: float"

        self.class_weights = class_weights
        self.class_dict = None

    def build(self, framework: str, device: Optional[torch.device] = None) -> None:
        if self.class_weights:
            if framework == 'tf':
                keys_tensor = tf.constant(list(self.class_weights.keys()))
                vals_tensor = tf.constant(list(self.class_weights.values()))
                self.class_dict = tf.lookup.StaticHashTable(
                    tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), default_value=1.0)
            elif framework == 'torch':
                self.class_dict = self.class_weights
            else:
                raise ValueError("unrecognized framework: {}".format(framework))

    def forward(self, data: List[Tensor], state: Dict[str, Any]) -> Tensor:
        y_pred, y_true = data
        form = self.form
        if form is None:
            if len(y_pred.shape) == 2 and y_pred.shape[-1] > 1:
                if len(y_true.shape) == 2 and y_true.shape[-1] > 1:
                    form = "categorical"
                else:
                    form = "sparse"
            else:
                form = "binary"

        loss = self.cross_entropy_fn[form](y_pred,
                                           y_true,
                                           from_logits=self.from_logits,
                                           average_loss=self.average_loss,
                                           class_weights=self.class_dict)
        return loss


import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
import pickle

data = pd.read_csv("spam.csv", encoding="latin-1")#reading dataset

#Dataframe includes 3 unwanted columns Unnamed:2, Unnamed:3, Unnamed:4
data.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis=1, inplace=True)

#Since in the df, spam and ham are non numerical values, need to map spam and ham to 1 and 0 respectively
data['class']=data['class'].map({'ham':0, 'spam':1})#mapping func

#The messages must be represented as a numerical structure, use a count vectorizer to convert from text to numbers
cvectorizer = CountVectorizer()

X = data['message']
Y=data['class']

X = cvectorizer.fit_transform(X)
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)

#use a naive bayes classifier
NBmodel = MultinomialNB()
NBmodel.fit(x_train, y_train)

print(NBmodel.score(x_test, y_test)) #accuracy is approx. 98%

#save current model into pickle format
pickle.dump(NBmodel, open("spam.pkl", "wb"))
pickle.dump(cvectorizer, open("vectorizer.pkl", "wb"))

#test model using string
message = input("Enter your mail here: ")

def is_spam(message):
    data = [message]
    vector = cvectorizer.transform(data).toarray()#saved into vector format
    result = NBmodel.predict(vector)
    if(result==1):
        result="The mail is spam"
    else:
        result = "The mail is not spam"
    print(result)

is_spam(message)


from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QAbstractButton, QApplication, QDialog, QDialogButtonBox,
    QLabel, QSizePolicy, QSpacerItem, QVBoxLayout,
    QWidget)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if not Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(400, 307)
        self.root_layout = QVBoxLayout(Dialog)
        self.root_layout.setObjectName(u"root_layout")
        self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)

        self.root_layout.addItem(self.verticalSpacer)

        self.warning_label = QLabel(Dialog)
        self.warning_label.setObjectName(u"warning_label")
        self.warning_label.setStyleSheet(u"QLabel  { color : red; }")

        self.root_layout.addWidget(self.warning_label)

        self.button_box = QDialogButtonBox(Dialog)
        self.button_box.setObjectName(u"button_box")
        self.button_box.setOrientation(Qt.Horizontal)
        self.button_box.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)

        self.root_layout.addWidget(self.button_box)

        self.retranslateUi(Dialog)
        self.button_box.accepted.connect(Dialog.accept)
        self.button_box.rejected.connect(Dialog.reject)

        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
        self.warning_label.setText("")
    # retranslateUi


"""healthyfirst URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.urls import path, include
from rest_framework import routers
from healthyfirst.api import views
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

router = routers.DefaultRouter()
router.register(r'users', views.PersonViewSet, basename='users')
router.register(r'premises', views.PremiseViewSet, basename='premises')
router.register(r'certificate', views.CertificateViewSet, basename='certificate')
router.register(r'businesstype', views.BusinessTypeViewSet, basename='businesstype')
router.register(r'inspectionplan', views.InspectionPlanViewSet, basename='inspectionplan')
router.register(r'sample', views.SampleViewSet, basename='sample')
router.register(r'area', views.AreaViewSet, basename='area')

urlpatterns = [
    path('api/', include(router.urls)),
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('gen_form/<int:cert_id>/', views.generate_form, name='gen_form')
]


import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import explained_variance_score, max_error, r2_score, mean_squared_error

st.title('For your new, recommended dose, please enter the following data:')

TSH2_const_val=2.0

# get model data
@st.cache_data
def load_data(fname = "ForAnalysis.xlsx", TSH2_const_val=2.0):
        df = pd.read_excel(fname, header=1)
        df["TSH2_const"]=TSH2_const_val

        return df.dropna(how="any")

# model
@st.cache_resource
def linear_regression_model_basic(
        df,
        input_cols=['weight', 'Initial weekly dose', "TSH1", "TSH2_const"],
        output_col='New Dose'
        ):
    y = df[output_col].values
    X = df[input_cols].values
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    linreg = LinearRegression()
    linreg.fit(X_train, y_train)
    y_pred = linreg.predict(X_test)
    print(f"r^2: {linreg.score(X_test, y_test): .2f}")
    print(f"MSE: {mean_squared_error(y_test, y_pred): .2f}")
    print(f"Coefficients: {linreg.coef_}")
    print(f"Intercept: {linreg.intercept_}")

    return linreg, X_train, X_test, y_train, y_test

def predict_new_dose(model, weight, initial_weekly_dose, TSH1, TSH2_const_val=2.0):
    return model.predict([[float(weight), float(initial_weekly_dose), float(TSH1), float(TSH2_const_val)]])[0]

for key in st.session_state.keys():
    print(key)
    del st.session_state[key]

# load data
data_load_state = st.text('Loading data...')
df = load_data()
# data_load_state.text('Done! (using cached data)')

# run model
model, _, _, _, _ = linear_regression_model_basic(df)

# predict new dose
min_wt = float(df["weight"].min())
max_wt = float(df["weight"].max())
min_init = float(df["Initial weekly dose"].min())
max_init = float(df["Initial weekly dose"].max() )
min_tsh1 = float(df["TSH1"].min())
max_tsh1 = float(df["TSH1"].max() )

#weight = st.number_input("Enter your weight in Kg (48.0-140.0):", step=0.1, min_value=min_wt, max_value=max_wt)
#initial_weekly_dose = st.number_input("Enter your weekly dose (175.0-1300.0 mL):", step=0.1, min_value=min_init, max_value=max_init)
#TSH1 = st.number_input("Enter your original TSH level (1.0-13.0 mL):", step=0.1, min_value=min_tsh1, max_value=max_tsh1)weight = st.number_input("Enter your weight in Kg (48.0-140.0):", step=0.1, min_value=min_wt, max_value=max_wt)
weight = st.slider("Enter your weight in Kg:", step=0.1, min_value=min_wt, max_value=max_wt)
initial_weekly_dose = st.slider("Enter your weekly dose:", step=0.1, min_value=min_init, max_value=max_init)
TSH1 = st.slider("Enter your original TSH level:", step=0.1, min_value=min_tsh1, max_value=max_tsh1)

#new_dose = False
st.session_state["new_dose"] = np.round(predict_new_dose(model, weight, initial_weekly_dose, TSH1, TSH2_const_val), 1)

#if new_dose:
delta=round(st.session_state["new_dose"]-initial_weekly_dose, 1)
st.metric("Your new dose is:", value=f'{st.session_state["new_dose"]} mL', delta=f'{delta} mL')


from scipy.stats import boxcox
import os
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import MinMaxScaler
import statsmodels.api as sm
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.stattools import adfuller, kpss
from statsmodels.tsa.seasonal import seasonal_decompose
import math
from sklearn.metrics import mean_squared_error, mean_absolute_error
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from statsmodels.tsa.arima_model import ARMA

# https://docs.oracle.com/cd/E16582_01/doc.91/e15111/und_forecast_levels_methods.htm#EOAFM00004

def error_check(orig, prev, nome_col='', nome_indice=''):
    vies = np.mean(orig - prev)
    mse = mean_squared_error(orig, prev)
    rmse = math.sqrt(mean_squared_error(orig, prev))
    mae = mean_absolute_error(orig, prev)
    mape = np.mean(np.abs((orig-prev)/orig))*100
    grupo_erro = [vies, mse, rmse, mae, mape]
    serie = pd.DataFrame(grupo_erro, index=[
        'VIÉS', 'MSE', 'RMSE', 'MAE', 'MAPE'], columns=[nome_col])
    serie.index.name = nome_indice
    return serie

def errorPLot(dados, figsize=(18, 8)):
    dados['Erro'] = dados.iloc[:, 0] - dados.iloc[:, 1]
    plt.figure(figsize=figsize)
    ax1 = plt.subplot2grid((2, 2), (0, 0))
    ax2 = plt.subplot2grid((2, 2), (0, 1))
    ax3 = plt.subplot2grid((2, 2), (1, 0))
    ax4 = plt.subplot2grid((2, 2), (1, 1))

    # Graph real x prev
    ax1.plot(dados.iloc[:, 0:2])
    ax1.legend(['Real', 'Prev'])
    ax1.set_title('Valores Reais vs Previstos')

    # Error x Prev
    ax2.scatter(dados.iloc[:, 1], dados.iloc[:, 2])
    ax2.set_xlabel('Valores Previstos')
    ax2.set_ylabel('Resíduo')
    ax2.set_title('Resíduo vs Valores Previstos')

    # QQ Plot
    sm.graphics.qqplot(dados.iloc[:, 2], line='r', ax=ax3)

    # Graph correlation
    plot_acf(dados.iloc[:, 2], lags=60, zero=False, ax=ax4)
    plt.tight_layout()
    plt.show()

os.chdir('C:\\Users\\arthu\\OneDrive\\Ambiente de Trabalho\\Projects\\Python\\DataSeries\\Etanol- Esalq\\Etanol_priceAnalysis')
df = pd.read_excel("cepea-consulta-20210110095152.xls")
# Transform string to date format in Pandas
df['Data'] = pd.to_datetime(df['Data'], dayfirst=True)
df['À vista R$'] = df['À vista R$'].str.replace(",", ".")
df['À vista US$'] = df['À vista US$'].str.replace(",", ".")
df['À vista R$'] = df['À vista R$'].astype(float)
df['À vista US$'] = df['À vista US$'].astype(float)
print(df.head())
df.sort_values('Data')
df.set_index('Data', inplace=True)
df.columns = ['Preço R$', 'Preço US$']
df['Preço R$'] = df['Preço R$'] * 1000
print(df.head())
print(df.shape[0])
print(df.dtypes)
base_testSize = int(round(df.shape[0]*0.2, 0))
treino = df.iloc[:-base_testSize, 0:1].copy()
teste = df.iloc[-base_testSize:, 0:1].copy()

# Inflation Price adjust !Important for prices
infl = pd.read_excel('IPCA.xls', sheet_name='IPCA')
infl['Data'] = pd.to_datetime(infl['Data'])
infl.set_index('Data', inplace=True)
print(infl.head(-10))
infl['Acumulado'] = pd.to_numeric(infl['Acumulado'])
inf_actual = infl.loc[infl.index.max()]
inf_actual = inf_actual['Acumulado']
# Creating New columns in training DB for the Month and Year
treino['Ano'] = treino.index.year
treino['Mês'] = treino.index.month
index = treino.index

# Merge Inflation DataFrame Into Training DB
treino = treino.merge(
    infl.loc[:, ['Ano', 'Mês', 'Acumulado']], how='left', on=['Ano', 'Mês'])
treino['Preço Ajustado'] = (
    treino['Preço R$']/treino['Acumulado']) * inf_actual
treino.set_index(index, inplace=True)
treino.dropna(inplace=True)

# Model using previous period value
# Just Used to generate error and validations values as base to other methods of prediction
# Cannot be used to do forecasting
# Training DB
simples_treino = treino[['Preço Ajustado']]
simples_treino.columns = ['Real']
simples_treino['Previsão'] = simples_treino['Real'].shift()
simples_treino.dropna(inplace=True)
simples_treino.plot()
plt.show()
erro_treino = error_check(
    simples_treino['Real'], simples_treino['Previsão'], nome_col='Simples', nome_indice='Base Treino')
errorPLot(simples_treino)
# Testing DB
simples_teste = teste[['Preço R$']]
simples_teste.columns = ['Real']
hist = [simples_treino.iloc[i, 0] for i in range(len(simples_treino))]
prev = []
for t in range(len(simples_teste)):
    yhat = hist[-1]
    obs = simples_teste.iloc[t, 0]
    prev.append(yhat)
    hist.append(obs)

simples_teste['Previsão'] = prev
erro_teste = error_check(
    simples_teste['Real'], simples_teste['Previsão'], nome_col='Simples', nome_indice='Base Teste')

# Simple Mean Model
# Training DB
ms_treino = treino[['Preço Ajustado']]
ms_treino.columns = ['Real']
ms_treino['Previsão'] = ms_treino['Real'].expanding().mean()
erro_treino['Média Simples'] = error_check(
    ms_treino['Real'], ms_treino['Previsão'])
errorPLot(ms_treino)
# Testing DB
ms_teste = teste[['Preço R$']]
ms_teste.columns = ['Real']
hist = [ms_treino.iloc[i, 0] for i in range(len(ms_treino))]
prev = []
for t in range(len(ms_teste)):
    # Mean of array Hist, all values
    yhat = np.mean(hist)
    obs = ms_teste.iloc[t, 0]
    prev.append(yhat)
    hist.append(obs)

ms_teste['Previsão'] = prev
erro_teste['Média Simples'] = error_check(
    ms_teste['Real'], ms_teste['Previsão'], nome_col='Mean Model')

# Simple Moving Mean
# Training DB
mm_treino = treino[['Preço Ajustado']]
mm_treino.columns = ['Real']
mm_treino['Previsão'] = mm_treino.rolling(5).mean()
mm_treino.dropna(inplace=True)
erro_treino['Média Movel'] = error_check(
    mm_treino['Real'], mm_treino['Previsão'])
# Testing DB
mm_teste = teste[['Preço R$']]
mm_teste.columns = ['Real']
hist = [mm_treino.iloc[i, 0] for i in range(len(mm_treino))]
prev = []
for t in range(len(mm_teste)):
    yhat = np.mean(hist[-5:])
    obs = mm_teste.iloc[t, 0]
    prev.append(yhat)
    hist.append(obs)

mm_teste['Previsão'] = prev
errorPLot(mm_teste)
erro_teste['Média Móvel'] = error_check(mm_teste['Real'], mm_teste['Previsão'])

# Exp Moving Mean
# Testing for Alpha
mme = treino[['Preço Ajustado']]
alpha_ = [0, 0.2, 1]
for i, valor in enumerate(alpha_):
    model = ExponentialSmoothing(
        mme['Preço Ajustado']).fit(smoothing_level=valor)
    mme['Alpha {}'.format(valor)] = model.predict(start=0, end=len(mme)-1)

mme.plot(figsize=(18, 5), title='Vários valores de Alpha contra a série de treino')
plt.show()
# Model with 0.5 alpha Training DB
mme_treino = treino[['Preço Ajustado']]
mme_treino.columns = ['Real']
alpha = 0.5
modelo = ExponentialSmoothing(mme_treino['Real']).fit(smoothing_level=alpha)
mme_treino['Previsão'] = modelo.predict(start=0, end=len(mme_treino)-1)
erro_treino['Média Móvel Exp'] = error_check(
    mme_treino['Real'], mme_treino['Previsão'])
# Model with Testing DB
mme_teste = teste[['Preço R$']]
mme_teste.columns = ['Real']
print(mme_treino.head(10))
hist = [mme_treino.iloc[i, 0] for i in range(len(mme_treino))]
hist_prev = [mme_treino.iloc[i, 1] for i in range(len(mme_treino))]
prev = []
for t in range(len(mme_teste)):
    yhat = hist_prev[-1] + alpha * (hist[-1] - hist_prev[-1])
    obs = mme_teste.iloc[t, 0]
    prev.append(yhat)
    hist.append(obs)
    hist_prev.append(yhat)

mme_teste['Previsão'] = prev

erro_teste['Média Móvel Exp'] = error_check(
    mme_teste['Real'], mme_teste['Previsão'])
errorPLot(mme_teste)

# Regressive model
plot_acf(treino['Preço Ajustado'], lags=60, zero=False)
plot_pacf(treino['Preço Ajustado'], lags=60, zero=False)
# Model in Training DB
ar_treino = treino[['Preço Ajustado']]
ar_treino.columns = ['Real']
modelo = ARMA(ar_treino['Real'], order=[2, 0]).fit()
ar_treino['Previsão'] = modelo.predict(start=0, end=len(ar_treino)-1)
erro_treino['ARMA'] = error_check(ar_treino['Real'], ar_treino['Previsão'])
# Model in Testing DB
ar_teste = teste[['Preço R$']]
ar_teste.columns = ['Real']
print(modelo.arparams)
coef_1, coef_2 = modelo.arparams
hist = [ar_treino.iloc[i, 0] for i in range(len(ar_treino))]
prev = []
for t in range(len(ar_teste)):
    yhat = (hist[-1]*coef_1)+(hist[-2]*coef_2)
    obs = ar_teste.iloc[t, 0]
    prev.append(yhat)
    hist.append(obs)

ar_teste['Previsão'] = prev
errorPLot(ar_teste)
erro_teste['ARMA'] = error_check(ar_teste['Real'], ar_teste['Previsão'])