#python 3.0 이상부터는 urllib 보다 requests 모듈이 더 효율적

import sys
import io
import requests

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

s = requests.Session()
#r = s.get('https://www.naver.com')#PUT(FETCH), DELETE, GET, POST
#print('1',r.text)

#r = s.get('http://httpbin.org/cookies',cookies={'from' : 'myName'}) # 들어가보면 요청한 것을 반환해주는 연습 사이트
#print(r.text) #cookies 를 response해줌

url = 'http://httpbin.org/get'
headers = {'user-agent':'myPythonApp_1.0.0'}

#r = s.get(url, headers=headers)
#print(r.text)

s.close()

with requests.Session() as s:
    r = s.get('https://www.naver.com')
    print(r.text)


#!/usr/bin/env python

import rospy
import rosbag
import tf

import geometry_msgs.msg
import nav_msgs.msg
import sensor_msgs.msg
import std_msgs.msg

import argparse
import matplotlib.pyplot as plt
import numba
import numpy as np
import skimage.draw
import sys
import copy

import numpy as np

dtype = np.float64

LINEAR_MODEL_VAR_X = 0.5
LINEAR_MODEL_VAR_Y = 0.5
ANGULAR_MODEL_VAR = 0.3
SENSOR_MODEL_VAR = 15.0
NUM_PARTICLES = 2000

@numba.jit(nopython=True)
def _CastRay(p0, p1, grid_data):
    '''
    Cast a ray from p0 in the direction toward p1 and return the first cell the
    sensor will detect.
    '''
    delta = np.abs(np.array([p1[0] - p0[0], p1[1] - p0[1]]))
    p = p0.copy()
    n = 1 + np.sum(delta)
    inc = np.array([1 if p1[0] > p0[0] else -1, 1 if p1[1] > p0[1] else -1])
    error = delta[0] - delta[1]
    delta *= 2

    last_p = p.copy()
    for i in range(n):
        if not (0 <= p[0] < grid_data.shape[1]
                and 0 <= p[1] < grid_data.shape[0]):
            return last_p
        last_p = p.copy()

        if grid_data[p[1], p[0]] != 0:  # coord flip for handedness
            return p

        if error > 0:
            p[0] += inc[0]
            error -= delta[1]
        else:
            p[1] += inc[1]
            error += delta[0]

    return p1

@numba.jit(nopython=True)
def _ComputeSimulatedRanges(scan_angles, scan_range_max, world_t_map,
                            map_t_particle, map_R_particle, grid_data,
                            grid_resolution):
    '''
    Cast rays in all directions and calculate distances to obstacles in all
    directions from a given particle.
    '''
    src_cell = ((map_t_particle - world_t_map) / grid_resolution).astype(
        np.int32)

    # Compute max possible cell indices
    angles = scan_angles + map_R_particle
    max_points = map_t_particle + scan_range_max * np.stack(
        (np.cos(angles), np.sin(angles)), axis=-1)
    max_cells = ((max_points - world_t_map) / grid_resolution).astype(np.int32)

    simulated_cell_ranges_sq = np.zeros_like(scan_angles)
    for i in range(len(max_cells)):
        hit_cell = _CastRay(src_cell, max_cells[i], grid_data)
        simulated_cell_ranges_sq[i] = np.sum(np.square(hit_cell - src_cell))

    return grid_resolution * np.sqrt(simulated_cell_ranges_sq)

@numba.jit(nopython=True)
def RotateBy(x, angle):
    c, s = np.cos(angle), np.sin(angle)
    m = np.zeros((2, 2), dtype=dtype)
    m[0, 0] = c
    m[0, 1] = -s
    m[1, 0] = s
    m[1, 1] = c
    return np.dot(m, x)

class Pose(object):
    '''
    2D pose representation of robot w.r.t. world frame.
    Given by the translation from origin to the robot, and then rotation to math
    robot orientation.
    '''
    def __init__(self, rotation=0, translation=[0, 0]):
        self.rotation = rotation
        self.translation = np.array(translation, dtype=dtype)

    @staticmethod
    def FromGeometryMsg(pose_msg):
        q = pose_msg.orientation
        tx = 1 - 2 * (q.y * q.y + q.z * q.z)
        ty = 2 * (q.x * q.y + q.z * q.w)
        yaw = np.arctan2(ty, tx)
        x, y = pose_msg.position.x, pose_msg.position.y
        return Pose(yaw, [x, y])

    def ToGeometryMsg(self):
        msg = geometry_msgs.msg.Pose()
        msg.position.x = self.translation[0]
        msg.position.y = self.translation[1]
        msg.orientation.z = np.sin(self.rotation / 2)
        msg.orientation.w = np.cos(self.rotation / 2)
        return msg

class Grid(object):
    '''
    Occupancy grid representation, with coordinate helpers.
    World is global coordinate frame and grid is index-based array coordinates.
    '''
    def __init__(self, grid_msg):
        self.cols = grid_msg.info.width
        self.rows = grid_msg.info.height
        self.resolution = grid_msg.info.resolution
        self.map_t_extents = [
            self.cols * self.resolution,
            self.rows * self.resolution,
        ]
        self.world_T_map = Pose.FromGeometryMsg(grid_msg.info.origin)
        self.data = np.asarray(grid_msg.data,
                               dtype=np.int8).reshape(self.rows, self.cols)

    def GridToWorld(self, cell_index):
        return self.resolution * cell_index + self.world_T_map.translation

    def WorldToGrid(self, point):
        return ((point - self.world_T_map.translation) /
                self.resolution).astype(np.int32)

    def GetWorldCoords(self, point):
        x, y = self.WorldToGrid(point)
        return self.data[y, x]

    def ToNavMsg(self):
        msg = nav_msgs.msg.OccupancyGrid()
        msg.info.resolution = self.resolution
        msg.info.width = self.cols
        msg.info.height = self.rows
        msg.info.origin = self.world_T_map.ToGeometryMsg()
        msg.data = self.data.flatten().tolist()
        return msg

class Scan(object):
    '''
    Scan representation / cache.
    Scan consists of angles and distances (ranges).
    '''
    def __init__(self, scan_msg):
        angle_min = scan_msg.angle_min
        angle_max = scan_msg.angle_max
        self.range_max = scan_msg.range_max

        self.angles = np.linspace(angle_min,
                                  angle_max,
                                  num=len(scan_msg.ranges))

        self.ranges = np.array(scan_msg.ranges, dtype=dtype)

class Particle(object):
    '''Individual particle representation.'''
    def __init__(self, grid, map_T_particle=None):
        if map_T_particle is None:
            ll = grid.world_T_map.translation
            ul = ll + grid.map_t_extents

            found = False
            while not found:
                translation = np.random.uniform(ll, ul)
                if grid.GetWorldCoords(translation) == 0:
                    break

            map_T_particle = Pose(rotation=np.random.uniform(-np.pi, np.pi),
                                  translation=translation)

        self.grid = grid
        self.map_T_particle = map_T_particle
        self.last_odom_timestamp = None

    def UpdateOdom(self, odom_msg):
        '''Propagate this particle according to the sensor data and motion model.'''
        if self.last_odom_timestamp is not None:
            dt = (odom_msg.header.stamp - self.last_odom_timestamp).to_sec()
        else:
            dt = 0
        self.last_odom_timestamp = odom_msg.header.stamp

        #  Extract the particle's velocity in its local frame
        vel_x = odom_msg.twist.twist.linear.x
        vel_y = odom_msg.twist.twist.linear.y
        #  Add noise to the velocity, with component variance
        vel_x += np.random.normal(0, np.sqrt(LINEAR_MODEL_VAR_X))
        vel_y += np.random.normal(0, np.sqrt(LINEAR_MODEL_VAR_Y))
        #  Transform the linear velocity into map frame
        vel = np.array([vel_x, vel_y]).reshape((2, 1))
        curr_pose = self.map_T_particle
        actual_vel = RotateBy(vel, curr_pose.rotation)
        #  Integrate the linear velocity to the particle pose
        self.map_T_particle.translation += (actual_vel * dt).reshape((-1))
        #  Extract the particle's rotational velocity
        rotational_vel = odom_msg.twist.twist.angular.z
        #  Add noise to the rotational velocity
        rotational_vel += np.random.normal(0, np.sqrt(ANGULAR_MODEL_VAR))
        #  Integrate the rotational velocity into the particle pose
        self.map_T_particle.rotation += rotational_vel * dt

    def _ComputeSimulatedRanges(self, scan):
        translation = self.map_T_particle.translation
        rotation = self.map_T_particle.rotation
        return _ComputeSimulatedRanges(scan.angles, scan.range_max,
                                       self.grid.world_T_map.translation,
                                       translation, rotation, self.grid.data,
                                       self.grid.resolution)

    def UpdateScan(self, scan):
        '''
        Calculate weights of particles according to scan / map matching.
        '''
        sim_ranges = self._ComputeSimulatedRanges(scan)

        #  Compute the likelihood of each beam, compared to sim_ranges
        probs = []
        for i in range(len(sim_ranges)):
            r = scan.ranges[i]
            r_est = sim_ranges[i]
            p = 1 / np.sqrt(2 * np.pi * SENSOR_MODEL_VAR) * np.exp(-pow(r - r_est, 2) / (2 * SENSOR_MODEL_VAR))
            probs.append(p)
        #  Assign the weight of this particle
        prod = 1
        for i in probs:
            prod *= i
        self.weight = prod

class ParticleFilter(object):
    def __init__(self, grid, num_particles):
        self.grid = grid
        self.last_timestamp = None
        self.particles = [Particle(grid) for _ in range(num_particles)]
        self.pose_publisher = rospy.Publisher("pose_hypotheses",
                                              geometry_msgs.msg.PoseArray,
                                              queue_size=10)
        self.scan_publisher = rospy.Publisher("lidar",
                                              sensor_msgs.msg.LaserScan,
                                              queue_size=10)
        self.tf_broadcaster = tf.TransformBroadcaster()

    def GetMeanPose(self):
        sum_translation = np.zeros(2, dtype=dtype)
        sum_rotation = 0
        for particle in self.particles:
            sum_translation += particle.map_T_particle.translation
            sum_rotation += particle.map_T_particle.rotation

        avg_translation = sum_translation / len(self.particles)
        avg_rotation = sum_rotation / len(self.particles)

        return Pose(avg_rotation, avg_translation)

    def GetPoseArray(self):
        msg = geometry_msgs.msg.PoseArray()
        msg.poses = [
            particle.map_T_particle.ToGeometryMsg()
            for particle in self.particles
        ]
        msg.header.stamp = self.last_timestamp
        msg.header.frame_id = "map"

        return msg

    def UpdateOdom(self, odom_msg):
        if self.last_timestamp is None:
            self.last_timestamp = odom_msg.header.stamp

        for particle in self.particles:
            particle.UpdateOdom(odom_msg)

    def UpdateScan(self, scan_msg):
        if self.last_timestamp is None:
            self.last_timestamp = scan_msg.header.stamp

        scan = Scan(scan_msg)

        # Update weights for particles.
        for particle in self.particles:
            particle.UpdateScan(scan)

        #  Each particle has a weight
        weights = np.array([self.particles[i].weight for i in range(len(self.particles))])
        #  Use these weights to build a normalized discrete probability distribution for sampling each particle
        norm_weights = weights / np.sum(weights)
        #  Randomly choose the indices of the particles to propagate
        select_indices = np.random.choice(NUM_PARTICLES, size=NUM_PARTICLES, p=norm_weights)
        #  Copy the proper particles into a new population
        select_particles = [copy.deepcopy(self.particles[i]) for i in select_indices]
        #  Set self.particles to the new population of particles.
        self.particles = select_particles

        # Publish cloud for visualization.
        self.pose_publisher.publish(self.GetPoseArray())
        avg_pose = self.GetMeanPose()
        avg_trans = (avg_pose.translation[0], avg_pose.translation[1], 0)
        avg_quat = tf.transformations.quaternion_from_euler(
            0, 0, avg_pose.rotation)
        self.tf_broadcaster.sendTransform(avg_trans, avg_quat,
                                          scan_msg.header.stamp, "robot",
                                          "map")
        self.scan_publisher.publish(scan_msg)

