"""Implementation of the rpki-rtr client function for EOM.

This module interfaces with a rpki-rtr server component to retrieve
validated RPKI information for use by routers. The received data is
saved through the aggregator module to an internal data store.
"""

import asyncore
import netaddr
from eom.rpki.rtr.client   import ClientChannel
from eom.rpki.rtr.channels import Timestamp
from eom.rpki.rtr.pdus     import ResetQueryPDU, SerialQueryPDU

from eom.generic_poller import EOMGenericPoller

ClientChannelClass = ClientChannel

class EOMRPKIRtrCli(EOMGenericPoller):
    """Poller for fetching RPKI prefix information from an RPKI-Rtr Manager instance"""
    def __init__(self, args, aggregator):
        """Instantiate the rpki-rtr client object.

        Args:
            conf(dict): Configuration parameters for the client
            aggregator(EOMAggregator): The data aggregator object
        """
        assert aggregator != None
        if args.reset_session:
            aggregator.reset_rpki_rtr_session(args.host, args.port)
        constructor = getattr(ClientChannelClass, args.protocol)
        self.client = constructor(args)
        self.sql = aggregator.get_sql_connection()
        self.client.set_sql_connection(self.sql)
        self.client.update_session()
        self.polled = self.client.updated
        self.pending = False

    def cleanup(self):
        """Clean up the client object associated with the poller.

        Args:
            None

        Returns:
            None
        """
        self.client.cleanup()

    def poll(self, now):
        """Poll data from configured RPKI-rtr server instances.

        Process the queue to identfy events that must be processed
        during this time interval.

        Args:
            now: current timestamp

        Returns:
            (Boolean, int): A tuple comprising of a pending status and
                            the timestamp for the next event in the queue.
        """
        # Check if we've either never polled or polled earlier than the last update
        if not self.polled or self.polled < self.client.updated:
            if self.client.serial is not None and now > self.client.updated + self.client.expire:
                logging.info("[Expiring client data: serial %s, last updated %s, expire %s]",
                             self.client.serial, self.client.updated, self.client.expire)
                self.client.cache_reset()

            if self.client.serial is None or self.client.nonce is None:
                self.polled = now
                self.client.push_pdu(ResetQueryPDU(version = self.client.version))

            elif now >= self.client.updated + self.client.refresh:
                self.polled = now
                self.client.push_pdu(SerialQueryPDU(version = self.client.version,
                                                    serial  = self.client.serial,
                                                    nonce   = self.client.nonce))

        # Find how long we must wait
        timer = self.client.retry if (now >= self.client.updated + self.client.refresh) else self.client.refresh
        wakeup = max(now, Timestamp(max(self.polled, self.client.updated) + timer))
        # Find if we're still waiting for data to arrive
        if self.client.updated < self.polled:
            pending = True
        else:
            pending = False
            if self.pending:
                # Fix the additional fields in the database
                self.update_rpki_rtr_minmax()
        self.pending = pending
        return (pending, wakeup)

    def update_rpki_rtr_minmax(self):
        """
        For each prefix corresponding to the client's cache id update
        the min and max prefix fields
        """
        cache_id = int(self.client.cache_id)
        cur = self.sql.cursor()
        cur.execute("SELECT asn, prefix, prefixlen, max_prefixlen "
                    "FROM prefix "
                    "WHERE cache_id = ?", (cache_id, ))
        rows = cur.fetchall()
        for r in rows:
            (asn, prefix, prefixlen, max_prefixlen) = r
            ipstr = unicode(str(prefix) + "/" + str(prefixlen), "utf-8")
            prefix_min = EOMGenericPoller.get_ip_str(netaddr.IPNetwork(ipstr).first)
            prefix_max = EOMGenericPoller.get_ip_str(netaddr.IPNetwork(ipstr).last)

            cur = self.sql.cursor()
            cur.execute("UPDATE prefix "
                        "SET prefix_min = ?, prefix_max = ? "
                        "WHERE cache_id = ? AND asn = ? AND prefix = ? AND prefixlen = ? AND max_prefixlen = ?",
                        (prefix_min, prefix_max, cache_id, asn, prefix, prefixlen, max_prefixlen))

        self.sql.commit()


import pygame
import random
from game import square_tile, player_object
import time

pygame.init()
screen = pygame.display.set_mode((800, 1000))
clock = pygame.time.Clock()
bg_grid = square_tile(0, 0, screen.get_width(), 800, (0, 0, 0))
smiley = pygame.image.load("game_img.png")

def random_colour():
    return random.choice([(255, 255, 255), (255, 0, 0), (0, 255, 0), (255, 182, 193)])

def generate_tile_list():
    colour_list = [(255, 255, 255), (255, 0, 0), (0, 255, 0), (255, 182, 193)]

    tile_list = []
    for x in range(0, 800, 200):
        colour_order = [0, 1, 2, 3]
        random.shuffle(colour_order)
        for y in range(0, 800, 200):
            tile_list.append(
                square_tile(x, y, 200, 200, colour_list[colour_order[int(y / 200)]]))
    return tile_list

def display_key(x, y, string, fontsize=100):
    text = pygame.font.Font('freesansbold.ttf', fontsize).render(string, True, (0, 0, 0))
    rect = text.get_rect(center=(x, y))
    pygame.draw.rect(screen, (200, 200, 200), rect)
    screen.blit(text, rect)

game_is_running = True
while game_is_running:

    intro_is_active = True
    game_is_active = True

    # Start screen
    while intro_is_active:
        screen.fill((200, 45, 45))

        # Title text
        title_text = pygame.font.Font('freesansbold.ttf', 100).render("COLOUR VOID", True, (0, 0, 0))
        title_rect = title_text.get_rect(center=(screen.get_width() / 2, 200))
        screen.blit(title_text, title_rect)

        # Enter box
        display_key(screen.get_width() / 2, 900, "ENTER", 150)

        # W A S D
        display_key(screen.get_width() / 2, 400, "W")
        display_key(screen.get_width() / 2, 525, "S")
        display_key(screen.get_width() / 2 - 100, 525, "A")
        display_key(screen.get_width() / 2 + 100, 525, "D")

        # Get enter button to next screen
        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[pygame.K_RETURN]:
            intro_is_active = False

        # Used to close program
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                intro_is_active = False
                game_is_active = False

        pygame.display.update()

    # Countdown used to set duration of each round
    countdown = 3
    player = player_object(386, 386, 35, 35, smiley)

    while game_is_active:
        # Set up round parameters
        clock.tick(30)
        screen.fill((0, 0, 0))

        tile_list = generate_tile_list()
        land_on_colour = random_colour()
        in_a_round = True

        start_time = time.time()

        while in_a_round:

            # To quit using x on the window
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    in_a_round = False
                    game_is_active = False
                    game_is_running = False

            # Draw bottom
            pygame.draw.rect(screen, land_on_colour, ((0, 800), (800, 200)))
            round_text = pygame.font.Font('freesansbold.ttf', 50).render("LAND ON THIS COLOUR", True, (0, 0, 0))
            round_text_rect = round_text.get_rect(center=(screen.get_width() / 2, 900))
            screen.blit(round_text, round_text_rect)

            # Draw each colour tile
            for i in tile_list:
                i.draw(screen)

            # When timer reaches 0, delay + next round
            if time.time() - start_time >= countdown:
                # Progressively gets faster, however not by a constant rate. By a variable rate
                if countdown > 1.3:
                    countdown = countdown * 0.80

                is_safe = False
                for i in tile_list:
                    i.end_round_delete(land_on_colour)
                    # If the player successfully lands on the colour
                    if i.check_inside(player) and not is_safe:
                        is_safe = True

                for i in tile_list:
                    i.draw(screen)
                player.draw(screen)
                pygame.display.update()

                if is_safe:
                    delay_s_time = time.time()
                    delay = True

                    # After if statement to update all the other colours
                    while delay:

                        # To quit using x on the window
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                in_a_round = False
                                game_is_active = False
                                game_is_running = False

                        # Draw the ones supposed to land on
                        if time.time() - delay_s_time > 3:
                            delay = False

                    # Next round
                    break

                else:
                    # Getting input to keep playing or not, y or n
                    while True:

                        # To quit using x on the window
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                in_a_round = False
                                game_is_active = False
                                game_is_running = False

                        # Text to ask if user wants to play again or not
                        pygame.draw.rect(screen, (255, 255, 255), ((0, 800), (800, 200)))
                        round_text = pygame.font.Font('freesansbold.ttf', 50).render("PLAY AGAIN? Y or N", True,
                                                                                (0, 0, 0))
                        round_text_rect = round_text.get_rect(center=(screen.get_width() / 2, 900))
                        screen.blit(round_text, round_text_rect)

                        pressed_keys = pygame.key.get_pressed()
                        if pressed_keys[pygame.K_y]:
                            in_a_round = False
                            game_is_active = False
                            break
                        elif pressed_keys[pygame.K_n]:
                            in_a_round = False
                            game_is_active = False
                            game_is_running = False
                            break

                        pygame.display.update()

            # Update player
            player.update_movement(bg_grid)
            player.draw(screen)

            pygame.display.update()


from collections import OrderedDict

GAME_WORDS = ['ability', 'about', 'above', 'absolute', 'accessible',
              'accommodation', 'accounting', 'ambassador', 'anniversary',
              'appointment', 'awkward', 'bagpipes', 'beautiful', 'birthday',
              'bookstore', 'brand', 'calculator', 'circumstance', 'clever',
              'confused', 'congratulate', 'dangerous', 'decrease', 'development',
              'dictionary', 'dwarves', 'earthquake', 'engaged', 'engineer',
              'enough', 'entertainment', 'environment', 'environment', 'exhausted',
              'frequent', 'gypsy', 'handsome', 'haphazard', 'hyphen', 'impossible',
              'information', 'interested', 'ivory', 'jukebox', 'luggage',
              'mathematics', 'memento', 'mystify', 'necessary', 'opposite',
              'overweight', 'oxygen', 'perhaps', 'pixel', 'possible',
              'refrigerator', 'responsible', 'rhythmic', 'rogue', 'scissors',
              'sphinx', 'strawberry', 'temperature', 'twelfth', 'vacation', 'yacht',
              'xylophone', 'zealous', 'zombie']

MESSAGES = OrderedDict([
    ('NEXT', {
        'msg': "What's your next guess?",
    }),
    ('ALREADY_GUESSED', {
        'msg': "You can't guess a letter that you've guessed before. Try again.",
    }),
    ('INVALID', {
        'msg': "Invalid guess. Try again."
    }),
    ('CORRECT', {
        'msg': "Good guess!"
    }),
    ('INCORRECT', {
        'msg': "Oops... That letter isn't in this word."
    }),
    ('GAME_OVER', {
        'msg': "Oh no! You couldn't guess the word in time.\nGame over!"
    }),
    ('GAME_WON', {
        'msg': "You've guessed the word correctly.\nCongratulations!"
    })
])

