import torch.nn as nn
try:
    from .resnet import resnet50_v1b
except:
    from resnet import resnet50_v1b
import torch.nn.functional as F
import torch

class SegBaseModel(nn.Module):
    r"""Base Model for Semantic Segmentation

    Parameters
    ----------
    backbone : string
        Pre-trained dilated backbone network type (default:'resnet50'; 'resnet50',
        'resnet101' or 'resnet152').
    """

    def __init__(self, nclass, aux, backbone='resnet50', dilated=True, pretrained_base=False, **kwargs):
        super(SegBaseModel, self).__init__()
        self.aux = aux
        self.nclass = nclass
        if backbone == 'resnet50':
            self.pretrained = resnet50_v1b(pretrained=pretrained_base, dilated=dilated, **kwargs)

    def base_forward(self, x):
        """forwarding pre-trained network"""
        x = self.pretrained.conv1(x)
        x = self.pretrained.bn1(x)
        x = self.pretrained.relu(x)
        x = self.pretrained.maxpool(x)
        c1 = self.pretrained.layer1(x)
        c2 = self.pretrained.layer2(c1)
        c3 = self.pretrained.layer3(c2)
        c4 = self.pretrained.layer4(c3)

        return c1, c2, c3, c4

class _FCNHead(nn.Module):
    def __init__(self, in_channels, channels, norm_layer=nn.BatchNorm2d, **kwargs):
        super(_FCNHead, self).__init__()
        inter_channels = in_channels // 4
        self.block = nn.Sequential(
            nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
            norm_layer(inter_channels),
            nn.ReLU(inplace=True),
            nn.Dropout(0.1),
            nn.Conv2d(inter_channels, channels, 1)
        )

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

class _PositionAttentionModule(nn.Module):
    """ Position attention module"""

    def __init__(self, in_channels, **kwargs):
        super(_PositionAttentionModule, self).__init__()
        self.conv_b = nn.Conv2d(in_channels, in_channels // 8, 1)
        self.conv_c = nn.Conv2d(in_channels, in_channels // 8, 1)
        self.conv_d = nn.Conv2d(in_channels, in_channels, 1)
        self.alpha = nn.Parameter(torch.zeros(1))
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, x):
        batch_size, _, height, width = x.size()
        feat_b = self.conv_b(x).view(batch_size, -1, height * width).permute(0, 2, 1)
        feat_c = self.conv_c(x).view(batch_size, -1, height * width)
        attention_s = self.softmax(torch.bmm(feat_b, feat_c))
        feat_d = self.conv_d(x).view(batch_size, -1, height * width)
        feat_e = torch.bmm(feat_d, attention_s.permute(0, 2, 1)).view(batch_size, -1, height, width)
        out = self.alpha * feat_e + x

        return out

class _ChannelAttentionModule(nn.Module):
    """Channel attention module"""

    def __init__(self, **kwargs):
        super(_ChannelAttentionModule, self).__init__()
        self.beta = nn.Parameter(torch.zeros(1))
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, x):
        batch_size, _, height, width = x.size()
        feat_a = x.view(batch_size, -1, height * width)
        feat_a_transpose = x.view(batch_size, -1, height * width).permute(0, 2, 1)
        attention = torch.bmm(feat_a, feat_a_transpose)
        attention_new = torch.max(attention, dim=-1, keepdim=True)[0].expand_as(attention) - attention
        attention = self.softmax(attention_new)

        feat_e = torch.bmm(attention, feat_a).view(batch_size, -1, height, width)
        out = self.beta * feat_e + x

        return out

class _DAHead(nn.Module):
    def __init__(self, in_channels, nclass, aux=True, norm_layer=nn.BatchNorm2d, norm_kwargs=None, **kwargs):
        super(_DAHead, self).__init__()
        self.aux = aux
        inter_channels = in_channels // 4
        self.conv_p1 = nn.Sequential(
            nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
            norm_layer(inter_channels, **({} if norm_kwargs is None else norm_kwargs)),
            nn.ReLU(True)
        )
        self.conv_c1 = nn.Sequential(
            nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
            norm_layer(inter_channels, **({} if norm_kwargs is None else norm_kwargs)),
            nn.ReLU(True)
        )
        self.pam = _PositionAttentionModule(inter_channels, **kwargs)
        self.cam = _ChannelAttentionModule(**kwargs)
        self.conv_p2 = nn.Sequential(
            nn.Conv2d(inter_channels, inter_channels, 3, padding=1, bias=False),
            norm_layer(inter_channels, **({} if norm_kwargs is None else norm_kwargs)),
            nn.ReLU(True)
        )
        self.conv_c2 = nn.Sequential(
            nn.Conv2d(inter_channels, inter_channels, 3, padding=1, bias=False),
            norm_layer(inter_channels, **({} if norm_kwargs is None else norm_kwargs)),
            nn.ReLU(True)
        )
        self.out = nn.Sequential(
            nn.Dropout(0.1),
            nn.Conv2d(inter_channels, nclass, 1)
        )
        if aux:
            self.conv_p3 = nn.Sequential(
                nn.Dropout(0.1),
                nn.Conv2d(inter_channels, nclass, 1)
            )
            self.conv_c3 = nn.Sequential(
                nn.Dropout(0.1),
                nn.Conv2d(inter_channels, nclass, 1)
            )

    def forward(self, x):
        feat_p = self.conv_p1(x)
        feat_p = self.pam(feat_p)
        feat_p = self.conv_p2(feat_p)

        feat_c = self.conv_c1(x)
        feat_c = self.cam(feat_c)
        feat_c = self.conv_c2(feat_c)

        feat_fusion = feat_p + feat_c

        outputs = []
        fusion_out = self.out(feat_fusion)
        outputs.append(fusion_out)
        if self.aux:
            p_out = self.conv_p3(feat_p)
            c_out = self.conv_c3(feat_c)
            outputs.append(p_out)
            outputs.append(c_out)

        return tuple(outputs)

class DANet(SegBaseModel):
    r"""Pyramid Scene Parsing Network

    Parameters
    ----------
    nclass : int
        Number of categories for the training dataset.
    backbone : string
        Pre-trained dilated backbone network type (default:'resnet50'; 'resnet50',
        'resnet101' or 'resnet152').
    norm_layer : object
        Normalization layer used in backbone network (default: :class:`mxnet.gluon.nn.BatchNorm`;
        for Synchronized Cross-GPU BachNormalization).
    aux : bool
        Auxiliary loss.
    Reference:
        Jun Fu, Jing Liu, Haijie Tian, Yong Li, Yongjun Bao, Zhiwei Fang,and Hanqing Lu.
        "Dual Attention Network for Scene Segmentation." *CVPR*, 2019
    """

    def __init__(self, nclass, backbone='resnet50', aux=False, pretrained_base=False, **kwargs):
        super(DANet, self).__init__(nclass, aux, backbone, pretrained_base=pretrained_base, **kwargs)
        self.head = _DAHead(2048, nclass, aux, **kwargs)

    def forward(self, x):
        size = x.size()[2:]
        _, _, c3, c4 = self.base_forward(x)
        outputs = []
        x = self.head(c4)
        x0 = F.interpolate(x[0], size, mode='bilinear', align_corners=True)
        if self.aux:
            x1 = F.interpolate(x[1], size, mode='bilinear', align_corners=True)
            x2 = F.interpolate(x[2], size, mode='bilinear', align_corners=True)
            outputs.append(x0)
            outputs.append(x1)
            outputs.append(x2)
            return outputs
        return x0

if __name__ == '__main__':
    from tools.flops_params_fps_count import flops_params_fps
    model = DANet(nclass=6)
    flops_params_fps(model)


from flask import Flask, jsonify, request, make_response
from functions import calculate_penalty,possession

app = Flask(__name__)

@app.route('/')
def index():
    return 'This is index page'

@app.route('/penalties', methods={'POST'})
def getpenalities():

        drug_class=request.form.get('drug_class')
        culpability = request.form.get('culpability')
        harm = request.form.get('harm')
        print((drug_class,culpability,harm))

        result = calculate_penalty(drug_class,culpability,int(harm))
        return jsonify( response = result, status=200,message="success" )

@app.route('/possession_of_a_drug', methods={'POST'})
def possession_of_a_drugapi():
    drug_class = request.form.get('drug_class')
    result = possession(drug_class)
    return jsonify(response=result, status=200, message="success")

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


import numpy as np
import sys
import time
sys.path.append("Interface/python/")
from init import NeuronLayerBox
import cv2

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

if __name__ == '__main__':
	NLB=NeuronLayerBox(step_ms=1,model=1,spike=0,restore=0)

	input_src=[]
	img=cv2.imread("load_data/input.bmp")
	img=rgb2gray(img).astype(int)

	input_src.append(img)
	NLB.step(20)
	NLB.input(input_src)
	for i in range(50):
		NLB.step(5)
		a=(NLB.output()['11']/max(np.max(NLB.output()['11']),0.0000001))*255
		cv2.imshow("1.jpg",a)
		cv2.waitKey(1)
	time.sleep(10)
	NLB.save()
	NLB.exit()


import json

no = 0
groups = 0
reserve = []

data = {
        "no": no,
        "groups": groups,
        "reserve": reserve
}

with open("data.json", "w") as f:
    json.dump(data, f)

opend = open("./data.json","r")
loaded = json.load(opend)

print(loaded)
print("no: ", loaded["no"], "groups: ", loaded["groups"])


from typing import Dict, Optional, Type, Union, Callable, Any
from types import TracebackType
from functools import wraps
from threading import Lock

from qiskit_ibm_provider.utils.converters import hms_to_seconds

from qiskit_ibm_runtime import QiskitRuntimeService
from .runtime_job import RuntimeJob
from .utils.result_decoder import ResultDecoder
from .ibm_backend import IBMBackend
from .utils.default_session import set_cm_session
from .utils.deprecation import deprecate_arguments

def _active_session(func):  # type: ignore
    """Decorator used to ensure the session is active."""

    @wraps(func)
    def _wrapper(self, *args, **kwargs):  # type: ignore
        if not self._active:
            raise RuntimeError("The session is closed.")
        return func(self, *args, **kwargs)

    return _wrapper