def main():
    # rospy initialization.
    rospy.init_node('usc545mcl')
    args = rospy.myargv(argv=sys.argv)

    argparser = argparse.ArgumentParser()
    argparser.add_argument('--num_particles', type=int, default=NUM_PARTICLES)
    argparser.add_argument('bag_filename', type=str)
    args = argparser.parse_args(args[1:])

    # Wait for map server to start up.
    print("Waiting for map_server...")
    grid_msg = rospy.wait_for_message("/map", nav_msgs.msg.OccupancyGrid)
    print("Map received.")

    # Construct particle filter for the received map.
    grid = Grid(grid_msg)
    mcl = ParticleFilter(grid, args.num_particles)

    # "Subscribe" to message channels from bag.
    # You can see the relevant channels by running
    # $ rosbag info /path/to/bag
    bag = rosbag.Bag(args.bag_filename)
    subscribers = {"lidar": mcl.UpdateScan, "odom": mcl.UpdateOdom}
    x_err = []
    y_err = []
    yaw_err = []
    for topic, msg, t in bag.read_messages(topics=["gt_odom"] +
                                           subscribers.keys()):
        if topic == "gt_odom":
            x = msg.pose.pose.position.x
            y = msg.pose.pose.position.y
            yaw = 2 * np.arcsin(msg.pose.pose.orientation.z)

            mcl_pose = mcl.GetMeanPose()
            x_err.append((x - mcl_pose.translation[0])**2)
            y_err.append((y - mcl_pose.translation[1])**2)
            yaw_err.append((yaw - mcl_pose.rotation)**2)

        else:
            subscribers[topic](msg)

    plt.plot(x_err, label="x_mse")
    plt.plot(y_err, label="y_mse")
    plt.plot(yaw_err, label="yaw_mse")
    plt.legend()
    plt.show()

if __name__ == '__main__':
    main()


from django.core.management.base import BaseCommand
from places.models import Place, Image
from django.core.files.base import ContentFile
from io import BytesIO
import requests
import json
import os

def update_or_create_place(place_dataset):

    place, created = Place.objects.update_or_create(
        title=place_dataset['title'],
        defaults={
            'description_short': place_dataset.get('description_short', ''),
            'description_long': place_dataset.get('description_long', ''),
            'lon': place_dataset['coordinates']['lng'],
            'lat': place_dataset['coordinates']['lat']
        }

    )

    if not created:
        place.photos.all().delete()

    related_images = []
    for img_number, img_url in enumerate(place_dataset['imgs'], 1):
        image = requests.get(img_url)
        image_binary_content = BytesIO(image.content).read()
        image_content_file = ContentFile(image_binary_content, name=f'{img_number} {place.title}.jpg')
        current_image = Image(
            location=place,
            photo=image_content_file
        )

        related_images.append(current_image)

    Image.objects.bulk_create(related_images)

class Command(BaseCommand):
    help = 'Creating or update a place model from JSON-file'

    def add_arguments(self, parser):
        parser.add_argument('place_dir', nargs='+', type=str)

    def handle(self, *args, **options):
        file_path = options['place_dir'][0]

        if file_path.startswith(('https://', 'http://',)):
            place_datasets = [requests.get(file_path).json() for file_path in file_path.split()]
        else:
            place_datasets = []
            for root, dirs, files in os.walk(file_path):
                for file in files:
                    if not file.endswith('.json'):
                        continue
                    with open(os.path.join(root, file), encoding='utf8') as place_file:
                        place_dataset = json.load(place_file)
                    place_datasets.append(place_dataset)

        for place_dataset in place_datasets:
            update_or_create_place(place_dataset)


import os
import argparse
import sys
from easydev.console import red, purple, darkgreen
from easydev import DevTools
from dreamtools import Challenge
import dreamtools
import textwrap

def get_challenge(challenge_name):
    """Test validity of the challenge and returns an instance

    :param challenge_name: a valid name e.g. D5C3, D9dot5C1
    :return: instance of the challenge
    """
    try:
        c = Challenge(challenge_name)
    except:
        txt = "The challenge %s could not be found. " % challenge_name
        txt += "See --help for valid names"
        print_color(txt, red)
        sys.exit()
    return c

# Define the simple scoring functions here below
def generic_scoring(challenge_name, filename, subname=None, goldstandard=None):
    c = get_challenge(challenge_name)
    class_inst = c.import_scoring_class()
    try:
        score = class_inst.score(filename, subname=subname,
                                 goldstandard=goldstandard)
    except:
        try:
            score = class_inst.score(filename, subname=subname)
        except:
            try:
                score = class_inst.score(filename)
            except Exception as err:
                msg = "Error was caught in dreamtools.\n"
                msg += "Most probably an incorrect file format.\n"
                msg += "Here is the full error message to report it if needed\n"
                print_color(msg, red)
                raise err
    return {'Results': score}

# Figure out if the challenge exists and what are the sub challenges
def get_subchallenges(challenge_name):
    c = get_challenge(challenge_name)
    class_inst = c.import_scoring_class()
    return class_inst.sub_challenges

# ------------------------------------------------ The User Interface
def print_color(txt, func_color, underline=False):
    import easydev
    try:
        if underline:
            print(easydev.underline(func_color(txt)))
        else:
            print(func_color(txt))
    except:
        print(txt)

def scoring(args=None):
    """This function is used by the standalone application called dreamscoring

    ::

        dreamscoring --help

    """
    d = DevTools()

    if args is None:
        args = sys.argv[:]
    user_options = Options(prog="dreamtools")

    if len(args) == 1:
        user_options.parse_args(["prog", "--help"])
    else:
        options = user_options.parse_args(args[1:])

    if options.version is True:
        print("%s" % dreamtools.version)
        sys.exit()

    # Check on the challenge name
    if options.challenge is None:
        print_color('--challenge must be provided', red)
        sys.exit()
    else:
        options.challenge = options.challenge.upper()
        options.challenge = options.challenge.replace('DOT', 'dot')

        from dreamtools.admin.download_data import get_challenge_list
        if options.challenge not in get_challenge_list():
            print_color("This challenge %s is not registered in dreamtools." %
                    options.challenge, red)
            print("Here is the list of registered challenges: " +
                ", ".join(get_challenge_list()))
            sys.exit()

    # Check that the challenge can be loaded
    class_inst = get_challenge(options.challenge)
    try:
        this = class_inst.import_scoring_class()
    except NotImplementedError as err:
        print("\n"+str(err))
        sys.exit()
    else:
        # User may just request some information about the challenge.
        if options.info is True:
            print(this)
            sys.exit()
        elif options.onweb is True:
            this.onweb()
            sys.exit()

    # Checks name of the sub-challenges
    subchallenges = get_subchallenges(options.challenge)

    if len(subchallenges) and options.sub_challenge is None:
        txt = "This challenge requires a sub challenge name. "
        txt += "Please use --sub-challenge followed by one value in %s " % subchallenges
        print_color(txt, red)
        sys.exit(0)

    if options.sub_challenge is not None and len(subchallenges) != 0:
        try:
            d.check_param_in_list(options.sub_challenge, subchallenges)
        except ValueError as err:
            txt = "DREAMTools error: unknown sub challenge or not implemented"
            txt += "--->" + str(err)
            print_color(txt, red)
            sys.exit()

    # maybe users just need a template
    if options.download_template is True:
        c = Challenge(options.challenge)
        class_inst = c.import_scoring_class()
        if options.sub_challenge is None:
            print(class_inst.download_template())
        else:
            print(class_inst.download_template(options.sub_challenge))
        return

    # similary for the GS
    if options.download_goldstandard is True:
        c = Challenge(options.challenge)
        class_inst = c.import_scoring_class()
        if options.sub_challenge is None:
            print(class_inst.download_goldstandard())
        else:
            print(class_inst.download_goldstandard(options.sub_challenge))
        return

    # finally, we need a submission
    if options.filename is None:
        txt = "---> filename not provided. You must provide a filename with correct format\n"
        txt += "You may get a template using --download-template \n"
        txt += "Alternatively, you can user either --info or --onweb option to get information about the challenge.\n"
        txt += "https://github.com/dreamtools/dreamtools, or http://dreamchallenges.org\n"
        print_color(txt, red)
        sys.exit()

    # filename
    # filename in general is a single string but could be a list of filenames
    # Because on the parser, we must convert the string into a single string
    # if the list haa a length of 1
    for filename in options.filename:
        if os.path.exists(filename) is False:
            raise IOError("file %s does not seem to exists" % filename)
    if len(options.filename) == 1:
        options.filename = options.filename[0]

    print_color("DREAMTools scoring", purple, underline=True)
    print('Challenge %s (sub challenge %s)\n\n' % (options.challenge,
        options.sub_challenge))

    res = generic_scoring(options.challenge,
            options.filename,
            subname=options.sub_challenge,
            goldstandard=options.goldstandard)

    txt = "Solution for %s in challenge %s" % (options.filename,
            options.challenge)

    if options.sub_challenge is not None:
        txt += " (sub-challenge %s)" % options.sub_challenge
    txt += " is :\n"

    for k in sorted(res.keys()):
        txt += darkgreen("     %s:\n %s\n" %(k, res[k]))
    print(txt)

class Options(argparse.ArgumentParser):
    description = "tests"
    def __init__(self, prog=None):

        usage = """usage: python %s --challenge D8C1 --sub-challenge SC1A --submission <filename>\n""" % prog
        usage += """      python %s --challenge D5C2 --submission <filename>""" % prog
        epilog="""Author(s): Thomas Cokelaer (DREAMTools framework) and authors from
the DREAM consortium. Please see the scoring files headers for details
and the GitHub repository.

Source code on: https://github.com/dreamtools/dreamtools
Issues or bug report ? Please fill an issue on http://github.com/dreamtools/dreamtools/issues """
        description = """General Description:
    You must provide the challenge alias (e.g., D8C1 for DREAM8, Challenge 1)
    and if there were several sub-challenges, you also must provide the
    sub-challenge alias (e.g., sc1). Finally, the submission has to be
    provided. The format must be in agreement with the description of the
    challenge itself.

    Help and documentation about the templates may be found either within
    the online documentation http://pythonhosted.org/dreamtools/ or within
    the source code hosted on github http://github.org/dreamtools/dreamtools.

    Registered challenge so far (and sub-challenges) are:
"""
        # FIXME : not robust but will work for now
        registered = sorted([x for x in dir(dreamtools) if x.startswith('D')
                 and 'C' in x])

        for this in textwrap.wrap(", ".join(registered), 80):
            description += this + "\n"
        super(Options, self).__init__(usage=usage, prog=prog,
                epilog=epilog, description=description,
                formatter_class=argparse.RawDescriptionHelpFormatter)
        self.add_input_options()

    def add_input_options(self):
        """The input oiptions.

        Default is None. Keep it that way because otherwise, the contents of
        the ini file is overwritten in :class:`apps.Apps`.
        """
        group = self.add_argument_group("General",
                "General options (compulsary or not)")

        group.add_argument("--version", dest='version',
                         action="store_true",
                         help="verbose option.")

        group.add_argument("--challenge", dest='challenge',
                         default=None, type=str,
                         help="alias of the challenge (e.g., D8C1 stands for"
                         "dream8 challenge 1).")
        group.add_argument("--sub-challenge", dest='sub_challenge',
                         default=None, type=str,
                         help="Name of the data files")
        group.add_argument("--verbose", dest='verbose',
                         action="store_true",
                         help="verbose option.")
        group.add_argument("--submission", dest='filename', nargs='*',
                         help="submission/filename to score.")
        group.add_argument("--filename", dest='filename', nargs='*',
                         help="submission/filename to score.")
        group.add_argument("--gold-standard", dest='goldstandard',
                         help="""a gold standard filename. This may be
                         required in some challenges e.g. D2C3""")
        group.add_argument("--onweb", dest='onweb',action='store_true',
                help="Open synapse project page in a browser")
        group.add_argument("--info", dest='info', action="store_true",
                help="Prints general information about the challenge")
        group.add_argument("--download-template",
                dest='download_template',
                help="""Download template. Templates for challenge may be
                downloaded using this option. It returns the path to
                template.""",
                action='store_true')
        group.add_argument("--download-gold-standard",
                dest='download_goldstandard',
                help="""Download a gold standard, which can be used as a
                submissions as well. It returns the location of the file.""",
                action='store_true')