IMG_PATH = {
    0: 'images/hangman-0.png',
    1: 'images/hangman-1.png',
    2: 'images/hangman-2.png',
    3: 'images/hangman-3.png',
    4: 'images/hangman-4.png',
    5: 'images/hangman-5.png',
    6: 'images/game-over.png'
}


#code for pressing of the “*” key:

from gpiozero import Button, LED, Buzzer
from time import sleep

col1 = Button(3)
col2 = Button(21)
col3 = Button(5)
row1 = LED(2)
row2 = LED(7)
row3 = LED(6)
row4 = LED(4)

def star_code():
    row1.on()
    row2.on()
    row3.on()
    row4.off()
    while True:
        if col1.is_pressed:
            print(" * key is pressed")


import json
import logging
from collections import deque
from functools import wraps
from typing import Any, Deque, Dict, List, Optional, Set, Tuple, overload

from aiohttp import web

from gear import Database, maybe_parse_bearer_header
from hailtop.utils import secret_alnum_string

log = logging.getLogger('utils')

@web.middleware
async def unavailable_if_frozen(request: web.Request, handler):
    if request.method in ("POST", "PATCH") and request.app['frozen']:
        raise web.HTTPServiceUnavailable()
    return await handler(request)

def authorization_token(request):
    auth_header = request.headers.get('Authorization')
    if not auth_header:
        return None
    session_id = maybe_parse_bearer_header(auth_header)
    if not session_id:
        return None
    return session_id

def add_metadata_to_request(fun):
    @wraps(fun)
    async def wrapped(request, *args, **kwargs):
        request['batch_telemetry'] = {'operation': fun.__name__}

        batch_id = request.match_info.get('batch_id')
        if batch_id is not None:
            request['batch_telemetry']['batch_id'] = batch_id

        job_id = request.match_info.get('job_id')
        if job_id is not None:
            request['batch_telemetry']['job_id'] = job_id

        return await fun(request, *args, **kwargs)

    return wrapped

def coalesce(x, default):
    if x is not None:
        return x
    return default

class Box:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f'{self.value}'

class WindowFractionCounter:
    def __init__(self, window_size: int):
        self._window_size = window_size
        self._q: Deque[Tuple[str, bool]] = deque()
        self._n_true = 0
        self._seen: Set[str] = set()

    def clear(self):
        self._q.clear()
        self._n_true = 0
        self._seen = set()

    def push(self, key: str, x: bool):
        self.assert_valid()
        if key in self._seen:
            return
        while len(self._q) >= self._window_size:
            old_key, old = self._q.popleft()
            self._seen.remove(old_key)
            if old:
                self._n_true -= 1
        self._q.append((key, x))
        self._seen.add(key)
        if x:
            self._n_true += 1
        self.assert_valid()

    def fraction(self) -> float:
        self.assert_valid()
        # (1, 1) prior
        return (self._n_true + 1) / (len(self._q) + 2)

    def __repr__(self):
        self.assert_valid()
        return f'{self._n_true}/{len(self._q)}'

    def assert_valid(self):
        assert len(self._q) <= self._window_size
        assert 0 <= self._n_true <= self._window_size

class ExceededSharesCounter:
    def __init__(self):
        self._global_counter = WindowFractionCounter(10)

    def push(self, success: bool):
        token = secret_alnum_string(6)
        self._global_counter.push(token, success)

    def rate(self) -> float:
        return self._global_counter.fraction()

    def __repr__(self):
        return f'global {self._global_counter}'

async def query_billing_projects_with_cost(db, user=None, billing_project=None) -> List[Dict[str, Any]]:
    where_conditions = ["billing_projects.`status` != 'deleted'"]
    args = []

    if user:
        where_conditions.append("JSON_CONTAINS(users, JSON_QUOTE(%s))")
        args.append(user)

    if billing_project:
        where_conditions.append('billing_projects.name_cs = %s')
        args.append(billing_project)

    if where_conditions:
        where_condition = f'WHERE {" AND ".join(where_conditions)}'
    else:
        where_condition = ''

    sql = f'''
SELECT billing_projects.name as billing_project,
  billing_projects.`status` as `status`,
  users, `limit`, COALESCE(cost_t.cost, 0) AS accrued_cost
FROM billing_projects
LEFT JOIN LATERAL (
  SELECT billing_project, JSON_ARRAYAGG(`user_cs`) as users
  FROM billing_project_users
  WHERE billing_project_users.billing_project = billing_projects.name
  GROUP BY billing_project_users.billing_project
  LOCK IN SHARE MODE
) AS t ON TRUE
LEFT JOIN LATERAL (
  SELECT SUM(`usage` * rate) as cost
  FROM (
    SELECT billing_project, resource_id, CAST(COALESCE(SUM(`usage`), 0) AS SIGNED) AS `usage`
    FROM aggregated_billing_project_user_resources_v3
    WHERE billing_projects.name = aggregated_billing_project_user_resources_v3.billing_project
    GROUP BY billing_project, resource_id
    LOCK IN SHARE MODE
  ) AS usage_t
  LEFT JOIN resources ON resources.resource_id = usage_t.resource_id
  GROUP BY usage_t.billing_project
) AS cost_t ON TRUE
{where_condition}
LOCK IN SHARE MODE;
'''

    billing_projects = []
    async for record in db.execute_and_fetchall(sql, tuple(args)):
        record['users'] = json.loads(record['users']) if record['users'] is not None else []
        billing_projects.append(record)

    return billing_projects

async def query_billing_projects_without_cost(
    db: Database, user: Optional[str] = None, billing_project: Optional[str] = None
) -> List[Dict[str, Any]]:
    where_conditions = ["billing_projects.`status` != 'deleted'"]
    args = []

    if user:
        where_conditions.append("JSON_CONTAINS(users, JSON_QUOTE(%s))")
        args.append(user)

    if billing_project:
        where_conditions.append('billing_projects.name_cs = %s')
        args.append(billing_project)

    if where_conditions:
        where_condition = f'WHERE {" AND ".join(where_conditions)}'
    else:
        where_condition = ''

    sql = f'''
SELECT billing_projects.name as billing_project,
  billing_projects.`status` as `status`,
  users, `limit`
FROM billing_projects
LEFT JOIN LATERAL (
  SELECT billing_project, JSON_ARRAYAGG(`user_cs`) as users
  FROM billing_project_users
  WHERE billing_project_users.billing_project = billing_projects.name
  GROUP BY billing_project_users.billing_project
  LOCK IN SHARE MODE
) AS t ON TRUE
{where_condition}
LOCK IN SHARE MODE;
'''

    billing_projects = []
    async for record in db.execute_and_fetchall(sql, tuple(args)):
        record['users'] = json.loads(record['users']) if record['users'] is not None else []
        billing_projects.append(record)

    return billing_projects

def json_to_value(x):
    if x is None:
        return x
    return json.loads(x)

def regions_to_bits_rep(selected_regions, all_regions_mapping):
    result = 0
    for region in selected_regions:
        idx = all_regions_mapping[region]
        assert idx < 64, str(all_regions_mapping)
        result |= 1 << (idx - 1)
    return result

@overload
def regions_bits_rep_to_regions(regions_bits_rep: None, all_regions_mapping: Dict[str, int]) -> None:
    ...

@overload
def regions_bits_rep_to_regions(regions_bits_rep: int, all_regions_mapping: Dict[str, int]) -> List[str]:
    ...

def regions_bits_rep_to_regions(
    regions_bits_rep: Optional[int], all_regions_mapping: Dict[str, int]
) -> Optional[List[str]]:
    if regions_bits_rep is None:
        return None
    result = []
    for region, idx in all_regions_mapping.items():
        selected_region = bool((regions_bits_rep >> idx - 1) & 1)
        if selected_region:
            result.append(region)
    return result


import logging

import pkg_resources
from datetime import datetime

import six
from formencode import validators
from tg import request
from tg import tmpl_context as c, app_globals as g
from pytz import timezone
from tg import expose, redirect, validate, flash
from tg.decorators import without_trailing_slash
from decorator import decorator
from webob import exc

from allura import version
from allura.app import Application, SitemapEntry
from allura.controllers import BaseController
from allura.controllers.feed import FeedArgs, FeedController
from allura.controllers.rest import AppRestControllerMixin
from allura.lib import helpers as h
from allura.lib.decorators import require_post
from allura.lib.plugin import AuthenticationProvider
from allura.lib.security import require_access
from allura.lib.widgets.user_profile import SendMessageForm, SectionsUtil, SectionBase, ProjectsSectionBase
from allura.model import User, ACE, ProjectRole

log = logging.getLogger(__name__)

class F:
    send_message = SendMessageForm()

class UserProfileApp(Application):

    """
    This is the Profile tool, which is automatically installed as
    the default (first) tool on any user project.
    """
    __version__ = version.__version__
    tool_label = 'Profile'
    max_instances = 0
    has_notifications = False
    icons = {
        24: 'images/home_24.png',
        32: 'images/home_32.png',
        48: 'images/home_48.png'
    }

    def __init__(self, user, config):
        Application.__init__(self, user, config)
        self.root = UserProfileController()
        self.templates = pkg_resources.resource_filename(
            'allura.ext.user_profile', 'templates')
        self.api_root = UserProfileRestController()

    @property
    @h.exceptionless([], log)
    def sitemap(self):
        return [SitemapEntry('Profile', '.')]

    def admin_menu(self):
        return []

    def main_menu(self):
        return [SitemapEntry('Profile', '.')]

    def is_visible_to(self, user):
        # we don't work with user subprojects
        return c.project.is_root

    def install(self, project):
        pr = ProjectRole.by_user(c.user)
        if pr:
            self.config.acl = [
                ACE.allow(pr._id, perm)
                for perm in self.permissions]

    def uninstall(self, project):  # pragma no cover
        pass

    @property
    def profile_sections(self):
        """
        Loads and caches user profile sections from the entry-point
        group ``[allura.user_profile.sections]``.

        Profile sections are loaded unless disabled (see
        `allura.lib.helpers.iter_entry_points`) and are sorted according
        to the `user_profile_sections.order` config value.

        The config should contain a comma-separated list of entry point names
        in the order that they should appear in the profile.  Unknown or
        disabled sections are ignored, and available sections that are not
        specified in the order come after all explicitly ordered sections,
        sorted randomly.
        """
        if hasattr(UserProfileApp, '_sections'):
            return UserProfileApp._sections
        UserProfileApp._sections = SectionsUtil.load_sections('user_profile')
        return UserProfileApp._sections