class Session:
    """Class for creating a flexible Qiskit Runtime session.

    A Qiskit Runtime ``session`` allows you to group a collection of iterative calls to
    the quantum computer. A session is started when the first job within the session
    is started. Subsequent jobs within the session are prioritized by the scheduler.
    Data used within a session, such as transpiled circuits, is also cached to avoid
    unnecessary overhead.

    You can open a Qiskit Runtime session using this ``Session`` class and submit jobs
    to one or more primitives.

    For example::

        from qiskit.test.reference_circuits import ReferenceCircuits
        from qiskit_ibm_runtime import Sampler, Session, Options

        options = Options(optimization_level=3)

        with Session(backend="ibmq_qasm_simulator") as session:
            sampler = Sampler(session=session, options=options)
            job = sampler.run(ReferenceCircuits.bell())
            print(f"Sampler job ID: {job.job_id()}")
            print(f"Sampler job result: {job.result()}")

    """

    def __init__(
        self,
        service: Optional[QiskitRuntimeService] = None,
        backend: Optional[Union[str, IBMBackend]] = None,
        max_time: Optional[Union[int, str]] = None,
    ):  # pylint: disable=line-too-long
        """Session constructor.

        Args:
            service: Optional instance of the ``QiskitRuntimeService`` class.
                If ``None``, the service associated with the backend, if known, is used.
                Otherwise ``QiskitRuntimeService()`` is used to initialize
                your default saved account.
            backend: Optional instance of :class:`qiskit_ibm_runtime.IBMBackend` class or
                string name of backend. An instance of :class:`qiskit_ibm_provider.IBMBackend` will not work.
                If not specified, a backend will be selected automatically (IBM Cloud channel only).

            max_time: (EXPERIMENTAL setting, can break between releases without warning)
                Maximum amount of time, a runtime session can be open before being
                forcibly closed. Can be specified as seconds (int) or a string like "2h 30m 40s".
                This value must be less than the
                `system imposed maximum
                <https://docs.quantum.ibm.com/run/max-execution-time>`_.

        Raises:
            ValueError: If an input value is invalid.
        """

        if service is None:
            if isinstance(backend, IBMBackend):
                self._service = backend.service
            else:
                self._service = (
                    QiskitRuntimeService()
                    if QiskitRuntimeService.global_service is None
                    else QiskitRuntimeService.global_service
                )

        else:
            self._service = service

        if self._service.channel == "ibm_quantum" and not backend:
            raise ValueError('"backend" is required for ``ibm_quantum`` channel.')

        self._instance = None
        if isinstance(backend, IBMBackend):
            self._instance = backend._instance
            backend = backend.name
        self._backend = backend

        self._setup_lock = Lock()
        self._session_id: Optional[str] = None
        self._active = True
        self._max_time = (
            max_time
            if max_time is None or isinstance(max_time, int)
            else hms_to_seconds(max_time, "Invalid max_time value: ")
        )

    @_active_session
    def run(
        self,
        program_id: str,
        inputs: Dict,
        options: Optional[Dict] = None,
        callback: Optional[Callable] = None,
        result_decoder: Optional[Type[ResultDecoder]] = None,
    ) -> RuntimeJob:
        """Run a program in the session.

        Args:
            program_id: Program ID.
            inputs: Program input parameters. These input values are passed
                to the runtime program.
            options: Runtime options that control the execution environment.
                See :class:`qiskit_ibm_runtime.RuntimeOptions` for all available options.
            callback: Callback function to be invoked for any interim results and final result.

        Returns:
            Submitted job.
        """

        options = options or {}

        if "instance" not in options:
            options["instance"] = self._instance

        options["backend"] = self._backend

        if not self._session_id:
            # Make sure only one thread can send the session starter job.
            self._setup_lock.acquire()
            # TODO: What happens if session max time != first job max time?
            # Use session max time if this is first job.
            options["session_time"] = self._max_time

        try:
            job = self._service.run(
                program_id=program_id,
                options=options,
                inputs=inputs,
                session_id=self._session_id,
                start_session=self._session_id is None,
                callback=callback,
                result_decoder=result_decoder,
            )

            if self._session_id is None:
                self._session_id = job.job_id()
        finally:
            if self._setup_lock.locked():
                self._setup_lock.release()

        if self._backend is None:
            self._backend = job.backend().name

        return job

    def cancel(self) -> None:
        """Cancel all pending jobs in a session."""
        self._active = False
        if self._session_id:
            self._service._api_client.cancel_session(self._session_id)

    def close(self) -> None:
        """Close the session so new jobs will no longer be accepted, but existing
        queued or running jobs will run to completion. The session will be terminated once there
        are no more pending jobs."""
        self._active = False
        if self._session_id:
            self._service._api_client.close_session(self._session_id)

    def backend(self) -> Optional[str]:
        """Return backend for this session.

        Returns:
            Backend for this session. None if unknown.
        """
        return self._backend

    def status(self) -> Optional[str]:
        """Return current session status.

        Returns:
            The current status of the session, including:
            Pending: Session is created but not active.
            It will become active when the next job of this session is dequeued.
            In progress, accepting new jobs: session is active and accepting new jobs.
            In progress, not accepting new jobs: session is active and not accepting new jobs.
            Closed: max_time expired or session was explicitly closed.
            None: status details are not available.
        """
        details = self.details()
        if details:
            state = details["state"]
            accepting_jobs = details["accepting_jobs"]
            if state in ["open", "inactive"]:
                return "Pending"
            if state == "active" and accepting_jobs:
                return "In progress, accepting new jobs"
            if state == "active" and not accepting_jobs:
                return "In progress, not accepting new jobs"
            return state.capitalize()

        return None

    def details(self) -> Optional[Dict[str, Any]]:
        """Return session details.

        Returns:
            A dictionary with the sessions details, including:
            id: id of the session.
            backend_name: backend used for the session.
            interactive_timeout: The maximum idle time (in seconds) between jobs that
            is allowed to occur before the session is deactivated.
            max_time: Maximum allowed time (in seconds) for the session, subject to plan limits.
            active_timeout: The maximum time (in seconds) a session can stay active.
            state: State of the session - open, active, inactive, or closed.
            accepting_jobs: Whether or not the session is accepting jobs.
            last_job_started: Timestamp of when the last job in the session started.
            last_job_completed: Timestamp of when the last job in the session completed.
            started_at: Timestamp of when the session was started.
            closed_at: Timestamp of when the session was closed.
        """
        if self._session_id:
            response = self._service._api_client.session_details(self._session_id)
            if response:
                return {
                    "id": response.get("id"),
                    "backend_name": response.get("backend_name"),
                    "interactive_timeout": response.get("interactive_ttl"),
                    "max_time": response.get("max_ttl"),
                    "active_timeout": response.get("active_ttl"),
                    "state": response.get("state"),
                    "accepting_jobs": response.get("accepting_jobs"),
                    "last_job_started": response.get("last_job_started"),
                    "last_job_completed": response.get("last_job_completed"),
                    "started_at": response.get("started_at"),
                    "closed_at": response.get("closed_at"),
                }
        return None

    @property
    def session_id(self) -> str:
        """Return the session ID.

        Returns:
            Session ID. None until a job runs in the session.
        """
        return self._session_id

    @property
    def service(self) -> QiskitRuntimeService:
        """Return service associated with this session.

        Returns:
            :class:`qiskit_ibm_runtime.QiskitRuntimeService` associated with this session.
        """
        return self._service

    @classmethod
    def from_id(
        cls,
        session_id: str,
        service: Optional[QiskitRuntimeService] = None,
        backend: Optional[Union[str, IBMBackend]] = None,
    ) -> "Session":
        """Construct a Session object with a given session_id

        Args:
            session_id: the id of the session to be created. This must be an already
                existing session id.
            service: instance of the ``QiskitRuntimeService`` class.
            backend: instance of :class:`qiskit_ibm_runtime.IBMBackend` class or
                string name of backend.

        Returns:
            A new Session with the given ``session_id``

        """
        if backend:
            deprecate_arguments("backend", "0.15.0", "Sessions do not support multiple backends.")

        session = cls(service, backend)
        session._session_id = session_id
        return session

    def __enter__(self) -> "Session":
        set_cm_session(self)
        return self

    def __exit__(
        self,
        exc_type: Optional[Type[BaseException]],
        exc_val: Optional[BaseException],
        exc_tb: Optional[TracebackType],
    ) -> None:
        set_cm_session(None)
        self.close()


"""Module contains functionality that parses main page for RedHat vulnerabilities."""

import time

import lxml
import lxml.etree
from selenium import webdriver
from selenium.common.exceptions import WebDriverException, NoSuchElementException
from selenium.webdriver.chrome.options import Options
from cve_connector.vendor_cve.implementation.parsers.general_and_format_parsers\
    .html_parser import HtmlParser
from cve_connector.vendor_cve.implementation.utilities.utility_functions import string_to_date

class RedHatMainPageParser(HtmlParser):
    """
    Class providing functionality for parsing RedHat main page.
    """
    def __init__(self, url, logger, from_date=None, to_date=None):
        super().__init__(url, from_date, to_date)
        self.date_format = '%d %b %Y'  # 20 Apr 2018
        self.driver = None
        self.entry_added = False
        self.last_added = False
        self.first_cve_on_page = ''
        self.logger = logger
        try:
            self.load_content()
        except ValueError:
            self.logger.error('Unable to load content from {0}'.format(self.url))

    def get_content_from_ulr(self):
        """
        Gets and returns content from URL.

        :return: content
        """
        if not self.url:
            raise ValueError('Url must not be empty.')
        options = Options()
        options.add_argument('--headless')
        options.add_argument('--no-sandbox')

        driver = webdriver.Chrome(chrome_options=options)
        driver.get(self.url)
        driver.implicitly_wait(10)
        content = driver.page_source
        self.driver = driver
        return content

    def parse(self):
        """
        Provides parsing functionality.

        :return: None
        """
        try:
            loading = True
            while loading:
                loading = self.parse_current_page_content()
                if loading:
                    loaded = False
                    while not loaded:
                        loaded = self.load_next_page()
        except ValueError as val_err:
            self.logger.error('Error while parsing RH.')
            self.logger.error(val_err)
        finally:
            self.driver.close()

    def do_click(self):
        """
        Accomplishes clicking on a web page.

        :return: True if successful
        """
        try:
            elm = self.driver.find_element_by_link_text("›")
            elm.click()
            return True
        except WebDriverException:
            return False

    def load_next_page(self):
        """
        Load web page.

        :return: True if successful
        """
        driver = self.driver
        try:
            click = False
            start = time.time()
            end = time.time()
            while not click:
                if (end - start) > 120:
                    raise ValueError('RedHat page could not be loaded.')
                driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                click = self.do_click()
                self.logger.debug("CLICK in RedHat")
                end = time.time()

            time.sleep(3)
            content = driver.page_source
            self.data = lxml.etree.HTML(content.replace('<br>', ''))
            self.driver = driver
            return True
        except (NoSuchElementException, WebDriverException):
            return False

    def parse_current_page_content(self):
        """
        Parses current page in property data.

        :return: True if next page is needed to load, False otherwise.
        """
        table_rows = self.data.xpath(
            './/table[contains(@summary, "Common Vulnerabilities and Exposures")]//tbody/tr')
        for row in table_rows:
            url_list = row.xpath('.//th//a/@href')
            date_str_list = row.xpath('.//td//time/text()')
            if len(url_list) != 1 or len(date_str_list) != 1:
                raise ValueError('Format of data provided in RH table has changed.')

            date = string_to_date(date_str_list[0], self.date_format)
            if date < self.from_date:
                return False
            if self.from_date <= date <= self.to_date:
                self.entities.append(url_list[0])
                self.entry_added = True
                self.last_added = True
            else:
                self.last_added = False
            if not self.last_added and self.entry_added:
                return False
        return self.last_added or not self.entry_added


