from django.test import TestCase
from django.conf import settings

from rpa.forms import (
    RpaForm,
    ProcessingActivityNameForm,
    DataControllerForm,
    JointControllerForm,
    DataProtectionOfficerForm,
    InternallyResponsibleDeptForm,
    CategoryOfPersonalDataForm,
    CategoriesOfPersonalDataOriginForm,
    PurposeAndLegalBasisForm,
    DataSubjectForm,
    TimeLimitForErasureForm,
    CategoryOfRecipientsForm,
    TransferToThirdCountryForm,
    AccessGroupForm,
    TransparencyForm,
    DataProcessorForm,
    PrivacyImpactAssessmentForm,
    TOMForm,
    RPAAnnexForm,
)

class RpaFormTest(TestCase):
    def test_create_rpa_new(self):
        """Test whether filling out a new RPA form basically works;
        simple form.is_valid() assertion
        """
        form_data = {
            "slug": "satva2022",
            "rpa_bumper": True,
            "helptext_display_default": "show",
        }
        form = RpaForm(data=form_data)
        self.assertTrue(form.is_valid())

class ProcessingActivityNameFormTest(TestCase):
    def test_create_pa_name_new(self):
        """Test whether filling out a new RPA name form basically works;
        simple form.is_valid() assertion (new activity)
        """
        form_data = {
            "name": "Some mildly useful processing activity",
            "is_new": True,
            "date_intro": "2022-11-16",
            "has_changed": None,
            "date_changed": None,
            "former_name": "",
        }
        form = ProcessingActivityNameForm(data=form_data)
        self.assertTrue(form.is_valid())

    def test_create_pa_name_changed(self):
        """Test whether filling out a new RPA name form basically works;
        simple form.is_valid() assertion (changed activity)
        """
        form_data = {
            "name": "Some mildly useful processing activity",
            "is_new": False,
            "date_intro": None,
            "has_changed": True,
            "date_changed": "2022-11-16",
            "former_name": "An even less useful processing activity",
        }
        form = ProcessingActivityNameForm(data=form_data)
        self.assertTrue(form.is_valid())

    def test_create_pa_name_validation(self):
        """Test whether filling out a new RPA name form basically works;
        several form.is_valid() assertions to test form validation
        """
        form_data = {
            "name": "",
            "is_new": False,
            "date_intro": None,
            "has_changed": True,
            "date_changed": "2022-11-16",
            "former_name": "An even less useful processing activity",
        }
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 2nd form run
        form_data["name"] = "Some mildly useful processing activity"
        form_data["is_new"] = False
        form_data["has_changed"] = False
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["is_new"] = True
        form_data["has_changed"] = True
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 4th form run
        form_data["is_new"] = None
        form_data["has_changed"] = None
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 5th form run
        form_data["is_new"] = None
        form_data["has_changed"] = False
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 6th form run
        form_data["is_new"] = False
        form_data["has_changed"] = None
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 7th form run
        form_data["is_new"] = True
        form_data["date_intro"] = None
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 8th form run
        form_data["is_new"] = None
        form_data["has_changed"] = True
        form_data["date_changed"] = None
        form = ProcessingActivityNameForm(data=form_data)
        self.assertFalse(form.is_valid())

class DataControllerFormTest(TestCase):
    def test_create_dcon_new(self):
        """Test whether filling out a new RPA data controller form
        basically works; simple form.is_valid() assertion
        """
        form_data = {
            "dcon_name": settings.CONTROLLER_NAME,
            "dcon_repby": settings.CONTROLLER_REPBY,
            "dcon_street": settings.CONTROLLER_STREET,
            "dcon_pcode": settings.CONTROLLER_PCODE,
            "dcon_city": settings.CONTROLLER_CITY,
            "dcon_country": settings.CONTROLLER_COUNTRY,
            "dcon_phone": settings.CONTROLLER_PHONE,
            "dcon_email": settings.CONTROLLER_EMAIL,
            "dcon_web": settings.CONTROLLER_WEB,
        }
        form = DataControllerForm(data=form_data)
        self.assertTrue(form.is_valid())

class JointControllerFormTest(TestCase):
    def test_jcon_detail_validation(self):
        """Test whether filling out a new RPA joint controller form
        basically works; several form.is_valid() assertions to test form
        validation
        """
        form_data = {
            "jcon_exists": False,
        }
        form = JointControllerForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["jcon_exists"] = None
        form = JointControllerForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["jcon_exists"] = True
        form = JointControllerForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 4th form run
        form_data = {
            "jcon_exists": True,
            "jcon_name": "Some other entity",
        }
        form = JointControllerForm(data=form_data)
        self.assertTrue(form.is_valid())

class DataProtectionOfficerFormTest(TestCase):
    def test_create_dpo_new(self):
        """Test whether filling out a new RPA data protection officer
        form basically works; simple form.is_valid() assertion
        """
        form_data = {
            "dpo_name": settings.DPO_NAME,
            "dpo_street": settings.DPO_STREET,
            "dpo_pcode": settings.DPO_PCODE,
            "dpo_city": settings.DPO_CITY,
            "dpo_country": settings.DPO_COUNTRY,
            "dpo_phone": settings.DPO_PHONE,
            "dpo_email": settings.DPO_EMAIL,
            "dpo_web": settings.DPO_WEB,
        }
        form = DataProtectionOfficerForm(data=form_data)
        self.assertTrue(form.is_valid())

class InternallyResponsibleDeptFormTest(TestCase):
    def test_ird_detail_validation(self):
        """Test whether filling out a new RPA internally responsible
        dept. form basically works; several form.is_valid() assertions
        to test form validation
        """
        form_data = {
            "ird_name": "Some dept. of Some Entity",
            "ird_street": "Some Street 42",
            "ird_pcode": "12345",
            "ird_city": "Some City",
            "ird_country": "Some Country",
            "ird_phone": "555-123456",
            "ird_email": "some_dept@subdomain" + settings.CONTROLLER_MAILDOM,
            "ird_comments": "",
        }
        form = InternallyResponsibleDeptForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["ird_email"] = "some_dept@some-other-entity.org"
        form = InternallyResponsibleDeptForm(data=form_data)
        self.assertFalse(form.is_valid())

class CategoryOfPersonalDataFormTest(TestCase):
    def test_cpd_detail_validation(self):
        """Test whether filling out a new RPA category of personal data
        form basically works; several form.is_valid() assertions to test
        form validation
        """
        form_data = {
            "cpd_index": 9,
            "cpd_name": "Some category of personal data",
            "cpd_is_special": False,
        }
        form = CategoryOfPersonalDataForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["cpd_is_special"] = True
        form = CategoryOfPersonalDataForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 3rd form run
        form_data["cpd_index"] = None
        form = CategoryOfPersonalDataForm(data=form_data)
        self.assertFalse(form.is_valid())

class CategoriesOfPersonalDataOriginFormTest(TestCase):
    def test_cpdo_detail_validation(self):
        """Test whether filling out a new RPA category of personal data
        origin form basically works; several form.is_valid() assertions
        to test form validation
        """
        form_data = {
            "cpdo_descr": "Some description of personal data origin",
        }
        form = CategoriesOfPersonalDataOriginForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["cpdo_descr"] = ""
        form = CategoriesOfPersonalDataOriginForm(data=form_data)
        self.assertFalse(form.is_valid())

class PurposeAndLegalBasisFormTest(TestCase):
    def test_create_plb_new(self):
        """Test whether filling out a new RPA purpose and legal basis
        form basically works; simple form.is_valid() assertion
        """
        form_data = {
            "plb_purpose": "Some description of processing activity purpose",
            "plb": [
                "plb_art6_1a",
            ],
            "plb_reasons": "Some explanation of choice for legal bases",
        }
        form = PurposeAndLegalBasisForm(data=form_data)
        self.assertTrue(form.is_valid())

class DataSubjectFormTest(TestCase):
    def test_dsub_detail_validation(self):
        """Test whether filling out a new RPA data subject form
        basically works; several form.is_valid() assertions to test form
        validation
        """
        form_data = {
            "dsub_name": "Some data subject category",
            "dsub_cpd_sel": [
                "1",
                "2",
            ],
        }
        form = DataSubjectForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["dsub_name"] = ""
        form = DataSubjectForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["dsub_name"] = "Some data subject category"
        form_data["dsub_cpd_sel"] = []
        form = DataSubjectForm(data=form_data)
        self.assertFalse(form.is_valid())

class TimeLimitForErasureFormTest(TestCase):
    def test_create_tle_new(self):
        """Test whether filling out a new RPA time limits for erasure
        form basically works; simple form.is_valid() assertion
        """
        form_data = {
            "tle_handling": "tle_in_rpa",
            "tle_start": "Some start description.",
            "tle_length": "Some length description.",
            "tle_comment": "Some comment.",
            "tle_cpd_sel": [
                "1",
                "2",
            ],
        }
        form = TimeLimitForErasureForm(data=form_data)
        self.assertTrue(form.is_valid())

class CategoryOfRecipientsFormTest(TestCase):
    def test_create_crec_new(self):
        """Test whether filling out a new RPA category of recipients
        form basically works; simple form.is_valid() assertion
        """
        form_data = {
            "crec_handling": "crec_in_rpa",
            "crec_designation": "Some category of recipients.",
            "crec_is_external": False,
            "crec_cpd_sel": [
                "1",
                "2",
            ],
        }
        form = CategoryOfRecipientsForm(data=form_data)
        self.assertTrue(form.is_valid())

class TransferToThirdCountryFormTest(TestCase):
    def test_ttc_detail_validation(self):
        """Test whether filling out a new RPA transfer to third country
        form basically works; several form.is_valid() assertions to test
        form validation
        """
        form_data = {
            "ttc_3rdcountry_intended": True,
            "ttc_3rdcountry": "Some 3rd country recipient.",
            "ttc_3rdcountry_adequacy": False,
            "ttc_non_adequacy_choices": [
                "ttc_sdpc",
            ],
            "ttc_non_adequacy_explanation": "Some explanation.",
        }
        form = TransferToThirdCountryForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["ttc_3rdcountry_intended"] = None
        form = TransferToThirdCountryForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["ttc_3rdcountry_intended"] = True
        form_data["ttc_3rdcountry"] = ""
        form = TransferToThirdCountryForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 4th form run
        form_data["ttc_3rdcountry"] = "Some 3rd country recipient."
        form_data["ttc_3rdcountry_adequacy"] = None
        form = TransferToThirdCountryForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 5th form run
        form_data["ttc_3rdcountry_adequacy"] = False
        form_data["ttc_non_adequacy_choices"] = [
            "ttc_sdpc",
        ]
        form_data["ttc_non_adequacy_explanation"] = ""
        form = TransferToThirdCountryForm(data=form_data)
        self.assertFalse(form.is_valid())