class UserProfileController(BaseController, FeedController):

    def _check_security(self):
        require_access(c.project, 'read')

    def _check_can_message(self, from_user, to_user):
        if from_user.is_anonymous():
            flash('You must be logged in to send user messages.', 'info')
            redirect(six.ensure_text(request.referer or '/'))

        if not (from_user and from_user.get_pref('email_address')):
            flash('In order to send messages, you must have an email address '
                  'associated with your account.', 'info')
            redirect(six.ensure_text(request.referer or '/'))

        if not (to_user and to_user.get_pref('email_address')):
            flash('This user can not receive messages because they do not have '
                  'an email address associated with their account.', 'info')
            redirect(six.ensure_text(request.referer or '/'))

        if to_user.get_pref('disable_user_messages'):
            flash('This user has disabled direct email messages', 'info')
            redirect(six.ensure_text(request.referer or '/'))

    @expose('jinja:allura.ext.user_profile:templates/user_index.html')
    def index(self, **kw):
        """
        https://sf-11.xb.sf.net/u/admin1
        """
        user = c.project.user_project_of
        if not user:
            raise exc.HTTPNotFound()
        provider = AuthenticationProvider.get(request)
        sections = [section(user, c.project)
                    for section in c.app.profile_sections]

        noindex = True
        has_projects = False
        has_timeline = False
        for s in sections:
            s.setup_context()
            if s.context.get('projects'):
                has_projects = True
            if s.context.get('timeline'):
                has_timeline = True
        if has_projects and has_timeline:
            noindex = False
        return dict(
            user=user,
            reg_date=provider.user_registration_date(user),
            sections=sections,
            noindex=noindex,
        )

    def get_feed(self, project, app, user):
        """Return a :class:`allura.controllers.feed.FeedArgs` object describing
        the xml feed for this controller.

        Overrides :meth:`allura.controllers.feed.FeedController.get_feed`.

        """
        user = project.user_project_of
        return FeedArgs(
            {'author_link': user.url()},
            'Recent posts by %s' % user.display_name,
            project.url())

    @expose('jinja:allura.ext.user_profile:templates/send_message.html')
    def send_message(self):
        """Render form for sending a message to another user.

        """
        self._check_can_message(c.user, c.project.user_project_of)

        delay = c.user.time_to_next_user_message()
        expire_time = str(delay) if delay else None
        c.form = F.send_message
        if c.user.get_pref('message_reply_real_address'):
            c.form.fields.reply_to_real_address.attrs = {'checked': 'checked'}
        return dict(user=c.project.user_project_of, expire_time=expire_time)

    @require_post()
    @expose()
    @validate(dict(subject=validators.NotEmpty,
                   message=validators.NotEmpty))
    def send_user_message(self, subject='', message='', cc=None, reply_to_real_address=None):
        """Handle POST for sending a message to another user.

        """
        self._check_can_message(c.user, c.project.user_project_of)

        if cc:
            cc = c.user.get_pref('email_address')

        if c.user.can_send_user_message():
            if reply_to_real_address:
                c.user.set_pref('message_reply_real_address', True)
            else:
                c.user.set_pref('message_reply_real_address', False)
            c.user.send_user_message(
                c.project.user_project_of, subject, message, cc, reply_to_real_address, c.user.preferences.email_address)
            flash("Message sent.")
        else:
            flash("You can't send more than %i messages per %i seconds" % (
                g.user_message_max_messages,
                g.user_message_time_interval), 'error')
        return redirect(c.project.user_project_of.url())

    @without_trailing_slash
    @expose('jinja:allura.ext.user_profile:templates/user_card.html')
    def user_card(self):
        u = c.project.user_project_of
        locationData = u.get_pref('localization')
        webpages = u.get_pref('webpages')
        location = ''
        website = ''
        if locationData.city and locationData.country:
            location = locationData.city + ', ' + locationData.country
        elif locationData.country and not locationData.city:
            location = locationData.country
        elif locationData.city and not locationData.country:
            location = locationData.city

        if len(webpages) > 0:
            website = webpages[0]

        return dict(
            user=u,
            location=location,
            website=website)

class UserProfileRestController(AppRestControllerMixin):
    @expose('json:')
    def index(self, **kw):
        user = c.project.user_project_of
        if not user:
            raise exc.HTTPNotFound()
        sections = [section(user, c.project)
                    for section in c.app.profile_sections]
        json = {}
        for s in sections:
            if hasattr(s, '__json__'):
                json.update(s.__json__())
        return json

class ProfileSectionBase(SectionBase):

    """
    This is the base class for sections on the Profile tool.

    .. py:attribute:: template

       A resource string pointing to the template for this section.  E.g.::

           template = "allura.ext.user_profile:templates/projects.html"

    Sections must be pointed to by an entry-point in the group
    ``[allura.user_profile.sections]``.
    """

    def __init__(self, user, project):
        """
        Creates a section for the given :param:`user` and user
        :param:`project`.  Stores the values as attributes of
        the same name.
        """
        super().__init__(user)
        self.project = project

class PersonalDataSection(ProfileSectionBase):
    template = 'allura.ext.user_profile:templates/sections/personal-data.html'

    def prepare_context(self, context):
        context['timezone'] = self.user.get_pref('timezone')
        if context['timezone']:
            tz = timezone(context['timezone'])
            context['timezone'] = tz.tzname(datetime.utcnow())
        return context

    def __json__(self):
        auth_provider = AuthenticationProvider.get(request)
        return dict(
            username=self.user.username,
            name=self.user.display_name,
            joined=auth_provider.user_registration_date(self.user),
            localization=self.user.get_pref('localization')._deinstrument(),
            sex=self.user.get_pref('sex'),
            telnumbers=self.user.get_pref('telnumbers')._deinstrument(),
            skypeaccount=self.user.get_pref('skypeaccount'),
            webpages=self.user.get_pref('webpages')._deinstrument(),
            availability=self.user.get_pref('availability')._deinstrument())

class ProjectsSection(ProfileSectionBase, ProjectsSectionBase):
    template = 'allura.ext.user_profile:templates/sections/projects.html'

class SkillsSection(ProfileSectionBase):
    template = 'allura.ext.user_profile:templates/sections/skills.html'

    def __json__(self):
        return dict(skills=self.user.get_skills())

    def check_display(self):
        return bool(self.user.skills)

class ToolsSection(ProfileSectionBase):
    template = 'allura.ext.user_profile:templates/sections/tools.html'

class SocialSection(ProfileSectionBase):
    template = 'allura.ext.user_profile:templates/sections/social.html'

    def __json__(self):
        return dict(
            socialnetworks=self.user.get_pref('socialnetworks')._deinstrument())

    def check_display(self):
        return bool(self.user.get_pref('socialnetworks'))


# This is a script to run the data scraping process from the SEC API.

from datetime import datetime
import os

from sec_api import QueryApi, ExtractorApi
from dotenv import load_dotenv
import csv
from codes import CODES

load_dotenv()

# environment variables
SEC_API_KEY = os.environ.get('sec-api-key')
COMPANIES = []
SECTORS = []