from tensorflow.keras import layers, models
import glob
import numpy as np
from PIL import Image
from sklearn.model_selection import train_test_split
from datetime import datetime

width = 75
height = 100
channel = 1

def load_data():
    images = np.array([]).reshape(0, height, width)
    labels = np.array([])

    dictionary = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10,
                  'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20,
                  'L': 21, 'M': 22, 'N': 23, 'P': 24, 'Q': 25, 'R': 26, 'S': 27, 'T': 28, 'U': 29, 'V': 30,
                  'W': 31, 'X': 32, 'Y': 33, 'Z': 34}

    directories = [directory for directory in glob.glob('datasets/BelgianLicencePlates/TrainLetters/*')]
    for directory in directories:
        file_list = glob.glob(directory + '/*.jpg')
        sub_images = np.array([np.array(Image.open(file_name)) for file_name in file_list])
        sub_labels = [dictionary[directory[-1]]] * len(sub_images)
        images = np.append(images, sub_images, axis=0)
        labels = np.append(labels, sub_labels, axis=0)
    x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42, shuffle=True)
    return (x_train, y_train), (x_test, y_test)

(train_images, train_labels), (test_images, test_labels) = load_data()
train_images = train_images.reshape((train_images.shape[0], height, width, channel))
test_images = test_images.reshape((test_images.shape[0], height, width, channel))
train_images, test_images = train_images / 255.0, test_images / 255.0
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(height, width, channel)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(35, activation='softmax'))
start = datetime.now().replace(microsecond=0)
model.summary()
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=8)
end = datetime.now().replace(microsecond=0)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy: ', test_acc)
print('Test loss: ', test_loss)
print('Training duration: ', (end - start))
model.save('models/character_recognition_cnn.h5')
print('> Saved model to disk <')


from django.conf import settings
from django.db.models import Q

from django_filters import rest_framework as filters

from ...models import Event

class EventFilterSet(filters.FilterSet):

    category = filters.MultipleChoiceFilter(
        method='filter_category',
        label='Category',
        choices=settings.EVENT_TYPE_CHOICES,
    )
    follow_type = filters.MultipleChoiceFilter(
        method='filter_follow_type',
        label='Follow Type',
        choices=settings.EVENT_FOLLOW_MODE_CHOICES,
    )

    languages = filters.CharFilter(
        method='filter_languages',
    )

    location = filters.CharFilter(
        method='filter_location',
    )

    class Meta:
        model = Event
        fields = [
            'category',
            'follow_type',
            'languages',
            'location'
        ]

    def filter_category(self, queryset, name, value):
        query = Q()
        for query_value in value:
            query |= Q(category__code=query_value)
        return queryset.filter(query)

    def filter_follow_type(self, queryset, name, value):
        query = Q()
        for query_value in value:
            query |= Q(follow_type__contains=query_value)
        return queryset.filter(query)

    def filter_languages(self, queryset, name, value):
        query = Q()
        for language in value.split(','):
            query |= Q(languages__contains=[language])
        return queryset.filter(query)

    def filter_location(self, queryset, name, value):
        query = Q()
        for location in value.split(','):
            query |= Q(location__icontains=location)

        return queryset.filter(query)


import cv2
import os
import numpy as np
import json
import mmcv
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import matplotlib.pyplot as plt
from glob import glob
import ast
from mmrotate.core import poly2obb_np

def poly2obb_np_oc(poly):
    """Convert polygons to oriented bounding boxes.

    Args:
        polys (ndarray): [x0,y0,x1,y1,x2,y2,x3,y3]

    Returns:
        obbs (ndarray): [x_ctr,y_ctr,w,h,angle]
    """
    bboxps = np.array(poly).reshape((4, 2))
    rbbox = cv2.minAreaRect(bboxps)
    x, y, w, h, a = rbbox[0][0], rbbox[0][1], rbbox[1][0], rbbox[1][1], rbbox[
        2]
    if w < 2 or h < 2:
        return
    while not 0 < a <= 90:
        if a == -90:
            a += 180
        else:
            a += 90
            w, h = h, w
    a = a / 180 * np.pi
    assert 0 < a <= np.pi / 2
    return x, y, w, h, a

def _get_adaptive_scales(areas, min_area=800, max_area=30000):
    """Get adaptive scales according to areas.

    The scale range is [0.5, 1.0]. When the area is less than
    ``'min_area'``, the scale is 0.5 while the area is larger than
    ``'max_area'``, the scale is 1.0.

    Args:
        areas (ndarray): The areas of bboxes or masks with the
            shape of (n, ).
        min_area (int): Lower bound areas for adaptive scales.
            Default: 800.
        max_area (int): Upper bound areas for adaptive scales.
            Default: 30000.

    Returns:
        ndarray: The adaotive scales with the shape of (n, ).
    """
    scales = 0.5 + (areas - min_area) / (max_area - min_area)
    scales = np.clip(scales, 0.5, 1.0)
    return scales

def draw_rbboxes(ax, bboxes, color='g', alpha=0.3, thickness=2):
    """Draw oriented bounding boxes on the axes.

    Args:
        ax (matplotlib.Axes): The input axes.
        bboxes (ndarray): The input bounding boxes with the shape
            of (n, 5).
        color (list[tuple] | matplotlib.color): the colors for each
            bounding boxes.
        alpha (float): Transparency of bounding boxes. Default: 0.8.
        thickness (int): Thickness of lines. Default: 2.

    Returns:
        matplotlib.Axes: The result axes.
    """
    polygons = []
    for i, bbox in enumerate(bboxes):
        xc, yc, w, h, ag = bbox[:5]
        wx, wy = w / 2 * np.cos(ag), w / 2 * np.sin(ag)
        hx, hy = -h / 2 * np.sin(ag), h / 2 * np.cos(ag)
        p1 = (xc - wx - hx, yc - wy - hy)
        p2 = (xc + wx - hx, yc + wy - hy)
        p3 = (xc + wx + hx, yc + wy + hy)
        p4 = (xc - wx + hx, yc - wy + hy)
        poly = np.int0(np.array([p1, p2, p3, p4]))
        polygons.append(Polygon(poly))
    p = PatchCollection(
        polygons,
        facecolor='none',
        edgecolors=color,
        linewidths=thickness,
        alpha=alpha)
    ax.add_collection(p)

    return ax

# arirang json to txt

ann_path = '/data/2_data_server/cv_data/arirang/validate_objects_labeling_json/'
# out_path = '/data/2_data_server/cv_data/arirang/val/annfiles/'

# ann_path = '/data/2_data_server/cv_data/arirang/validate_objects_labeling_json/'
label_test = []

for i in glob(ann_path+'*.json'):
    # print(i)
    with open(i) as f:
        json_data = json.load(f)
    img_id = json_data['features'][0]['properties']['image_id']
    for j in range(len(json_data['features'])):

        bbox_info = json_data['features'][j]['properties']['object_imcoords']
        bbox_info = ast.literal_eval(bbox_info)
        bbox_info = list(bbox_info)

        bbox_label = json_data['features'][j]['properties']['type_name'].replace(" ","-")
        bbox_id = json_data['features'][j]['properties']['type_id']

        if bbox_label == "military-aircraft":
            print(img_id)
            exit()
        if label_test == []:
            # label_test.append(bbox_id)
            label_test.append(bbox_label)
        if bbox_label not in label_test:
            # label_test.append(bbox_id)
            label_test.append(bbox_label)

        # first [:4] 지운 후
        # if j == 0:
        #     with open(out_path+img_id[:-4]+'.txt',"w") as (fw):
        #         for k in range(len(bbox_info)):
        #             fw.write(str(int(bbox_info[k])))
        #             fw.write(" ")
        #         # fw.write(bbox_info)
        #         # fw.write(" ")
        #         fw.write(bbox_label)
        #         fw.write(" ")
        #         fw.write("0\n")
        # else:
        #     with open(out_path+img_id[:-4]+'.txt',"a") as (fw):
        #         for k in range(len(bbox_info)):
        #             fw.write(str(int(bbox_info[k])))
        #             fw.write(" ")
        #         # fw.write(bbox_info)
        #         # fw.write(" ")
        #         fw.write(bbox_label)
        #         fw.write(" ")
        #         fw.write("0\n")

# aitod json to txt

# ann_path = '/data/2_data_server/cv_data/ai_todv2/aitodv2_train.json'
# out_path = '/data/2_data_server/cv_data/ai_todv2/train/annfiles/'

# # ann_path = '/data/2_data_server/cv_data/arirang/validate_objects_labeling_json/'
# label_test = []

# for i in glob(ann_path):
#     # print(i)
#     with open(i) as f:
#         json_data = json.load(f)

#     img_id = json_data['features'][0]['properties']['image_id']
#     for j in range(len(json_data['features'])):

#         bbox_info = json_data['features'][j]['properties']['object_imcoords']
#         bbox_info = ast.literal_eval(bbox_info)
#         bbox_info = list(bbox_info)

#         bbox_label = json_data['features'][j]['properties']['type_name'].replace(" ","-")
#         # bbox_id = json_data['features'][j]['properties']['type_id']
#         # if label_test == []:
#         #     # label_test.append(bbox_id)
#         #     label_test.append(bbox_label)
#         # if bbox_label not in label_test:
#         #     # label_test.append(bbox_id)
#         #     label_test.append(bbox_label)