class AccessGroupFormTest(TestCase):
    def test_create_agrp_new(self):
        """Test whether filling out a new RPA access group form
        basically works; simple form.is_valid() assertion
        """
        form_data = {
            "agrp_handling": "agrp_in_rpa",
            "agrp_name": "Some access group.",
            "agrp_can_read": True,
            "agrp_can_edit": True,
            "agrp_can_delete": True,
            "agrp_cpd_sel": [
                "1",
                "2",
            ],
        }
        form = AccessGroupForm(data=form_data)
        self.assertTrue(form.is_valid())

class TransparencyFormTest(TestCase):
    def test_tran_detail_validation(self):
        """Test whether filling out a new RPA transparency form
        basically works; several form.is_valid() assertions to test form
        validation
        """
        form_data = {
            "tran_choices": [
                "tran_leaflet",
                "tran_online",
            ],
            "tran_explanation": "Some explanation.",
        }
        form = TransparencyForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["tran_choices"] = []
        form = TransparencyForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["tran_choices"] = [
            "tran_leaflet",
            "tran_online",
        ]
        form_data["tran_explanation"] = ""
        form = TransparencyForm(data=form_data)
        self.assertFalse(form.is_valid())

class DataProcessorFormTest(TestCase):
    def test_dpro_detail_validation(self):
        """Test whether filling out a new RPA data processor form
        basically works; several form.is_valid() assertions to test form
        validation
        """
        form_data = {
            "dpro_is_assigned": True,
            "dpro_name": "Some DP name.",
            "dpro_street": "Some DP street.",
            "dpro_pcode": "12345",
            "dpro_city": "Some DP city.",
            "dpro_country": "Some DP country.",
            "dpro_contact": "Some contact at DP.",
        }
        form = DataProcessorForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["dpro_is_assigned"] = None
        form = DataProcessorForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["dpro_is_assigned"] = True
        form_data["dpro_name"] = ""
        form = DataProcessorForm(data=form_data)
        self.assertFalse(form.is_valid())

class PrivacyImpactAssessmentFormTest(TestCase):
    def test_pia_detail_validation(self):
        """Test whether filling out a new RPA PIA form basically works;
        several form.is_valid() assertions to test form validation
        """
        form_data = {
            "pia_required": True,
            "pia_not_required_reason": [],
            "pia_results": [
                "pia_additional_measures",
            ],
        }
        form = PrivacyImpactAssessmentForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["pia_required"] = None
        form = PrivacyImpactAssessmentForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["pia_required"] = True
        form_data["pia_results"] = []
        form = PrivacyImpactAssessmentForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 4th form run
        form_data["pia_required"] = False
        form_data["pia_not_required_reason"] = []
        form = PrivacyImpactAssessmentForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 5th form run
        form_data["pia_not_required_reason"] = [
            "pia_nr_automated_processing",
            "pia_nr_special_categories",
            "pia_nr_monitoring_public",
        ]
        form = PrivacyImpactAssessmentForm(data=form_data)
        self.assertFalse(form.is_valid())