def extract_meta():
    # the endpoints of the sec-api we will be using
    queryApi = QueryApi(api_key=SEC_API_KEY)

    # using CSV writer, open document for writing
    with open('metadata.csv', 'w', encoding='UTF8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(['Ticker', 'Company Name', 'Sector', 'Date', 'Incorporation Region', 'Filing Link'])
        for i in range(len(COMPANIES)):
            query = {
                'query': {'query_string': {
                    'query': f'ticker:{COMPANIES[i]} AND formType:\"10-K\"'
                }},
                'from': '0',
                'size': '1',
                'sort': [{'filedAt': {'order': 'desc'}}]
            }
            filings = queryApi.get_filings(query)
            try:
                country = filings['filings'][0]['entities'][0]['stateOfIncorporation']
            except:
                country = "XX"
            date = datetime.fromisoformat(filings['filings'][0]['filedAt']).strftime('%Y-%m-%d')
            link = filings['filings'][0]['linkToFilingDetails']
            company_name = filings['filings'][0]['companyName']
            data = [COMPANIES[i], company_name, SECTORS[i], date, CODES[country], link]
            writer.writerow(data)
            print(f'extracted data for: {company_name}')

# Populates the list of companies using the file provided of company tickers
def populate_companies():
    tickers = open('../data-scripts/companies.txt', 'r')
    for line in tickers:
        comp_str = line.split(",")
        COMPANIES.append(comp_str[0].strip('"'))
        SECTORS.append(comp_str[4].strip('"'))

if __name__ == '__main__':
    populate_companies()
    extract_meta()


import spacy
import nltk

class NEREntityExtractor:

    def __init__(self, model_folder):
        self.model = spacy.load(model_folder)

    def find_ner_entities(self, articles_df, columns_to_process=["abstract"], column_name="column_with_ner", entity_labels=[]):
        articles_df[column_name] = ""
        for i in range(len(articles_df)):
            result_set = set()
            for column in columns_to_process:
                for sentence in nltk.sent_tokenize(articles_df[column].values[i]):
                    for r in self.model(sentence).ents:
                        if not entity_labels or r.label_ in entity_labels:
                            result_set.add(r.text.strip())
            articles_df[column_name].values[i] = list(result_set)
            if i % 3000 == 0:
                print("Processed %d rows" % i)
        return articles_df


from __future__ import print_function
import networkx as nx
import pandas as pd
from datetime import date
import requests
import json
import os

# Set globals
# Get source url from local `source_db.txt`
SOURCE_URL = open(
    os.path.join(
        os.path.dirname(
            os.path.abspath(__file__)),
        '..',
        'source_url.txt'),
    'r').read().strip()
# Set curation directory
NEIGHBOR_DIR='nbdb'
TEMPLATE_DIR='mvrs'

"""Import internal dependencies
"""
try:
    from analyze.prepare_data import __main__ as prepare_data
    from analyze.model import __template__
    from analyze.model import __model__
    from analyze.model import load_references
    from analyze.model import load_metabolite_synonym_dictionary
    from analyze.utils import remove_defective_reactions
    from utils import progress_feed, track_progress, read_network, \
                      get_metaboverse_cli_version, write_database, safestr, \
                      update_session_vars
except:
    import importlib.util
    spec = importlib.util.spec_from_file_location(
        "", os.path.abspath("./metaboverse_cli/analyze/prepare_data.py"))
    prepare_data = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(prepare_data)
    prepare_data = prepare_data.__main__

    spec = importlib.util.spec_from_file_location(
        "", os.path.abspath("./metaboverse_cli/analyze/model.py"))
    model = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(model)
    __template__ = model.__template__
    __model__ = model.__model__
    load_references = model.load_references
    load_metabolite_synonym_dictionary = model.load_metabolite_synonym_dictionary

    module_path = os.path.abspath(
        os.path.join(".", "metaboverse_cli", "analyze", "utils.py"
                     ))
    spec = importlib.util.spec_from_file_location("", module_path)
    analyze_utils = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(analyze_utils)
    remove_defective_reactions = analyze_utils.remove_defective_reactions

    spec = importlib.util.spec_from_file_location(
        "", os.path.abspath("./metaboverse_cli/utils.py"))
    utils = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(utils)
    progress_feed = utils.progress_feed
    track_progress = utils.track_progress
    read_network = utils.read_network
    get_metaboverse_cli_version = utils.get_metaboverse_cli_version
    write_database = utils.write_database
    safestr = utils.safestr
    update_session_vars = utils.update_session_vars

def process_data(
        network,
        args_dict):
    """
    """
    if str(args_dict['transcriptomics']).lower() != 'none' \
            or str(args_dict['proteomics']).lower() != 'none' \
            or str(args_dict['metabolomics']).lower() != 'none':

        data, stats, unmapped = prepare_data(
            network=network,
            transcriptomics_url=args_dict['transcriptomics'],
            proteomics_url=args_dict['proteomics'],
            metabolomics_url=args_dict['metabolomics'],
            database_source=args_dict['database_source'])
        flag_data = False

    else:
        data = pd.DataFrame()
        data['NoSample'] = [0, 0, 0]
        data.index = ['dummy_index1', 'dummy_index2', 'dummy_index3']

        stats = pd.DataFrame()
        stats['NoSample'] = [0, 0, 0]
        stats.index = ['dummy_index1', 'dummy_index2', 'dummy_index3']

        unmapped = {
            'transcriptomics_unmapped': [],
            'proteomics_unmapped': []
        }
        flag_data = True

    return data, stats, unmapped, flag_data

def read_template(
        args_dict,
        network,
        url,
        user_provided=False):
    """
    """
    def get_template_file(url, args_dict):
        file = os.path.join(
            args_dict['output'],
            args_dict['organism_id'] + '_template.mvrs')
        print('Downloading graph template database...', '\n\t', url)
        os.system('curl -kL ' + url + ' -o \"' + file + '\"')
        return file

    print('Downloading Metaboverse graph template for organism...')

    if user_provided == False:
        file = get_template_file(url, args_dict)
    else:
        file = url

    with open(file) as graph_template:
        graph_data = json.load(graph_template)

    graph = nx.readwrite.json_graph.node_link_graph(
        {
            'nodes': graph_data['nodes'],
            'links': graph_data['links']
        },
        directed=graph_data['directed'],
        multigraph=graph_data['multigraph'])
    network['reaction_database'] = graph_data['reaction_dictionary']
    network['pathway_database'] = graph_data['pathway_dictionary']
    super_pathways = graph_data['super_pathways']
    degree_dictionary = graph_data['degree_dictionary']

    reverse_genes, protein_dictionary, chebi_dictionary, \
    name_reference, uniprot_mapper = load_references(
        args_dict=args_dict,
        ensembl=network['ensembl_synonyms'],
        uniprot=network['uniprot_synonyms'],
        chebi=network['chebi_mapper'],
        uniprot_metabolites=network['uniprot_metabolites'])
    metabolite_mapper = load_metabolite_synonym_dictionary()

    args_dict["curation_version"] = network["metaboverse-curate_version"]
    args_dict["curation_date"] = network["curation_date"]
    args_dict["database_version"] = network["database_version"]
    args_dict['template_url'] = file
    args_dict['template_version'] = graph_data['metadata']['template_version']
    args_dict['template_date'] = graph_data['metadata']['template_date']

    progress_feed(args_dict, "graph", 9)

    return graph, args_dict, network, name_reference, \
        degree_dictionary, super_pathways, chebi_dictionary, \
        uniprot_mapper, metabolite_mapper

def download_neighbors_dictionary(
        args_dict,
        url,
        user_provided=False):
    """
    """
    print('Downloading Metaboverse neighbors dictionary for organism...')

    def get_neighbor_file(url, args_dict):
        file = os.path.join(
            args_dict['output'],
            args_dict['organism_id'] + '.nbdb')
        print('Downloading nearest neighbors database...', '\n\t', url)
        os.system('curl -kL ' + url + ' -o \"' + file + '\"')
        return file

    if user_provided == False:
        file = get_neighbor_file(url, args_dict)
    else:
        file = url

    neighbors_dictionary = read_network(
        file_path=args_dict['output'],
        network_url=file)

    neighbors_dictionary['nbdb-Metaboverse-url'] = file

    return neighbors_dictionary

def make_neighbors_dictionary(
        args_dict,
        graph,
        reaction_dictionary):
    """
    """
    reaction_ids = set(reaction_dictionary.keys())

    print('Generating Metaboverse neighbors dictionary for organism...')

    counter = 0
    edges = list(graph.edges)
    edge_len = len(edges)
    neighbors_dictionary = {}
    for e in edges:
        one = e[0]
        two = e[1]
        if one in neighbors_dictionary.keys():
            neighbors_dictionary[one].add(two)
        else:
            neighbors_dictionary[one] = set()
            neighbors_dictionary[one].add(two)
        if two in neighbors_dictionary.keys():
            neighbors_dictionary[two].add(one)
        else:
            neighbors_dictionary[two] = set()
            neighbors_dictionary[two].add(one)
        counter = track_progress(args_dict, counter, edge_len, 3)

    print('Tuning neighbors dictionary...')
    reaction_neighbors_dictionary = {}
    counter = 0
    neighbors = list(neighbors_dictionary.keys())
    neighbors_number = len(neighbors)
    for neighbor in neighbors:
        if neighbor in reaction_ids:
            components = neighbors_dictionary[neighbor]
            connected_reactions = set()
            for _c in components:
                for _c_ in neighbors_dictionary[_c]:
                    if _c_ in reaction_ids:
                        connected_reactions.add(_c_)
            connected_reactions = list(connected_reactions)
            reaction_neighbors_dictionary[neighbor] = connected_reactions
        counter = track_progress(args_dict, counter, neighbors_number, 3)

    reaction_neighbors_dictionary['nbdb-Metaboverse-version'] = get_metaboverse_cli_version()
    reaction_neighbors_dictionary['nbdb-Metaboverse-date'] = date.today().strftime('%Y-%m-%d')
    reaction_neighbors_dictionary['nbdb-Metaboverse-url'] = os.path.join(args_dict['output'], args_dict['organism_id'] + '.nbdb')

    print('Writing neighbors dictionary to database file...')
    write_database(
        output=args_dict['output'],
        file=args_dict['organism_id'] + '.nbdb',
        database=reaction_neighbors_dictionary)

    return reaction_neighbors_dictionary

def __main__(
        args_dict):
    """Analyze data on network model
    """

    # Get network curation info
    network = read_network(
        file_path=args_dict['output'],
        network_url=args_dict['curation'])
    progress_feed(args_dict, "graph", 1)

    if args_dict['organism_curation_file'] != 'None':
        args_dict['organism_id'] = network['organism_id']

    # Read in data (if any)
    data, stats, unmapped, flag_data = process_data(
        network=network,
        args_dict=args_dict)
    progress_feed(args_dict, "graph", 2)
    print("Data processed of dimensions: " + str(data.shape))

    # Generate graph template
    this_version = get_metaboverse_cli_version()
    test_url = (
        SOURCE_URL
        + 'v' + this_version + '/'
        + TEMPLATE_DIR + '/'
        + args_dict['organism_id'] + '_template.mvrs')

    # If unable to access pre-curated network, force new curation
    if args_dict['force_new_curation'] != True:
        try:
            url_response = requests.head(test_url)
        except:
            print("Unable to access source files from: " + str(test_url))
            print("Will force a new curation of source files instead...")
            args_dict['force_new_curation'] == True
            url_response = ''
    else:
        url_response = ''

    if (args_dict['force_new_curation'] == False \
    or args_dict['force_new_curation'] == "False") \
    and 'graph_template_file' in args_dict \
    and safestr(args_dict['graph_template_file']) != None \
    and safestr(args_dict['graph_template_file']) != 'None':
        try:
            graph, args_dict, network, name_reference, \
            degree_dictionary, super_pathways, chebi_dictionary, \
            uniprot_mapper, metabolite_mapper = read_template(
                args_dict=args_dict,
                network=network,
                url=args_dict['graph_template_file'],
                user_provided=True)
        except:
            graph, args_dict, network, name_reference, \
            degree_dictionary, super_pathways, chebi_dictionary, \
            uniprot_mapper, metabolite_mapper = __template__(
                args_dict=args_dict,
                network=network,
                species_id=args_dict['organism_id'],
                output_file=args_dict['output_file'])
    elif (args_dict['force_new_curation'] == False \
    or args_dict['force_new_curation'] == "False") \
    and url_response.status_code != 404:
        graph, args_dict, network, name_reference, \
        degree_dictionary, super_pathways, chebi_dictionary, \
        uniprot_mapper, metabolite_mapper = read_template(
            args_dict=args_dict,
            network=network,
            url=test_url)
    else:
        graph, args_dict, network, name_reference, \
        degree_dictionary, super_pathways, chebi_dictionary, \
        uniprot_mapper, metabolite_mapper = __template__(
            args_dict=args_dict,
            network=network,
            species_id=args_dict['organism_id'],
            output_file=args_dict['output_file'])

    if len(graph.nodes) == 0 or len(graph.edges) == 0:
        raise Exception("Unable to generate a reaction-based network based on the input organism template.")
    else:
        print("Successfully loaded network with " + str(len(graph.nodes)) + " nodes and " + str(len(graph.edges)) + " edges")

    # Generate graph template
    neighbors_url = (
        SOURCE_URL
        + 'v' + this_version + '/'
        + NEIGHBOR_DIR + '/'
        + args_dict['organism_id'] + '.nbdb')

    # If unable to access pre-curated network, force new curation
    if args_dict['force_new_curation'] != True:
        try:
            neighbor_response = requests.head(neighbors_url)
        except:
            print("Unable to access source files from: " + str(test_url))
            print("Will force a new curation of source files instead...")
            args_dict['force_new_curation'] == True
            neighbor_response = ''
    else:
        neighbor_response = ''

    force_neighbors = False
    if (args_dict['force_new_curation'] == False \
    or args_dict['force_new_curation'] == "False") \
    and 'neighbor_dictionary_file' in args_dict \
    and safestr(args_dict['neighbor_dictionary_file']) != None \
    and safestr(args_dict['neighbor_dictionary_file']) != 'None':
        try:
            neighbors_dictionary = download_neighbors_dictionary(
                args_dict=args_dict,
                url=args_dict['neighbor_dictionary_file'],
                user_provided=True)
        except:
            force_neighbors = True

    elif (args_dict['force_new_curation'] == False \
    or args_dict['force_new_curation'] == "False") \
    and neighbor_response.status_code != 404:
        try:
            neighbors_dictionary = download_neighbors_dictionary(
                args_dict=args_dict,
                url=neighbors_url)
        except:
            force_neighbors = True
    else:
        force_neighbors = True

    if force_neighbors == True:
        no_defective_reactions = remove_defective_reactions(
            network=network)
        neighbors_dictionary = make_neighbors_dictionary(
            args_dict=args_dict,
            graph=graph,
            reaction_dictionary=no_defective_reactions)
    else:
        progress_feed(args_dict, "graph", 6)

    # Overlay data on graph and collapse as able
    print("Modeling data onto network...")
    graph_name = __model__(
        graph=graph,
        args_dict=args_dict,
        network=network,
        data=data,
        stats=stats,
        species_id=args_dict['organism_id'],
        output_file=args_dict['output_file'],
        neighbors_dictionary=neighbors_dictionary,
        name_reference=name_reference,
        degree_dictionary=degree_dictionary,
        chebi_dictionary=chebi_dictionary,
        uniprot_mapper=uniprot_mapper,
        metabolite_mapper=metabolite_mapper,
        super_pathways=super_pathways,
        unmapped=unmapped,
        flag_data=flag_data)

    args_dict = update_session_vars(args_dict)

    return graph_name

def test():
    args_dict = {
        'output': "C:\\Users\\jorda\\Desktop",
        'curation': "MMU.mvdb",
        'organism_id': 'MMU',
        'output_file': "C:\\Users\\jorda\\Desktop\\MMU.mvrs",
        'labels': "0",
        'blocklist': "H+",
        'database_date': "",
        'curation_date': ""}
    args_dict = {
        'output': "C:\\Users\\jorda\\Desktop",
        'curation': "HSA.mvdb",
        'graph_template_file': "C:\\Users\\jorda\\Desktop\\HSA_template.mvrs",
        'organism_id': 'HSA'}

    network = read_network(
        file_path="C:\\Users\\jorda\\Desktop",
        network_url="MMU.mvdb")

    neighbors = read_network(
        file_path="C:\\Users\\jorda\\Desktop",
        network_url="MODEL1604210000.nbdb")

    neighbors_dictionary = download_neighbors_dictionary(
        args_dict=args_dict,
        url="C:\\Users\\jorda\\Desktop\\HSA.nbdb",
        user_provided=True)

    graph, args_dict, network, name_reference, \
    degree_dictionary, super_pathways, chebi_dictionary, \
        uniprot_mapper, metabolite_mapper = read_template(
        args_dict=args_dict,
        network=network,
        url=args_dict['graph_template_file'],
        user_provided=True)


import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from ComplexNN import *

class AlexNet(nn.Module):

    def __init__(self, num_classes=100):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.conv1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2)
        )

        self.conv2 = nn.Sequential(
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2)
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True)
        )
        self.conv4 = nn.Sequential(
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True)
        )
        self.conv5 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2)
        )

        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        # x = self.features(x)
        print(x.shape)
        x = self.conv1(x)
        print(x.shape)
        x = self.conv2(x)
        print(x.shape)
        x = self.conv3(x)
        print(x.shape)
        x = self.conv4(x)
        print(x.shape)
        x = self.conv5(x)
        print(x.shape)
        # x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x