#         # first [:4] 지운 후
#         if j == 0:
#             with open(out_path+img_id[:-4]+'.txt',"w") as (fw):
#                 for k in range(len(bbox_info)):
#                     fw.write(str(int(bbox_info[k])))
#                     fw.write(" ")
#                 # fw.write(bbox_info)
#                 # fw.write(" ")
#                 fw.write(bbox_label)
#                 fw.write(" ")
#                 fw.write("0\n")
#         else:
#             with open(out_path+img_id[:-4]+'.txt',"a") as (fw):
#                 for k in range(len(bbox_info)):
#                     fw.write(str(int(bbox_info[k])))
#                     fw.write(" ")
#                 # fw.write(bbox_info)
#                 # fw.write(" ")
#                 fw.write(bbox_label)
#                 fw.write(" ")
#                 fw.write("0\n")

# min,max 출력
# ann_path = '/data/2_data_server/cv_data/arirang_split/train_ms/annfiles/'

# num_min = 100000
# num_max = 0

# num_total = 0
# for i in glob(ann_path+'*.txt'):
#     # print(i)
#     num_lines = sum(1 for line in open(i))

#     num_min = min(num_lines, num_min)
#     num_max = max(num_lines, num_max)
#     if num_max == 1891:
#         print(i)
#         exit()

# print(num_min,num_max)

# # gt 개수
# ann_path = '/data/2_data_server/cv_data/arirang_split/val_ms/annfiles/'

# # CLASSES = ('small-ship', 'large-ship', 'civilian-aircraft', 'military-aircraft', 'small-car', 'bus', 'truck', 'train', 'crane', 'bridge',
# #             'oil-tank', 'dam', 'outdoor-playground', 'helipad', 'roundabout', 'indoor-playground','helicopter','individual-container','grouped-container','swimming-pool','etc')

# CLASSES = ('small-ship', 'large-ship', 'civilian-aircraft', 'military-aircraft', 'small-car', 'bus', 'truck', 'train')

# label_cnt = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
# cls_map = {c: i
#             for i, c in enumerate(CLASSES)
#             }

# for i in glob(ann_path+'*.txt'):
#     # print(i)
#     f = open(i,"r")
#     lines = f.readlines()
#     for line in lines:
#         label = line.split()
#         cls_name = label[8]
#         if cls_name == 'military-aircraft':
#             print(i)
#             exit()
#         # label = cls_map[cls_name]
#         # label_cnt[label] = label_cnt[label] + 1

# # print(label_cnt)

##### size check
# ann_path = '/data/2_data_server/cv_data/arirang/validate_objects_labeling_json/'
# # out_path = '/data/2_data_server/cv_data/arirang/val/annfiles/'

# # ann_path = '/data/2_data_server/cv_data/arirang/validate_objects_labeling_json/'
# label_test = []

# c1 = 0
# c2 = 0
# c3 = 0
# c4 = 0
# c5 = 0
# c6 = 0
# c7 = 0
# c8 = 0
# c9 = 0
# c10 = 0
# c11 = 0
# c12 = 0
# c13 = 0
# c14 = 0
# c15 = 0
# c16 = 0
# c17 = 0
# c18 = 0
# c19 = 0
# c20 = 0
# c21 = 0

# c1_num = 0
# c2_num = 0
# c3_num = 0
# c4_num = 0
# c5_num = 0
# c6_num = 0
# c7_num = 0
# c8_num = 0
# c9_num = 0
# c10_num = 0
# c11_num = 0
# c12_num = 0
# c13_num = 0
# c14_num = 0
# c15_num = 0
# c16_num = 0
# c17_num = 0
# c18_num = 0
# c19_num = 0
# c20_num = 0
# c21_num = 0

# for i in glob(ann_path+'*.json'):
#     # print(i)
#     with open(i) as f:
#         json_data = json.load(f)

#     for j in range(len(json_data['features'])):

#         bbox_info = json_data['features'][j]['properties']['object_imcoords']
#         bbox_info = ast.literal_eval(bbox_info)

#         poly = np.array(bbox_info,dtype=np.float32)

#         poly = poly2obb_np(poly)

#         if poly is not None:
#             w = poly[2]
#             h = poly[3]
#             area = w*h
#         # area =
#             bbox_label = json_data['features'][j]['properties']['type_name'].replace(" ","-")

#             if bbox_label =="small-ship":
#                 c1 += 1
#                 c1_num += area
#             if bbox_label =="large-ship":
#                 c2 += 1
#                 c2_num += area
#             if bbox_label =="civilian-aircraft":
#                 c3 += 1
#                 c3_num += area
#             if bbox_label =="military-aircraft":
#                 c4 += 1
#                 c4_num += area
#             if bbox_label =="small-car":
#                 c5 += 1
#                 c5_num += area
#             if bbox_label =="bus":
#                 c6 += 1
#                 c6_num += area
#             if bbox_label =="truck":
#                 c7 += 1
#                 c7_num += area
#             if bbox_label =="train":
#                 c8 += 1
#                 c8_num += area
#             if bbox_label =="crane":
#                 c9 += 1
#                 c9_num += area
#             if bbox_label =="bridge":
#                 c10 += 1
#                 c10_num += area
#             if bbox_label =="oil-tank":
#                 c11 += 1
#                 c11_num += area
#             if bbox_label =="dam":
#                 c12 += 1
#                 c12_num += area
#             if bbox_label =="outdoor-playground":
#                 c13 += 1
#                 c13_num += area
#             if bbox_label =="helipad":
#                 c14 += 1
#                 c14_num += area
#             if bbox_label =="roundabout":
#                 c15 += 1
#                 c15_num += area
#             if bbox_label =="indoor-playground":
#                 c16 += 1
#                 c16_num += area
#             if bbox_label =="helicopter":
#                 c17 += 1
#                 c17_num += area
#             if bbox_label =="individual-container":
#                 c18 += 1
#                 c18_num += area
#             if bbox_label =="grouped-container":
#                 c19 += 1
#                 c19_num += area
#             if bbox_label =="swimming-pool":
#                 c20 += 1
#                 c20_num += area

# print("c1------")
# print(c1,c1_num)
# print("------")
# print("c2------")
# print(c2,c2_num)
# print("------")
# print("c3------")
# print(c3,c3_num)
# print("------")
# print("c4------")
# print(c4,c4_num)
# print("------")
# print("c5------")
# print(c5,c5_num)
# print("------")
# print("c6------")
# print(c6,c6_num)
# print("------")
# print("c7------")
# print(c7,c7_num)
# print("------")
# print("c8------")
# print(c8,c8_num)
# print("------")
# print("c9------")
# print(c9,c9_num)
# print("------")
# print("c10------")
# print(c10,c10_num)
# print("------")
# print("c11------")
# print(c11,c11_num)
# print("------")
# print("c12------")
# print(c12,c12_num)
# print("------")
# print("c13------")
# print(c13,c13_num)
# print("------")
# print("c14------")
# print(c14,c14_num)
# print("------")
# print("c15------")
# print(c15,c15_num)
# print("------")
# print("c16------")
# print(c16,c16_num)
# print("------")
# print("c17------")
# print(c17,c17_num)
# print("------")
# print("c18------")
# print(c18,c18_num)
# print("------")
# print("c19------")
# print(c19,c19_num)
# print("------")
# print("c20------")
# print(c20,c20_num)
# print("------")


# -*- coding: utf-8 -*-
"""
Created on Fri Nov 27 07:57:10 2020

@author: KANNAN
"""

from flask import Flask, render_template, request
import pandas as pd
#import sklearn
import pickle

model = pickle.load(open("flight_rf.pkl", "rb"))

app = Flask(__name__)

@app.route('/')

def home():
    return render_template('Airlines.html')

@app.route('/Predict', methods = ["GET", "POST"])