if __name__ == "__main__":
    scoring(sys.argv)


from array import array
from app import app, db

familia = db.Table(
    'related',
    db.Column('main_id', db.Integer, db.ForeignKey('monster.id')),
    db.Column('subspecies_id', db.Integer, db.ForeignKey('monster.id'))
)

class Subgroup(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    division = db.Column(db.String(140), index=True, unique=True, nullable=False)

    monster = db.relationship('Monster', backref='subgroup', lazy=True)

    def __init__(self, division: str):
        self.division = division

    @staticmethod
    def create(division):  # create Subgroup
        new_form = Subgroup(division)
        db.session.add(new_form)
        db.session.commit()

class Phylum(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    category = db.Column(db.String(140), unique=True, nullable=False)
    codename = db.Column(db.String(100), index=True, unique=True, nullable=False)

    monster = db.relationship('Monster', backref='phylum', lazy=True)

    #def __repr__(self):
        #return '<Phylum {}>'.format(self.name)

    def __init__(self, category: str, codename: str):
        self.category = category
        self.codename = codename

    @staticmethod
    def create(category, codename):  # create Phylum
        new = Phylum(category, codename)
        db.session.add(new)
        db.session.commit()

class Videogames(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    gamename = db.Column(db.String(140), unique=True, nullable=False)
    shortname = db.Column(db.String(100), index=True, unique=True, nullable=False)

    egames = db.relationship('Egames', backref='videogames', lazy=True)

    def __init__(self, gamename: str, shortname: str):
        self.gamename = gamename
        self.shortname = shortname

    @staticmethod
    def create(gamename, shortname):  # create Vidoegames
        new = Videogames(gamename, shortname)
        db.session.add(new)
        db.session.commit()

class Monster(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), index=True, unique=True, nullable=False)
    generation = db.Column(db.Integer, index=True, nullable=False)
    group = db.Column(db.String(100), db.ForeignKey('phylum.codename'), nullable=False)
    variation = db.Column(db.Integer, db.ForeignKey('subgroup.id'), nullable=False)

    weakpoints = db.relationship('Weakpoints', backref='monster', lazy=True, uselist=False)
    weakness = db.relationship('Weakness', backref='monster', lazy=True, uselist=False)
    proficiency = db.relationship('Proficiency', backref='monster', lazy=True, uselist=False)
    item_weak = db.relationship('Item_weak', backref='monster', lazy=True, uselist=False)
    ailments = db.relationship('Ailments', backref='monster', lazy=True, uselist=False)
    egames = db.relationship('Egames', backref='monster', lazy=True)

    relative = db.relationship(
        'Monster', secondary=familia,
        primaryjoin=(familia.c.main_id == id),
        secondaryjoin=(familia.c.subspecies_id == id),
        backref=db.backref('familia', lazy='dynamic'), lazy='dynamic')

    def __repr__(self):
            return '<Monster {}>'.format(self.name)

    def __init__(self, name: str, generation: int, group: str, variation: int):
        self.name = name
        self.generation = generation
        self.group = group
        self.variation = variation

    @staticmethod
    def create(self):
        db.session.add(self)
        db.session.commit()

    def __repr__(self):
        return '<Monster {}>'.format(self.name)

    def family(self, monster):
        if not self.is_family(monster):
            self.relative.append(monster)
            monster.relative.append(self)

    def is_family(self, monster):
        return self.relative.filter(
            familia.c.subspecies_id == monster.id).count() > 0

# Items Monsters are weak to
class Item_weak(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    mon_id = db.Column(db.Integer, db.ForeignKey('monster.id'), nullable=False)
    shock_trap = db.Column(db.Boolean)
    pitfall_trap = db.Column(db.Boolean)
    flash_bomb = db.Column(db.Boolean)
    sonic_bomb = db.Column(db.Boolean)

    def __repr__(self):
        return '<Item_weak {}>'.format(self.shock_trap)

    def __init__(self, mon_id: int, shock_trap: bool, pitfall_trap: bool, flash_bomb: bool, sonic_bomb: bool):
        self.mon_id = mon_id
        self.shock_trap = shock_trap
        self.pitfall_trap = pitfall_trap
        self.flash_bomb = flash_bomb
        self.sonic_bomb = sonic_bomb

    # Add item weakness to the database
    @staticmethod
    def applyItemWeakness(mon_id: int, shock_trap: bool, pitfall_trap: bool, flash_bomb: bool, sonic_bomb: bool):
        new = Item_weak(mon_id, shock_trap, pitfall_trap, flash_bomb, sonic_bomb)
        db.session.add(new)
        db.session.commit()

    # All boss monsters are immuned to items
    @staticmethod
    def elderBlock(mon_id: int):
        new = Item_weak(mon_id, False, False, False, False)
        db.session.add(new)
        db.session.commit()

# Defines the monster's elemental and status weaknesses
class Weakness(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    mon_id = db.Column(db.Integer, db.ForeignKey('monster.id'), nullable=False)
    fire = db.Column(db.Boolean)
    water = db.Column(db.Boolean)
    thunder = db.Column(db.Boolean)
    ice = db.Column(db.Boolean)
    dragon = db.Column(db.Boolean)
    poison = db.Column(db.Boolean)
    sleep = db.Column(db.Boolean)
    para = db.Column(db.Boolean)
    blast = db.Column(db.Boolean)

    def __repr__(self):
        return '<Weakness {}>'.format(self.mon_id)

    # Add the elemental weaknesses to the database if the monsters have more than 1 weakness
    def applyWeaknessElement(self, mon_id: int, fire: bool, water: bool, thunder: bool, ice: bool, dragon: bool):
        self.mon_id = mon_id
        self.fire = fire
        self.water = water
        self.thunder = thunder
        self.ice = ice
        self.dragon = dragon
        db.session.add(self)
        db.session.commit()

    # Add a elemental weakness to the database if the monsters ONLY HAVE 1 weakness
    def onlyWeakElement(self, mon_id: int, element: str):
        self.mon_id = mon_id
        self.fire = False
        self.water = False
        self.thunder = False
        self.ice = False
        self.dragon = False

        if  element == 'fire':
            self.fire = True
        elif  element == 'water':
            self.water = True
        elif  element == 'thunder':
            self.thunder = True
        elif  element == 'ice':
            self.ice = True
        elif  element == 'dragon':
            self.dragon = True

        db.session.add(self)
        db.session.commit()

    # Add the status weaknesses to the database if the monsters have more than 1 weakness
    def applyWeaknessStatus(self, mon_id: int, poison: bool, sleep: bool, para: bool, blast: bool):
        self.mon_id = mon_id
        self.poison = poison
        self.sleep = sleep
        self.para = para
        self.blast = blast
        db.session.add(self)
        db.session.commit()

    # Add a staus weakness to the database if the monsters ONLY HAVE 1 weakness
    def onlyWeakStatus(self, mon_id: int, status: str):
        self.mon_id = mon_id
        self.poison = False
        self.sleep = False
        self.para = False
        self.blast = False

        if  status == 'poison':
            self.poison = True
        elif  status == 'sleep':
            self.sleep = True
        elif  status == 'para':
            self.para = True
        elif  status == 'blast':
            self.blast = True

        db.session.add(self)
        db.session.commit()

    # Add a no weakness to the database if the monsters doesn't have any weakness
    def noWeaknessStatus(self, mon_id: int):
        self.mon_id = mon_id
        self.poison = False
        self.sleep = False
        self.para = False
        self.blast = False
        db.session.add(self)
        db.session.commit()

# Defines the monster's elemental attacks
class Proficiency(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    mon_id = db.Column(db.Integer, db.ForeignKey('monster.id'), nullable=False)
    fire = db.Column(db.Boolean)
    water = db.Column(db.Boolean)
    thunder = db.Column(db.Boolean)
    ice = db.Column(db.Boolean)
    dragon = db.Column(db.Boolean)

    def __repr__(self):
        return '<Proficiency {}>'.format(self.water)

    def __init__(self, mon_id: int, fire: bool, water: bool, thunder: bool, ice: bool, dragon: bool):
        self.mon_id = mon_id
        self.fire = fire
        self.water = water
        self.thunder = thunder
        self.ice = ice
        self.dragon = dragon

    # Adds elemental attacks for monsters that have more than on element
    @staticmethod
    def applyStrenghts(mon_id: int, fire: bool, water: bool, thunder: bool, ice: bool, dragon: bool):
        new = Proficiency(mon_id, fire, water, thunder, ice, dragon)
        db.session.add(new)
        db.session.commit()

    # Does not Add any elemental attack for monsters that don't have
    @staticmethod
    def noElement(mon_id: int):
        new = Proficiency(mon_id, False, False, False, False, False)
        db.session.add(new)
        db.session.commit()

    # Adds 1 elemental attack for monsters that only have one
    def onlyGoodAt(mon_id: int, element: str):
        if  element == 'fire':
            new = Proficiency(mon_id, fire=True, water=False, thunder=False, ice=False, dragon=False)

        elif  element == 'water':
            new = Proficiency(mon_id, fire=False, water=True, thunder=False, ice=False, dragon=False)

        elif  element == 'thunder':
            new = Proficiency(mon_id, fire=False, water=False, thunder=True, ice=False, dragon=False)

        elif  element == 'ice':
            new = Proficiency(mon_id, fire=False, water=False, thunder=False, ice=True, dragon=False)

        elif  element == 'dragon':
            new = Proficiency(mon_id, fire=False, water=False, thunder=False, ice=False, dragon=True)

        db.session.add(new)
        db.session.commit()

class Weakpoints(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    cut = db.Column(db.String(140))
    impact = db.Column(db.String(140))
    projectile = db.Column(db.String(140))
    mon_id = db.Column(db.Integer, db.ForeignKey('monster.id'), nullable=False)

    def __repr__(self):
        return '<Weakpoints {}>'.format(self.cut)

    def __init__(self, mon_id: int, cut: str, impact: str, projectile: str):
        self.mon_id = mon_id
        self.cut = cut
        self.impact = impact
        self.projectile = projectile

    @staticmethod
    def createWeakPoints(mon_id: int, cut: str, impact: str, projectile: str):
        new = Weakpoints(mon_id, cut, impact, projectile)
        db.session.add(new)
        db.session.commit()

#Defines the ailments induced by the monster
class Ailments(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    mon_id = db.Column(db.Integer, db.ForeignKey('monster.id'), nullable=False)
    status = db.Column(db.String(250)) # Status Effects
    blight = db.Column(db.String(140)) # Blight Effects
    # For Tremors, Roars, Wind, Stun
    natural = db.Column(db.String(140))

    def __repr__(self):
        return '<Ailments {}>'.format(self.status)

    def __init__(self, mon_id: int, status: str, blight: str, natural: str):
        self.mon_id = mon_id
        self.status = status
        self.blight = blight
        self.natural = natural

    # Add Ailments
    @staticmethod
    def createStatus( mon_id: int, status: str, blight: str, natural: str):
        new = Ailments(mon_id, status, blight, natural)
        db.session.add(new)
        db.session.commit()

class Egames(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    mon_id = db.Column(db.Integer, db.ForeignKey('monster.id'), nullable=False)
    games = db.Column(db.String(100), db.ForeignKey('videogames.shortname'), nullable=False)

    def __repr__(self):
        return '<Egames {}>'.format(self.games)

    # Add Monsters to a game
    def inGame(mid: int,  games: str):
        new = Egames(mon_id= mid, games= games)
        db.session.add(new)
        db.session.commit()

    # Add Monsters to multiple games
    def putInGames(mon_id: int,  games: array):
        for x in games:
            new = Egames(mon_id= mon_id, games= x)
            db.session.add(new)
            db.session.commit()


_base_ = [
    './retinanet_r50_fpn_1x_TinyPerson640.py'
]

optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001) # 4 gpu
optimizer_config = dict(_delete_=True, grad_clip=dict(max_norm=1, norm_type=2))           # add grad clip

# model settings
model = dict(
    neck=dict(
        start_level=0,  # start_level=1,
        # add_extra_convs='on_input',  # note
        num_outs=5),
    bbox_head=dict(
        type='RetinaHead',
        num_classes=1,  # 80
        anchor_generator=dict(
            type='AnchorGenerator',
            octave_base_scale=2,    # 4
            ratios=[0.5, 1.0, 2.0],
            strides=[4, 8, 16, 32, 64]  # [8, 16, 32, 64, 128]
        )
    )
)


#FormattedTradingNews.py

def storys(data):
	length = len(data)
	for i in range(0, length):
		string = data[i].strip(']')
		print (string)

News = open('Todays Crypto news.txt', 'a+')
TradingNews = News.read().split(',')

storys(TradingNews)


import PySimpleGUI as sg
from shutil import move
from os import listdir
from os.path import isfile, join, isdir, splitext, abspath
from tkinter import messagebox

def updateFileList():
  fnames = [
        f
        for f in files
        if isfile(join(to_move_folder_path, f))
    ]
  window['scanned_files_list'].update(fnames)

def moveApplicableFiles():
  for filename in files:
    # check if the patient has a folder in the destination folder and if a file of the same name doesn't already exist in the patient's folder. if true, move the file ()

    # splitext removes the .<ext> and split separates the file name on spaces; both return an array
    patient_folder_path = join(organize_in_folder_path, splitext(filename)[0].split()[0])
    potential_file_in_dest_path = join(patient_folder_path, filename)
    file_to_be_moved_path = join(to_move_folder_path, filename)

    if isdir(patient_folder_path) and not isfile(potential_file_in_dest_path):
      move(file_to_be_moved_path, patient_folder_path)
      print('moved', file_to_be_moved_path, 'to', patient_folder_path)

# create GUI layout
layout = [[sg.Text('Scanned Files Folder'),
        sg.In('Source', size=(23, 1), enable_events=True, key='scanned_files_directory'),sg.FolderBrowse()],
        [sg.Listbox( values=[], enable_events=True, size=(40, 15), key='scanned_files_list')],
        [sg.Text('Folder of Patient Folders'),
        sg.In('Destination', size=(20, 1), enable_events=True, key='patient_folders_directory'),sg.FolderBrowse()],
        [sg.Button('Organize away!', button_color=(sg.theme_background_color()), font=('Courier', 13))]]

# create the window
window = sg.Window('File Organizer', layout)

# create an event loop
while True:
  event, values = window.read()
  # End program if user closes window
  if event == sg.WIN_CLOSED:
    break
  if event == 'scanned_files_directory':
    # get folder path
    to_move_folder_path = values['scanned_files_directory']
    print('Scanned Files Folder Path: ', to_move_folder_path)
    try:
        # Get list of files in folder
        files = listdir(to_move_folder_path)
        print('File Names:', files)
    except:
        files = []
    updateFileList() # update the file list box
  elif event == 'patient_folders_directory':
    # get folder path
    organize_in_folder_path = values['patient_folders_directory']
    print('Folder of Patient Folder Path: ', organize_in_folder_path)
  elif event == 'Organize away!':
    # move files to correct destination
    try:
      moveApplicableFiles() # moves files while avoiding duplicates
      updateFileList() # update the file list box
      # alert if files weren't moved
      if (len(listdir(to_move_folder_path)) > 0):
        messagebox.showinfo('Head\'s up!', str(len([f for f in listdir(to_move_folder_path) if isfile(join(to_move_folder_path, f))])) + ' files could not be moved. You can view unmoved files in the file list box. They may not have been moved because duplicates exist in their destination folder, the patient\'s folder hasn\'t been created yet, or the file was incorrectly named.')
      else:
        messagebox.showinfo('Success!', 'All files were able to be moved! You may close the program!')
    except:
      messagebox.showwarning('Oops!', 'Something may have went wrong. Contact the developer for help.')

window.close()


import cv2

from picamera import PiCamera
from time import sleep
import numpy as np
import runModel

def picture(x):
    path = '/home/pi/Desktop/'
    # start picamera
    camera = PiCamera()
    #camera.start_preview()

    #start logitech
    cap = cv2.VideoCapture(0)
    cap.set(3, 1280)
    cap.set(4, 720)

    # wait to focus cams
    sleep(3)
    # take pic from picam
    pipath = f'{path}pi{x}.jpg'
    camera.capture(pipath)
    #camera.stop_preview()

    # take pic from logitech
    _, img = cap.read()
    logpath = f'{path}log{x}.jpg'
    cv2.imwrite(logpath, img)
    cap.release()

    return pipath, logpath

def main():
    pipath, path = picture(1)
    pimg = cv2.imread(pipath, cv2.IMREAD_GRAYSCALE)
    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    piPred = runModel.runModel(pimg)
    pred = runModel.runModel(img)
    print(piPred, pred)

if __name__ == "__main__":
    main()


#!/usr/bin/env python
import argparse
import re
import os, sys
import pandas as pd
import collections
import csv
from collections import Counter, OrderedDict
import itertools as iter
import numpy as np

class OrderedCounter(Counter, OrderedDict):
    pass

def df_to_dictionary(args, filepath, meta_dict):
    tmp_dict = {}
    table_df = pd.read_csv(filepath, sep="\t", comment="#")

    for row in table_df.itertuples(index=False, name='Pandas'):
        gene_type = getattr(row, "Type")
        unique_id = getattr(row, "Identifier")
        genome = getattr(row, "Genome")
        start = int(getattr(row, "Start"))
        stop = int(getattr(row, "Stop"))
        strand = getattr(row, "Strand")
        gene_name = getattr(row, "locus_tag")
        aa_count = int(getattr(row, "codon_count"))
        peak_height = float(getattr(row, "peak_height"))
        start_codon = getattr(row, "start_codon")
        stop_codon = getattr(row, "stop_codon")
        nt_upstream = getattr(row, "_11")
        nt_seq = getattr(row, "nt_seq")
        aa_seq = getattr(row, "aa_seq")
        relative_density = float(getattr(row, "relative_density"))
        fiveprime = getattr(row, "_15")
        threeprime = getattr(row, "_16")

        tmp_dict[unique_id] = (peak_height, relative_density)
        if unique_id not in meta_dict.keys():
            meta_dict[unique_id] = (gene_name, gene_type, aa_count, start_codon, stop_codon, nt_upstream, nt_seq, aa_seq, fiveprime, threeprime)

    return tmp_dict, meta_dict

#
def parse_input(args):
    """
    Check if each RIBO file has a TIS counterpart and create two lists of dictionaries as well as a meta dictionary
    """

    constrasts = [constrast.split("_") for constrast in args.contrast_list]

    table_path = os.path.dirname(os.path.abspath(args.table_list[0]))
    suffix = "." + ".".join(os.path.splitext(os.path.basename(args.table_list[0]))[0].split(".")[1:]) + ".csv"
    wildcards = [os.path.splitext(os.path.basename(x))[0].split(".")[0] for x in sorted(args.table_list)]

    meta_dict = {}
    wildcard_dicts = {}
    for card in wildcards:
        tmp_dict, meta_dict = df_to_dictionary(args, os.path.join(table_path, card + suffix), meta_dict)
        wildcard_dicts[card] = tmp_dict

    return meta_dict, wildcard_dicts, wildcards, constrasts

def calculate_fold_changes(unique_id, wildcard_dicts, contrasts):
    """
    calculate log2FC of the different samples
    """
    data = []

    for key, wild_dict in wildcard_dicts.items():
        if unique_id in wild_dict:
            peak_height = wild_dict[unique_id][0]
        else:
            peak_height = ""

        data.append(peak_height)

    for contrast in contrasts:
        con1, con2 = contrast
        if unique_id in wildcard_dicts[con1]:
            con1_height = wildcard_dicts[con1][unique_id][0]
        else:
            con1_height = ""

        if unique_id in wildcard_dicts[con2]:
            con2_height = wildcard_dicts[con2][unique_id][0]
        else:
            con2_height = ""

        if con1_height != "" and con2_height != "":
            fc = np.log2(con2_height / con1_height)
        else:
            fc = ""

        data.append(fc)

    return data

def excel_writer(args, data_frames):
    """
    create an excel sheet out of a dictionary of data_frames
    correct the width of each column
    """
    header_only =  ["Aminoacid_seq", "Nucleotide_seq"]
    writer = pd.ExcelWriter(args.out_xlsx, engine='xlsxwriter')
    for sheetname, df in data_frames.items():
        df.to_excel(writer, sheet_name=sheetname, index=False)
        worksheet = writer.sheets[sheetname]
        for idx, col in enumerate(df):
            series = df[col]
            if col in header_only:
                max_len = len(str(series.name)) + 2
            else:
                max_len = max(( series.astype(str).str.len().max(), len(str(series.name)) )) + 1
            print("Sheet: %s | col: %s | max_len: %s" % (sheetname, col, max_len))
            worksheet.set_column(idx, idx, max_len)
    writer.save()

def create_wildcard_header(wildcards, contrasts):
    """
    create the header based on the given wildcards
    """
    header = ["%s_peak_height" % wildcard for wildcard in wildcards]
    header.extend(["%s_%s_log2FC" % (contrast[0], contrast[1]) for contrast in contrasts])
    return header

def get_relative_density(unique_id, wildcard_dicts):
    """
    extract relative_density
    """
    data = []
    for key, wild_dict in wildcard_dicts.items():
        if unique_id in wild_dict:
            rel_density = wild_dict[unique_id][1]
        else:
            rel_density = ""

        data.extend([rel_density])

    return data

def create_excel_file(args):
    """
    generate an overview excel file with the combined results
    """
    meta_dict, wildcard_dicts, wildcards, contrasts = parse_input(args)

    header = ["Identifier", "Genome", "Start", "Stop", "Strand", "Locus_tag", "Gene_type", "Codon_count", "Start_codon", "Stop_codon"] +\
             create_wildcard_header(wildcards, contrasts) + ["15nt upstream", "Nucleotide_seq", "Aminoacid_seq"] +\
             ["%s_relative_density" % (sample) for sample in wildcards] + ["5'distance", "3'distance"]

    name_list = ["s%s" % str(x) for x in range(len(header))]
    nTuple = collections.namedtuple('Pandas', name_list)

    all_sheet = []
    for unique_id in meta_dict.keys():
        meta_info = meta_dict[unique_id]

        chrom, mid, strand = unique_id.split(":")
        start, stop = mid.split("-")

        result = [unique_id, chrom, int(start), int(stop), strand, meta_info[0], meta_info[1], meta_info[2], meta_info[3], meta_info[4]] +\
                 calculate_fold_changes(unique_id, wildcard_dicts, contrasts) + [meta_info[5], meta_info[6], meta_info[7]] +\
                 get_relative_density(unique_id, wildcard_dicts) + [meta_info[8], meta_info[9]]

        all_sheet.append(nTuple(*result))
    all_df = pd.DataFrame.from_records(all_sheet, columns=[header[x] for x in range(len(header))])
    all_df = all_df.astype({"Start" : "int32", "Stop" : "int32"})
    all_df = all_df.sort_values(by=["Genome", "Start", "Stop"])
    dataframe_dict = { "all" : all_df }
    excel_writer(args, dataframe_dict)

def main():
    # store commandline args
    parser = argparse.ArgumentParser(description='create an overview table containing merged RET results.')
    parser.add_argument("-t", "--tables", nargs="+", dest="table_list", required=True, help= "list of input tables.")
    parser.add_argument("--contrasts", nargs="+", dest="contrast_list", required=True, help= "list of contrasts used for log2FC.")
    parser.add_argument("-x", "--xlsx", action="store", dest="out_xlsx", required=True, help= "Output excel file.")

    args = parser.parse_args()

    create_excel_file(args)

if __name__ == '__main__':
    main()


#Guarda a somatoria dos valores informados
somatoria = 0
#Conta a quantidade de valores informados
contador = 0
#Guarda o maior valor informado
maior = 0
#Guarda o menor valor informado
menor = 0
#COnstroi a lógica de repetição - enquanto se mantiver verddaeiro, repete o código
continuar = True
while continuar == True:
  valor = int(input("Valor:"))
  #Adiciona o valor na somatoria
  somatoria += valor #somatoria = somatoria + valor
  #Adiciona mais um na contagem
  contador += 1 #contador = contador + 1
  #Para verificar se é oprimeiro numero informado
  if contador == 1:
    maior = valor
    menor = valor
  else:
    if valor > maior:
      maior = valor
    elif valor < menor:
      menor = valor
  #Verifica se o usuario deseja continuar
  continuar = input("Continuar?") == 's'

media = somatoria/contador
print("Media:", media)
print("Maior:", maior)
print("Menor:", menor)


from django.urls import path
from accounts import views

urlpatterns = [

    path('login/', views.login, name='login'),
    path('signup/', views.signup, name='signup'),
    path('logout/', views.logout, name='logout'),
    path('mobile-login/', views.mobile_login, name='mobile_login'),
    path('verify-codelogin/', views.verify_codelogin, name='verify_codelogin'),
    path('user-dashboard/', views.user_dashboard, name='user_dashboard'),
    path('my-orders/', views.my_orders, name='my_orders'),
    path('edit-profile/', views.edit_profile, name='edit_profile'),
    path('change-password/', views.change_password, name='change_password'),
    path('order-detail/<int:order_id>/', views.order_detail, name='order_detail'),
    path('myAddress/', views.myAddress, name='myAddress'),
    path('addAddress/', views.addAddress, name='addAddress'),
    path('deleteAddress/<int:id>/', views.deleteAddress, name='deleteAddress'),
    path('editAddress/<int:id>/', views.editAddress, name='editAddress'),
    path('deleteCheckoutAddress/<int:id>/', views.deleteCheckoutAddress, name='deleteCheckoutAddress'),
    path('AddCheckoutAddress/', views.AddCheckoutAddress, name='AddCheckoutAddress'),

]


"""
File with the User Story US04
Author: Varun Mullins
"""

def US_04(family):
    """Function to implement user story US04"""
    for item in family.values():
        if item._divorce_date != 'NA':
            if item._marriage_date > item._divorce_date:
                """Compares the marriage and the divorce dates"""

                yield f"Marriage date Line: {item._line_numbers.get('date').get('marriage')}" \
                      f"\nDivorce date Line: {item._line_numbers.get('date').get('divorce')}\n" \
                      f"US_04: This family id {item._family_id} has an illegal dates for marriage and divorce"


import os
from numpy import dot, inner, average, zeros
from copy import deepcopy
from random import sample
from scipy.io import loadmat
from scipy.special import expit

class SVM:
    def __init__(self):

        # loading the input file
        self.dataDict = dict()
        inputFile = "C:/Users/Akash/Desktop/MLHW2/data.mat"
        self.dataDict = loadmat(os.path.join(os.getcwd(), inputFile))

        self.MAX_VALUE = float('inf')
        self.MIN_VALUE = -float('inf')

        # segregating the  training and testing data sets
        self.trnData = self.dataDict['X_trn']
        self.tstData = self.dataDict['X_tst']

        # segregating the  training and testing label sets
        self.trnLabels = self.dataDict['Y_trn']
        self.tstLabels = self.dataDict['Y_tst']

        # casting the array to specific type
        self.trnData = self.trnData.astype(float, copy=False)
        self.tstData = self.tstData.astype(float, copy=False)
        self.trnLabels = self.trnLabels.astype(int, copy=False)
        self.tstLabels = self.tstLabels.astype(int, copy=False)

        # find the unique classes present in the given label
        self.classes = self.findClasses()
        self.trainingLabelsForClass = None
        self.testingLabelsForClass = None
        self.createLabelForClass()

        self.trnSampleCount = self.trnData.shape[0]
        self.featuresCount = self.trnData.shape[1]
        self.classCount = len(self.classes)

    # Function to find the unique classes present in the given labels
    def findClasses(self):
        uniqueClassSet = set()
        for lbl in self.trnLabels:
            uniqueClassSet.add(lbl[0])
        for lbl in self.tstLabels:
            uniqueClassSet.add(lbl[0])
        return list(sorted(uniqueClassSet))

    # This function creates the labels for class and assigns 1 if the label
    # belongs to the class else it is assigned -1. This is to differentiate a class from rest of the classes
    def createLabelForClass(self):
        self.trainingLabelsForClass = list()
        self.testingLabelsForClass = list()
        # creating copies of the labels
        for cls in self.classes:
            cpy = deepcopy(self.trnLabels)
            self.trainingLabelsForClass.append(cpy)
            cpy = deepcopy(self.tstLabels)
            self.testingLabelsForClass.append(cpy)
        # Differentiate each class from the rest of the classes
        for cls in self.classes:
            for i in range(self.trnLabels.shape[0]):
                if cls == self.trnLabels[i]:
                    self.trainingLabelsForClass[cls][i] = 1
                else:
                    self.trainingLabelsForClass[cls][i] = -1
            for i in range(self.tstLabels.shape[0]):
                if cls == self.tstLabels[i]:
                    self.testingLabelsForClass[cls][i] = 1
                else:
                    self.testingLabelsForClass[cls][i] = -1

    # computing using (2) from the paper
    def E(self, i, data, labels, alphas, b):
        return self.func(data, b, data[i], labels, alphas) - labels[i]

    def func(self, data, b, dataPoint, labels, alphas):
        op = 0
        for i in range(len(data)):
            op += alphas[i] * labels[i] * inner(data[i], dataPoint)
        op += b
        return op

    def computeB(self, b, i, j, Ei, Ej, C, alphas, prevAlphaI, prevAlphaJ, data, labels):
        t1 = b
        t2 = Ei
        t3 = labels[i] * (alphas[i] - prevAlphaI) * inner(data[i], data[i])
        t4 = labels[j] * (alphas[j] - prevAlphaJ) * inner(data[i], data[j])

        b1 = t1 - t2 - t3 - t4

        t2 = Ej
        t3 = labels[i] * (alphas[i] - prevAlphaI) * inner(data[i], data[j])
        t4 = labels[j] * (alphas[j] - prevAlphaJ) * inner(data[j], data[j])
        b2 = t1 - t2 - t3 - t4

        if ((0 < alphas[i]) and (alphas[i] < C)) and not ((0 < alphas[j]) and (alphas[j] < C)):
            return b1
        elif ((0 < alphas[j]) and (alphas[j] < C)) and not ((0 < alphas[i]) and (alphas[i] < C)):
            return b2
        else:
            return (b1 + b2) / 2

    def computeLH(self, i, j, labels, alphas, C):
        L = self.MIN_VALUE
        H = self.MAX_VALUE
        if (labels[i] != labels[j]):
            L = max(0, alphas[j] - alphas[i])
            H = min(C, C + alphas[j] - alphas[i])
        else:
            L = max(0, alphas[i] + alphas[j] - C)
            H = min(C, alphas[i] + alphas[j])
        return (L, H)

    # compute Eta using (14) in paper
    def computeEta(self, i, j, data):
        op = 2 * inner(data[i], data[j]) - inner(data[i], data[i]) - inner(data[j], data[j])
        return op

    # computing new alpha j using (12) and (15) form paper
    def findNewAJ(self, i, j, alphas, data, labels, Ei, Ej, L, H):
        eta = self.computeEta(i, j, data)
        newAJ = alphas[j] - (labels[j] * (Ei - Ej) / eta)
        if newAJ > H:
            return H
        elif newAJ < L:
            return L
        else:
            return newAJ

    # compute the weight vector for the specific class
    def findWV(self, alphas, inputLabels, data):
        # get the labels from the inputLabels
        labels = inputLabels.transpose().tolist()[0]
        w = alphas[0] * labels[0] * data[0]
        for i in range(1, len(alphas)):
            w += alphas[i] * labels[i] * data[i]
        return w

    def performSmo(self, inData, inputLabels):
        data = inData

        # lagrange multipliers for solution and initializing all alpha to 0
        labels = inputLabels.transpose().tolist()[0]
        alphas = zeros(inputLabels.shape)
        alphas = alphas.transpose().tolist()[0]

        # threshold for solution, initializing to 0
        b = 0
        passes = 0
        MAX_PASSES = 2
        C = 1
        tolerance = 0.01
        trnSamplesIndx = list(range(self.trnSampleCount))

        while passes < MAX_PASSES:

            numberOfChangedAlphas = 0

            for i in trnSamplesIndx:
                Ei = self.E(i, data, labels, alphas, b)
                if ((labels[i] < -1 * tolerance) and (alphas[i] < C)) or (
                            (labels[i] > -1 * tolerance) and (alphas[i] > 0)):
                    # selecting j randomly and j != i
                    j = sample(trnSamplesIndx[:i] + trnSamplesIndx[i + 1:], 1)[0]

                    # Compute Ej using (2) in paper
                    Ej = self.E(j, data, labels, alphas, b)

                    # saving old alphas
                    prevAlphaI = alphas[i]
                    prevAlphaJ = alphas[j]

                    L, H = self.computeLH(i, j, labels, alphas, C)

                    if L == H:
                        continue
                    eta = self.computeEta(i, j, data)
                    if eta >= 0:
                        continue
                    # computing alpha j using (12) and (15) form paper
                    alphas[j] = self.findNewAJ(i, j, alphas, data, labels, Ei, Ej, L, H)

                    if abs(alphas[j] - prevAlphaJ) < 0.00001:
                        continue
                    alphas[i] = alphas[i] + labels[i] * labels[j] * (prevAlphaJ - alphas[j])

                    # compute b using (19) from paper
                    b = self.computeB(b, i, j, Ei, Ej, C, alphas, prevAlphaI, prevAlphaJ, data, labels)
                    numberOfChangedAlphas += 1
            if numberOfChangedAlphas == 0:
                passes += 1
            else:
                passes = 0

        return (alphas, b)

    def softMaxforSMO(self):
        # list of all the weight vectors for a specific class
        weigthVforClass = list()
        # bias for class
        classwiseB = list()
        # predictions for classes
        classPredictions = list()

        for i in range(len(self.classes)):
            labels = self.trainingLabelsForClass[i]
            alphas, b = self.performSmo(self.trnData, labels)

            wtv = self.findWV(alphas, labels, self.trnData)

            # weight vector for class
            weigthVforClass.append(wtv)

            # bias for class
            classwiseB.append(b)

            # prediction for class
            predictions = expit(dot(self.tstData, wtv.transpose()) + b)
            classPredictions.append(predictions)

        actualPredictions = list()

        # computing the predictions
        for i in range(len(self.tstLabels)):
            predictedClass = None
            maxprob = self.MIN_VALUE
            for cIdx in range(len(self.classes)):
                if classPredictions[cIdx][i] > maxprob:
                    maxprob = classPredictions[cIdx][i]
                    predictedClass = self.classes[cIdx]
            actualPredictions.append(predictedClass)

        # print weight vector for class
        print("weigth Vectors for Class: " + str(weigthVforClass))
        # print bias for class
        print("classwise Biases: " + str(weigthVforClass))
        # print the actual prediction
        print("Predictions: " + str(actualPredictions))
        # print testing labels for class
        print("Testing labels: " + str(self.tstLabels.transpose().tolist()))

if __name__ == "__main__":
    obj = SVM()
    obj.softMaxforSMO()


import argparse
import template
import torch
import serverUtility
import data
import model
import loss
from trainer import Trainer

parser = argparse.ArgumentParser(description='EDSR and MDSR')

parser.add_argument('--alpha', type=float, default= -1 ,
                    help='controller')

parser.add_argument('--debug', action='store_true',
                    help='Enables debug mode')
parser.add_argument('--template', default='.',
                    help='You can set various templates in option.py')

# Hardware specifications
parser.add_argument('--n_threads', type=int, default=6,
                    help='number of threads for data loading')
parser.add_argument('--cpu', action='store_true',
                    help='use cpu only')
parser.add_argument('--n_GPUs', type=int, default=1,
                    help='number of GPUs')
parser.add_argument('--seed', type=int, default=1,
                    help='random seed')

# Data specifications
parser.add_argument('--dir_data', type=str, default='/data2/',
                    help='dataset directory')
parser.add_argument('--dir_demo', type=str, default='../test',
                    help='demo image directory')
parser.add_argument('--data_train', type=str, default='DIV2K',
                    help='train dataset name')
parser.add_argument('--data_test', type=str, default='DIV2K',
                    help='test dataset name')
parser.add_argument('--data_range', type=str, default='1-800/801-810',
                    help='train/test data range')
parser.add_argument('--ext', type=str, default='sep',
                    help='dataset file extension')
parser.add_argument('--scale', type=str, default='4',
                    help='super resolution scale')
parser.add_argument('--patch_size', type=int, default=192,
                    help='output patch size')
parser.add_argument('--rgb_range', type=int, default=255,
                    help='maximum value of RGB')
parser.add_argument('--n_colors', type=int, default=3,
                    help='number of color channels to use')
parser.add_argument('--chop', action='store_true',
                    help='enable memory-efficient forward')
parser.add_argument('--no_augment', action='store_true',
                    help='do not use data augmentation')

# Model specifications
parser.add_argument('--model', default='EDSR',
                    help='model name')

parser.add_argument('--act', type=str, default='relu',
                    help='activation function')
parser.add_argument('--pre_train', type=str, default='',
                    help='pre-trained model directory')
parser.add_argument('--extend', type=str, default='.',
                    help='pre-trained model directory')
parser.add_argument('--n_resblocks', type=int, default=16,
                    help='number of residual blocks')
parser.add_argument('--n_feats', type=int, default=64,
                    help='number of feature maps')
parser.add_argument('--res_scale', type=float, default=1,
                    help='residual scaling')
parser.add_argument('--shift_mean', default=True,
                    help='subtract pixel mean from the input')
parser.add_argument('--dilation', action='store_true',
                    help='use dilated convolution')
parser.add_argument('--precision', type=str, default='single',
                    choices=('single', 'half'),
                    help='FP precision for test (single | half)')

# Option for Residual dense network (RDN)
parser.add_argument('--G0', type=int, default=64,
                    help='default number of filters. (Use in RDN)')
parser.add_argument('--RDNkSize', type=int, default=3,
                    help='default kernel size. (Use in RDN)')
parser.add_argument('--RDNconfig', type=str, default='B',
                    help='parameters config of RDN. (Use in RDN)')

# Option for Residual channel attention network (RCAN)
parser.add_argument('--n_resgroups', type=int, default=10,
                    help='number of residual groups')
parser.add_argument('--reduction', type=int, default=16,
                    help='number of feature maps reduction')

# Training specifications
parser.add_argument('--reset', action='store_true',
                    help='reset the training')
parser.add_argument('--test_every', type=int, default=1000,
                    help='do test per every N batches')
parser.add_argument('--epochs', type=int, default=300,
                    help='number of epochs to train')
parser.add_argument('--batch_size', type=int, default=1,
                    help='input batch size for training')
parser.add_argument('--split_batch', type=int, default=1,
                    help='split the batch into smaller chunks')
parser.add_argument('--self_ensemble', action='store_true',
                    help='use self-ensemble method for test')
parser.add_argument('--test_only', action='store_true',
                    help='set this option to test the model')
parser.add_argument('--gan_k', type=int, default=1,
                    help='k value for adversarial loss')

# Optimization specifications
parser.add_argument('--lr', type=float, default=1e-4,
                    help='learning rate')
parser.add_argument('--decay', type=str, default='200',
                    help='learning rate decay type')
parser.add_argument('--gamma', type=float, default=0.5,
                    help='learning rate decay factor for step decay')
parser.add_argument('--optimizer', default='ADAM',
                    choices=('SGD', 'ADAM', 'RMSprop'),
                    help='optimizer to use (SGD | ADAM | RMSprop)')
parser.add_argument('--momentum', type=float, default=0.9,
                    help='SGD momentum')
parser.add_argument('--betas', type=tuple, default=(0.9, 0.999),
                    help='ADAM beta')
parser.add_argument('--epsilon', type=float, default=1e-8,
                    help='ADAM epsilon for numerical stability')
parser.add_argument('--weight_decay', type=float, default=0,
                    help='weight decay')
parser.add_argument('--gclip', type=float, default=0,
                    help='gradient clipping threshold (0 = no clipping)')

# Loss specifications
parser.add_argument('--loss', type=str, default='1*L1',
                    help='loss function configuration')
parser.add_argument('--skip_threshold', type=float, default='1e8',
                    help='skipping batch that has large error')

# Log specifications
parser.add_argument('--save', type=str, default='test',
                    help='file name to save')
parser.add_argument('--load', type=str, default='',
                    help='file name to load')
parser.add_argument('--resume', type=int, default=0,
                    help='resume from specific checkpoint')
parser.add_argument('--save_models', action='store_true',
                    help='save all intermediate models')
parser.add_argument('--print_every', type=int, default=100,
                    help='how many batches to wait before logging training status')
parser.add_argument('--save_results', action='store_true',
                    help='save output results')
parser.add_argument('--save_gt', action='store_true',
                    help='save low-resolution and high-resolution images together')

args = parser.parse_args()

args.data_test = ["Demo"]
args.scale = [4]
args.pre_train = "../experiment/edsr_baseline_x4/model/model_best.pt"
args.test_only = True
args.save_results = True

torch.manual_seed(args.seed)
checkpoint = serverUtility.checkpoint(args)

if args.alpha != -1:
   print('alpha: ',args.alpha)
global model
if args.data_test == ['video']:
   from videotester import VideoTester
   model = model.Model(args, checkpoint)
   t = VideoTester(args, model, checkpoint)
   t.test()
else:
   if checkpoint.ok:

       _loss = loss.Loss(args, checkpoint) if not args.test_only else None
       _model = model.Model(args, checkpoint)

#######################################################################################

#-*- coding:utf-8 -*-
"""
__author__ = BlingBling
建立TCP的基本流程
ss = socket() # 创建服务器套接字
ss.bind() # 套接字与地址绑定
ss.listen() # 监听连接
inf_loop: # 服务器无限循环
    cs = ss.accept() # 接受客户端连接
    comm_loop: # 通信循环
        cs.recv()/cs.send() # 对话（接收/发送）
    cs.close() # 关闭客户端套接字
ss.close() # 关闭服务器套接字#（可选）
"""
#!/usr/bin/env python

import os
from socket import *
from time import ctime

HOST = ''  #对bind（）方法的标识，表示可以使用任何可用的地址
PORT = 21567  #设置端口
BUFSIZ = 1024  #设置缓存区的大小
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)  #定义了一个套接字
tcpSerSock.bind(ADDR)  #绑定地址
tcpSerSock.listen(10)     #规定传入连接请求的最大数，异步的时候适用

while True:
    print('waiting for connection...')
    tcpCliSock, addr = tcpSerSock.accept()
    print ('...connected from:', addr)
    while True:
        rec = tcpCliSock.recv(BUFSIZ)
        print("recv:",rec.decode("utf-8"))
        if not rec:
            break

        args.alpha = rec.decode("utf-8")
        print(args.alpha)
        loader = data.Data(args)
        t = Trainer(args, loader, _model, _loss, checkpoint)
        t.test()

        filename = '../experiment/test/results-Demo/'+ 'result.png'
        if os.path.exists(filename):
            filesize = str(os.path.getsize(filename))
            print("文件大小为：",filesize)
            tcpCliSock.send(filesize.encode())
            rec = tcpCliSock.recv(BUFSIZ)   #挂起服务器发送，确保客户端单独收到文件大小数据，避免粘包
            print("开始发送")
            f = open(filename, "rb")
            for line in f:
                tcpCliSock.send(line)
        else:
            tcpCliSock.send("0001".encode())   #如果文件不存在，那么就返回该代码
    tcpCliSock.close()
tcpSerSock.close()

########################################################################################
checkpoint.done()


import os
import requests
from bs4 import BeautifulSoup

class BaseScraper:
  def __init__(self, market_name, urls):
    self.market_name = market_name
    self.urls = urls
    self.raw_pages_content = []
    self.items_data = []
    self.content = ""

  def start_requests(self):
    raw_pages_content = []

    for url in self.urls:
      try:
        response = requests.get(url)
        website_text = response.text
        raw_pages_content.append(website_text)
      except requests.exceptions:
        print(requests.exceptions)
        continue

    self.raw_pages_content = raw_pages_content

  def generate_text_list(self):
    list_header = f"\n-------------------------- {self.market_name} 🏪 --------------------------"

    items_str = "\n".join([ f"🔵 {item['name']} - €{item['price']}" for item in self.items_data ])

    content_str = f"{list_header}\n{items_str}"

    self.content = content_str

  def parse(self, parse_data):
    items_data = []

    for html_content in self.raw_pages_content:
      soup = BeautifulSoup(html_content, 'html.parser')

      parsed_data = parse_data(soup=soup)
      items_data.append(parsed_data)

    self.items_data = items_data

  def store_data(self):
    try:
      file_path = "email.txt"
      new_content = f"{self.content}\n"

      # Check if file exists
      if os.path.isfile(file_path):
        with open(file_path, "a") as file:
          file.write(new_content)
      else:
        with open(file_path, "w") as file:
          file.write(new_content)
    except:
      pass


class Node:
    def __init__(self, key, left_child, right_child):
        self.left = left_child
        self.right = right_child
        self.value = key

def inorder(root):
    if(root):
        inorder(root.left)
        print(root.value, end = " ")
        inorder(root.right)
def preorder(root):
    if(root):
        print(root.value, end = " ")
        preorder(root.left)
        preorder(root.right)
def postorder(root):
    if(root):
        postorder(root.left)
        postorder(root.right)
        print(root.value, end=" ")

d = Node(20, None, None)
e = Node(40, None, None)
f = Node(60, None, None)
g = Node(80, None, None)
b = Node(30, d, e)
c = Node(70, f, g)
a = Node(50, b, c)

print("inorder")
inorder(a)
print("preorder")
preorder(a)
print("postorder")
postorder(a)


# 792. Number of Matching Subsequences
class Solution:
    def numMatchingSubseq(self, s: str, words: List[str]) -> int:
        '''
        1/2 State machines.

        Keep the mapping of current char -> iterator for each word, and as
        we go through s, we update the iterators whose keys match the current
        char.

        At the end, the number of exhausted iterators is the number of matched
        words.

        Time: O(T) where T is the total lengths of words.
        Space: O(W) where W is the number of words.

        Same as 524. Longest Word in Dictionary through Deleting
        '''
        states = defaultdict(list)
        for it in map(iter, words):
            states[next(it)].append(it)
        for c in s:
            for it in states.pop(c, ()):
                states[next(it, None)].append(it)

        return len(states[None])
        '''
        2/2 Collect the s indexes and do binary search for each word.

        Time: O(S + T*log(S)) where S is len(s)
        Space: O(S)
        '''
        pos = defaultdict(list)
        for i, c in enumerate(s):
            pos[c].append(i)

        ans = 0
        for w in words:
            found = True
            prev = 0
            for i, c in enumerate(w):
                idx = bisect_left(pos[c], prev)
                if idx == len(pos[c]):
                    found = False
                    break
                prev = pos[c][idx] + 1
            if found:
                ans += 1

        return ans

medium/792. Number of Matching Subsequences.py


import sys
import pygame

def title_page(screen, settings):
	#music and music loop
	menu_song = pygame.mixer.music.load('sounds/Epoq-Lepidoptera.ogg')
	pygame.mixer.music.play(-1)
	#front images and position
	frontpage = pygame.image.load("assets/main.jpg").convert()
	frontpage = pygame.transform.scale(frontpage,
        (settings.screen_width,settings.screen_height), screen)

	screen.blit(frontpage,(0,0))
	pygame.display.update()

	while True:
		#poll() retrieves only a single event
		eventpoll = pygame.event.poll()
		if eventpoll.type == pygame.KEYDOWN:
			if eventpoll.key == pygame.K_RETURN:
				break
			elif eventpoll.key == pygame.K_ESCAPE:
				sys.exit()
		else:
			text_button(screen, "Press [ENTER] To Begin", 32, settings.screen_width/2,
						settings.screen_height/2-25)
			text_button(screen, "Press [ESC] To Quit", 32, settings.screen_width/2,
						settings.screen_height/2)
			pygame.display.update()

    #make music stop
	readypage = pygame.image.load("assets/readybar.jpg").convert()
	readypage = pygame.transform.scale(readypage,
        (settings.screen_width,settings.screen_height), screen)
	screen.blit(readypage,(0,0))
	pygame.display.update()
	pygame.mixer.music.stop()

def text_button(surf, text, size, x, y):
    ## selecting a cross platform font to display the score
    font = pygame.font.Font(pygame.font.match_font('font/PTN55F.ttf'), size)
    text_surface = font.render(text, True, (189,179,137))   ## True denotes the font to be anti-aliased
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)


import math, os, pickle, re
import numpy as np
import nltk
from nltk.collocations import *
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

#PriorP_positive=0

class Bayes_Classifier:
    def __init__(self, trainDirectory = "movie_reviews/"):
        #if my pickled file exist then directly load that else go to train method and train data first
        self.sfilename="databasebest.txt"
        if os.path.isfile(self.sfilename) and os.path.getsize(self.sfilename)>0:
            self.load(self.sfilename)

        else:
            f= open(self.sfilename,"w+")

            self.negativeDict = {}
            self.positiveDict = {}
            self.PriorP_positive=0
            self.PriorP_negative=0
            self.cn=0
            self.cp=0
            self.doc_class=""
            self.sText=''
            self.train_set()

    #start train get list of all the file names
    def train_set(self):
        trainDir = "testing/"
        lFileList = []
        for fFileObj in os.walk("testing/"):
            lFileList = fFileObj[2]
            break
        print(" Total training  reviews",len(lFileList))
        #creating list of positive and negative reviews
        positive_list=[]
        negative_list=[]
        length=len(lFileList)
        for rating in range(length):
            if lFileList[rating][7]=="1":
                negative_list.append(lFileList[rating])
                self.doc_class="negative"
                sTxt=self.loadFile(trainDir + lFileList[rating])
                self.tokensize(sTxt,self.doc_class)
            elif lFileList[rating][7]=="5":
                positive_list.append(lFileList[rating])
                self.doc_class="positive"
                sTxt=self.loadFile(trainDir + lFileList[rating])
                self.tokensize(sTxt,self.doc_class)

        self.save()
        self.load(self.sfilename)
        self.prior_probability(negative_list,positive_list)

        print("total negative",self.cn)
        print("total positive",self.cp)

 #reading
    def loadFile(self,filename):

            #f = open(filename, "r")
            f = open(filename, 'r', encoding='latin-1')
            sTxt = f.read()
            f.close()
            return sTxt

    def tokensize(self,sText,doc_class):
        #splitting the text in file
        stop_words = set(stopwords.words('english'))
        lTokens = []
        sToken = ""
        for c in sText:
            if re.match("[a-zA-Z0-9]", str(c)) != None or c == "\'" or c == "_" or c == '-':
                sToken += c

            else:
                if sToken != "":
                    lTokens.append(sToken)
                    sToken = ""
                if c.strip() != "":
                    lTokens.append(str(c.strip()))

        if sToken != "":
            lTokens.append(sToken)
        lTokens = [w for w in lTokens if not w in stop_words]
        self.bigram(lTokens)
   #create pair for tokens extracted from text
    def bigram(self,lTokens):
        bigram_measures = nltk.collocations.BigramAssocMeasures()
        trigram_measures = nltk.collocations.TrigramAssocMeasures()
        finder = BigramCollocationFinder.from_words(lTokens)
        all_pair=finder.nbest(bigram_measures.pmi,30)
        for pair in all_pair:
            self.build_dictionary(pair,self.doc_class)
    #create dictionary add each pair exacted above
    def build_dictionary(self,pair,doc_class):
        if self.doc_class=="negative":
                if self.negativeDict.get(pair,0)==0:
                    self.negativeDict.update({pair:1})
                    self.cn=self.cn+1
                elif self.negativeDict.get(pair,0)>0:
                    self.negativeDict[pair]=self.negativeDict[pair]+1
                    self.cn=self.cn+1

        if self.doc_class=="positive":
            if self.positiveDict.get(pair,0)==0:
                self.positiveDict.update({pair:1})
                self.cp=self.cp+1
            elif self.positiveDict.get(pair,0)>0:
                self.positiveDict[pair]=self.positiveDict[pair]+1
                self.cp=self.cp+1

    def save(self):
        f = open(self.sfilename,"wb")
        pickle.dump(self,f)
        f.close()

    def load(self, sFilename):
        f = open(sFilename, "rb")
        u = pickle.Unpickler(f)
        dObj = u.load()
        self.__dict__.update(dObj.__dict__)
        f.close()
        return dObj

    def prior_probability(self,negative_list,positive_list):
        lengthN=len(negative_list)
        lengthP=len(positive_list)
        print("P length of dict",lengthP)
        print("n length of dict",lengthN)
        totalcount=lengthN+lengthP
        self.PriorP_positive=np.log(lengthP/totalcount)
        self.PriorP_negative=np.log(lengthN/totalcount)
        print("in pp function ","neg",self.PriorP_negative,"\npos",self.PriorP_positive)

    def classify(self,filetext):

        stop_words = set(stopwords.words('english'))
        obj1=self.load(self.sfilename)
        print("positive dict",obj1.positiveDict)
        print("\n \n negaitive dict",obj1.negativeDict)
        self.prior_probability(obj1.negativeDict,obj1.positiveDict)
        class_review=["positive","negative"]
        result=[]
        lTokens = []
        sToken = ""
        for c in filetext:
            if re.match("[a-zA-Z0-9]", str(c)) != None or c == "\'" or c == "_" or c == '-':
                sToken += c

            else:
                if sToken != "":
                    lTokens.append(sToken)
                    sToken = ""
                if c.strip() != "":
                    lTokens.append(str(c.strip()))
        if sToken != "":
            lTokens.append(sToken)
        lTokens = [w for w in lTokens if not w in stop_words]
        bigram_measures = nltk.collocations.BigramAssocMeasures()
        trigram_measures = nltk.collocations.TrigramAssocMeasures()
        finder = BigramCollocationFinder.from_words(lTokens)
        all_pair=finder.nbest(bigram_measures.pmi, 10)

        lenp=len(obj1.positiveDict)
        lenn=len(obj1.negativeDict)

        for x in class_review:
            con_prob_positive=0
            con_prob_negative=0

            if x=="positive":
                for pair in all_pair:
                    count=obj1.positiveDict.get(pair,0)

                    Pword=np.log((count+1)/(self.cp+lenp))
                    con_prob_positive=con_prob_positive+Pword
                result.append(con_prob_positive+self.PriorP_positive)
            elif x=="negative":
                for pair in all_pair:
                    count=obj1.negativeDict.get(pair,0)

                    Pword=np.log((count+1)/(self.cn+lenn))
                    con_prob_negative=con_prob_negative+Pword
                result.append(con_prob_negative+self.PriorP_negative)

        if result[1]-result[0]<1 and result[1]-result[0]>-1:
            return "neutral"
        else:
            if(result[1]<result[0]):
                return "positive"
            if(result[1]>result[0]):
                return "negative"


from django.urls import path
from rest_framework.routers import DefaultRouter

#My imports
from apps.cards.views import CardAPI,CardSearchAPIView,CommentAPIViewSet

router = DefaultRouter()
router.register(prefix="cards", viewset= CardAPI, basename="cards")

urlpatterns = [
    path('cards/search/', CardSearchAPIView.as_view(), name='search'),
    path('cards/comment/', CommentAPIViewSet.as_view(), name='comment'),

]
urlpatterns += router.urls


import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense
from .process import Text
import pickle
import os

class Model:

    def __init__(self, device = "auto"):

        if device=="cpu":
            os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
            self.device = '/cpu:0'
        elif device=="auto":
            self.find_id()
        else:
            self.device = device

        self.load_model()

        with open('weights/tokenizer.pickle', 'rb') as handle:
            self.tokenizer = pickle.load(handle)

        self.warmup()

    def warmup(self):
        for _ in range(5):
            self.predict("hi how are you")

    def find_id(self):
        visible_devices = tf.config.experimental.get_visible_devices()
        gpu_id = None

        for device in visible_devices:
            if 'GPU' in device.device_type:
                gpu_id = int(device.name.split(':')[-1])
                break
        if gpu_id is not None and gpu_id>=0:
            self.device = "/gpu:" + str(gpu_id)
            print(f"GPU ID FOUND BY AUTO DETECT>> {gpu_id}")
        else:
            self.device = '/cpu:0'
            print("NO GPU ID FOUND BY AUTO DETECT LOADING MODEL ON CPU. IF YOU THINK THIS IS A MISTAKE PASS DEIVCE eg:'gpu:0'")
            os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

    def load_model(self):

        with tf.device(self.device):
            self.model = tf.keras.Sequential([
                Embedding(25000, 200, input_length=300),
                LSTM(16, activation="tanh"),
                Dense(6, activation="sigmoid"),
            ])

            self.model.load_weights('weights/weights.h5')
            # self.model = self.model.to("gpu:0")

    def predict(self, text):
        text = Text.preprocess(text)

        text_tokenized = self.tokenizer.texts_to_sequences([text])
        text_sequence = pad_sequences(text_tokenized, maxlen=300,
                                padding="post", truncating="post")
        with tf.device(self.device):
            tensor = tf.convert_to_tensor(text_sequence, dtype=tf.float32)
            out = self.model.predict(tensor)[0]
        return out


import collections
import functools
import operator
import re
import logging
from typing import List, Union, Callable, Dict, Any

import pandas as pd
import numpy as np

from .merging_functions import merge_with_msmsscanstxt, merge_with_msmstxt, merge_with_summarytxt
from .utils import utils

logger = logging.getLogger(__name__)

PHOSPHO_REGEX = re.compile(r'([STY])\(Phospho \(STY\)\)')
PROBABILITY_REGEX = re.compile(r'\((\d(?:\.?\d+)?)\)')

def transfer(summary_df, max_pep=False, mask=False, ambiguity_decision='majority'):
    """
    Main function for transfers by clustering. Transfers identifications for merged dataframe and adds a column for
    identification type. Transferred columns are Sequence, Modified sequence, Proteins, Gene names, Protein Names,
    Charge, m/z, and Mass.
    :param summary_df: Summary dataframe, merged from cleaned msms.txt and MaRaCluster clusters.tsv file
    :param max_pep: Maximum PEP of a PSM to be considered for transfers
    :param mask: if false, uses "identification" column from summary frame. If set to a value, transfer uses the
    'identification_{mask}' column, needed for the masking analysis
    :param ambiguity_decision: if 'all', returns raw sequences with potential phospho positions when encountering isomer
    clusters; if 'majority', decides for one sequence by majority vote
    :return: DataFrame with transferred identifications resembling MaxQuant msmsScans.txt
    """
    if mask:
        identification_column = f'identification_{mask}'
    else:
        identification_column = 'identification'

    if ambiguity_decision == 'majority':
        # TODO: Generate modified sequence from probability string rather than taking it from the cluster
        mod_seq_func = lambda s: get_consensus_modified_sequence(s, get_most_common_sequence)
    elif ambiguity_decision == 'all':
        mod_seq_func = get_consensus_modified_sequence
    else:
        raise ValueError("The parameter 'ambiguity_decision' has to be set on 'all' or 'majority'!")

    agg_funcs = {'Sequence': utils.get_unique_else_nan,
                 'Modifications': utils.get_unique_else_nan,
                 'Modified sequence': mod_seq_func,
                 'Phospho (STY) Probabilities': calculate_average_probabilities,
                 'Proteins': utils.csv_list_unique,
                 'Gene Names': utils.csv_list_unique,
                 'Protein Names': utils.csv_list_unique,
                 'Charge': utils.get_unique_else_nan,
                 'm/z': 'mean',
                 'Mass': 'mean',
                 'Missed cleavages': utils.get_unique_else_nan,
                 'Length': utils.get_unique_else_nan,
                 'PEP': 'max',
                 'Reverse': utils.get_unique_else_nan}

    identified_scans = summary_df['Modified sequence'].notna()
    pep_filtered = pd.Series(np.ones_like(summary_df.index), dtype='bool')
    if max_pep:
        pep_filtered = summary_df['PEP'].astype(float) <= max_pep / 100
    cluster_info_df = summary_df[identified_scans & pep_filtered].groupby('clusterID', as_index=False).agg(agg_funcs)
    # Mark all clusters with a unique identification as transferred ('t').
    # Identifications by MQ will overwrite this column as direct identification ('d') a few lines below.
    cluster_info_df[identification_column] = np.where(cluster_info_df['Modified sequence'].notna(), 't', None)

    replacement_dict = {k: "grouped_" + k for k in agg_funcs.keys()}
    cluster_info_df.rename(columns=replacement_dict, inplace=True)

    summary_df = pd.merge(left=summary_df, right=cluster_info_df, on=['clusterID'], how='left')
    summary_df.loc[identified_scans, identification_column] = 'd'
    # Now we have 'd' in every ID that came from MaxQuant and 't' in every ID that came from the clustering
    # And now we copy the contents of the 'grouped_...' columns into the original columns for every row where we have a 't'
    summary_df.loc[
        # Aren't all of these cells by definition NaNs? Can we make use of that?
        # Like 'Merge, and if you find two columns with the same name always use the value that is not NaN!'
        # Then we could do this directly in the merge and wouldn't have to go over the df again here
        # Try pd.DataFrame.combine(). For this the dfs would need to be in the same shape though.
        summary_df[identification_column] == 't', replacement_dict.keys()] = summary_df.loc[
        summary_df[identification_column] == 't', replacement_dict.values()].to_numpy()
    # The 'grouped_...' columns have made themselves redundant
    summary_df.drop(columns=replacement_dict.values(), inplace=True)
    return summary_df

def transform_phospho_psp_format(sequences: List[str]) -> List[str]:
    """Replaces phosphorylation modification by lower case letter (PhosphositePlus convention),
    e.g. 'AAAAAAAGDS(Phospho (STY))DS(Phospho (STY))WDADAFSVEDPVRK' => 'AAAAAAAGDsDsWDADAFSVEDPVRK'
    """
    sequences = [re.sub('_', '', x) for x in sequences]
    return [re.sub(PHOSPHO_REGEX, lambda pat: pat.group(1).lower(), x) for x in sequences]

def remove_probabilities(sequences_with_probabilities: List[str]) -> List[str]:
    return [remove_probabilities_from_sequence(s) for s in sequences_with_probabilities]

def remove_probabilities_from_sequence(sequence: str) -> str:
    """remove probability strings noted in parentheses,
    e.g. 'AAAAAAAGDS(0.988)DS(0.012)WDADAFSVEDPVRK' => 'AAAAAAAGDSDSWDADAFSVEDPVRK'
    """
    return re.sub(PROBABILITY_REGEX, '', sequence)

def check_ambiguity(sequences: List[str],
                    transform_sequence: Callable[[List[str]], List[str]] = transform_phospho_psp_format):
    sequences = utils.remove_nan_values(set(sequences))
    if len(sequences) == 0:
        return np.nan, np.nan
    if len(sequences) == 1:
        return sequences[0], np.nan

    transformed_sequences = transform_sequence(sequences)
    unique_sequences = set(map(str.upper, transformed_sequences))
    if len(unique_sequences) != 1:
        return np.NaN, np.nan
    return sequences, transformed_sequences

def is_unambiguous(sequences):
    return (type(sequences) == float and np.isnan(sequences)) or type(sequences) == str

def get_mod_probabilities_dict(probstring: str) -> Dict[int, float]:
    probsplit = re.split(PROBABILITY_REGEX, probstring)
    start = 0
    probdict = dict()
    for amino_acids, prob in zip(probsplit[0::2], probsplit[1::2]):
        start += len(amino_acids)
        probdict[start] = float(prob)
    return probdict

def sum_dictionaries(dictionary):
    return dict(functools.reduce(operator.add, map(collections.Counter, dictionary)))

def average_dictionaries(dictionary):
    summed_dictionary = sum_dictionaries(dictionary)

    num_dictionaries = len(dictionary)

    def get_average_and_round(x):
        return round(x / num_dictionaries, 3)

    return {pos: get_average_and_round(summed_prob) for pos, summed_prob in summed_dictionary.items()}

def add_probabilities_to_sequence(sequence, probability_dict):
    for position, probability in reversed(sorted(list(probability_dict.items()))):
        sequence = sequence[:position] + f'({probability})' + sequence[position:]
    return sequence

def calculate_average_probabilities(mod_probability_sequences):
    mod_probability_sequences, _ = check_ambiguity(mod_probability_sequences, remove_probabilities)
    if is_unambiguous(mod_probability_sequences):
        return mod_probability_sequences

    mod_probabilities = [get_mod_probabilities_dict(p) for p in mod_probability_sequences]
    average_mod_probabilities = average_dictionaries(mod_probabilities)

    sequence = remove_probabilities_from_sequence(mod_probability_sequences[0])
    sequence_with_probabilities = add_probabilities_to_sequence(sequence, average_mod_probabilities)
    # # this does not work for multiple phosphorylations in one sequence
    # if not 0.95 < sum(resdict.values()) < 1.05:
    #     raise ValueError(f'Probability sum deviates from expected value: {resdict}')
    return sequence_with_probabilities

def get_modified_sequence_annotation(sequences, sequences_psp_format):
    # TODO: Make ambiguous localization handling work for acetylation (and p-ac-combinations) as well
    # TODO: If underscores are handed over in the modified seq, the modification positions are incorrect in the end!
    num_mods = len(re.findall(r'[sty]', sequences_psp_format[0]))
    phospho_positions = {m.start() + 1 for x in sequences_psp_format for m in re.finditer(r'[sty]', x)}
    phospho_positions = sorted(list(phospho_positions))
    phopsho_positions_string = "/".join(map(lambda x: f'p{x}', phospho_positions))
    mod_sequence_without_phospho = utils.remove_modifications(sequences[0], remove_phospho_only=True)
    return f"{mod_sequence_without_phospho}.{num_mods}.{phopsho_positions_string}"

def get_most_common_sequence(sequences, sequences_psp_format):
    sequences = utils.remove_nan_values(sequences)
    return collections.Counter(sequences).most_common(1)[0][0]

def get_consensus_modified_sequence(sequences, consensus_function=get_modified_sequence_annotation):
    unique_sequences, sequences_psp_format = check_ambiguity(sequences)
    if is_unambiguous(unique_sequences):
        return unique_sequences
    return consensus_function(sequences, sequences_psp_format)

def flag_ambiguous_clusters(sumdf, rawseq='Sequence', modseq='Modified sequence'):
    """
    adds MIC flags to all scans in MICs
    :param sumdf: Summary dataframe, merged from cleaned msms.txt and MaRaCluster clusters.tsv file
    :param rawseq: Column name of the raw sequence column
    :param modseq: Column name of the modified sequence column
    :return:
    """
    # Store number of unique modified sequences per cluster in group_dataframe
    group_dataframe = sumdf.groupby('clusterID')[modseq].nunique(dropna=True).reset_index(name='seqcount')

    # Filter for clusters with more than one modified sequence and flag with column mod_ambiguous
    group_dataframe = group_dataframe[group_dataframe['seqcount'] >= 2]
    group_dataframe.drop(columns=['seqcount'], axis=1, inplace=True)
    group_dataframe['mod_ambiguous'] = 1

    # merge with initial frame
    merged_dataframe = pd.merge(left=sumdf, right=group_dataframe, on='clusterID', how='left')

    # Store number of unique raw sequences per cluster in group_dataframe
    group_dataframe = sumdf.groupby('clusterID')[rawseq].nunique(dropna=True).reset_index(name='seqcount')

    # Filter for clusters with more than one raw sequence and flag with column raw_ambiguous
    group_dataframe = group_dataframe[group_dataframe['seqcount'] >= 2]
    group_dataframe.drop(columns=['seqcount'], axis=1, inplace=True)
    group_dataframe['raw_ambiguous'] = 1

    # merge with initial frame
    merged_dataframe = pd.merge(left=merged_dataframe, right=group_dataframe, on='clusterID', how='left')

    # remove mod_ambiguous flags if cluster is raw_ambiguous
    merged_dataframe.loc[merged_dataframe['raw_ambiguous'] == 1, 'mod_ambiguous'] = np.nan
    return merged_dataframe