# net = AlexNet()
# x = torch.rand(64,3,32,32)
# net(x)
class preEncoder(nn.Module):
    def __init__(self):
        super(preEncoder, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, stride=1, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=5, stride=1, padding=1),
            nn.ReLU(inplace=True)
        )
    def forward(self, I):
        return self.features(I)

class Discriminator(nn.Module):
    def __init__(self):
        super(Discriminator, self).__init__()

        self.main = nn.Sequential(
            nn.Conv2d(384,192,3,1),
            nn.LeakyReLU(),
            nn.Conv2d(192,64,3,1),
            nn.LeakyReLU(),
            nn.Conv2d(64,32,3,1),
            nn.LeakyReLU(),
        )
        self.linear = nn.Linear(256,1)
        self.relu = nn.LeakyReLU()

    def forward(self, x):
        x = self.main(x) #[128, 32, 4, 2]
        # print(x.shape)
        x = x.view(x.shape[0], -1)  #[128, 256]
        # print(x.shape)
        x = self.linear(x)
        # print(x.shape)
        x = self.relu(x) #[128, 256]

        return x

class Net(nn.Module):
    def __init__(self, identities=10177):
        super(Net, self).__init__()
        self.conv4 = ComplexConv2d(384, 256, kernel_size=3, stride=1, padding=1)
        self.conv5 = ComplexConv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.Crelu = ComplexRelu()
        self.Cmax = ComplexMaxPool2d(3, 2)
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 4 * 3, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, identities),
        )

    def forward(self, a, b, theta):
        x1, x2 = encode(a, b, theta)
        x1, x2 = self.conv4(x1, x2)
        x1, x2 = self.Crelu(x1, x2)
        x1, x2 = self.conv5(x1, x2)
        x1, x2 = self.Crelu(x1, x2)
        x1, x2 = self.Cmax(x1, x2)

        x1 = x1.view(x1.size()[0], -1)
        x2 = x2.view(x2.size()[0], -1)

        x = decode(x1, x2, theta)
        x = self.classifier(x)

        return x

g = preEncoder()
discriminator = Discriminator()
net = Net()
# model = AlexNet()
x = torch.rand(128, 3, 218, 178)
# b = g(torch.rand_like(x))
x = g(x)
print(x.shape)
# theta = torch.Tensor(1).uniform_(0,np.pi)
# x = net(x,b,theta)
x = discriminator(x)
print(x.shape)
# a = g(x)
# b = g(torch.rand_like(x))
# delta_theta = torch.Tensor(5).uniform_(0,np.pi)
# output = net(a, b, delta_theta[0])
# print(ED(a, b, delta_theta[0]).shape)
# print(discriminator(ED(a, b, delta_theta[0])))
# fake_imgs = torch.stack([discriminator(ED(a, b, dt)) for dt in delta_theta])
# loss_D = -torch.mean(discriminator(a)) + torch.mean(fake_imgs)
# print(x.shape)
# x = model(x)
# print(x.shape)
# x = d(x)
# print(x.shape)
#
# target = torch.ones([256, 40], dtype=torch.int64).float()  # 64 classes, batch size = 10
# output = torch.full([256, 40], 0.999)  # A prediction (logit)
# pos_weight = torch.ones([40])  # All weights are equal to 1
# criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
# loss = criterion(output, target)  # -log(sigmoid(0.999))
# print(loss)
# def encode(a, b, theta):
#     x = torch.mul(a, torch.cos(theta)) - torch.mul(b, torch.sin(theta))
#     y = torch.mul(a, torch.sin(theta)) + torch.mul(b, torch.cos(theta))
#     # print(theta)
#     return x, y

# def decode(a, b, theta):
#     return torch.mul(a, torch.cos(-theta)) - torch.mul(b, torch.sin(-theta))

# print(x.shape[0])
# b = g(torch.rand_like(x))
# x = g(x)

# print(x.shape)

# theta = torch.Tensor(1).uniform_(0,2*np.pi)
# output = net(x, b, theta)
# print(output.shape)
# def encode1(a, b, theta):
#     x = a * torch.cos(theta) - b * torch.sin(theta)
#     y = a * torch.sin(theta) + b * torch.cos(theta)
#     # print(theta)
#     return x, y

# class preEncoder(nn.Module):
#     def __init__(self):
#         super(preEncoder, self).__init__()
#         self.features = nn.Sequential(
#             nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
#             nn.ReLU(inplace=True),
#             nn.MaxPool2d(kernel_size=3, stride=2),
#             nn.Conv2d(64, 192, kernel_size=3, stride=1, padding=1),
#             nn.ReLU(inplace=True),
#             nn.MaxPool2d(kernel_size=3, stride=2),
#             nn.Conv2d(192, 384, kernel_size=3, stride=1, padding=1),
#             nn.ReLU(inplace=True),
#         )
#     def forward(self, I):
#         return self.features(I)

# class Discriminator(nn.Module):
#     def __init__(self):
#         super(Discriminator, self).__init__()

#         self.main = nn.Sequential(
#             nn.Conv2d(384,192,3,1),
#             nn.LeakyReLU(),
#             nn.Conv2d(192,64,2,1),
#             nn.LeakyReLU(),
#         )
#         self.linear = nn.Linear(64, 1)
#         self.relu = nn.LeakyReLU()

#     def forward(self, x):
#         x = self.main(x)
#         x = x.view(x.shape[0], -1)
#         x = self.linear(x)
#         x = self.relu(x)

#         return x

# # g = preEncoder()
# d = Discriminator()
# x = torch.rand(10,384,4,4)
# # x = g(x)
# # print(x.shape)
# x = d(x)
# print(x.shape)

# print(3e-4)

# a = torch.tensor([2])
# b = torch.tensor([3])

# b = torch.rand(1, 6, 1, 2)
# # print(b)

# pool = nn.MaxPool2d(2, stride=2, return_indices=True)
# unpool = nn.MaxUnpool2d(2, stride=2)
# Input = torch.tensor([[[[ 1.,  2,  3,  4],
#                         [ 5,  6,  7,  8],
#                         [ 9, 10, 11, 12],
#                         [13, 14, 15, 16]],[[ 1.,  2,  3,  4],
#                         [ 5,  6,  7,  8],
#                         [ 9, 10, 11, 12],
#                         [13, 14, 15, 16]]],[[[ 0.,  0,  0,  0],
#                         [ 0,  1,  0,  1],
#                         [ 0,0,0,0],
#                         [3,4,0,1]],[[ 0.,  0,  0,  0],
#                         [ 0,  1,  0,  1],
#                         [ 0,0,0,0],
#                         [0,1,0,1]]]])
# output, indices = pool(Input)

# I = torch.tensor([[[[ 0.,  0,  0,  0],
#                         [ 0,  1,  0,  1],
#                         [ 0,0,0,0],
#                         [0,1,0,1]]]])
# def unravel_index(index, shape):
#     out = []
#     for dim in reversed(shape):
#         out.append(index % dim)
#         index = index // dim
#     return tuple(reversed(out))
# print(Input)
# print(torch.mean(Input,(0)))

# A  = torch.tensor([[ 0.,  1,  0,  1],[ 0,0,0,0],[0,1,0,1]])
# A[0,:] = 3
# print(A)
# print(torch.mean(A))

# print(unravel_index(1, Input.shape))
# print(Input[unravel_index(1, Input.shape)])
# # Input[15] = 100
# y = Input.view(16)
# indices[0,0,0,1] = y[indices[0,0,0,1]]

# theta = torch.Tensor(10).uniform_(0,2*np.pi)
# b = torch.rand(100, 6, 14, 14)
# print(b)

# G_DIM = 6
# G_SIZE = 14
# D_CHANNEL=256
# x = torch.rand(10,100,6,14,14)