def Predict():
    if request.method == "POST":
        #Date of Journey
        # Departure DMY
        date_dep = request.form["Departure_Date"]
        Dep_day = int(pd.to_datetime(date_dep, format="%Y-%m-%dT%H:%M").day)
        Dep_month = int(pd.to_datetime(date_dep, format="%Y-%m-%dT%H:%M").month)
        Dep_year = int(pd.to_datetime(date_dep, format="%Y-%m-%dT%H:%M").year)
        # Departure Time
        Dep_hour = int(pd.to_datetime(date_dep, format="%Y-%m-%dT%H:%M").hour)
        Dep_minute = int(pd.to_datetime(date_dep, format="%Y-%m-%dT%H:%M").minute)

        date_arrival = request.form["Arrival_Date"]
        #Arrival DMY
        Arrival_day = int(pd.to_datetime(date_arrival, format="%Y-%m-%dT%H:%M").day)
        Arrival_month = int(pd.to_datetime(date_arrival, format="%Y-%m-%dT%H:%M").month)
        Arrival_year = int(pd.to_datetime(date_arrival, format="%Y-%m-%dT%H:%M").year)
        # Arrival Time
        Arrival_hour = int(pd.to_datetime(date_arrival, format="%Y-%m-%dT%H:%M").hour)
        Arrival_minute = int(pd.to_datetime(date_arrival, format="%Y-%m-%dT%H:%M").minute)

        # Duration in hrs
        from datetime import datetime
        dep_date = datetime(Dep_year,Dep_month,Dep_day,Dep_hour,Dep_minute)
        arrival_date = datetime(Arrival_year,Arrival_month,Arrival_day,Arrival_hour,Arrival_minute)
        diff = arrival_date - dep_date
        t = pd.to_datetime(diff,format="%H:%M:%S")
        duration_hour = t.hour
        duration_minute = t.minute

        # Source
        source = request.form["Source"]
        if (source == 'New Delhi'):
            s_New_Delhi = 1
            s_Kolkata = 0
            s_Mumbai = 0
            s_Chennai = 0

        elif (source == 'Kolkata'):
            s_New_Delhi = 0
            s_Kolkata = 1
            s_Mumbai = 0
            s_Chennai = 0

        elif (source == 'Mumbai'):
            s_New_Delhi = 0
            s_Kolkata = 0
            s_Mumbai = 1
            s_Chennai = 0

        elif (source == 'Chennai'):
            s_New_Delhi = 0
            s_Kolkata = 0
            s_Mumbai = 0
            s_Chennai = 1

        else:
            s_New_Delhi = 0
            s_Kolkata = 0
            s_Mumbai = 0
            s_Chennai = 0

        # Destination
        destination = request.form["Destination"]
        if (destination == 'Cochin'):
            d_Cochin = 1
            d_New_Delhi = 0
            d_Hyderabad = 0
            d_Kolkata = 0

        elif (destination == 'New Delhi'):
            d_Cochin = 0
            d_New_Delhi = 1
            d_Hyderabad = 0
            d_Kolkata = 0

        elif (destination == 'Hyderabad'):
            d_Cochin = 0
            d_New_Delhi = 0
            d_Hyderabad = 1
            d_Kolkata = 0

        elif (destination == 'Kolkata'):
            d_Cochin = 0
            d_New_Delhi = 0
            d_Hyderabad = 0
            d_Kolkata = 1

        else:
            d_Cochin = 0
            d_Delhi = 0
            d_New_Delhi = 0
            d_Hyderabad = 0
            d_Kolkata = 0

        # Airline
        airline = request.form["Airline"]

        if(airline=='Jet Airways'):
            Jet_Airways = 1
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='IndiGo'):
            Jet_Airways = 0
            IndiGo = 1
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='Air India'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 1
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='Multiple carriers'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 1
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='SpiceJet'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 1
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='Vistara'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 1
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='GoAir'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 1
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='Multiple carriers Premium economy'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 1
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='Jet Airways Business'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 1
            Vistara_Premium_economy = 0
            Trujet = 0

        elif (airline=='Vistara Premium economy'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 1
            Trujet = 0

        elif (airline=='Trujet'):
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 1

        else:
            Jet_Airways = 0
            IndiGo = 0
            Air_India = 0
            Multiple_carriers = 0
            SpiceJet = 0
            Vistara = 0
            GoAir = 0
            Multiple_carriers_Premium_economy = 0
            Jet_Airways_Business = 0
            Vistara_Premium_economy = 0
            Trujet = 0

        # Total_Stops
        stops = int(request.form["Total_Stops"])

        prediction=model.predict([[
            stops,
            Dep_day,
            Dep_month,
            Dep_hour,
            Dep_minute,
            Arrival_hour,
            Arrival_minute,
            duration_hour,
            duration_minute,
            Air_India,
            GoAir,
            IndiGo,
            Jet_Airways,
            Jet_Airways_Business,
            Multiple_carriers,
            Multiple_carriers_Premium_economy,
            SpiceJet,
            Trujet,
            Vistara,
            Vistara_Premium_economy,
            s_Chennai,
            s_Kolkata,
            s_Mumbai,
            s_New_Delhi,
            d_Cochin,
            d_Hyderabad,
            d_Kolkata,
            d_New_Delhi
        ]])

        output=round(prediction[0],2)

        return render_template('Airlines.html',prediction_text = "Your Flight Fare is {} INR".format(output))

    return render_template('Airlines.html')

if __name__ == '__main__':
    app.run()


import matplotlib.pyplot as plt
from pymol import cmd
from multiprocessing import Pool, cpu_count
from tqdm import tqdm  # Import tqdm for progress bar

# Define a function to calculate RMSD for a single frame
def calculate_rmsd(frame_number, object_1, reference_object):
    cmd.frame(frame_number)
    #cmd.align(object_1, reference_object, frame_number, 1)
    return cmd.rms_cur(object_1, reference_object, frame_number, 0)

# Define the process_frame function outside of RMSD_vs_frame
def process_frame(args):
    return calculate_rmsd(*args)

# Define a function to plot RMSD vs frame number
def RMSD_vs_frame(object_1, stride=1, num_process=1):
    # Initialize an empty list to store RMSDs
    RMSDs = []
    reference_object = 'ref'
    cmd.create(reference_object, object_1, 1, 0)

    # Calculate total number of frames
    total_frames = cmd.count_states(object_1)

    # Set default number of processes if not specified
    if num_process == 1:
        num_process = min(cpu_count(), total_frames)  # Use the number of CPU cores or total_frames, whichever is smaller

    # Create a list of frame numbers to process
    frames_to_process = range(1, total_frames + 1, stride)

    # Create a list of arguments for the process_frame function
    args_list = [(frame_number, object_1, reference_object) for frame_number in frames_to_process]

    # Create a pool of workers for multiprocessing
    with Pool(processes=num_process) as pool, tqdm(total=len(args_list), desc="Calculating RMSD") as pbar:
        # Use multiprocessing to calculate RMSD for each frame
        for rmsd in pool.imap_unordered(process_frame, args_list):
            RMSDs.append(rmsd)
            pbar.update(1)  # Update the progress bar

    # Create x and y data for the plot
    x1 = frames_to_process    # Frame numbers will be on the x-axis
    y1 = RMSDs                # RMSDs will be on the y-axis

    # Plot the data
    plt.figure()
    plt.plot(x1, y1, label=object_1, color='blue', linewidth=2)
    plt.xlabel('Frame', fontsize=12)
    plt.ylabel('Distance', fontsize=12)
    plt.title('Distance vs Frame', fontsize=16)
    plt.legend(fontsize=10)
    plt.grid(True, linestyle='--', linewidth=0.5, alpha=0.7)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tight_layout()  # Adjust the spacing for better layout
    plt.show()  # Display the plot

# Usage: RMSD_vs_frame('object_name', stride=1, num_process=1)
print("USAGE: RMSD_vs_frame('protein selection', stride=5, num_process=4)")


import discord
from discord.ext import commands
from datetime import datetime
from fun_config import *

class Work(commands.Cog):

    def __init__(self, client: commands.Bot):
        self.client = client

    @commands.command()
    @commands.cooldown(1, 86400, commands.BucketType.user)
    async def sleep(self, ctx):
        # Get the data as users
        users = await get_inventory_data()
        # Check the user's max energy
        max_energy = int(users[str(ctx.author.id)]['Stats']['MaxEnergy'])
        users[str(ctx.author.id)]['Stats']['Energy'] = max_energy

        with open(inventory_json_file, "w") as json_file:
            json.dump(users, json_file, indent=1)

        await ctx.reply("You slept and refilled your energy!")

async def setup(client: commands.Bot) -> None:
    await client.add_cog(Work(client))


# -*- coding: utf-8 -*-
"""
Created on Mon Jan 13 18:52:23 2020

@author: Hasnain Khan
"""

import numpy as np
import cv2
from matplotlib import pyplot as plt

# Convolv function for convolving the kernel with real image matrix
def convolve_np(image, kernel):
    X_height = image.shape[0]
    X_width = image.shape[1]

    F_height = kernel.shape[0]
    F_width = kernel.shape[1]

    H = int((F_height - 1) / 2)
    W = int((F_width - 1) / 2)

    out = np.zeros((X_height, X_width))

    for i in np.arange(H, X_height-H):
        for j in np.arange(W, X_width-W):
            sum = 0
            for k in np.arange(-H, H+1):
                for l in np.arange(-W, W+1):
                    a = image[i+k, j+l]
                    w = kernel[H+k, W+l]
                    sum += (w * a)
            out[i,j] = sum

    return out # Returning the Convolved Image

def box_blur():
    img = cv2.imread('Lenna.png', 0)

    # Sobel Operator for Horizontal Edge Detection
    Hx = np.array([[-1, 0, 1],
                   [-2, 0, 2],
                   [-1, 0, 1]])

    # Sobel Operator for Vertical Edge Detection
    Hy = np.array([[-1, -2, -1],
                   [0, 0, 0],
                   [1, 2, 1]])

    img_x = convolve_np(img, Hx) / 8.0 # Output of Sobel Horizontal
    img_y = convolve_np(img, Hy) / 8.0 # Output of Sobel Vertical

if __name__ == '__main__':
    box_blur()


import numpy as np
import plotly.offline as ply
import plotly.graph_objs as go
from scipy import stats as st
from scipy import signal as sg
import source_localization as src

file_name = "/home/dima/Projects/Touchdown/Data/test.bin"
ch_total = 6
length = 2**15 + 100000
fs = 100e6
t_pulse_width = 100e-6
t_period = 200e-6
snr = 1000000  # dB
start_period = 1
f0 = 50e6
post_id = 0
serial = 0

y = st.norm.rvs(size=(ch_total, length), scale=10**(-snr/20)) +\
    1j * st.norm.rvs(size=(ch_total, length), scale=10**(-snr/20))

frame = src.SpecFrame(post_id=post_id, serial=serial, f0=f0, fs=fs, f_res=fs/length, sig=y)

writer = src.SpecFrameSaver(post_id)
writer.open(file_name)
writer.save_title(start_period)
writer.save(frame)
writer.close()

print("Successfully saved frame")

loader = src.SpecFrameLoader(post_id)
success = loader.open(file_name)

if not success:
    print("Enable to open file")
    exit(1)

i_start_period = loader.read_title()
i_frame = src.SpecFrame()
loader.load(i_frame)
loader.close()

error = 1e-7
failed = False
if abs(start_period - i_start_period) > error:
    print("\n\n\nTest failed. \nSaved start period was: ", start_period, "\nLoaded start period is: ", i_start_period)
    failed = True

if post_id is not i_frame.get_post_id():
    print("\n\n\nTest failed. \nSaved post id was: ", post_id, "\nLoaded post id is: ", i_frame.get_post_id())
    failed = True

if serial is not i_frame.get_serial():
    print("\n\n\nTest failed. \nSaved serial was: ", serial, "\nLoaded serial is: ", i_frame.get_serial())
    failed = True

if abs(f0 - i_frame.get_central_frequency()) > error:
    print("\n\n\nTest failed. \nSaved central frequency was: ", f0, "\nLoaded central frequency is: ",
          i_frame.get_central_frequency())
    failed = True

if abs(fs - i_frame.get_sampling_frequency()) > error:
    print("\n\n\nTest failed. \nSaved sampling frequency was: ", fs, "\nLoaded sampling frequency is: ",
          i_frame.get_sampling_frequency())
    failed = True

if abs(fs/length - i_frame.get_frequency_resolution()) > error:
    print("\n\n\nTest failed. \nSaved frequency resolution was: ", fs/length, "\nLoaded frequency resolution is: ",
          i_frame.get_frequency_resolution())
    failed = True

data = i_frame.get_data()

if ch_total is not data.shape[0]:
    print("\n\n\nTest failed. \nSaved channels total was: ", ch_total, "\nLoaded channels total is: ",
         data.shape[0])
    failed = True

if abs(length  - data.shape[1]) > error:
    print("\n\n\nTest failed. \nSaved samples per channel was: ", length, "\nLoaded samples per channel is: ",
         data.shape[1])
    failed = True

for ch in range(ch_total):
    for s in range(length):
        if abs((data[ch, s]) - y[ch, s]) > error:
            print("\n\n\nTest failed. \nData mismatch. \nChannel: ", ch, "\nSample: ", s)
            failed = True

if not failed:
    print("Test passed successfully")


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from models import Company

def index(request):
    companies = Company.objects.all().order_by('name')
    paginator = Paginator(companies, 10)
    if request.method == 'POST' and request.POST.get('search'):
        companies = companies.filter(name__icontains=request.POST.get('search'))
        return render(request, 'index.html', {'data': companies})
    else:
        page = request.GET.get('page')
        try:
            companies = paginator.page(page)
        except PageNotAnInteger:
            companies = paginator.page(1)
        except EmptyPage:
            companies = paginator.page(paginator.num_pages)
        return render(request, 'index.html', {'data': companies})


import datetime, requests, csv, argparse

class MarketwatchScraper():
    def __init__(self, stock: str = "AAPL", timeout: int = 1) -> None:
        self.stock = stock
        self.timeout = timeout
        pass

    def scrape(self) -> None:
        self.saveToFile(self.getURLS())

    def saveToFile(self, urls: list) -> None:
        localFile = f"{self.stock.lower()}.csv"
        with open(localFile, "w") as f:
            f.write("Date,Open,High,Low,Close,Volume\n")
            f.close()
        for url in urls:
            print(f"Getting data from url {url}...")

            try:
                resp = requests.get(url, timeout=self.timeout, headers={
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 OPR/96.0.0.0 (Edition std-1)"
                })
                if resp.status_code != 200:
                    print(f"Error! Status code: {resp.status_code}")
                    continue
            except Exception as e:
                print(f"Error! Exception: {e}")
                continue

            data = resp.content.decode("utf-8")
            csvData = csv.reader(data.splitlines(), delimiter=",")
            next(csvData)
            with open(localFile, "a") as f:
                print(f"Writing data to {localFile}...")
                writer = csv.writer(f)
                for row in csvData:
                    writer.writerow(row)
                f.close()

    def getURLS(self) -> list:
        urls = []
        startDate = datetime.datetime(1970, 1, 1)
        endDate = datetime.datetime.today()
        if endDate > datetime.datetime.today():
            endDate = datetime.datetime.today()

        while startDate < endDate:
            date1 = startDate.strftime("%m/%d/%Y%%2000:00:00")
            date2 = (startDate + datetime.timedelta(days=366)).strftime("%m/%d/%Y%%2000:00:00")
            url = f"https://www.marketwatch.com/investing/stock/{self.stock}/downloaddatapartial?startdate={date1}&enddate={date2}&daterange=d30&frequency=p1d&csvdownload=true&downloadpartial=false&newdates=false"
            print(f"Added URL for {startDate.strftime('%m/%d/%Y')} to {(startDate + datetime.timedelta(days=366)).strftime('%m/%d/%Y')}")
            urls.append(url)
            startDate = startDate + datetime.timedelta(days=365)

        return urls

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--stock", type=str, required=True)
    parser.add_argument("--timeout", type=int, required=False)
    args = parser.parse_args()

    scraper = MarketwatchScraper(stock=args.stock, timeout=args.timeout if args.timeout else 1)
    scraper.scrape()


#!/usr/bin/env python3

import os
import re
import click
import json
import logging
import zipfile
import portalocker
import contextlib
import traceback
import imghdr
import multiprocessing
import functools
import threading
import time
import sys
import ctypes
import psutil

from pathlib import Path
from functools import partial
from tqdm import tqdm
from atomicwrites import atomic_write
from contextlib import contextmanager, nullcontext
from functools import partial

from origami.core.time import elapsed_timer
from origami.batch.core.io import *
from origami.batch.core.utils import Spinner
from origami.batch.core.mutex import DatabaseMutex, FileMutex, DummyMutex

def qt_app():
	try:
		from PySide2 import QtGui
	except ImportError:
		from PySide6 import QtGui

	os.environ["QT_QPA_PLATFORM"] = "offscreen"
	return QtGui.QGuiApplication()

class WatchdogState(enum.Enum):
	RUNNING = 0
	DONE = 1
	CANCEL = 2

class StopWatch:
	def __init__(self):
		self._last_reset = time.time()

	def reset(self):
		self._last_reset = time.time()

	@property
	def age(self):
		return time.time() - self._last_reset

class SharedMemoryStopWatch:
	def __init__(self):
		self._shared = multiprocessing.Value('L', int(time.time()))

	def reset(self):
		with self._shared.get_lock():
			self._shared.value = int(time.time())

	@property
	def age(self):
		with self._shared.get_lock():
			return time.time() - self._shared.value

WorkSetEntry = collections.namedtuple(
	'WorkSetEntry', ['path', 'pid', 'age'])

class SharedMemoryWorkSet:
	def __init__(self, access, n):
		assert n >= 1

		self._array = multiprocessing.Array(
			ctypes.c_int64, n * 4)

		# each slot has 4 integer entries:
		# 0: value
		# 1: pid
		# 2: timestamp
		# 3: not used

		self._n = n
		for i in range(self._n * 4):
			self._array[i] = -1

		self._access = access

	def _cleanup(self):
		with self._array.get_lock():
			for i in range(self._n):
				pid = self._array[4 * i + 1]
				if pid >= 0 and not psutil.pid_exists(pid):
					logging.warning(f"removing killed pid {pid} from work set.")
					self._array[4 * i] = -1
					self._array[4 * i + 1] = -1
					self._array[4 * i + 2] = -1

	def add(self, value):
		assert value >= 0
		with self._array.get_lock():
			self._cleanup()

			free = None
			for i in range(self._n):
				if self._array[4 * i] == value:
					return
				elif free is None and self._array[4 * i] < 0:
					free = i

			if free is None:
				raise RuntimeError(
					f"no free slots for adding {value}, pid {os.getpid()}: {self.active}")

			self._array[4 * free] = value
			self._array[4 * free + 1] = int(os.getpid())
			self._array[4 * free + 2] = int(time.time())

	def remove(self, value):
		assert value >= 0
		with self._array.get_lock():
			found = None
			for i in range(self._n):
				if self._array[4 * i] == value:
					found = i
					break
			assert found is not None
			self._array[4 * found] = -1
			self._array[4 * found + 1] = -1
			self._array[4 * found + 2] = -1

	@property
	def active(self):
		result = []
		with self._array.get_lock():
			self._cleanup()

			for i in range(self._n):
				if self._array[4 * i] >= 0:
					result.append(WorkSetEntry(
						path=self._access(self._array[4 * i]),
						pid=self._array[4 * i + 1],
						age=int(time.time() - self._array[4 * i + 2])))
		return result

	def print(self):
		active = self.active
		if active:
			logging.error(f"{len(active)} entries in work set:")
			for i, entry in enumerate(active):
				logging.error(f"  ({i + 1}) {entry}]")
		else:
			logging.error("no entries in work set.")

global global_stop_watch
global_stop_watch = SharedMemoryStopWatch()

# global_stop_watch needs to be global indeed, as pickling
# over the fork in imap_unordered will not work otherwise.

global global_work_set

class Watchdog(threading.Thread):
	def __init__(self, pool, stop_watch, work_set, timeout):
		threading.Thread.__init__(self)
		self._pool = pool
		self._timeout = timeout
		self._stop_watch = stop_watch
		self._work_set = work_set
		self._state = WatchdogState.RUNNING
		self._cond = threading.Condition()
		stop_watch.reset()

	def _print_work_set(self):
		self._work_set.print()

	def _cancel(self):
		if self._state != WatchdogState.CANCEL:
			logging.error("no new results after %d s. stopping." % self._stop_watch.age)
			self._print_work_set()
			self._state = WatchdogState.CANCEL
			self._pool.terminate()
			t = threading.Thread(target=lambda: self._pool.join(), args=())
			t.start()
			self._stop_watch.reset()
		elif self._state == WatchdogState.CANCEL:
			logging.error("stopping failed. killing process.")
			self._print_work_set()
			os._exit(1)

	def run(self):
		with self._cond:
			while True:
				self._cond.wait(
					max(0, self._timeout - self._stop_watch.age))

				if self._state == WatchdogState.DONE:
					break

				if self._stop_watch.age > self._timeout:
					self._cancel()

	def set_is_done(self):
		with self._cond:
			if self._state == WatchdogState.RUNNING:
				self._state = WatchdogState.DONE
				self._cond.notify()

	def is_cancelled(self):
		return self._state == WatchdogState.CANCEL

def chunks(items, n):
	for i in range(0, len(items), n):
		yield items[i:i + n]

class Processor:
	def __init__(self, options, needs_qt=False):
		self._overwrite = options.get("overwrite", False)
		self._processes = options.get("processes", 1)
		self._timeout = options.get("alive", 600)
		self._name = options.get("name", "")
		self._verbose = False

		self._lock_strategy = options.get("lock_strategy", "DB")
		self._lock_level = options.get("lock_level", "PAGE")
		self._lock_timeout = options.get("lock_timeout", "60")
		self._max_lock_age = options.get("max_lock_age")
		self._lock_chunk_size = 25
		self._mutex = None

		if self._lock_strategy == "DB":
			self._lock_database = options.get("lock_database")
		elif self._lock_strategy in ("FILE", "NONE"):
			pass
		else:
			raise ValueError(self._lock_strategy)

		if needs_qt:
			self._qt_app = qt_app()
			if self._processes > 1:
				logging.warning(
					"this batch does not support multiple processes.")
				self._processes = 1  # cannot safely fork here.
		else:
			self._qt_app = None

		if options.get("profile"):
			from profiling.sampling import SamplingProfiler
			self._profiler = SamplingProfiler()
			self._overwrite = True  # profile implies overwrite
		else:
			self._profiler = None

		self._print_paths = False
		self._plain = options.get("plain")
		if self._plain:
			self._print_paths = True

		self._debug_write = options.get("debug_write", False)
		self._track_changes = options.get("track_changes", False)

	@staticmethod
	def options(f):
		options = [
			click.option(
				'--processes',
				type=int,
				default=1,
				help="Number of parallel processes to employ."),
			click.option(
				'--alive',
				type=int,
				default=600,
				help="Seconds to wait after inactive process is killed."),
			click.option(
				'--name',
				type=str,
				default="",
				help="Only process paths that conform to the given pattern."),
			click.option(
				'--lock-strategy',
				type=click.Choice(['FILE', 'DB', 'NONE'], case_sensitive=False),
				default="DB",
				help="How to implement locking for concurrency."),
			click.option(
				'--lock-level',
				type=click.Choice(['PAGE', 'TASK'], case_sensitive=False),
				default="PAGE",
				help="Lock granularity."),
			click.option(
				'--lock-database',
				type=click.Path(),
				required=False,
				help="Mutex database path used for concurrent processing"),
			click.option(
				'--lock-timeout',
				type=int,
				default=60,
				required=False,
				help="Seconds to wait to acquire locking. NFS volumes might need high values."),
			click.option(
				'--max-lock-age',
				type=int,
				default=600,
				required=False,
				help="Maximum age of a lock in seconds until it is considered invalid."),
			click.option(
				'--overwrite',
				is_flag=True,
				default=False,
				help="Recompute and overwrite existing result files."),
			click.option(
				'--profile',
				is_flag=True,
				default=False,
				help="Enable profiling and show results."),
			click.option(
				'--plain',
				is_flag=True,
				default=False,
				help="Print plain output that is friendly to piping."),
			click.option(
				'--debug-write',
				is_flag=True,
				default=False,
				help="Debug which files are written."),
			click.option(
				'--track-changes',
				type=str,
				default="",
				help="Recompute files and track changes with given tag.")
		]
		return functools.reduce(lambda x, opt: opt(x), options, f)

	@property
	def processor_name(self):
		return self.__class__.__name__

	def is_image(self, path):
		# imghdr might be the perfect tool for this, but
		# it fails to detect some valid images. so we go
		# with extenstions for the most part.
		# see https://stackoverflow.com/questions/36870661/
		# imghdr-python-cant-detec-type-of-some-images-image-extension

		if path.suffix.lower() in (".jpg", ".png", ".tif", ".tiff"):
			return True

		return imghdr.what(path) is not None

	def should_process(self, page_path):
		return True

	def prepare_process(self, page_path):
		artifacts = self.artifacts()

		if self._track_changes:
			file_writer = TrackChangeWriter(self._track_changes)
		else:
			file_writer = AtomicFileWriter(overwrite=self._overwrite)
			if self._debug_write:
				file_writer = DebuggingFileWriter(file_writer)

		kwargs = dict()
		for arg, spec in artifacts:
			f = spec.instantiate(
				page_path=page_path,
				processor=self,
				file_writer=file_writer)

			f.fix_inconsistent()

			if not f.is_ready():
				if self._verbose:
					print("skipping %s: missing %s" % (page_path, f.missing))
				return False

			kwargs[arg] = f

		return kwargs

	def _trigger_process1(self, p, kwargs, locked):
		work = locked

		if not locked:
			logging.warning(f"failed to obtain lock for {p}. ignoring.")

		try:
			if work:
				# a concurrent worker might already have done this.
				for f in kwargs.values():
					if not f.is_ready():
						work = False
						break

			if work:
				with elapsed_timer() as elapsed:
					data_path = find_data_path(p)
					data_path.mkdir(exist_ok=True)

					runtime_info = self.process(p, **kwargs)

				if runtime_info is None:
					runtime_info = dict()
				runtime_info["status"] = "COMPLETED"
				runtime_info["elapsed"] = round(elapsed(), 2)

				self._update_runtime_info(
					p, {self.processor_name: runtime_info})

		except KeyboardInterrupt:
			logging.exception("Interrupted at %s." % p)
			raise

		except:
			logging.exception("Failed to process %s." % p)
			runtime_info = dict(
				status="FAILED",
				traceback=traceback.format_exc())
			self._update_runtime_info(p, {
				self.processor_name: runtime_info})

		finally:
			# free memory allocated in cached io.Reader
			# attributes. this can get substantial for
			# long runs.
			kwargs.clear()

	def _trigger_process(self, chunk):
		if self._lock_level == "PAGE":
			lock_actor_name = "page"
		elif self._lock_level == "TASK":
			lock_actor_name = self.processor_name
		else:
			raise ValueError(self._lock_level)

		with self._mutex.lock(
			lock_actor_name,
			[str(p) for _, p, _ in chunk]) as locked:

			for i, p, kwargs in chunk:
				global_work_set.add(i)
				try:
					self._trigger_process1(p, kwargs, locked)
				finally:
					global_work_set.remove(i)
				yield i, p

	def _trigger_process_async(self, chunk):
		results = []
		for i, p in self._trigger_process(chunk):
			results.append((i, p))
			global_stop_watch.reset()
		return results

	def _process_queue(self, queued):
		global global_work_set
		global_work_set = SharedMemoryWorkSet(
			lambda i: queued[i][1], max(1, self._processes))

		with self._profiler or nullcontext():
			chunked_queue_gen = chunks(queued, self._lock_chunk_size)

			def iprogress(i):
				nd = len(str(len(queued)))
				return f"[{str(i + 1).rjust(nd)} / {len(queued)}]"

			if self._processes > 1:
				with multiprocessing.Pool(self._processes, maxtasksperchild=4) as pool:
					watchdog = Watchdog(
						pool=pool,
						stop_watch=global_stop_watch,
						work_set=global_work_set,
						timeout=self._timeout)
					watchdog.start()

					with tqdm(total=len(queued), disable=self._print_paths) as progress:
						for chunk in pool.imap_unordered(
							self._trigger_process_async, chunked_queue_gen):

							if self._print_paths:
								for i, p in chunk:
									print(f"{iprogress(i)} {p}", flush=True)
							else:
								progress.update(len(chunk))

							global_stop_watch.reset()

				if watchdog.is_cancelled():
					watchdog.kill()
					sys.exit(1)
				else:
					watchdog.set_is_done()
			else:
				with tqdm(total=len(queued), disable=self._print_paths) as progress:
					for chunk in chunked_queue_gen:
						for i, p in self._trigger_process(chunk):
							if self._print_paths:
								print(f"{iprogress(i)} {p}", flush=True)
							else:
								progress.update(1)

	def _build_queue(self, path):
		path = Path(path)
		if not path.exists():
			raise FileNotFoundError("%s does not exist." % path)

		queued = []
		counts = dict(images=0)

		def add_path(p):
			if not p.exists():
				print("skipping %s: path does not exist." % p)
				return

			if self._name and not re.search(self._name, str(p)):
				return

			if not self.is_image(p):
				if self._verbose:
					print("skipping %s: not an image." % p)
				return

			counts['images'] += 1

			if not self.should_process(p):
				if self._verbose:
					print("skipping %s: should_process is False" % p)
				return

			kwargs = self.prepare_process(p)
			if kwargs is not False:
				queued.append((len(queued), p, kwargs))

		if not path.is_dir():
			if path.suffix == ".txt":
				with open(path, "r") as f:
					for line in f:
						line = line.strip()
						if line:
							add_path(Path(line))
			else:
				raise FileNotFoundError(
					"%s is not a valid path or text file of paths." % path)
		else:
			print(f"scanning {path}... ", flush=True, end="")

			with Spinner(disable=self._plain):
				for folder, dirs, filenames in os.walk(path):
					folder = Path(folder)
					if folder.name.endswith(".out"):
						dirs.clear()
						continue
					else:
						dirs.sort()

						for filename in sorted(filenames):
							add_path(folder / filename)

			print("done.", flush=True)
			print(f"{counts['images']} documents found, {len(queued)} ready to process.")

		return queued

	def traverse(self, path: Path):
		print(f"running {self.processor_name}.", flush=True)

		queued = self._build_queue(path)

		if self._lock_strategy == "DB":
			if self._lock_database:
				db_path = Path(self._lock_database)
			elif Path(path).is_dir():
				db_path = Path(path) / "origami.lock.db"
			else:
				db_path = Path(path).parent / "origami.lock.db"

			self._mutex = DatabaseMutex(
				db_path, timeout=self._lock_timeout)

			self._mutex.clear_locks(self._max_lock_age)

		elif self._lock_strategy == "FILE":
			self._mutex = FileMutex()

		elif self._lock_strategy == "NONE":
			self._mutex = DummyMutex()

		else:
			raise ValueError(self._lock_strategy)

		try:
			self._process_queue(queued)
		finally:
			self._mutex = None

		if self._profiler:
			self._profiler.run_viewer()

	def process(self, p: Path):
		pass

	def lock_or_open(self, path, mode):
		if self._lock_strategy == "FILE":
			return portalocker.Lock(
				path,
				mode,
				flags=portalocker.LOCK_EX,
				timeout=1,
				fail_when_locked=True)
		else:
			return open(path, mode)

	def _update_json(self, page_path, artifact, updates):
		try:
			data_path = find_data_path(page_path)
			json_path = data_path / artifact.filename()

			new_json_path = json_path.parent / (
				json_path.stem + ".updated" + json_path.suffix)
			if new_json_path.exists():
				os.remove(new_json_path)

			if json_path.exists():
				with open(json_path, "r") as f:
					file_data = f.read()
					data = json.loads(file_data)
			else:
				data = dict()

			for k, v in updates.items():
				if v is None:
					del data[k]
				else:
					data[k] = v

			with open(new_json_path, "w") as f:
				json.dump(data, f)

			if json_path.exists():
				os.remove(json_path)
			os.rename(new_json_path, json_path)

		except:
			logging.error(traceback.format_exc())

	def _update_runtime_info(self, page_path, updates):
		self._update_json(page_path, Artifact.RUNTIME, updates)


import sys
import numpy as np
import pyautogui
import win32api, win32con, win32gui
import cv2
import time
import torch
import time

class_names = [ 'counter-terrorist', 'terrorist' ]
opponent = 'terrorist'
opponent_color = (255, 0, 0)
ally_color = (0, 128, 255)
model_path = 'best-640.pt'
image_size = 640
scale_map = { 1280:1.7, 640:4.5, 320:8 }

def load_frame():
    hwnd = win32gui.FindWindow(None, 'Counter-Strike: Global Offensive - Direct3D 9')
    rect = win32gui.GetWindowRect(hwnd)
    region = rect[0], rect[1] + 27, rect[2] - rect[0], rect[3] - rect[1] - 27

    frame = np.array(pyautogui.screenshot(region=region))

    frame = cv2.resize(frame, (image_size, int(image_size / 16 * 9)))
    return frame

def process_frame(frame):
    height, width = frame.shape[:2]
    top_padding = int((image_size - height) / 2)
    padded_frame = np.zeros((image_size, image_size, 3), dtype=np.uint8)
    padded_frame.fill(255)
    padded_frame[top_padding:top_padding+height, :width] = frame
    return padded_frame

def is_opponent(label):
    return class_names[label] == opponent

def find_closest(detected_boxes):
    max = 0
    closest_at = 0
    for i, box in enumerate(detected_boxes):
        x1, _, x2, _ = box
        w = int(x1 - x2)

        if w > max:
            closest_at = i
            max = w

        return closest_at

if __name__ == "__main__":
    opponent = sys.argv[1]
    image_size = int(sys.argv[2])
    model_path = "model/best-%d.pt" % image_size
    model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path)

    while True:
        frame = load_frame()
        frame = process_frame(frame)
        height, width = frame.shape[:2]

        display_frame = cv2.resize(frame, (500, 500))

        # Detection
        start_time = time.time()
        results = model(frame)
        print(time.time() - start_time)
        rl = results.xyxy[0].tolist()

        # Check every detected object
        detected_boxes = []
        color = (0, 0, 0)
        for item in rl:

            x1, y1, x2, y2, confidence, label = item

            if confidence > 0.5:
                if is_opponent(int(label)):
                    detected_boxes.append((x1, y1, x2, y2))
                    color = opponent_color
                else:
                    color = ally_color

                cv2.rectangle(display_frame, (int(x1/image_size*500), int(y1/image_size*500)), (int(x2/image_size*500), int(y2/image_size*500)), color, 1)

        print("Detected:", len(detected_boxes), "enemies.")

        # Check Closest
        if len(detected_boxes) >= 1:

            closest_at = find_closest(detected_boxes)

            x1, y1, x2, y2 = detected_boxes[closest_at]
            x = int((x1 + x2) / 2 - width / 2)
            y = int((y1 + y2) / 2 - height / 2) - (y2 - y1) * 0.43 # For head shot

            scale = scale_map[image_size]
            x = int(x * scale)
            y = int(y * scale)
            # Move mouse and shoot
            win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x, y, 0, 0)
            time.sleep(0.05)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
            time.sleep(0.1)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)

        display_frame = cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
        cv2.imshow("frame", display_frame)
        cv2.waitKey(1)