class TOMFormTest(TestCase):
    def test_tom_detail_validation(self):
        """Test whether filling out a new RPA TOM form basically works;
        several form.is_valid() assertions to test form validation
        """
        form_data = {
            "tom_handling": "tom_in_rpa",
            "tom_pseudonym_selection": [],
            "tom_pseudonym": "Some measures regarding pseudonymization.",
            "tom_encryption_selection": [],
            "tom_encryption": "Some measures regarding encryption.",
            "tom_integrity_selection": [],
            "tom_integrity": "Some measures regarding integrity/confidentiality.",
            "tom_availability_selection": [],
            "tom_availability": "Some measures regarding availability/resilience.",
            "tom_evaluation_selection": [],
            "tom_evaluation": "Some measures regarding evaluation.",
            "tom_appropriation_selection": [],
            "tom_appropriation": "Some measures regarding appropriation.",
            "tom_transparency_selection": [],
            "tom_transparency": "Some measures regarding transparency.",
            "tom_subject_rights_selection": [],
            "tom_subject_rights": "Some measures regarding subject rights.",
        }
        form = TOMForm(data=form_data)
        self.assertTrue(form.is_valid())
        # 2nd form run
        form_data["tom_handling"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 3rd form run
        form_data["tom_handling"] = "tom_in_rpa"
        form_data["tom_pseudonym"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 4th form run
        form_data["tom_pseudonym"] = "Some measures regarding pseudonymization."
        form_data["tom_encryption"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 5th form run
        form_data["tom_encryption"] = "Some measures regarding encryption."
        form_data["tom_integrity"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 6th form run
        form_data[
            "tom_integrity"
        ] = "Some measures regarding integrity/confidentiality."
        form_data["tom_availability"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 7th form run
        form_data[
            "tom_availability"
        ] = "Some measures regarding availability/resilience."
        form_data["tom_evaluation"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 8th form run
        form_data["tom_evaluation"] = "Some measures regarding evaluation."
        form_data["tom_appropriation"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 9th form run
        form_data["tom_appropriation"] = "Some measures regarding appropriation."
        form_data["tom_transparency"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())
        # 10th form run
        form_data["tom_transparency"] = "Some measures regarding transparency."
        form_data["tom_subject_rights"] = ""
        form = TOMForm(data=form_data)
        self.assertFalse(form.is_valid())

class RPAAnnexFormTest(TestCase):
    def test_create_annex_new(self):
        """Test whether filling out a new RPA annex form basically
        works; simple form.is_valid() assertion
        """
        form_data = {
            "annex_index": 4,
            "annex_name": "Some annex.",
        }
        form = RPAAnnexForm(data=form_data)
        self.assertTrue(form.is_valid())


from airflow.hooks.postgres_hook import PostgresHook
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults

class LoadFactOperator(BaseOperator):
    ui_color = '#F98866'

    @apply_defaults
    def __init__(self,
                 redshift_conn_id="",
                 table="",
                 create_table_sql="",
                 insert_sql="",
                 *args, **kwargs):

        super(LoadFactOperator, self).__init__(*args, **kwargs)
        self.redshift_conn_id = redshift_conn_id
        self.table = table
        self.insert_sql = insert_sql
        self.create_table_sql = create_table_sql

    def execute(self, context):
        redshift = PostgresHook(postgres_conn_id=self.redshift_conn_id)

        self.log.info(f"Create fact table {self.table}")
        redshift.run(self.create_table_sql)

        self.log.info(f"Loading data from staging tables to the {self.table}")
        insert_query = """INSERT INTO {table} {insert_sql} """.format(
                table = self.table,
                insert_sql = self.insert_sql)
        self.log.info(f"Final insert query: {insert_query}")

        self.log.info(f"Copy data to the fact table: {self.table}")
        redshift.run(insert_query)

        self.log.info(f"Finished loading data from staging tables to the {self.table}")


import base64

from osv import osv, fields

class carrier_file_model(osv.osv):
    _inherit = 'delivery.carrier.file'

    def get_write_mode_selection(self, cr, uid, context=None):
        res = super(carrier_file_model, self).get_write_mode_selection(cr, uid, context=context)
        if 'document' not in res:
            res.append(('document', 'Document'))
        return res

    _columns = {
        'write_mode': fields.selection(get_write_mode_selection, 'Write on', required=True),
        'document_directory_id': fields.many2one('document.directory', 'Document Directory'),
        'export_path': fields.char('Export Path', size=256),
    }

    def _prepare_attachment(self, carrier_file, filename, file_content, context=None):
        return {'name': "%s_%s" % (carrier_file.name, filename),
                'datas_fname': filename,
                'datas': base64.encodestring(file_content),
                'parent_id': carrier_file.document_directory_id.id,
                'type': 'binary'}

    def _write_file(self, cr, uid, carrier_file, filename, file_content, context=None):
        if carrier_file.write_mode == 'document':
            vals = self._prepare_attachment(carrier_file, filename, file_content)
            self.pool.get('ir.attachment').create(cr, uid, vals, context=context)
            return True
        else:
            return (super(carrier_file_model, self)
                    ._write_file(cr, uid, carrier_file, filename, file_content, context=None))

carrier_file_model()


from typing import Union, List, Dict
from Mattermost_Base import Base

class Opengraph(Base):
    def __init__(self, token: str, server_url: str):
        super().__init__(token, server_url)
        self.api_url = f"{self.base_url}/opengraph"

    def get_og_mdata_for_url(self, url: str) -> dict:
        """
        Get Open Graph Metadata for a specif URL.
        Use the Open Graph protocol to get some generic metadata about a URL.
        Used for creating link previews.

        Minimum server version: 3.10
        No permission required but must be logged in.

        :param url: The URL to get Open Graph Metadata.
        :return: Open Graph retrieval.
        """

        url = f"{self.api_url}/"

        self.reset()
        self.add_application_json_header()
        self.add_to_json('url', url)

        return self.request(url, request_type='POST', body=True)


# look for occurrences of four anywhere.
# has a list of T integers, for each of them
# calculate the number of occurrences of the digit 4 in the decimal representation
# input : The first line of input consists of a single integer T, denoting the number of integers in list.Then, there are T lines, each of them contain a single integer from the list.
# output : Print the number of occurrences of the digit 4 in the decimal representation of each integer in the list.

def lucky_four(lst):
    count = 0
    for i in lst:
        if str(i).count('4') > 0:
            count += 1
    return count


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import abc

import numpy as np
import pandas as pd
from pathlib import Path

class WriteResultHookBase(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def __call__(self, split, output_path, outputs, indices, labels=None, data=None,
                 is_train=False, reference_csv_filename=None):
        pass

class DefaultWriteResultHook(WriteResultHookBase):
    def __call__(self, split, output_path, outputs, indices ,labels=None, data=None,
                 is_train=False, reference_csv_filename=None):
        output_path = Path(output_path)
        if not output_path.is_dir():
            output_path.mkdir(parents=True)
        csv_path = output_path/'result.csv'

        if csv_path.exists(): #　overwrite existing csv
            df = pd.read_csv(csv_path)
        else:
            if reference_csv_filename is not None:
                df = pd.read_csv(reference_csv_filename)
            else:
                df = pd.DataFrame(index=indices)
            df['AnomalyScore'] = np.nan

        df.loc[indices,'AnomalyScore'] = outputs['anomaly_score']
        df.to_csv(csv_path, index=False)

        return


import os
import readline
from flask import Flask, request, redirect, url_for, send_from_directory, render_template
from werkzeug.utils import secure_filename
import numpy as np
from model.models.densenet import DenseNet
import torch, cv2

ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])
UPLOAD_FOLDER = 'uploads'

# load resnet model

resnet_model = DenseNet(growthRate=12,
                        depth=100,
                        reduction=0.5,
                        bottleneck=True,
                        nClasses=4)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
resnet_model.to(device)
resnet_model.eval()

trained_model_path = 'model/ckpt/checkpoint_01/bs128_lr1e-06_epoch50_bestacc0.7324324250221252_sgd.pt'
if os.path.exists(trained_model_path):
    resnet_model.load_state_dict(
        torch.load(trained_model_path, map_location='cpu'))
else:
    print('You do not load model !')
    exit()

# load label map

with open('model/data/label.map', 'r', encoding='utf8') as fi_label:
    id2label = {idx: label for idx, label in enumerate(fi_label.readlines())}

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

import torchvision.transforms as transforms

def inference(file_path):
    img = cv2.imread(file_path)
    img = cv2.resize(img, (32, 32))
    mean = [117.40523108152614, 129.08895341687355, 138.8146819924039]
    std = [71.07845902847068, 67.67320379657173, 67.58323701160265]

    local_transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize(mean, std)])

    img = local_transform(img).unsqueeze(0).to(device)

    img_out = resnet_model(img)
    _, preds = img_out.max(0)
    preds = preds.item()
    return preds

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            file.save(file_path)
            output = inference(file_path)
            output = f'Category is【{id2label[output]}】'

            return render_template('home.html',
                                   label=output,
                                   imagesource=file_path,
                                   mode_code=1)
    return render_template('home.html',
                           label='',
                           imagesource='static/imgs/example.jpg',
                           mode_code=0)

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

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


""" Test transform robustness to feature visualization """
import pickle as pkl
from os.path import join
from core.CNN_scorers import load_featnet
from core.GAN_utils import upconvGAN
from core.CorrFeatFactor.featvis_lib import vis_feattsr_factor
from lucent.optvis.transform import random_rotate, jitter

G = upconvGAN().cuda().eval()

expdir = r"E:\OneDrive - Washington University in St. Louis\corrFeatTsr_FactorVis\models\resnet50_linf8-layer3_NF3_bdr1_Tthresh_3__nobdr_res-robust_CV"
#%%
tfms = [
    jitter(8),
    # random_scale([1 + (i - 5) / 50.0 for i in range(11)]),
    random_rotate(list(range(-10, 11)) + 5 * [0]),
    jitter(4),
]
#%%
saveDict = pkl.load(open(join(expdir, "Alfa_Exp03_factors.pkl"),'rb'))
ccfactor = saveDict.ccfactor
Hmaps = saveDict.Hmaps
netname = saveDict.netname
layer = saveDict.layer
featvis_mode = saveDict.featvis_mode
bdr = saveDict.bdr
featnet, net = load_featnet(netname)
tsrimgs, mtg, score_traj = vis_feattsr_factor(ccfactor, Hmaps, net, G, layer, netname=netname, tfms=tfms,#[],
              score_mode=featvis_mode, featnet=featnet, Bsize=5, saveImgN=1, bdr=bdr, figdir=expdir, savestr="corr",
              saveimg=False, imshow=True)


from color.Color import *
from settings.Settings import screen_height, screen_width
from character.CharacterBase import Base
import pygame

class Player(Base):
    def __init__(self, screen, vector, height, width):
        super().__init__(screen, vector, height, width)
        self.alive = True

    def is_collided_with(self, platform):
        return self.rect.colliderect(platform.rect)

    def update(self):
        if self.alive:
            self.get_gravity()

            keys = pygame.key.get_pressed()
            r_border = screen_width - self.width
            b_border = screen_height - self.height
            if keys[pygame.K_LEFT]:
                self.vector.x -= 1
                if self.vector.x < 0:
                    self.vector.x = 0
            if keys[pygame.K_RIGHT]:
                self.vector.x += 1
                if self.vector.x > r_border:
                    self.vector.x = r_border
            if keys[pygame.K_SPACE]:
                self.vector.y -= 2
            if self.vector.y < 0:
                self.vector.y = 0
            if self.vector.y > b_border:
                print("You lost")
                quit()

    def draw(self):
        if self.alive:
            pygame.draw.rect(self.screen, color_rect, pygame.Rect(self.vector.x, self.vector.y, self.height, self.width))

    @staticmethod
    def get_draw():
        return pygame.draw

    def get_gravity(self):
        self.vector.y += 1
        return self


import os
from flask import Blueprint, render_template, session, request, jsonify
from app.main.view import check_session
from app.main.view.CommentsModel import Comment
from app.main.view.FansModel import Fans

blueprint = Blueprint('comments', __name__,
                    template_folder='templates',
                    static_folder='static')
@blueprint.route('/comments',methods=['GET'])
@check_session
def query_comments():
    comments = Comment.objects()
    comments_list = []

    response_data = {}
    for comment in comments:
        comments_json = {}
        comments_json['host_id'] = comment.host_id
        comments_json['host_nickname'] = comment.host_nickname
        comments_json['weibo_content'] = comment.weibo_content
        comments_json['weibo_link'] = comment.weibo_link
        comments_json['comment_user'] = comment.comment_user
        comments_json['comment_content'] = comment.comment_content
        comments_json['comment_time'] = comment.comment_time
        comments_json['create_at'] = comment.create_at
        comments_list.append(comments_json)
    response_data['draw'] = 1
    response_data['data'] = comments_list
    return jsonify(response_data)


"""
Given an array of numbers A , in which exactly two elements appear only once and all the other elements appear
exactly twice. Find the two elements that appear only once.

Note: Output array must be sorted.

Problem Constraints
2 <= |A| <= 100000
1 <= A[i] <= 109
"""

def solve(A):
    xor = 0
    for num in A:
        xor ^= num
    last_set_bit = xor & ~(xor - 1)
    x, y = 0, 0
    for num in A:
        if num & last_set_bit != 0:
            x ^= num
        else:
            y ^= num
    return [x, y] if x < y else [y, x]


import os
import tensorflow as tf
from pathlib import Path
from constants import Constants as cts

class Noise:
    def __init__(self):
        self.noise_paths = []
        self.noises = []

    def noise_preparation(self):
        # NOISE PREPATATION
        if '.DS_Store' in os.listdir(cts.DATASET_NOISE_PATH):
            os.remove(cts.DATASET_NOISE_PATH + '/.DS_Store')

        for subdir in os.listdir(cts.DATASET_NOISE_PATH):
            subdir_path = Path(cts.DATASET_NOISE_PATH) / subdir
            if os.path.isdir(subdir_path):
                self.noise_paths += [
                    os.path.join(subdir_path, filepath)
                    for filepath in os.listdir(subdir_path)
                    if filepath.endswith(".wav")
                ]
        return self.noise_paths

    def get_noise_tensor(self, noise_paths):
        # Resampling all noise samples to 16000 hz
        resampling_noise_to_16000hz()
        # Split noise into chunks of 16000 each
        self.noises = []
        for path in noise_paths:
            sample = load_noise_sample(path)
            if sample:
                self.noises.extend(sample)
        self.noises = tf.stack(self.noises)

        return self.noises

def resampling_noise_to_16000hz():
    command = (
            "for dir in `ls -1 " + cts.DATASET_NOISE_PATH + "`; do "
                                                            "for file in `ls -1 " + cts.DATASET_NOISE_PATH + "/$dir/*.wav`; do "
                                                                                "sample_rate=`ffprobe -hide_banner -loglevel panic -show_streams "
                                                                                "$file | grep sample_rate | cut -f2 -d=`; "
                                                                                "if [ $sample_rate -ne 16000 ]; then "
                                                                                "ffmpeg -hide_banner -loglevel panic -y "
                                                                                "-i $file -ar 16000 temp.wav; "
                                                                                "mv temp.wav $file; "
                                                                                "fi; done; done"
    )
    os.system(command)

def load_noise_sample(path):
    sample, sampling_rate = tf.audio.decode_wav(
        tf.io.read_file(path), desired_channels=1
    )
    if sampling_rate == cts.SAMPLING_RATE:
        slices = int(sample.shape[0] / cts.SAMPLING_RATE)
        sample = tf.split(sample[: slices * cts.SAMPLING_RATE], slices)
        return sample
    else:
        print(f"Sampling rate {path} is incorrect. Ignorit it")
        return None


"""3次元シミュレーション."""

import numpy as np
from scipy.integrate import ode
from math import pi

import blade
import airfoils as af
import attitude

def blade_FM(x, blades, disp=True):
    """翼に働く力を出力する."""
    # q = x[0:4]
    pqr = x[4:7]
    # xyz = x[7:10]
    uvw = x[10:13]

    n_blade = len(blades)
    arrFM = np.zeros((n_blade, 2, 3), dtype=float)
    sumF = np.zeros(3, dtype=float)
    sumM = np.zeros(3, dtype=float)
    arrPressures = np.zeros((n_blade, 3, 11))

    for id, b in enumerate(blades):
        if disp:
            print(f'\r\n --- {id}_wing calculation ---')

        # 翼素ごとの回転による対気速度(機体座標系)
        arrRotation = blade.getRotationalAirSpeed(
            pqr,
            b.arrPosWingElems,
            b.n_elements
        )
        if disp:
            print(
                '\r\n  --- AirSpeed only from the rotation ---\r\n',
                arrRotation
            )

        # 翼素ごとの対気速度(翼素座標系)
        arrAeroVelocity = blade.getAirSpeed(
            uvw,
            arrRotation,
            b.blade_attitude
        )
        if disp:
            print(
                '\r\n  --- AirSpeed (Wing Frame) --- \r\n',
                arrAeroVelocity
            )

        # 翼素ごとの圧力(機体座標系)
        arrPressure = blade.getPressureInWingElem(
            arrAeroVelocity,
            b.blade_attitude,
            b.airfoil
        )
        arrPressures[id, :, :] = arrPressure
        if disp:
            print(
                '\r\n  --- Pressure distribution (Body Frame) --- \r\n',
                arrPressure
            )

        # ブレード全体の力・モーメント（機体座標系）
        F, M = blade.getBladeForceMoment(
            arrPressure,
            b.arrPosWingElems,
            b.chord_length,
            b.blade_length,
            b.n_elements
        )
        if disp:
            print(
                '\r\n  --- F and M of the blade (Body Frame) ---\r\n',
                F, M
            )

        arrFM[id, 0, :] = F
        arrFM[id, 1, :] = M

        sumF += F
        sumM += M

    return sumF, sumM, arrPressures

def func(model, x, F, M):
    """状態量の時間微分の計算."""
    # 加速度の更新
    force_body = F + model.gravityBody()
    accel = model.calcDerivativeOfVelocityBody(force_body)
    # 角加速度の更新
    omega_dot = model.calcDerivativeOfOmegaBody(M)
    # 速度の更新
    vel_inertial = model.getVelocityInertial()
    # クオータニオンの時間微分の更新
    omega_inertial = model.bodyVector2InertialVector(x[4:7])
    q_dot = model.calcDerivativeOfQuartanion(omega_inertial)
    f = np.hstack((q_dot, omega_dot, vel_inertial, accel))
    return f

def postProcess(model, x):
    """積分計算後の値をモデルに更新する."""
    model.updateQuartanionODE(x[0:4])
    model.omegaBody = x[4:7]
    model.position = x[7:10]
    model.velocityBody = x[10:13]
    print('Quartanion: ', x[0:4])
    print('PQR: ', x[4:7])
    print('Position: ', x[7:10])
    print('Velocity: ', x[10:13])
    x[0:4] = model.quartanion
    return x

def main():
    """実行用関数."""
    # クオータニオンモジュールの初期化.
    att = attitude.Attitude6DoF()
    att.omegaBody = [0.0, 0.0*pi/180, 0.0*pi/180]

    # ブレードの初期化（複数の翼をつける場合は逐次appendする）
    blade_pitch = -80.0
    Blades = []
    Blades.append(blade.Blade(
        n_elem=10,
        pos_root=[0.0, 0.06, -0.05],
        att=[0.0 * pi / 180, blade_pitch * pi/180, 0.0 * pi/180],
        b_len=0.13,
        c_len=0.1,
        airfoil=af.NACA0012_181127
    ))

    Blades.append(blade.Blade(
        n_elem=10,
        pos_root=[0.0, -0.06, -0.05],
        att=[0.0 * pi / 180, blade_pitch * pi/180, 180.0 * pi/180],
        b_len=0.13,
        c_len=0.1,
        airfoil=af.NACA0012_181127
    ))

    Blades.append(blade.Blade(
        n_elem=10,
        pos_root=[0.06, 0.0, -0.05],
        att=[0.0 * pi / 180, blade_pitch * pi/180, 270.0 * pi/180],
        b_len=0.13,
        c_len=0.1,
        airfoil=af.NACA0012_181127
    ))

    Blades.append(blade.Blade(
        n_elem=10,
        pos_root=[-0.06, 0.0, -0.05],
        att=[0.0 * pi / 180, blade_pitch * pi/180, 90.0 * pi/180],
        b_len=0.13,
        c_len=0.1,
        airfoil=af.NACA0012_181127
    ))

    # 状態量の初期化
    x0 = np.zeros((13), dtype=float)
    x0[0:4] = att.setQuartanionFrom(5.0*pi/180, 0.0*pi/180, 0.0*pi/180)
    x0[4:7] = att.omegaBody
    # x0[7:10]: position of the model
    x0[10:13] = att.velocityBody

    t0 = 0.0
    tf = 2.0
    dt = 0.0001

    F = np.array([0.0, 0.0, 0])
    M = np.array([0.0, 0.0, 0])

    # ODEの設定
    solver = ode(func).set_integrator('dopri5', method='bdf')
    solver.set_initial_value(x0, t0)
    solver.set_f_params(att, solver.y, F, M)
    x = np.zeros([int((tf-t0)/dt)+1, 13])
    t = np.zeros([int((tf-t0)/dt)+1, 1])
    F_log = np.zeros((int((tf-t0)/dt)+1, 3))
    M_log = np.zeros((int((tf-t0)/dt)+1, 3))
    arrP_log = np.zeros((int((tf-t0)/dt)+1, len(Blades), 3, 11), dtype=float)

    # TODO: solver.yのクオータニオン地を正規化しなければならない
    index = 0
    while solver.successful() and solver.t < tf and index < int((tf-t0)/dt)+1:
        solver.integrate(solver.t+dt)
        x[index] = solver.y
        t[index] = solver.t

        print(solver.y)
        F, M, arrPressures = blade_FM(solver.y, Blades)
        F_log[index] = F
        M_log[index] = M
        arrP_log[index, :, :, :] = arrPressures
        xt = postProcess(att, solver.y)
        solver.set_initial_value(xt, solver.t)

        solver.set_f_params(att, solver.y, F, M)

        index += 1

    x_log = np.hstack((t, x))
    F_log = np.hstack((t, F_log))
    M_log = np.hstack((t, M_log))

    np.save('x_1', x_log)
    np.save('M_1', M_log)
    np.save('F_1', F_log)
    np.save('dist_df', arrP_log)

if __name__ == '__main__':
    main()


from collections import defaultdict, deque
from heapq import heappush, heappop

class DSU:
    def __init__(self, n):
        self.parents = [i for i in range(n)]

    def _root(self, a):
        if self.parents[a] == a:
            return a
        ra = self._root(self.parents[a])
        self.parents[a] = ra
        return self.parents[a]

    def find(self, a):
        return self._root(a)

    def union(self, a, b):
        ra = self._root(a)
        rb = self._root(b)

        if ra == rb:
            return False

        self.parents[ra] = rb
        return True

tcs = int(input())
for _ in range(tcs):
    M, C = map(int, input().split(" "))
    g = defaultdict(list)
    s, total = [], 0

    dsu = DSU(C)
    for _ in range((C * (C - 1)) // 2):
        c1, c2, d = map(int, input().split(" "))
        s.append((d, c1, c2))

    s.sort(reverse = True)
    while s:
        d, c1, c2 = s.pop()
        if not dsu.union(c1, c2):
            continue
        total += d
        C -= 1
        if C == 0: # finish early
            break
    print("yes" if M >= total else "no")


#programa que calcula descontos de acordo com os tipos de pagamentos
print('>='*10,'CASAS FELPS','>='*10)
preco = float(input('Qual é o preço do seu produto? R$'))
print('As opções de pagamento são: ')
print('''(1) - À VISTA NO DINHEIRO OU CHEQUE
(2)- À VISTA NO CARTÃO
(3)- EM ATÉ 2X NO CARTÃO
(4)- 3X OU MAIS NO CARTÃO''')
opcao = int(input('Digite sua opção: '))
if opcao == 1:
    print(f'10% de desconto. Preço final do produto = R${preco*0.9}')
elif opcao == 2:
    print(f'5% de desconto. Preço final do produto = R${preco*0.95}')
elif opcao == 3:
    parcela = preco / 2
    print(f'Nenhum desconto. Preço das parcelas = R${parcela}')
elif opcao == 4:
    total = preco + (preco * 20 /100)
    totalparc = int(input('Quantas parcelas? '))
    parcela = total / totalparc
    print(f'20% de juros, serão {totalparc} parcelas no valor de R${parcela}')
    print(f'O preço final do produto vai ser de R${total}')
else:
    print('Opção inválida, tente novamente!')


"""Configuration file for the Sphinx documentation builder."""

import pkg_resources

from sphinx.ext import apidoc

apidoc.main(["-eTf", "-t", "../templates", "-o", ".", "../../src"])  # gen API docs

project = "seddy"
copyright = "2020, Laurie O"
author = "Laurie O"

release = pkg_resources.get_distribution("seddy").version  # full version
version = ".".join(release.split(".")[:2])  # short version

extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.viewcode",
    "sphinx.ext.napoleon",
    "autodocsumm",
]
html_theme = "sphinx_rtd_theme"
master_doc = "index"  # support read-the-docs


import sys
import pytest
import kevlar
from kevlar.tests import data_file
from kevlar.vcf import Variant, FormattedList
from kevlar.vcf import VariantFilter as vf

@pytest.fixture
def yrb_writer():
    writer = kevlar.vcf.VCFWriter(sys.stdout, source='py.test')
    writer.register_sample('NA19238')
    writer.register_sample('NA19239')
    writer.register_sample('NA19240')
    writer.describe_format('GT', 'String', '1', 'Genotype')
    return writer

def test_snv_obj():
    snv = Variant('scaffold42', 10773, 'A', 'G')
    assert str(snv) == 'scaffold42:10773:A->G'
    vcfvalues = ['scaffold42', '10774', '.', 'A', 'G', '.', 'PASS', '.']
    assert snv.vcf == '\t'.join(vcfvalues)
    assert snv.cigar is None

    snv2 = Variant('chr5', 500, 'T', 'G', CIGAR='10D200M10D')
    assert snv2.cigar == '10D200M10D'
    assert snv2.window is None

def test_indel_obj():
    """
    Test indel objects

    The coordinate used to construct the object is 0-based, but includes the
    nucleotide shared by the reference and alternate alleles. The str() output
    coordinate is increased by 1 to account for this nucleotide, while the VCF
    output is increased by 1 to transform to a 1-based system where the shared
    nucleotide is the point of reference.
    """
    indel1 = Variant('chr3', 8998622, 'GATTACA', 'G')
    assert str(indel1) == 'chr3:8998623:6D'
    vcfvalues = ['chr3', '8998623', '.', 'GATTACA', 'G', '.', 'PASS', '.']
    assert indel1.vcf == '\t'.join(vcfvalues)

    indel2 = Variant('chr6', 75522411, 'G', 'GATTACA')
    assert str(indel2) == 'chr6:75522412:I->ATTACA'
    vcfvalues = ['chr6', '75522412', '.', 'G', 'GATTACA', '.', 'PASS', '.']
    assert indel2.vcf == '\t'.join(vcfvalues)

def test_filter_field():
    v = Variant('scaffold1', 12345, '.', '.')
    assert v.filterstr == '.'
    v.filter(vf.InscrutableCigar)
    assert v.filterstr == 'InscrutableCigar'

    v = Variant('chr1', 55555, '.', '.')
    v.filter(vf.PerfectMatch)
    assert v.filterstr == 'PerfectMatch'

    v = Variant('1', 809768, 'C', 'CAT')
    assert v.filterstr == 'PASS'
    v.filter(vf.PassengerVariant)
    assert v.filterstr == 'PassengerVariant'
    v.filter(vf.Homopolymer)
    assert v.filterstr == 'Homopolymer;PassengerVariant'

    v = Variant('one', 112358, 'T', 'A')
    v.filter('SNPyMcSNPface')
    v.filter(6.022e23)
    v.filter(dict(chicken='waffles', biscuits='gravy'))
    v.filterstr == 'PASS'  # These "filters" shouldn't actually do anything

def test_info():
    """Test handling of "info" field attributes.

    This tests the mechanics of the .annotate() and .attribute() API, and the
    FormattedList class underpinning it.
    """
    values = FormattedList()
    assert str(values) == '.'
    values.append(42)
    assert str(values) == '42'
    values.append(1776)
    assert str(values) == '42,1776'
    values.append('B0gU$')
    with pytest.raises(kevlar.vcf.KevlarMixedDataTypeError):
        str(values)

    v = Variant('1', 12345, 'G', 'C')
    assert v.attribute('VW') is None

    v.annotate('VW', 'AGTNNNNNNNNNNNNNNNNNNNNNTGA')
    assert v.attribute('VW') == 'AGTNNNNNNNNNNNNNNNNNNNNNTGA'

    v.annotate('VW', 'GATTACA')
    assert v.attribute('VW') == 'GATTACA'
    assert v.attribute('VW', pair=True) == 'VW=GATTACA'

    v.annotate('VW', 'ATGCCCTAG', replace=False)
    assert v.info['VW'] == ['GATTACA', 'ATGCCCTAG']
    assert v.attribute('VW') == ['GATTACA', 'ATGCCCTAG']
    assert v.attribute('VW', string=True) == 'GATTACA,ATGCCCTAG'
    assert v.attribute('VW', pair=True) == 'VW=GATTACA,ATGCCCTAG'

    v.annotate('VW', 'AAAAAAAAA', replace=False)
    assert v.attribute('VW') == ['GATTACA', 'ATGCCCTAG', 'AAAAAAAAA']
    assert v.attribute('VW', pair=True) == 'VW=GATTACA,ATGCCCTAG,AAAAAAAAA'

    v.annotate('DROPPED', 3)
    assert v.attribute('DROPPED') == 3
    assert v.attribute('DROPPED', string=True) == '3'

    v.annotate('DROPPED', 31, replace=False)
    assert v.attribute('DROPPED') == [3, 31]
    assert v.attribute('DROPPED', string=True) == '3,31'
    assert v.attribute('DROPPED', pair=True) == 'DROPPED=3,31'

    v.annotate('MATEDIST', 432.1234, replace=False)
    v.annotate('MATEDIST', 8765.4321, replace=False)
    assert v.attribute('MATEDIST', string=True) == '432.123,8765.432'

    v.annotate('LLIH', -436.0111857750478)
    assert v.attribute('LLIH', pair=True) == 'LLIH=-436.011'

def test_format():
    v = Variant('1', 12345, 'G', 'C')
    v.format('NA19238', 'GT', '0/0')
    assert v.format('NA19238', 'GT') == '0/0'
    assert v.format('NA19238', 'XYZ') is None
    assert v.format('NA19239', 'GT') is None

def test_writer(yrb_writer, capsys):
    yrb_writer = kevlar.vcf.VCFWriter(sys.stdout, source='py.test')
    yrb_writer.register_sample('NA19238')
    yrb_writer.register_sample('NA19239')
    yrb_writer.register_sample('NA19240')
    yrb_writer.describe_format('GT', 'String', '1', 'Genotype')
    yrb_writer.write_header()

    v = Variant('1', 12345, 'G', 'C')
    v.annotate('PART', '42')
    v.annotate('CONTIG', 'A' * 100)
    v.format('NA19238', 'GT', '0/0')
    v.format('NA19239', 'GT', '0/0')
    v.format('NA19240', 'GT', '0/1')
    v.format('NA19238', 'ALTABUND', '12,9,8')
    v.format('NA19239', 'ALTABUND', '0,0,0')
    v.format('NA19240', 'ALTABUND', '0,0,0')
    yrb_writer.write(v)

    out, err = capsys.readouterr()
    print(out)

    outlines = out.strip().split('\n')
    fmtlines = [ln for ln in outlines if ln.startswith('##FORMAT')]
    assert len(fmtlines) == 2
    gtfmt = '##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">'
    assert gtfmt in fmtlines

    varlines = [ln for ln in outlines if not ln.startswith('#')]
    assert len(varlines) == 1
    values = varlines[0].split('\t')
    assert len(values) == 12
    assert values[8:12] == [
        'ALTABUND:GT', '12,9,8:0/0', '0,0,0:0/0', '0,0,0:0/1'
    ]

def test_writer_bad_fmt(yrb_writer):
    v = Variant('1', 12345, 'G', 'C')
    v.annotate('PART', '42')
    v.annotate('CONTIG', 'A' * 100)
    v.format('NA19238', 'GT', '0/0')
    v.format('NA19240', 'GT', '0/1')
    v.format('NA19239', 'ALTABUND', '0,0,0')
    v.format('NA19240', 'ALTABUND', '0,0,0')
    errormsg = r'samples not annotated with the same FORMAT fields'
    with pytest.raises(kevlar.vcf.VariantAnnotationError, match=errormsg):
        yrb_writer.write(v)

def test_reader():
    instream = kevlar.open(data_file('five-snvs-with-likelihood.vcf'), 'r')
    reader = kevlar.vcf.VCFReader(instream)
    calls = list(reader)
    print(calls)
    assert len(calls) == 5
    assert calls[1].attribute('PART') == '54'
    assert calls[3].format('Kid', 'ALTABUND') == (
        '21,20,20,19,17,19,20,19,18,17,17,17,17,17,17,17,18,19,19,19,18,18,18,'
        '17,19,18,17,17,17,15,15'
    )

@pytest.mark.parametrize('filename,errormsg', [
    ('five-snvs-fmt-mismatch.vcf', r'sample number mismatch'),
    ('five-snvs-fmtstr-mismatch.vcf', r'format data mismatch'),
])
def test_reader_format_mismatch(filename, errormsg):
    instream = kevlar.open(data_file(filename), 'r')
    reader = kevlar.vcf.VCFReader(instream)
    with pytest.raises(kevlar.vcf.VariantAnnotationError, match=errormsg):
        calls = list(reader)

def test_vcf_roundtrip(capsys):
    instream = kevlar.open(data_file('five-snvs-with-likelihood.vcf'), 'r')
    reader = kevlar.vcf.VCFReader(instream)

    writer = kevlar.vcf.VCFWriter(
        sys.stdout, source=None,
        refr='GCA_000001405.15_GRCh38_no_alt_analysis_set.fna.gz'
    )
    writer.register_sample('Kid')
    writer.register_sample('Mom')
    writer.register_sample('Dad')
    writer.describe_format('GT', 'String', '1', 'Genotype')
    writer.write_header(skipdate=True)
    calls = list()
    for call in reader:
        calls.append(call)
        writer.write(call)

    out, err = capsys.readouterr()
    outlines = out.strip().split('\n')
    reader2 = kevlar.vcf.VCFReader(outlines)
    calls2 = list(reader2)
    assert len(calls) == len(calls2)
    assert [c.position for c in calls] == [c.position for c in calls2]
    assert [str(c) for c in calls] == [str(c) for c in calls2]
    assert [c.window for c in calls] == [c.window for c in calls2]

def test_region():
    variant = Variant('chr12', 1033773, 'A', 'G')
    assert variant.region == ('chr12', 1033773, 1033774)
    variant = Variant('chr12', 1033773, 'A', 'AGTG')
    assert variant.region == ('chr12', 1033773, 1033774)
    variant = Variant('chr12', 1033773, 'AT', 'TG')
    assert variant.region == ('chr12', 1033773, 1033775)
    variant = Variant('chr12', 1033773, 'ATACCG', 'A')
    assert variant.region == ('chr12', 1033773, 1033779)


#!/usr/bin/python

# x_CRtxt_langdectect.py
# Detect the language for a given directory of text files.
# 2017-09-12
# 2017-11-25 newer file name convensions

from langdetect import detect
from langdetect.lang_detect_exception import LangDetectException
import codecs
from os import listdir, mkdir
from os.path import isfile, join, exists

parentPathIn = "U:/Ser-Huang_Poon/UK_CRunzip_txt/"
logPath = "x_CRtxt_langdectect.csv"
logFile = open(logPath, 'w')
SEP = ','
# Header
logFile.write('filename'+SEP+'lang\n')

# Get all files
files = [f for f in listdir(parentPathIn) if isfile(join(parentPathIn, f))]
files.sort()
print ('Files', len(files))
for filePath in files:
    fullPath = join(parentPathIn, filePath)
    data = ""
    with codecs.open(fullPath, 'r', encoding='utf8', errors='ignore') as file:
        data = file.read()
    if filePath.endswith('.pdf'):
        # Don't read PDF
        data = ""
    elif filePath.endswith('.htm'):
        # Don't read HTML
        data = ""
    try:
        lang = detect(data)
    except LangDetectException:
        lang = '-1'
    print(filePath, lang)
    logFile.write(filePath + SEP + lang + '\n')
logFile.close()


import attr
from cached_property import cached_property
from datetime import datetime, timedelta
from functools import reduce
import pandas as pd
import requests

from navigader_core.load.dataframe import resample_dataframe
from navigader_core.der.builder import (
    DER,
    DERProduct,
    DERSimulationBuilder,
    DERStrategy,
)
from navigader_core.load.intervalframe import PowerIntervalFrame

PVWATTS_URL = "https://developer.nrel.gov/api/pvwatts/v6.json"

@attr.s(frozen=True)
class SolarPV(DER):
    """
    A SolarPV models the physical characteristics of Solar Photovoltaics. These
    fields are based on the PVWatts API docs located at:
        - https://developer.nrel.gov/docs/solar/pvwatts/v6/

    The SolarPV configuration consists of a subset of available PVWatts
    parameters. See API docs for parameter descriptions and acceptable values.
    """

    # PVWatts API parameters
    address = attr.ib(type=str)
    array_type = attr.ib(type=int)
    azimuth = attr.ib(type=float)
    tilt = attr.ib(type=float)
    api_key = attr.ib(type=str, default="")
    module_type = attr.ib(type=int, default=0)
    timeframe = attr.ib(type=str, default="hourly")
    # non-parameter: pass stored response to avoid API call
    stored_response = attr.ib(type=dict, default={}, repr=False)

    @array_type.validator
    def _validate_array_type(self, attribute, value):
        """
        Validate array_type is 0, 1, 2, 3, or 4.
        """
        if value not in [0, 1, 2, 3, 4]:
            self.raise_validation_error(attribute, "Must be 0, 1, 2, 3, or 4")

    @azimuth.validator
    def _validate_azimuth(self, attribute, value):
        """
        Validate azimuth is between 0 and 360.
        """
        if not (0 <= value < 360):
            self.raise_validation_error(attribute, "Must be between 0 and 360")

    @module_type.validator
    def _validate_module_type(self, attribute, value):
        """
        Validate module_type is 0, 1, or 2.
        """
        if value not in [0, 1, 2]:
            self.raise_validation_error(attribute, "Must be 0, 1 or 2")

    @tilt.validator
    def _validate_tilt(self, attribute, value):
        """
        Validate tilt is between 0 and 90.
        """
        if not (0 <= value <= 90):
            self.raise_validation_error(attribute, "Must be between 0 and 90")

    @property
    def request_params(self) -> dict:
        """
        API request params generated from attributes.
        """
        request_params = attr.asdict(self)
        request_params.pop("stored_response")

        return request_params

    @cached_property
    def pvwatts_response(self) -> dict:
        """
        PVWatts API response based on input parameters.
        """
        if self.stored_response:
            return self.stored_response

        params = {"losses": 14.08, "system_capacity": 1}
        params.update(self.request_params)
        return requests.get(PVWATTS_URL, params=params, timeout=7).json()

    def get_annual_solar_intervalframe(
        self, year: int, target_period: timedelta = timedelta(hours=1)
    ) -> PowerIntervalFrame:
        """
        Get solar production intervals provided by PVWatts with timestamps from
        provided year.
        """
        datetime_index = pd.date_range(
            "{}-01-01".format(year), periods=8760, freq="H"
        )
        solar_readings = self.pvwatts_response["outputs"]["ac"]
        dataframe = pd.DataFrame(zip(datetime_index, solar_readings)).set_index(
            0
        )
        # convert W to kW and reverse polarity of readings
        dataframe = dataframe / -1000
        dataframe = dataframe.rename(columns={1: "kw"})
        # rename index
        dataframe.index.rename("index", inplace=True)

        return PowerIntervalFrame(
            dataframe=resample_dataframe(
                dataframe=dataframe, target_period=target_period
            )
        )

    def get_solar_intervalframe(
        self,
        start: datetime,
        end_limit: datetime,
        target_period: timedelta = timedelta(hours=1),
    ) -> PowerIntervalFrame:
        """
        Get solar production intervals provided by PVWatts with timestamps from
        provided date range.
        """
        # DataFrame with all years from start.year to end_limit.year
        dataframe = reduce(
            lambda x, y: x.append(y),
            [
                self.get_annual_solar_intervalframe(
                    year=year, target_period=target_period
                ).dataframe
                for year in range(start.year, end_limit.year + 1)
            ],
            pd.DataFrame(),
        )

        return PowerIntervalFrame(dataframe=dataframe).filter_by_datetime(
            start=start, end_limit=end_limit
        )

    def get_system_capacity(self, intervalframe: PowerIntervalFrame) -> float:
        """
        Get system capacity by dividing intervalframe yield by solar
        intervalframe yield over same timeframe.
        """
        solar_yield = (
            self.get_solar_intervalframe(
                start=intervalframe.start_datetime,
                end_limit=intervalframe.end_limit_datetime,
            )
            .total_frame288.dataframe.sum()
            .sum()
        )

        return intervalframe.total / solar_yield

@attr.s(frozen=True)
class SolarPVStrategy(DERStrategy):
    """
    A SolarPVStrategy models the behavioral characteristics of Solar
    Photovoltaics. These fields are based on the PVWatts model located at:
        - https://developer.nrel.gov/docs/solar/pvwatts/v6/

    The SolarPVStrategy consists of a value for serviceable_load_ratio that
    will determine the desired system size and resize a solar
    PowerIntervalFrame accordingly.
    """

    serviceable_load_ratio = attr.ib(type=float)

    @serviceable_load_ratio.validator
    def _validate_serviceable_load_ratio(self, attribute, value):
        """
        Validate serviceable_load_ratio is between 0 and 1.
        """
        if value <= 0:
            self.raise_validation_error(attribute, "Must be greater than zero")

    @staticmethod
    def get_annual_load(intervalframe: PowerIntervalFrame) -> float:
        """
        Calculate annual load of a intervalframe.
        """
        # calculate average daily load
        days = (
            intervalframe.end_limit_datetime - intervalframe.start_datetime
        ).days

        return (intervalframe.total / days) * 365

    def get_target_system_size_ratio(
        self, annual_load: float, solar_yield: float
    ) -> float:
        """
        Get target system size ratio used for resizing SolarPV.
        """
        target_offset = annual_load * self.serviceable_load_ratio
        system_ratio = target_offset / abs(solar_yield)

        return max(system_ratio, 0)  # ignore net exporters

    def resize_solar_intervalframe(
        self,
        intervalframe: PowerIntervalFrame,
        solar_intervalframe: PowerIntervalFrame,
    ) -> PowerIntervalFrame:
        """
        Resize solar production intervals based on a meter's total annual load,
        the percent of load to service, and the solar yield of a SolarPV.
        """
        annual_load = self.get_annual_load(intervalframe)
        solar_yield = self.get_annual_load(solar_intervalframe)
        target_system_size_ratio = self.get_target_system_size_ratio(
            annual_load=annual_load, solar_yield=solar_yield
        )

        return PowerIntervalFrame(
            dataframe=solar_intervalframe.dataframe * target_system_size_ratio
        )

@attr.s(frozen=True)
class SolarPVSimulationBuilder(DERSimulationBuilder):
    """
    Generates DERProducts a.k.a. SolarPV Simulations.
    """

    der = attr.ib(type=SolarPV)
    der_strategy = attr.ib(type=SolarPVStrategy)

    def run_simulation(self, intervalframe: PowerIntervalFrame) -> DERProduct:
        solar_intervalframe = self.der.get_solar_intervalframe(
            start=intervalframe.start_datetime,
            end_limit=intervalframe.end_limit_datetime,
            target_period=intervalframe.period,
        )
        solar_intervalframe = self.der_strategy.resize_solar_intervalframe(
            intervalframe=intervalframe,
            solar_intervalframe=solar_intervalframe,
        )

        return DERProduct(
            der=self.der,
            der_strategy=self.der_strategy,
            pre_der_intervalframe=intervalframe,
            der_intervalframe=solar_intervalframe,
            post_der_intervalframe=(intervalframe + solar_intervalframe),
        )


import logging

from google.appengine.api import datastore_errors
from google.appengine.ext import ndb
from google.appengine.runtime import apiproxy_errors

class _GroupRoot(ndb.Model):
  """Root entity of a group to support versioned children."""
  # Key id of the most recent child entity in the datastore. It is monotonically
  # increasing and is 0 if no child is present.
  current = ndb.IntegerProperty(indexed=False, default=0)

class VersionedModel(ndb.Model):
  """A model that supports versioning.

  Subclass will automatically be versioned, if use GetVersion() to
  read and use Save() to write.
  """

  @property
  def version(self):
    return self.key.integer_id() if self.key else 0

  @classmethod
  def GetVersion(cls, version=None):
    """Returns a version of the entity, the latest if version=None."""
    assert not ndb.in_transaction()

    root_key = cls._GetRootKey()
    root = root_key.get()
    if not root or not root.current:
      return None

    if version is None:
      version = root.current
    elif version < 1:
      #  Return None for versions < 1, which causes exceptions in ndb.Key()
      return None

    return ndb.Key(cls, version, parent=root_key).get()

  @classmethod
  def GetLatestVersionNumber(cls):
    root_entity = cls._GetRootKey().get()
    if not root_entity:
      return -1
    return root_entity.current

  def Save(self):
    """Saves the current entity, but as a new version."""
    root_key = self._GetRootKey()
    root = root_key.get() or self._GetRootModel()(key=root_key)

    def SaveData():
      if self.key.get():
        return False  # The entity exists, should retry.
      ndb.put_multi([self, root])
      return True

    def SetNewKey():
      root.current += 1
      self.key = ndb.Key(self.__class__, root.current, parent=root_key)

    SetNewKey()
    while True:
      while self.key.get():
        SetNewKey()

      try:
        if ndb.transaction(SaveData, retries=0):
          return self.key
      except (
          datastore_errors.InternalError,
          datastore_errors.Timeout,
          datastore_errors.TransactionFailedError) as e:
        # https://cloud.google.com/appengine/docs/python/datastore/transactions
        # states the result is ambiguous, it could have succeeded.
        logging.info('Transaction likely failed: %s', e)
      except (
          apiproxy_errors.CancelledError,
          datastore_errors.BadRequestError,
          RuntimeError) as e:
        logging.info('Transaction failure: %s', e)
      else:
        SetNewKey()

  @classmethod
  def _GetRootModel(cls):
    """Returns a root model that can be used for versioned entities."""
    root_model_name = '%sRoot' % cls.__name__

    class _RootModel(_GroupRoot):

      @classmethod
      def _get_kind(cls):
        return root_model_name

    return _RootModel

  @classmethod
  def _GetRootKey(cls):
    return ndb.Key(cls._GetRootModel(), 1)


from FileFinder.exception_handling_task import FindFile

file = FindFile()
print(file.create_file())
while True:
    print(file.read_file())
    if input("Would you like to add another item? (y/n) ").lower() == 'n':
        break
    item = input("What would you like to add? ")
    file.write_file(item)

print('The Shopping list has been closed.')


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

# # Importing Libraries

# Importing libraries is an essential step in any data analysis or machine learning project. These libraries provide various functions and tools to manipulate, visualize, and analyze data efficiently. Here are explanations of some popular data analysis libraries

# Pandas: Pandas is a powerful and widely used library for data manipulation and analysis. It provides data structures like DataFrames and Series, which allow you to store and manipulate tabular data. Pandas offers a wide range of functions for data cleaning, filtering, aggregation, merging, and more. It also supports reading and writing data from various file formats such as CSV, Excel, SQL databases, and more.

# NumPy: NumPy (Numerical Python) is a fundamental library for scientific computing in Python. It provides efficient data structures like arrays and matrices and a vast collection of mathematical functions. NumPy enables you to perform various numerical operations on large datasets, such as element-wise calculations, linear algebra, Fourier transforms, and random number generation. It also integrates well with other libraries for data analysis and machine learning.

# Matplotlib: Matplotlib is a popular plotting library that enables you to create a wide range of static, animated, and interactive visualizations. It provides a MATLAB-like interface and supports various types of plots, including line plots, scatter plots, bar plots, histograms, and more. Matplotlib gives you extensive control over plot customization, including labels, colors, legends, and annotations, allowing you to effectively communicate insights from your data

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')

# # Importing Dataset From keras.datasets

# In[2]:

from tensorflow.keras.datasets import mnist

# # Splitting Dataset

# Splitting the data refers to dividing the dataset into separate subsets for training, validation, and testing purposes. This division is essential to assess the performance of a machine learning model on unseen data and prevent overfitting. Here are the common types of data splits:

# Train-Test Split: This is the most basic type of split, where the dataset is divided into a training set and a testing set. The training set is used to train the machine learning model, while the testing set is used to evaluate its performance. The split is typically done using a fixed ratio, such as 80% for training and 20% for testing.

# In[3]:

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# In[4]:

x_train.shape, x_test.shape, y_train.shape, y_test.shape

# In[5]:

plt.imshow(x_train[4])

# # Converting To Categorical

# Converting data to categorical format is often necessary for certain machine learning tasks, especially when dealing with categorical or nominal variables. This conversion is typically performed to represent the categorical variables as numeric values that can be processed by machine learning algorithms.

# In[6]:

from tensorflow.keras.utils import to_categorical

# In[7]:

y_train_categorical = to_categorical(y_train)
y_test_categorical = to_categorical(y_test)

# In[8]:

x_train.max()

# In[9]:

x_train = x_train / 255
x_test = x_test / 255

# In[10]:

x_train.shape

# In[11]:

x_test.shape

# # Reshaping Images

# Reshaping data is a common operation in machine learning when you need to adjust the dimensions or structure of your data to meet the requirements of a particular algorithm or model.

# In[12]:

x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)

# In[13]:

x_train.shape, x_test.shape

# In[14]:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D

# In[27]:

from sklearn.metrics import classification_report, confusion_matrix

# In[15]:

from tensorflow.keras.callbacks import EarlyStopping

# # Creating Model

# Creating a deep learning model involves defining the architecture and structure of the neural network, specifying the layers, and configuring the parameters for training.

# In[16]:

model = Sequential()
model.add(Conv2D(filters = 32, kernel_size = (4, 4), input_shape = (28, 28, 1), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))

# In[17]:

model.summary()

# # Compiling Model

# Compiling the model in deep learning involves configuring essential components that define how the model will be trained.

# In[18]:

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

# In[19]:

early_stopping = EarlyStopping(monitor = 'val_loss', patience = 3)

# # Training Model

# Training the model in deep learning involves the process of iteratively updating the model's parameters (weights and biases) based on the provided training data to minimize the loss function and improve the model's performance

# In[20]:

model.fit(x_train,
          y_train_categorical,
          validation_data = (x_test, y_test_categorical),
          epochs = 20,
          callbacks = [early_stopping])

# In[21]:

model.save('CNN_model_for_MNIST.h5')

# In[22]:

loss = pd.DataFrame(model.history.history)

# In[23]:

loss.head()

# In[24]:

loss.plot()

# In[25]:

loss[['loss', 'val_loss']].plot()

# In[26]:

loss[['accuracy', 'val_accuracy']].plot()

# In[29]:

predictions = model.predict(x_test)
predicted_classes = predictions.argmax(axis=1)

# # Error Analysis

# Error analysis is an important step in evaluating and improving the performance of a machine learning model. It involves analyzing the errors made by the model during predictions to gain insights into the patterns, sources, and potential improvements.

# In[31]:

print(classification_report(y_test,predicted_classes))

# In[32]:

confusion_matrix(y_test,predicted_classes)

# In[33]:

plt.figure(figsize=(12, 5))
sns.heatmap(confusion_matrix(y_test,predicted_classes),annot=True)

# # Predicting

# In[34]:

test_prediction = x_test[5]

# In[35]:

plt.imshow(test_prediction.reshape(28,28))

# In[38]:

predictions = model.predict(test_prediction.reshape(1, 28, 28, 1))
predicted_class = np.argmax(predictions)

print("Predicted class:", predicted_class)

CNN on MNIST Dataset/CNN on MNIST Dataset.py


# Spanning Tree란 그래프 내의 모든 정점을 포함하는 트리로써 그래프의 최소 연결 부분 그래프이다.
# n 개의 정점을 가지는 그래프의 최소 연결 간선의 개수는 n - 1 개이다.
# 그 중 Minimum Spanning Tree란 그래프의 간선이 가중치를 가지고 있을 때,
# Spanning Tree 중 그 가중치의 합이 가장 작은 트리를 의미한다.

##### 문제 #####
# 그래프가 주어졌을 때, 그 그래프의 최소 스패닝 트리를 구하는 프로그램을 작성하시오.
# 최소 스패닝 트리는, 주어진 그래프의 모든 정점들을 연결하는 부분 그래프 중에서 그 가중치의 합이 최소인 트리를 말한다.

##### 입력 #####
# 첫째 줄에 정점의 개수 V(1 ≤ V ≤ 10,000)와 간선의 개수 E(1 ≤ E ≤ 100,000)가 주어진다.
# 다음 E개의 줄에는 각 간선에 대한 정보를 나타내는 세 정수 A, B, C가 주어진다.
# 이는 A번 정점과 B번 정점이 가중치 C인 간선으로 연결되어 있다는 의미이다.
# C는 음수일 수도 있으며, 절댓값이 1,000,000을 넘지 않는다.

##### 출력 #####
# 첫째 줄에 최소 스패닝 트리의 가중치를 출력한다.

# vertex가 속하는 group을 찾아주는 함수
def find_group(vertex):
    # vertex가 속하는 그룹이 초기 값과 다르다면 해당 그룹을 찾아서 리턴한다.
    if group[vertex] != vertex:
        group[vertex] = find_group(group[vertex])
    return group[vertex]

# vertex의 group을 찾아서 그 값을 비교하여 하나로 합치는 함수
def union_group(vertex_1, vertex_2):
    vertex_1 = find_group(vertex_1)
    vertex_2 = find_group(vertex_2)
    # 부등호의 방향은 사실 의미없다.
    # 서로 다른 group이란 것이 확인이 되면 어떤 vertex를 기준으로 하던
    # 하나의 group으로 만들어주는 것이 중요하다.
    if vertex_1 < vertex_2:
        group[vertex_2] = vertex_1
    else:
        group[vertex_1] = vertex_2

vertex, edge = map(int, input().split())

# vertex 수 만큼 group 리스트를 만든다.
# 초기에는 각각의 vertex들이 서로 다른 그룹(1, 2, 3)에 속한다.
group = [i for i in range(vertex + 1)]
print(group)

edges = []
result = 0

# edge 별로 연결하는 vertex와 weight를 리스트로 만든다.
# 이때, weight를 0번째 인덱스 자리로 순서를 바꾸어 입력하였는데
# 이는 weight 순으로 정렬을 편하게 하기 위함이니 원래대로 입력하고 정렬을 다르게 구현해도 된다.
for _ in range(edge):
    vertex_1, vertex_2, weight = map(int, input().split())
    edges.append((weight, vertex_1, vertex_2))

# weight를 오름차순으로 정렬한다.
edges.sort()
print(edges)

# edge들을 담은 리스트에서 하나씩 edge를 꺼낸다.
for edge in edges:
    weight, vertex_1, vertex_2 = edge
    print(vertex_1, vertex_2)

    # vertex의 그룹이 다를 경우, 두 vertex를 연결하여 하나의 group으로 만들어준다.
    if find_group(vertex_1) != find_group(vertex_2):
        union_group(vertex_1, vertex_2)

        # vertex를 연결하였다면, 해당 weight를 result에 더해준다.
        result += weight

print(result)


__author__ = 'shijianliu'

version = 1.0

try:
    import multiprocessing
except ImportError:
    pass

import os
import os.path
from setuptools import setup

os.chdir(os.path.dirname(os.path.abspath(__file__)))

dependencies = [
    'web.py',
    'requests',
    'ujson',
    'uwsgi'
]

setup(name='hacker',
      version=version,
      description='Netease music hacker',
      author='liverliu',
      packages=['hacker'],
      scripts=["hacker/hacker.py"],
      install_requires=dependencies
)


import sass
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import Qt, QDate
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QSplashScreen, QDesktopWidget
from ui.categorias_add import Ui_Form as CategoriasAdd
from ui.categorias_edit import Ui_Form as CategoriasEdit
from ui.dialog import Ui_Dialog as Mensagem
from ui.entrada_add import Ui_Form as EntradaAdd
from ui.fixo_add import Ui_Form as FixoAdd
from ui.gastos_add import Ui_Form as GastosAdd
from ui.gastos_edit import Ui_Form as GastosEdit
from ui.janela import Ui_Form as Main
from ui.reserva_add import Ui_Form as ReservaAdd
from ui.reserva_edit import Ui_Form as ReservaEdit
from ui.subcategorias_add import Ui_Form as SubCategoriasAdd
from ui.subcategorias_edit import Ui_Form as SubCategoriasEdit
from ui.ajuste import Ui_Form as Ajuste
from ui.investimento import Ui_Form as Investimento
from ui.recorrentes import Ui_Form as Recorrente
from ui.gerador import Ui_Form as Gerador
from ui.relatorio import Ui_Form as Relatorio
from ui.dividir import Ui_Form as Dividir
from ui.ajustar import Ui_Form as Ajustar

class gui:

    def __init__(self):
        # declarações da interface gráfica
        print("Inicia a interface gráfica")
        self.app = QApplication(sys.argv)

        # janela principal
        print("Janela principal")
        self.wMain = QDialog()
        self.ui = Main()
        self.ui.setupUi(self.wMain)

        self.wMain.setWindowIcon(QtGui.QIcon("resources/dinheiro.png"))

        self.ui.treeCategorias.setColumnWidth(0, 250)

        self.ui.treeFixo.setColumnWidth(0, 250)

        print("Janelas secundárias")
        # janela para adicionar novas entradas
        self.wEntradaAdd = QDialog()
        self.uiEntradaAdd = EntradaAdd()
        self.uiEntradaAdd.setupUi(self.wEntradaAdd)

        self.wInvestimento = QDialog()
        self.uiInvestimento = Investimento()
        self.uiInvestimento.setupUi(self.wInvestimento)

        self.wInvestimentoEdit = QDialog()
        self.uiInvestimentoEdit = Investimento()
        self.uiInvestimentoEdit.setupUi(self.wInvestimentoEdit)

        self.wRecorrente = QDialog()
        self.uiRecorrente = Recorrente()
        self.uiRecorrente.setupUi(self.wRecorrente)

        self.wRecorrenteEdit = QDialog()
        self.uiRecorrenteEdit = Recorrente()
        self.uiRecorrenteEdit.setupUi(self.wRecorrenteEdit)

        # janela para relatório
        self.wRelatorio = QDialog()
        self.uiRelatorio = Relatorio()
        self.uiRelatorio.setupUi(self.wRelatorio)

        # janela para ajustes
        self.wAjuste = QDialog()
        self.uiAjuste = Ajuste()
        self.uiAjuste.setupUi(self.wAjuste)

        # janela para editar entradas
        self.wEntradaEdit = QDialog()
        self.uiEntradaEdit = EntradaAdd()
        self.uiEntradaEdit.setupUi(self.wEntradaEdit)

        # janela para adicionar novos gastos
        self.wGastosAdd = QDialog()
        self.uiGastosAdd = GastosAdd()
        self.uiGastosAdd.setupUi(self.wGastosAdd)

        # janela para editar gastos
        self.wGastosEdit = QDialog()
        self.uiGastosEdit = GastosEdit()
        self.uiGastosEdit.setupUi(self.wGastosEdit)

        # janela para converter para gastos
        self.wGastosConverte = QDialog()
        self.uiGastosConverte = GastosEdit()
        self.uiGastosConverte.setupUi(self.wGastosConverte)

        # janela para adicionar novos fixos
        self.wFixoAdd = QDialog()
        self.uiFixoAdd = FixoAdd()
        self.uiFixoAdd.setupUi(self.wFixoAdd)

        # janela para converter para fixo
        self.wFixoConverte = QDialog()
        self.uiFixoConverte = FixoAdd()
        self.uiFixoConverte.setupUi(self.wFixoConverte)

        # janela para editar fixos
        self.wFixoEdit = QDialog()
        self.uiFixoEdit = FixoAdd()
        self.uiFixoEdit.setupUi(self.wFixoEdit)

        # janela para adicionar novas reservas
        self.wReservaAdd = QDialog()
        self.uiReservaAdd = ReservaAdd()
        self.uiReservaAdd.setupUi(self.wReservaAdd)

        # janela para editar reservas
        self.wReservaEdit = QDialog()
        self.uiReservaEdit = ReservaEdit()
        self.uiReservaEdit.setupUi(self.wReservaEdit)

        # janela para adicionar novas categorias
        self.wCategoriasAdd = QDialog()
        self.uiCategoriasAdd = CategoriasAdd()
        self.uiCategoriasAdd.setupUi(self.wCategoriasAdd)

        # janela para editar categorias
        self.wCategoriasEdit = QDialog()
        self.uiCategoriasEdit = CategoriasEdit()
        self.uiCategoriasEdit.setupUi(self.wCategoriasEdit)

        # janela para adicionar novas sub-categorias
        self.altura = 250
        self.extra = 0
        self.fator = 10
        self.lista = ''

        self.wSubCategoriasAdd = QDialog()
        self.uiSubCategoriasAdd = SubCategoriasAdd()
        self.uiSubCategoriasAdd.setupUi(self.wSubCategoriasAdd)

        # janela para editar sub-categorias
        self.wSubCategoriasEdit = QDialog()
        self.uiSubCategoriasEdit = SubCategoriasEdit()
        self.uiSubCategoriasEdit.setupUi(self.wSubCategoriasEdit)

        # janela para Dividir
        self.wDividir = QDialog()
        self.uiDividir = Dividir()
        self.uiDividir.setupUi(self.wDividir)

        # janela para Ajustar
        self.wAjustar = QDialog()
        self.uiAjustar = Ajustar()
        self.uiAjustar.setupUi(self.wAjustar)

        # janela das mensagens
        self.wMensagem = QDialog()
        self.uiMensagem = Mensagem()
        self.uiMensagem.setupUi(self.wMensagem)

        # janela do gerador de novo mês
        self.wGerador = QDialog()
        self.uiGerador = Gerador()
        self.uiGerador.setupUi(self.wGerador)

        # seta a mesma configuração para todas as janelas
        print("Configura a folha de estilos")

        self.style = sass.compile(filename="style/style.scss")

        for janela in [
            self.wMain,
            self.wCategoriasAdd,
            self.wCategoriasEdit,
            self.wSubCategoriasAdd,
            self.wSubCategoriasEdit,
            self.wGastosAdd,
            self.wGastosEdit,
            self.wGastosConverte,
            self.wEntradaAdd,
            self.wReservaAdd,
            self.wFixoAdd,
            self.wFixoEdit,
            self.wFixoConverte,
            self.wEntradaEdit,
            self.wReservaEdit,
            self.wMensagem,
            self.wAjuste,
            self.wRecorrente,
            self.wInvestimento,
            self.wRecorrenteEdit,
            self.wInvestimentoEdit,
            self.wGerador,
            self.wRelatorio,
            self.wDividir,
            self.wAjustar
        ]:
            janela.setStyleSheet(self.style)
            janela.setWindowModality(Qt.ApplicationModal)

        # inicializa a janela
        print("Inicia a janela")
        self.wMain.show()

    def calendario_habilita(self, janela):
        pago = [
            janela.labelPago,
            janela.dateEdit_2,
            janela.botaoHoje_2
        ]
        if janela.checkPago.checkState():
            for widget in pago:
                widget.setEnabled(True)
            janela.dateEdit_2.setDate(
                janela.dateEdit.date()
            )
        else:
            for widget in pago:
                widget.setEnabled(False)
            janela.dateEdit_2.setDate(QDate.currentDate())


"""
Authors: Wouter Van Gansbeke, Simon Vandenhende
Licensed under the CC BY-NC 4.0 license (https://creativecommons.org/licenses/by-nc/4.0/)
"""
import argparse
import os
import torch

from termcolor import colored
from utils.config import create_config
from utils.common_config import get_train_transformations, get_val_transformations,\
                                get_train_dataset, get_train_dataloader,\
                                get_val_dataset, get_val_dataloader,\
                                get_optimizer, get_model, get_criterion,\
                                adjust_learning_rate
from utils.evaluate_utils import get_predictions, scan_evaluate, hungarian_evaluate
from utils.train_utils import scan_train

FLAGS = argparse.ArgumentParser(description='SCAN Loss')
FLAGS.add_argument('--config_env', help='Location of path config file')
FLAGS.add_argument('--config_exp', help='Location of experiments config file')

def main():
    args = FLAGS.parse_args()
    p = create_config(args.config_env, args.config_exp)
    print(colored(p, 'red'))

    # CUDNN
    torch.backends.cudnn.benchmark = True

    # Data
    print(colored('Get dataset and dataloaders', 'blue'))
    train_transformations = get_train_transformations(p)
    val_transformations = get_val_transformations(p)
    train_dataset = get_train_dataset(p, train_transformations,
                                        split='train', to_neighbors_dataset = True)
    val_dataset = get_val_dataset(p, val_transformations, to_neighbors_dataset = True)
    train_dataloader = get_train_dataloader(p, train_dataset)
    val_dataloader = get_val_dataloader(p, val_dataset)
    print('Train transforms:', train_transformations)
    print('Validation transforms:', val_transformations)
    print('Train samples %d - Val samples %d' %(len(train_dataset), len(val_dataset)))

    # Model
    print(colored('Get model', 'blue'))
    model = get_model(p, p['pretext_model'])
    print(model)
    model = torch.nn.DataParallel(model)
    model = model.cuda()

    # Optimizer
    print(colored('Get optimizer', 'blue'))
    optimizer = get_optimizer(p, model, p['update_cluster_head_only'])
    print(optimizer)

    # Warning
    if p['update_cluster_head_only']:
        print(colored('WARNING: SCAN will only update the cluster head', 'red'))

    # Loss function
    print(colored('Get loss', 'blue'))
    criterion = get_criterion(p)
    criterion.cuda()
    print(criterion)

    # Checkpoint
    if os.path.exists(p['scan_checkpoint']):
        print(colored('Restart from checkpoint {}'.format(p['scan_checkpoint']), 'blue'))
        checkpoint = torch.load(p['scan_checkpoint'], map_location='cpu')
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        start_epoch = checkpoint['epoch']
        best_loss = checkpoint['best_loss']
        best_loss_head = checkpoint['best_loss_head']

    else:
        print(colored('No checkpoint file at {}'.format(p['scan_checkpoint']), 'blue'))
        start_epoch = 0
        best_loss = 1e4
        best_loss_head = None

    # Main loop
    print(colored('Starting main loop', 'blue'))

    for epoch in range(start_epoch, p['epochs']):
        print(colored('Epoch %d/%d' %(epoch+1, p['epochs']), 'yellow'))
        print(colored('-'*15, 'yellow'))

        # Adjust lr
        lr = adjust_learning_rate(p, optimizer, epoch)
        print('Adjusted learning rate to {:.5f}'.format(lr))

        # Train
        print('Train ...')
        scan_train(train_dataloader, model, criterion, optimizer, epoch, p['update_cluster_head_only'])

        # Evaluate
        print('Make prediction on validation set ...')
        predictions = get_predictions(p, val_dataloader, model)

        print('Evaluate based on SCAN loss ...')
        scan_stats = scan_evaluate(predictions)
        print(scan_stats)
        lowest_loss_head = scan_stats['lowest_loss_head']
        lowest_loss = scan_stats['lowest_loss']

        if lowest_loss < best_loss:
            print('New lowest loss on validation set: %.4f -> %.4f' %(best_loss, lowest_loss))
            print('Lowest loss head is %d' %(lowest_loss_head))
            best_loss = lowest_loss
            best_loss_head = lowest_loss_head
            torch.save({'model': model.module.state_dict(), 'head': best_loss_head}, p['scan_model'])

        else:
            print('No new lowest loss on validation set: %.4f -> %.4f' %(best_loss, lowest_loss))
            print('Lowest loss head is %d' %(best_loss_head))

        print('Evaluate with hungarian matching algorithm ...')
        clustering_stats = hungarian_evaluate(lowest_loss_head, predictions, compute_confusion_matrix=False)
        print(clustering_stats)

        # Checkpoint
        print('Checkpoint ...')
        torch.save({'optimizer': optimizer.state_dict(), 'model': model.state_dict(),
                    'epoch': epoch + 1, 'best_loss': best_loss, 'best_loss_head': best_loss_head},
                     p['scan_checkpoint'])

    # Evaluate and save the final model
    print(colored('Evaluate best model based on SCAN metric at the end', 'blue'))
    model_checkpoint = torch.load(p['scan_model'], map_location='cpu')
    model.module.load_state_dict(model_checkpoint['model'])
    predictions = get_predictions(p, val_dataloader, model)
    clustering_stats = hungarian_evaluate(model_checkpoint['head'], predictions,
                            class_names=val_dataset.dataset.classes,
                            compute_confusion_matrix=True,
                            confusion_matrix_file=os.path.join(p['scan_dir'], 'confusion_matrix.png'))
    print(clustering_stats)

if __name__ == "__main__":
    main()


from __future__ import absolute_import
import logging
import os
from data import products
from argparse import ArgumentParser
from datetime import datetime

def build_parser():
    parser = ArgumentParser(prog="Driver for STIR library")
    parser.add_argument("-m","--mode", dest="mode",
                        help="Spreads or GSCI",
                        metavar="MODE", default="GSCI")
    parser.add_argument("-a", "--action", dest="action",
                        help="simulate, clear_summ_tables, plot, plot_by_product, populate_data",
                        metavar="ACTION", default="plot")
    parser.add_argument("-p", "--product", dest="product",
                        help="product for by-product actions",
                        metavar="PRODUCT", default=None)
    parser.add_argument("-l", "--plot_mode",dest="plot_mode",
                        help="SERIAL or COMBINED",
                        default="COMBINED")
    parser.add_argument("-f", "--filename", dest="filename",
                        help="filename for plotting",
                        metavar="FILENAME", default="./fig_plot.pdf")
    return parser

def main():
    parser = build_parser()
    options = parser.parse_args()

    if options.action == 'populate_data':
        import src
        src.populate_data()
    if options.mode == 'GSCI':
        if options.action == 'simulate':
            import src
            src.GSCI_backtest()
        elif options.action == 'clear_summ_tables':
            pass
        elif options.action == 'plot':
            import src
            if options.plot_mode == 'COMBINED':
                src.plot_GSCI_book_combined(filename=options.filename)
            else:
                src.plot_GSCI_book(filename=options.filename)
    else:
        if options.action == 'plot':
            import src
            src.plot_book()
        if options.action == 'plot_by_product':
            import src
            filename = options.filename or './figs/all_{}_spreads.pdf'.format(product)
            src.plot_book_by_product(options.product, filename=filename)
        if options.action == 'simulate_slice':
            import src
            src.seasonal_slice_backtest()
        if options.action == 'simulate_strategy':
            import src
            src.seasonal_strategy_backtest()

if __name__ == '__main__':
    main()


import json
import pandas as pd
import os

def remove_duplicates():
    print('Terminado! Verificando se existe linhas duplicadas...')
    lines_seen = set()
    with open('profiles.txt', "w") as output_file:
        for each_line in open('raw_profile.txt', "r"):
            if each_line not in lines_seen:  # check if line is not duplicate
                output_file.write(each_line)
                lines_seen.add(each_line)

    print('Terminado! link duplicados removidos')

def save_to_json(data, filename):
    print('Salvando os dados em JSON')
    with open(f'{filename}.json', 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False)

def load_links(file_text):
    print('Iniciando leitura de links...')
    with open(f'{file_text}.txt', 'r') as text:
        return text.readlines()

def JSONtoExcel(filename):
    try:
        print('Iniciando convensão...')
        # temp_df = pd.read_json(f'{filename}.json')
        temp_df = load_json(filename)
        df = pd.json_normalize(temp_df)
        df.to_excel(f'{filename}.xlsx')
        print('Convensão realizada com sucesso!')

    except Exception as error:
        print('Falha na conversão, Arquivo incorreto ou não existe...')

def load_json(json_name):
    with open(f'{json_name}.json','r', encoding="utf-8") as file:
        data = json.load(file)

    return data

def save_to_html(data, title="test.html"):
    with open(title, 'w', encoding='utf8') as file:
        file.write(str(data))

def dataToExcel(dataDict, filename, custom=False):
    df = pd.DataFrame(dataDict)
    if not custom:
        df.to_csv(filename, mode="a", index=False, header=not os.path.exists(filename))

    df.to_csv(filename)

def read_html(filename="test.html"):
    import codecs
    from utils.parser_handler import init_parser

    with codecs.open('test.html','r','utf-8') as file:
        src_code = file.read()

    soap = init_parser(src_code)
    return soap