# model = nn.Sequential(
#         nn.Linear(G_DIM*G_SIZE*G_SIZE, 256),
#         nn.LeakyReLU(0.2),
#         nn.Linear(256, 128),
#         nn.LeakyReLU(0.2),
#         nn.Linear(128, 1),
#     )

# x = x.view(x.shape[0],x.shape[1],G_DIM*G_SIZE*G_SIZE)
# x = torch.mean(model(x))
# print(x.shape)
# x = torch.rand(50)
# print(x.shape)
# for i in range(x.shape[0]):
#     print(i)

# def ED(a, b, theta):
#     x, y = encode(a, b, theta)
#     return decode(x, y, theta)
# a = torch.rand(100,3,14,14)
# b = torch.rand(100,3,14,14)
# theta = torch.Tensor(10).uniform_(0,2*np.pi)
# x = torch.stack([ED(a, b, dt) for dt in theta])

# print(x.shape)

# a = torch.tensor([[1.,2.],[3.,4.]])
# non_Linear = torch.clamp(a, max = 2)

# print(non_Linear)
# non_Linear = a / torch.clamp(a, max = 2)
# print(a)
# print(non_Linear)

# x1 = torch.rand([100, 3, 32, 32])
# x_norm_max, inds = F.max_pool2d(x1, 2, 2, return_indices = True)
# print(inds[1,1,:,:])
# print(a_max.shape, "\n")
# print(inds.shape, "\n")
# n,c,h,w = inds.shape
# b = a.view(100,3,32*32)
# inds_b = inds.view(n,c,h*w)
# b_max = torch.gather(b, 2, inds_b)
# print(b_max.shape, "\n")
# b_max = b_max.view(n,c,h,w)
# print(b_max.shape, "\n")

# x1 = torch.rand([100, 3, 32, 32])
# x2 = torch.rand([100, 3, 32, 32])
# x_norm = x1*x1 + x2*x2
# x_norm_max, inds = F.max_pool2d(x_norm, 2, 2, return_indices = True)

# x1 = torch.rand([100, 3, 32, 32])
# x2 = torch.rand([100, 3, 32, 32])
# x_norm = x1*x1 + x2*x2
# x_norm_max, inds = F.max_pool2d(x_norm, 2, 2, 0, 1, False, True)
# n,c,h1,w1 = x_norm.shape
# n,c,h2,w2 = x_norm_max.shape
# X1 = x1.view(n,c,h1*w1)
# X2 = x2.view(n,c,h1*w1)
# inds_X = inds.view(n,c,h2*w2)
# X1_max = X1.gather(2, inds_X).view(n,c,h2,w2)
# X2_max = X2.gather(2, inds_X).view(n,c,h2,w2)

# x1 = torch.rand([100, 3, 32, 32])
# x2 = torch.rand(x1.shape)
# print(x2.shape)

# import argparse
# parser = argparse.ArgumentParser()
# parser.add_argument("--n_epochs", type=int, default=10, help="number of epochs of training")
# parser.add_argument("--batch_size", type=int, default=100, help="size of the batches")
# parser.add_argument("--lr", type=float, default=0.00005, help="learning rate")
# # parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
# # parser.add_argument("--latent_dim", type=int, default=100, help="dimensionality of the latent space")
# parser.add_argument("--img_size", type=int, default=32, help="size of each image dimension")
# parser.add_argument("--channels", type=int, default=3, help="number of image channels")
# parser.add_argument("--n_critic", type=int, default=10, help="number of training steps for discriminator per iter")
# parser.add_argument("--clip_value", type=float, default=0.01, help="lower and upper clip value for disc. weights")
# # parser.add_argument("--sample_interval", type=int, default=400, help="interval betwen image samples")
# opt = parser.parse_args()
# opt.channels = 64
# img_shape = (opt.channels, opt.img_size, opt.img_size)
# print(img_shape)

# a = torch.rand(3,5,64,64)
# b = nn.Conv2d(5,6,5,1)
# # b = torch.rand_like(a)

# print(b)


import xlrd

def run_shortest_distance(d):
    # Source: https://www.geeksforgeeks.org/floyd-warshall-algorithm-dp-16/
    dist = d
    V = len(d)
    global nex

    for i in range(V):
        for j in range(V):
            nex[i][j] = j

    print("Started run.")
    for k in range(V):
        for i in range(V):
            for j in range(V):
                possible = dist[i][k]+ dist[k][j]
                if possible < dist[i][j]:
                    dist[i][j] = possible
                    nex[i][j] = nex[i][k]

    print("Finished run.")
    return nex, dist

def get_path(i, j):
    path = [i]
    while i!=j:
        print("->",i,j,path)
        i = nex[i][j]
        print("****",i,j,path)
        if i in path:
            if j not in path:
                path.append(j)
            break
        path.append(i)
    return path

def initialize_next(d):
    nex = []
    t = []
    for i in range(d):
        t.append([])
    for i in range(d):
        nex.append(t)
    return nex

if __name__ == "__main__":
    try:
        sheet = xlrd.open_workbook(".\Honors Research Data.xlsx").sheet_by_index(0)
        print("Sheet imported.")
    except:
        print("Error! Cannot open sheet.")

    d = []

    for i in range(1, sheet.nrows):
        row = []
        for j in range(1, sheet.ncols):
            val = sheet.cell_value(j,i)
            if val == '':
                val = 9999
            row.append(val)
        d.append(row)
    print("Data read.")

    nex = initialize_next(len(d))

    if len(d) == 0:
        print("Error! Dataset empty.")
    else:
        # shortest distance between points
        ans = run_shortest_distance(d)
        # shortest paths


import os
import requests
import json
import time
from uuid import uuid4

license = 'GZEQhdOCI7a0VbikezFdz1WGlfhKZQKy4OrCLgsb'
authentication = 'Basic enJiaTl3N253eGhtdHZ3NXE5Y2RxcjZsY3NsY2pwMjQzcWRnMng4Y2w4OTJ2OXJwOkR7cjpqaypRSEgtMjpIZGRjNzUpJS19KTFHMF9fXCotIykzX0xoaF9+SFxmTjdQZQ=='

headers = {
   'Content-Type':'application/json',
   'x-spdra-license':license,
   'x-spdra-authentication':authentication
}

https_url = 'http://www.voiptroubleshooter.com/open_speech/american/OSR_us_000_0010_8k.wav'

def get_upload_destination():
	wflurl =  'https://spdra.deloitterisk.cloud/v1/upload-destination'
	resp = requests.post(wflurl, headers=headers)
	return resp.json()

def upload_file(filepath, url):
	file_size = os.path.getsize(filepath)
	with open(filepath, "rb") as f:
		data = f.read()
	headers = {
		'Content-Length': str(file_size),
	}
	resp = requests.put(url, data=data, headers=headers)
	return resp.status_code

def init_transcription(fileUri=https_url, protocol="https"):
	wflurl =  'https://spdra.deloitterisk.cloud/v1/job/workflow/transcription_only/'
	payload = {'correlationId': str(uuid4()),'location': {'fileUri': fileUri,'protocol': protocol}}
	data = json.dumps(payload)
	resp = requests.post(wflurl, data=data, headers=headers)
	return resp.json()

def upload_file_and_init_transcription(filepath):
	resp_urls = get_upload_destination()
	uploadUri = resp_urls['uploadUri']
	processingUri = resp_urls['processingUri']

	resp_upload = upload_file(filepath=filepath, url=uploadUri)
	assert resp_upload == 200

	return init_transcription(fileUri=processingUri, protocol="spdra")

def poll_transcription(jobID):
	url =  'https://spdra.deloitterisk.cloud/v1/job/'+jobID
	resp = requests.get(url, headers=headers)
	return resp.json()

if __name__ == "__main__":
	jobId = upload_file_and_init_transcription('/Users/rman/Documents/BEAT/audio/voice_recognition/OSR_us_000_0040_8k.wav')['jobId']
	time.sleep(2)
	result = poll_transcription(jobId)
	while result == {'statusCode': 202, 'message': 'Job not yet finished, try again later.'}:
		result = poll_transcription(jobId)
		print(result)
		time.sleep(30)


#!/usr/bin/python
import Settings
import tornado.web
import tornado.httpserver
import tornado.ioloop
import json
import decimal

class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return float(o)
        return super(DecimalEncoder, self).default(o)

class jsonHandler(tornado.web.RequestHandler):
    def get(self):

        import db
        c=db.mydb()
        self.write(json.dumps(c.get_list(),cls=DecimalEncoder))

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
	        (r"/()$", tornado.web.StaticFileHandler, {'path':'statik/index.html'}),
            (r"/json", jsonHandler),
	        (r"/statik/(.*)", tornado.web.StaticFileHandler, {"path": Settings.STATIC_PATH}),

        ]
        settings = {
            "template_path": Settings.TEMPLATE_PATH,
            "static_path": Settings.STATIC_PATH,
        }
        tornado.web.Application.__init__(self, handlers, **settings)

def main():
    applicaton = Application()
    http_server = tornado.httpserver.HTTPServer(applicaton)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
        main()


# application/flask imports
from application import app
from application.forms import ImageForm, LoginForm
from application.models import PredictionEntryUtils
from application.utils import Utils, ImgPredictionUtils
from flask import render_template, flash, redirect, url_for, abort, jsonify, request, session
from flask_cors import cross_origin

# other imports
import os
from datetime import datetime, timedelta
import jwt

# create uploads folder if not exists
path = os.path.dirname(os.path.abspath(__file__))[1:]
abs_path = "/" + os.path.join(path, "static/uploads")

if not os.path.exists(abs_path):
    os.makedirs(abs_path)
    print("Uploads folder created")

# folder name where the images are saved to (inside static folder)
folder = 'uploads/'

# URLs where the models are hosted on
model_apps = ['https://ca2-doaa-dlmodelapp.onrender.com/v1/models/coarse_cnn:predict', 'https://ca2-doaa-dlmodelapp.onrender.com/v1/models/fine_cnn:predict']
model_names = ['Coarse Classes CNN', 'Fine Classes CNN']

# --------------------------------------------------
# catch errors
# --------------------------------------------------
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(405)
def handle_error(error):
    print(error)
    # set error status explicitly
    return render_template('not_allowed.html', title="Error"), error.code

# --------------------------------------------------
# Routes
# --------------------------------------------------

#Handles http://127.0.0.1:5000/
@app.route('/')
@app.route('/home')
@app.route('/index')
def index():
    return render_template('index.html', verified_jwt=Utils.validJWT())