from datetime import datetime
import json

class Observation():
    def __init__(self, observationTime, numericValue, stringValue, booleanValue, sensorId):
        self.observationTime = observationTime.strftime("%m/%d/%Y, %H:%M:%S")
        self.Value = numericValue
        self.valueString = stringValue
        self.valueBoolean = booleanValue
        self.sensorId = sensorId

class RecEdgeMessage():
    def __init__(self, deviceId, observations):
        self.format = "rec3.2"
        self.deviceId = deviceId
        self.observations = observations
    def toJSON(self):
        return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)


from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.decorators import api_view
import io
from rest_framework import status
from todos.models import Task
from .serializers import TaskSerializer

@api_view(['GET'])
def get_task_list(request):
    tasks = Task.objects.all()
    # print('>>> tasks : ',tasks)
    serializer = TaskSerializer(tasks, many=True)
    return Response(serializer.data, status=status.HTTP_200_OK)

@api_view(['GET'])
def get_task_detail(request, pk):
    task = Task.objects.get(pk=pk)
    serializer = TaskSerializer(task)
    return Response(serializer.data, status=status.HTTP_200_OK)

@api_view(['POST'])
def create_task(request):
    serializer = TaskSerializer(data=request.data, context={'request':request})
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['DELETE'])
def delete_task(request, pk):
    task = Task.objects.get(pk=pk)
    task.delete()
    return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['PUT', 'PATCH'])
def update_task(request, pk):
    task = Task.objects.get(pk=pk)
    if request.method == 'PUT':
        serializer = TaskSerializer(task, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'PATCH':
        serializer = TaskSerializer(task, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['POST'])
def completed(request,pk):
        task = Task.objects.get(pk=pk)
        serializer = TaskSerializer(task, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


import numpy as np
import cv2
from sklearn.cluster import KMeans
import pdb
from skimage.util import montage
import matplotlib.pyplot as plt

''' K means clustering with 30 classes: 26 letters, 1 blank time, 1 double letter, 1 triple letter, 1 empty'''

# gather images that have been labelled
f = open('labels.txt')
dir = '/Users/Alex/Desktop/Summer 2019/scrabble/data/'
# since scrabble is 15 by 15 i should be divisible by 15
i = 825
# if you divide i by 15 (number of rows and columns in Scrabble) you get the width and height (pixels) of each square
s = int(i/15)
# data to be clustered
data = []
counter = 0
# number of boards to cluster
num_boards = 1
for line in f.readlines():
    strr = ''
    # split the line in the text file
    x = line.split()
    # store the image name
    img = dir + x[0]
    # read and resize the image
    img = cv2.imread(img, 0)
    img = cv2.resize(img, (640, 480))
    # store the 4 points in x
    x = x[1:]
    # convert the points to a string
    pts1 = strr.join(x)
    # eval converts the string to an array
    pts1 = np.float32(eval(pts1))
    # pts1 are the corners and pts2 is the width and height
    pts2 = np.float32([[0, 0], [i, 0], [0, i], [i, i]])
    # M is the perspective matrix
    M = cv2.getPerspectiveTransform(pts1, pts2)
    # dst is the resulting flat image
    dst = cv2.warpPerspective(img, M, (i, i))
    # now we need to extract the tiles
    for j in range(15):
        for k in range(15):
            fname = str(j) + str(k) + ".txt"
            square = np.float32(dst[s * j: s + s * j, s * k: s + s * k])
            square = square.reshape((-1))
            data.append(square)
    counter += 1
    if counter == num_boards:
        break

features = np.asarray(data)
kmeans = KMeans(n_clusters=30, random_state=0, max_iter=500).fit(features)
inds = np.where(kmeans.labels_ == 13)
fs = np.uint8(features)
fs = fs.reshape((225,55,-1))
y = montage(fs, grid_shape=(15, 15))
plt.imshow(y)
plt.show()
# montages = build_montages(features[inds], (128, 196), (7, 3))
# for montage in montages:
# 	cv2.imshow("Montage", montage)
# 	cv2.waitKey(0)
# # for x in features[inds]:
# #     cv2.imshow("yes", np.uint8(x).reshape((55,-1)))
# #     cv2.waitKey(0)
# pdb.set_trace()t


import argparse
import collections
from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy
import sys
import time
import json
import zenoh

# --- Command line argument parsing --- --- --- --- --- ---
parser = argparse.ArgumentParser(
    prog='z_plot',
    description='zenoh plotting example')
parser.add_argument('--mode', '-m', type=str, choices=['peer', 'client'],
                    help='The zenoh session mode.')
parser.add_argument('--connect', '-e', type=str, metavar='ENDPOINT', action='append',
                    help='Endpoints to connect to.')
parser.add_argument('--listen', '-l', type=str, metavar='ENDPOINT', action='append',
                    help='Endpoints to listen on.')
parser.add_argument('-k', '--key', type=str, default='demo/random',
                    help='The key expression to subscribe to.')
parser.add_argument('-i', '--history', type=float, default=10.0,
                    help='The history depth in seconds.')
parser.add_argument('-c', '--config', type=str, metavar='FILE',
                    help='A zenoh configuration file.')

args = parser.parse_args()
conf = zenoh.config_from_file(args.config) if args.config is not None else zenoh.Config()
if args.mode is not None:
    conf.insert_json5(zenoh.config.MODE_KEY, json.dumps(args.mode))
if args.connect is not None:
    conf.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(args.connect))
if args.listen is not None:
    conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))