#Handles http://127.0.0.1:5000/predict
@app.route("/predict", methods=['GET','POST'])
@cross_origin(origin='localhost',headers=['Content- Type','Authorization'])
def predict():
    # check if user is already logged in
    if Utils.validJWT() == False:
        abort(403)

    form = ImageForm()
    predicted = None
    probability = None
    filepath = None
    model = None

    if request.method == "POST":
        # first check if image is a jpeg or png using magic
        if ImgPredictionUtils.checkImageType(request.files['image']) == False:
            flash("Image must be a jpeg or png", "danger")

        # all other form validation checks
        elif form.validate_on_submit():
            # know which model to use
            url_idx = int(request.form['model'])

            # get image data from form
            image_data = request.files['image']

            # check if image data is less than 3MB
            if ImgPredictionUtils.checkImageSize(image_data):
                flash("Image size must be less than 3MB", "danger")
                return render_template("predict.html", form=form, predicted=predicted, probability=probability, filepath=filepath, model=model, verified_jwt=Utils.validJWT())

            # process image
            image = ImgPredictionUtils.processImage(image_data)

            # save image
            filename = ImgPredictionUtils.saveImage(image, image_data)
            filepath = folder + filename

            # send json request to server to get prediction and save to database
            current_url = model_apps[url_idx]
            predictions = ImgPredictionUtils.jsonPredictionRequest(image, current_url) # predictions are sorted by probabilities

            # get top 3 predictions. The current database can only store 3 predictions for each image
            probability, predicted = ImgPredictionUtils.getTopClasses(predictions, 3, url_idx)

            # get model name
            model = model_names[url_idx]

            # add entry to database
            id = PredictionEntryUtils.addEntry(
                filepath=filepath,
                model=model,
                pred1=predicted[0], proba1=probability[0],
                pred2=predicted[1], proba2=probability[1],
                pred3=predicted[2], proba3=probability[2],
                timestamp=datetime.utcnow()
            )

    return render_template("predict.html", form=form, predicted=predicted, probability=probability, filepath=filepath, model=model, verified_jwt=Utils.validJWT())

#Handles http://127.0.0.1:5000/history
@app.route("/history", methods=['GET'])
def history():
    # check if user is already logged in
    if Utils.validJWT() == False:
        abort(403)

    # get url query parameters
    model = request.args.get('model', default='All', type=str) # model name (coarse or fine)
    order = request.args.get('order', default='Latest', type=str) # order of sorting by timestamp (latest or earliest)
    entries = request.args.get('entries', default=9, type=int) # number of entries to display per page
    class_match = request.args.get('class', default='', type=str) # class name to filter by
    gotopage = request.args.get('gotopage', default=1, type=int) # page number to go to

    history = PredictionEntryUtils.getAll(gotopage=gotopage, entries=entries, order=order, model=model, class_match=class_match)

    return render_template("history.html", history=history, verified_jwt=Utils.validJWT())

# Deletes an entry from the database, and the image file from the static folder
@app.route("/delete", methods=['POST'])
def delete():
    # check if user is already logged in
    if Utils.validJWT() == False:
        abort(403)

    try:
        id = request.form.get('id')
        print("id is", id)

        # delete image from uploads folder
        filepath = PredictionEntryUtils.getEntry(id).filepath
        path_dir = os.path.dirname(os.path.abspath(__file__))[1:] # we do not want the slash at the start when using os.path.join
        abspath = "/" + os.path.join(path_dir, f"static/{filepath}")
        os.remove(abspath)

        # delete entry from database
        PredictionEntryUtils.deleteEntry(id)
        flash(f"Prediction entry of ID {id} deleted successfully", "success")
        # redirect back to history page
        return redirect(url_for('history'))

    except Exception as error:
        print(error)
        abort(500)

# Endpoint for login
@app.route('/login', methods=['GET', 'POST'])
def login():
    # check if user is already logged in
    if Utils.validJWT():
        abort(403)

    form = LoginForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            if form.username.data == 'devopspro' and form.password.data == 'hello':
                # create jwt for authorized user and save to session object
                # the session object will be gone when user closes the browser
                token = jwt.encode({'user': form.username.data, 'exp': datetime.utcnow() + timedelta(minutes=1440)}, app.config['SECRET_KEY'])
                session['jwt_token'] = token

                # redirect user to home page if login is successful
                flash("Successfully logged in", "success")
                return redirect(url_for('index'))
            else:
                flash("Username or Password is invalid", "danger")
                return render_template('login.html', form=form, verified_jwt=Utils.validJWT())

    # get request, reload login page
    return render_template('login.html', form=form, title="Login", verified_jwt=Utils.validJWT())

# Endpoint for logout
@app.route('/logout', methods=['GET'])
def logout():
    # check if user is logged in
    if Utils.validJWT():
        # remove jwt from session object
        session.pop('jwt_token', None)
        flash("Successfully logged out", "success")

        # redirect user to login page after successful logout
        return redirect(url_for('login'))
    else:
        abort(403)

### REST APIs for unit testing

# POST request to predict image class
@app.route('/api/predict', methods=['POST'])
def api_predict():
    try:
        # get image data and model of choice from form
        url_idx = int(request.form['model'])
        image_data = request.files['image']

        # check if model is valid
        if url_idx not in range(len(model_apps)):
            return jsonify({"error": "Invalid model"}), 400

        # check if image is uploaded
        if image_data.filename == '':
            return jsonify({"error": "No image uploaded"}), 400

        # check if image is jpg or png
        if ImgPredictionUtils.checkImageType(image_data) == False:
            return jsonify({"error": "Image must be jpg or png"}), 400

        # check if image data is less than 3MB
        if ImgPredictionUtils.checkImageSize(image_data):
            return jsonify({"error": "Image size must be less than 3MB"}), 400

        # process image
        image = ImgPredictionUtils.processImage(image_data)

        # send json request to server to get prediction and save to database
        current_url = model_apps[0]
        predictions = ImgPredictionUtils.jsonPredictionRequest(image, current_url)

        # get top predictions
        probability, predicted = ImgPredictionUtils.getTopClasses(predictions, 3, url_idx)

        # return json response
        return jsonify({
            "1st prediction": predicted[0], "1st probability": probability[0],
            "2nd prediction": predicted[1], "2nd probability": probability[1],
            "3rd prediction": predicted[2], "3rd probability": probability[2],
            }), 200

    except Exception as error:
        print(error)
        return jsonify({"error": "Internal server error"}), 500

# POST request to upload image to history
@app.route('/api/add/predict', methods=['POST'])
def api_add_entry():
    try:
        # get image data and model of choice from form
        url_idx = int(request.form['model'])
        image_data = request.files['image']

        # check if model is valid
        if url_idx not in range(len(model_apps)):
            return jsonify({"error": "Invalid model"}), 400

        # check if image is uploaded
        if image_data.filename == '':
            return jsonify({"error": "No image uploaded"}), 400

        # check if image is jpg or png
        if ImgPredictionUtils.checkImageType(image_data) == False:
            return jsonify({"error": "Image must be jpg or png"}), 400

        # check if image data is less than 3MB
        if ImgPredictionUtils.checkImageSize(image_data):
            return jsonify({"error": "Image size must be less than 3MB"}), 400

        # process image
        image = ImgPredictionUtils.processImage(image_data)

        # save image
        filename = ImgPredictionUtils.saveImage(image, image_data)
        filepath = folder + filename

        # send json request to server to get prediction and save to database
        current_url = model_apps[url_idx]
        predictions = ImgPredictionUtils.jsonPredictionRequest(image, current_url)

        # get top predictions
        probability, predicted = ImgPredictionUtils.getTopClasses(predictions, 3, url_idx)

        # save entry to database
        id = PredictionEntryUtils.addEntry(
            filepath=filepath,
            model=model_names[url_idx],
            pred1=predicted[0], proba1=probability[0],
            pred2=predicted[1], proba2=probability[1],
            pred3=predicted[2], proba3=probability[2],
            timestamp=datetime.utcnow()
        )

        # return json response
        return jsonify({
            "id": id,
            "1st prediction": predicted[0], "1st probability": probability[0],
            "2nd prediction": predicted[1], "2nd probability": probability[1],
            "3rd prediction": predicted[2], "3rd probability": probability[2],
            }), 200

    except Exception as error:
        print(error)
        return jsonify({"error": "Internal server error"}), 500

# DELETE request to delete an entry from the database by its id and the image file from the static folder
@app.route('/api/history/<id>', methods=['DELETE'])
def api_delete(id):
    try:
        id = int(id)

        # check if id less than 0
        if id <= 0:
            return jsonify({"error": "Invalid id"}), 400

        # check if entry exists
        if PredictionEntryUtils.getEntry(id) == None:
            return jsonify({"error": "Entry does not exist"}), 400

        # delete image from uploads folder
        filepath = PredictionEntryUtils.getEntry(id).filepath
        path_dir = os.path.dirname(os.path.abspath(__file__))[1:] # we do not want the slash at the start when using os.path.join
        abspath = "/" + os.path.join(path_dir, f"static/{filepath}")
        os.remove(abspath)

        # delete entry from database
        PredictionEntryUtils.deleteEntry(id)
        return jsonify({"message": "Successfully deleted"}), 200

    except Exception as error:
        print(error)
        return jsonify({"error": "Internal server error"}), 500

# GET request to get a prediction entry by its id
@app.route('/api/history/<id>', methods=['GET'])
def api_getEntry(id):
    try:
        id = int(id)

        # check if id less than 0
        if id <= 0:
            return jsonify({"error": "Invalid id"}), 400

        # get entry from database
        entry = PredictionEntryUtils.getEntry(id)
        print(entry)
        if entry == None:
            return jsonify({"error": "id does not exist"}), 400
        else:
            return jsonify({
                "id": entry.id,
                "model": entry.model,
                "1st_pred": entry.pred1, "1st_proba": entry.proba1,
                "2nd_pred": entry.pred2, "2nd_proba": entry.proba2,
                "3rd_pred": entry.pred3, "3rd_proba": entry.proba3,
                "timestamp": entry.timestamp
                }), 200

    except Exception as error:
        print(error)
        return jsonify({"error": "Internal server error"}), 500

ST1505 DOAA/CIFAR Image Predictor Web App/application/routes.py


from injector import inject

from vc.command.base import BaseCommand
from vc.service.abme import AbmeService, AbmeOptions

class AbmeCommand(BaseCommand):
    description = 'Runs abme on an input file'
    args = [
        {
            'dest': 'first_file',
            'type': str,
            'help': 'First file',
            'default': 'first.png',
            'nargs': '?',
        },
        {
            'dest': 'second_file',
            'type': str,
            'help': 'Second file',
            'default': 'second.png',
            'nargs': '?',
        },
        {
            'dest': 'output_file',
            'type': str,
            'help': 'Output file',
            'default': 'debug.png',
            'nargs': '?',
        },
    ]

    abme: AbmeService

    @inject
    def __init__(self, abme: AbmeService):
        self.abme = abme

    def handle(self, args):
        print('got %s %s %s' % (args.first_file, args.second_file, args.output_file))
        self.abme.handle(AbmeOptions(
            first_file=args.first_file,
            second_file=args.second_file,
            output_file=args.output_file,
        ))