lines = {}
fig, ax = plt.subplots()
ax.xaxis.axis_date()

def listener(sample):
    if not str(sample.key_expr) in lines:
        lines[str(sample.key_expr)] = ax.plot([], [], '-o', label=str(sample.key_expr))[0]
    now = time.time()
    xdata, ydata = lines[str(sample.key_expr)].get_data()
    xdata = numpy.append(xdata, datetime.fromtimestamp(now if sample.timestamp is None else sample.timestamp.time))
    ydata = numpy.append(ydata, float(sample.payload.decode("utf-8")))
    lines[str(sample.key_expr)].set_data(zip(*filter(lambda t: t[0].timestamp() > now - args.history, zip(xdata, ydata))))

def update(_):
    if len(lines):
        ax.axes.relim()
        ax.axes.autoscale_view(True,True,True)
        ax.legend(loc=2)

zenoh.init_logger()

print("Openning session...")
z = zenoh.open(conf)

print("Declaring Subscriber on '{}'...".format(args.key))
sub = z.declare_subscriber(args.key, listener)

ani = FuncAnimation(fig, update)
plt.show()


from flask import Flask
from flask import render_template, request, jsonify
from engine.inv_ind import get_inv_ind
from engine.retrieval import get_retrieved_docs

app = Flask(__name__)

@app.route('/')
def hello():
    return render_template("default.html")

@app.route('/index', methods=["GET", "POST"])
def index_files():
    if request.method == 'POST':
        form_result = request.form
        form_r = form_result.to_dict(flat=False)
    print("form_r", form_r)
    inv_ind, docs, stats = get_inv_ind(corpus=form_r["corpus"][0], do_stem=form_r["stem"][0], do_stop=form_r["sw"][0])
    print(stats)
    # return render_template("index.html", data=inv_ind, docs=docs, stats=stats, do_stem=form_r["stem"][0], corpus=form_r["corpus"][0])
    return render_template("index.html", data=inv_ind, docs=docs, stats=stats, opts=form_r)

@app.route('/result', methods=["GET", "POST"])
def send_result():
    if request.method == 'POST':
        form_result = request.form
        form_r = form_result.to_dict(flat=False)
    print(form_r)
    docs = get_retrieved_docs(form_r["query"][0], form_r["corpus"][0], form_r["stem"][0], form_r["sw"][0])
    num_docs = len(docs)
    if list(docs[0].keys())[0] == "null":
        num_docs = 0

    return render_template("display.html", docs=docs, query=form_r["query"][0], num_docs=num_docs)

if __name__ == '__main__':
    # app.run(host='0.0.0.0')
    app.run()