import os
import random
import torch
from torch.utils.data import Dataset

def news_sample(news, ratio):
    if ratio > len(news):
        return news + [0] * (ratio - len(news))
    else:
        return random.sample(news, ratio)

class MindDataset(Dataset):
    def __init__(self,
                 data_dir,
                 args,
                 news_index,
                 news_combined,
                 word_dict):
        super(MindDataset, self).__init__()
        self.data_dir = data_dir
        self.device = args.device_id
        self.npratio = args.npratio
        self.user_log_length = args.max_hist_len
        self.batch_size = args.batch_size
        # data loader only cares about the config after tokenization.
        self.sampler = None
        self.epoch = -1

        self.news_combined = news_combined
        self.news_index = news_index
        self.word_dict = word_dict
        self.behaviors = self._process()

    def trans_to_nindex(self, nids):
        return [self.news_index[i] if i in self.news_index else 0 for i in nids]

    def pad_to_fix_len(self, x, fix_length):
        pad_x = x[:fix_length] + [0] * (fix_length - len(x))
        mask = [1] * min(fix_length, len(x)) + [0] * max(0, fix_length - len(x))
        return pad_x, mask

    def parser_text(self, indices):
        title_text = self.news_combined['title_text'][indices]
        title_mask = self.news_combined['title_mask'][indices]
        body_text = self.news_combined['body_text'][indices]
        body_mask = self.news_combined['body_mask'][indices]
        category = self.news_combined['category'][indices]
        sub_category = self.news_combined['sub_category'][indices]

        title_text = torch.LongTensor(title_text).cuda(self.device)
        title_mask = torch.LongTensor(title_mask).cuda(self.device)
        body_text = torch.LongTensor(body_text).cuda(self.device)
        body_mask = torch.LongTensor(body_mask).cuda(self.device)
        category = torch.LongTensor(category).cuda(self.device)
        sub_category = torch.LongTensor(sub_category).cuda(self.device)

        return title_text, title_mask, body_text, body_mask, category, sub_category

    def _process(self):
        pass

    def __getitem__(self, item):
        pass

    def __len__(self):
        return len(self.behaviors)

class DatasetTrain(MindDataset):
    def __init__(self,
                 data_dir,
                 args,
                 news_index,
                 news_combined,
                 word_dict):
        super().__init__(data_dir, args, news_index, news_combined, word_dict)

    def _process(self):
        examples = []

        with open(os.path.join(self.data_dir, 'behaviors.tsv'), 'r', encoding='utf-8') as behavior_file:
            for behavior_index, line in enumerate(behavior_file):
                impression_ID, user_ID, time, history, impressions = line.split('\t')

                click_impressions = []
                non_click_impressions = []
                for impression in impressions.strip().split(' '):
                    if impression[-2:] == '-1':
                        click_impressions.append(impression[:-2])
                    else:
                        non_click_impressions.append(impression[:-2])

                if len(history) != 0:
                    history = history.strip().split(' ')
                    user_history = list((history[-self.user_log_length:]))

                    examples.append(
                        [user_ID, user_history, click_impressions, non_click_impressions])
            return examples

    def __getitem__(self, item):

        label = 0
        user_idx, user_history, click_impressions, non_click_impressions = self.behaviors[item]

        click_docs, log_mask = self.pad_to_fix_len(self.trans_to_nindex(user_history),
                                                   self.user_log_length)

        sess_pos = self.trans_to_nindex(click_impressions)
        sess_neg = self.trans_to_nindex(non_click_impressions)

        if len(sess_pos) > 0:
            sess_pos = random.choices(sess_pos, k=1)

        if len(sess_neg) > 0:
            neg_index = news_sample(list(range(len(sess_neg))), self.npratio)
            sam_negs = [sess_neg[i] for i in neg_index]
        else:
            sam_negs = [0] * self.npratio
        sample_news = sess_pos + sam_negs

        user_features = self.parser_text(click_docs)
        news_features = self.parser_text(sample_news)

        log_mask = torch.FloatTensor(log_mask).cuda(self.device)
        label = torch.tensor(0).cuda(self.device)
        # label = torch.zeros(1)
        return user_features, log_mask, news_features, label

class DatasetTest(MindDataset):
    def __init__(self,
                 data_dir,
                 args,
                 news_index,
                 news_combined,
                 word_dict,
                 mode='dev'):
        super().__init__(data_dir, args, news_index, news_combined, word_dict)
        self.mode = mode

    def _process(self):
        examples = []

        with open(os.path.join(self.data_dir, 'behaviors.tsv'), 'r', encoding='utf-8') as behavior_file:
            for behavior_index, line in enumerate(behavior_file):
                impression_ID, user_ID, time, history, impressions = line.split('\t')
                candidates = [article for article in impressions.strip().split(' ')]
                history = history.strip().split(' ')

                if len(history) != 0:
                    user_history = list((history[-self.user_log_length:]))
                    examples.append([user_ID, user_history, candidates])
                else:
                    user_history = [0] * self.user_log_length
                    examples.append([user_ID, user_history, candidates])

            return examples

    def __getitem__(self, item):

        user_idx, user_history, impressions = self.behaviors[item]

        sess = [i.split('-')[0] for i in impressions]
        if self.mode == 'dev':
            labels = [int(i.split('-')[1]) for i in impressions]
        else:
            labels = [0] * len(impressions)

        click_docs, log_mask = self.pad_to_fix_len(self.trans_to_nindex(user_history),
                                                   self.user_log_length)
        sample_news = self.trans_to_nindex(sess)

        user_features = self.parser_text(click_docs)
        news_features = self.parser_text(sample_news)

        log_mask = torch.FloatTensor(log_mask).cuda(self.device)

        label = torch.tensor(labels).cuda(self.device)
        # label = torch.zeros(1)
        return user_features, log_mask, news_features, label


import os
from setuptools import setup
import subprocess

# REDONE 20171023 TO CONFORM GREGORY setup.py
#   #packages=[]   #  directory with __init__.py
#   #modules=[]    #  single files in root dir
# search for       PyPackMan.ipynb

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

def package_files(directory):
    paths = []
    for (path, directories, filenames) in os.walk(directory):
        for filename in filenames:
            paths.append(os.path.join('..', path, filename))
    return paths
extra_files = package_files('NuPhyPy/') # here i dont know 20171022

#############################   version trick with git:  must.install
version_py = os.path.join(os.path.dirname(__file__), 'version.py')
try:
    version_git = subprocess.check_output(["git", "describe"]).decode("utf8").rstrip()
    print("D... version=", version_git)
except:
    with open(version_py, 'r') as fh:
        version_git = open(version_py).read().strip().split('=')[-1].replace('"','')
version_msg="# Do not edit this file, pipeline versioning is governed by git tags"
with open(version_py, 'w') as fh:
    fh.write(version_msg + str(os.linesep) + "__version__=" + version_git)

################################
#
# MAIN PART
#
#################################

setup(
    name = "NuPhyPy",
     version="{ver}".format(ver=version_git),  # this comes from http://blogs.nopcode.org/brainstorm/2013/05/20/pragmatic-python-versioning-via-setuptools-and-git-tags/
    zip_safe= False,
    author = "jaromir mrazek",
    author_email = "jaromrax@gmail.com",
    description = ("A Nuclear Physics Python package "
                                   "to address many things: kinematics, srim, fresco,....."),
    license = "GPLv2",
    keywords = "nuclear physics",
    url = "http://www.spiral2.cz",
    py_modules=['NuPhyPy'],
    packages=['NuPhyPy'],
    package_data={'':extra_files},
    scripts=['bin/nuphy.py'],
    long_description=read('README.md'),
    classifiers=[
        "Development Status :: 2 - Pre-Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
    ],
)


import pytube
from pytube.cli import on_progress

# Ask user for YouTube video URL and download location
url = input("Enter YouTube video URL: ")
download_path = r"C:\Users\chadl\Downloads"

# Create YouTube object and download video
yt = pytube.YouTube(url,on_progress_callback=on_progress)
stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
stream.download(download_path)
print("Video downloaded successfully to", download_path)


import requests
import json
import statistics
from keras.preprocessing import image
import numpy as np

def process_image(img_path):
    img = image.load_img(img_path, target_size=(128, 128), grayscale=True) #target_size must agree with what the trained model expects!!

    # Preprocessing the image
    img = image.img_to_array(img) / 255
    img = np.expand_dims(img, axis=0)

    return img

new_image = process_image("/home/user/Desktop/OMSCS/DVA/xrayai-server/test_images/00000017_001.png").tolist()
raw = {"signature_name": "serving_default"}
raw["instances"] = new_image
data = json.dumps(raw)
# print(data)

headers = {"content-type": "application/json"}
json_response = requests.post('http://localhost:9000/v1/models/xrayai:predict', data=data, headers=headers)
labels = ['Atelectasis','Cardiomegaly','Consolidation','Edema','Effusion','Emphysema','Fibrosis','Infiltration','Mass','Nodule','Pleural_Thickening','Pneumonia','Pneumothorax']
res = {}
res["predictions"] = []

for prediction in json.loads(json_response.text)["predictions"]:
	percentage = ["{0:.2f}%".format(val*100) for val in prediction]
	percentage_label = dict(zip(labels, percentage))
	res["predictions"].append(percentage_label)

print(res)


from multiprocessing import Pool
import os
trajectories = range(0,8)

from subprocess import check_output
out = check_output(["sed '3q;d' conf_box_oriented.gro"],shell=True)
ref = ",".join(out.split()[-3:])

def convert_to_gro(traj):
    print(traj)
    fout = open("scripted_fix_md_output%i.dat" %(traj),"w")
    fout.write(
"""
# include topology info: this is needed to identify atom types
MOLINFO STRUCTURE=structure.pdb

# define all heavy atoms using GROMACS index file
# which can be created with gmx_mpi make_ndx
protein-h: GROUP NDX_FILE=index.ndx NDX_GROUP=Protein-H
protein: GROUP NDX_FILE=index.ndx NDX_GROUP=Protein
water: GROUP NDX_FILE=index.ndx NDX_GROUP=Water

# make protein whole: add reference position of first heavy atom (in nm)
WHOLEMOLECULES ADDREFERENCE ENTITY0=protein REF0=%s

DUMPATOMS STRIDE=1 FILE=traj%i.gro ATOMS=protein
""" %(ref,traj))
    fout.close()
    os.system("plumed driver --plumed scripted_fix_md_output{traj}.dat --mf_trr traj{traj}.trr".format(traj=traj))
    os.system("echo 1 | gmx_mpi trjconv -f traj{traj}.gro -o traj{traj}.pdb -skip 5 -e 12000".format(traj=traj))

# TO analyze water specifically FOLLOW RECIPE on page 18-19-20 of plumed_chapter.pdf

p = Pool(8)
print(p.map(convert_to_gro,range(0,8,1)))