import email
import hashlib
import logging
from email.message import Message
from pathlib import Path
from typing import Optional, Tuple

from logger import get_logger
from model import Attachment, MsgMeta, RawMsg, db, pw

log = get_logger(__name__)

def process_message(email_msg: Message, checksum: str, m_uid: str):
    """
    Process an entire message object.

    Split attachment files from rawmsg, create db entries for
    each rawmsg, message meta and attachment.
    """

    # We need to parse attachments first.
    # They are extracted and removed from messages.
    _attachments = [process_attachment(part) for part in email_msg.walk()]
    attachments = list(filter(None, _attachments))
    has_attachments = len(attachments) > 0

    # Parse metadata
    from_ = email_msg.get('From')
    to = email_msg.get('To')
    subject = email_msg.get('Subject')
    date = (
        email.utils.parsedate_to_datetime(email_msg.get('Date'))
        if email_msg.get('Date')
        else None
    )

    with db.atomic():
        rmsg = RawMsg.create(email_blob=email_msg.as_bytes(), original_checksum=checksum)

        if has_attachments:
            for file_checksum, filename, content_type in attachments:
                att = Attachment.create(
                    file_checksum=file_checksum,
                    original_filename=filename,
                    content_type=content_type,
                )
                rmsg.attachments.add(att)

        mmeta = MsgMeta.create(
            rawmsg=rmsg,
            imap_uid=m_uid,
            from_=from_,
            to=to,
            subject=subject,
            date=date,
            has_attachments=has_attachments,
        )

    log.info(
        'Processed message: m_uid=%s, from_=%s, to=%s, subject=%s',
        m_uid,
        from_,
        to,
        subject,
    )

def process_attachment(part: Message) -> Optional[Tuple[str, str, str]]:
    """
    Remove attachments from email messages and save them as files.
    The message will be altered.
    """

    filename = part.get_filename()
    if not filename:
        return None

    content_type = part.get_content_type()
    payload = part.get_payload(decode=True)  # decode from base64
    file_checksum = hashlib.sha256(payload).hexdigest()
    file_path = Path(f'attachments/{file_checksum}')

    log.debug(
        'Attachment found: file_checksum=%s, filename=%s, content_type=%s',
        file_checksum,
        filename,
        content_type,
    )

    if file_path.is_file():
        log.info('Attachment file already exists for: %s, Skipping', file_checksum)
    else:
        log.info('Writing file: %s', file_checksum)
        file_path.write_bytes(payload)

    part.set_param(file_checksum, None, header='X-File-Checksum')
    part.set_payload(None)

    return file_checksum, filename, content_type


from ast import operator
import imp
import json
from dsutils.de.utils import dbg, get_var_name
import fnmatch
import functools
import os
import shutil
from tqdm import tqdm
import pandas as pd
import sys
import subprocess

#subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'tabulate'])
from tabulate import tabulate

def get_dir_and_doc_paths(path):
    if os.path.isfile(path):
        dir_path = get_parent_dir(path)
        filepath = path
    elif os.path.isdir(path):
        dir_path = path
        filepath = get_child_ext_path(dir_path=dir_path, ext='pdf')
    else:
        print('enter an existing path')
    return dir_path, filepath

def join_parentpath_childnames(parentpath,childnames):
    fullpaths = map(functools.partial(os.path.join,
                        parentpath),
        childnames)
    return list(fullpaths)

# find first data subdir in path
def get_data_path(wd = os.getcwd()):
    dircount = 0
    dirs = list(wd.split(os.path.normpath(os.sep)))+['data']
    for i in range(len(dirs)):
        data_dir = os.path.normpath(os.sep)+os.path.join(*list(dirs[:-i]),'data')
        if os.path.isdir(data_dir):
            return data_dir
        else:
            dircount += 1
    return None

def get_datafile_path(filename, data_path=get_data_path(), format='csv'):
    filepath = os.path.join(data_path,filename)
    if '.xls' in filename and 'csv' in format :
        filepath = xls_to_csv(filepath)
    return filepath

# lists paths with certain extension
def list_ext(path,
            exts,
            invert=False): # return files WITHOUT such extensions instead
    path_list = list()
    for ext in exts:
        if type(ext) == list:
            ext = ext[0]
        for filename in os.listdir(path):
            filepath = os.path.join(path,filename)
            ends_ext = filepath.endswith(ext)
            is_file = os.path.isfile(filepath)
            has_ext = ends_ext and is_file
            if not invert:
                ret_ext = has_ext
                if ret_ext:
                    path_list.append(filepath)
            elif is_file:
                ret_ext = not has_ext
                if ret_ext:
                    path_list.append(filepath)
    return path_list

# get one child with requested extension
def get_child_ext_path(dir_path, ext):
    child = list_ext(path=dir_path,exts=[ext])[0]
    return child

# creates a folder with the document's name and moves the document in it
def mv_to_custom_dir(doc_path):
    split_path = os.path.split(doc_path)
    ext = os.path.splitext(doc_path)[-1]
    new_dir_name = split_path[-1][:-len(ext)] # same as filename, without the extension
    filename = split_path[-1]
    input_dir_path = split_path[-2]

    new_dir_path = os.path.join(input_dir_path,new_dir_name.replace(" ", "_"))
    new_doc_path = os.path.join(new_dir_path,filename.replace(" ", "_"))

    if not os.path.exists(new_dir_path):
        os.mkdir(new_dir_path)
    if not os.path.exists(new_doc_path):
        os.rename(doc_path,new_doc_path)
    else:
        print('Warning: a document named {0} already exists. Deleting the newer copy.'.format(filename))
        os.remove(doc_path)
    return
def get_parent_dir(path):
    return os.path.split(path)[-2]

def get_child_dir_paths(dir_path):
    child_names = os.listdir(dir_path)
    child_paths = [os.path.join(dir_path,child_name) for child_name in child_names]
    dir_paths = [child_path for child_path in child_paths if os.path.isdir(child_path)]
    return dir_paths

# make directory if it doesn't exist
def mkdir_no_over(dir_name):
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    return dir_name

def save_file(filepath, data):
    with open(filepath, 'w+') as f:
        f.write(data)

def save_data(doc_dir,
                dir_name,
                file_name,
                content,
                ):
    dirpath = os.path.join(doc_dir,dir_name)
    mkdir_no_over(dir_name)
    filepath = os.path.join(dirpath,file_name)
    save_data(filepath, content)

def get_final_files(treeroot, pattern):
    results = []
    for base, dirs, files in os.walk(treeroot):
        goodfiles = fnmatch.filter(files, pattern)
        results.extend(os.path.join(base, f) for f in goodfiles)
    print('{0} FILES FOUND'.format(len(results)))
    return results

def store_final_files(final_name, data_path, target_folder):
    final_files = get_final_files(data_path, final_name)
    for t in tqdm(final_files, desc='files stored', leave=False):
        path = os.path.normpath(t)
        path_sects = path.split(os.sep)
        target_name = path_sects[-3]+'_'+final_name
        target_path = os.path.join(target_folder,target_name)
        shutil.copyfile(t, target_path)

def try_read(path, ext=None, alt=''):
    try:
        if ext is None:
            filepath = path
        else:
            filepath = get_child_ext_path(path, ext)
        with open(filepath) as f:
            content = f.read()
    except (IndexError, FileNotFoundError):
        content = alt
    return content

def xls_to_csv(xls_path, csv_path=''):
    read_file = pd.read_excel(xls_path)
    if not csv_path:
        csv_path = os.path.join(get_parent_dir(xls_path),os.path.splitext(os.path.basename(xls_path))[0]+'.csv')
    read_file.to_csv(csv_path, index = None, header=True)
    return csv_path

def get_filename(path):
    filename = os.path.basename(path)
    return filename
def get_extension(path):
    ext = os.path.splitext(path)[-1]
    return ext

def sample_csv(path, size=5, filename='', in_col_select_rows={}, operator='OR'):
    if not filename:
        filename = get_filename(path)
    sample_path = filename+'_sample'+get_extension(path)
    df = pd.read_csv(path)
    df_ = None
    if in_col_select_rows:
        if operator == 'OR':
            for c,strs in in_col_select_rows.items():
                dbg(c)
                c =0
                for s in strs:
                    c +=1
                    dbg(s)
                    if c>1:
                        quit()
                    if type(df_) == None:
                        print('NONE')
                        df_ = df[df[c].astype(str).str.contains(s, na = False)]
                    else:
                        df_ = pd.concat([df_, df[df[c].astype(str).str.contains(s, na = False)]])
                    print(set(df_['id']))
            df = df_
        elif operator == 'AND':
            for c,strs in in_col_select_rows.items():
                for s in strs:
                    df = df[df[c].astype(str).str.contains(s, na = False)]
    df.head(size).to_csv(sample_path, index = None, header=True)
    return sample_path

def get_csv_head(path, size=5, nrows=100):
    with open(path) as f:
        df = pd.read_csv(f, on_bad_lines='skip', nrows=nrows)
    if isinstance(size, str):
        return df
    return df.head(size)

def describe_csv(path):
    df = pd.read_csv(path, on_bad_lines='skip')
    return df.describe(include="all") #pd.DataFrame(df, c = df.shape),


"""
Assumptions made:
1. For the GET method, assuming that whenever that GET method is ran, list will always be size 500,
so no need to check and just return the sorted list.

2. For the overall API, assuming this would be ran locally so enabled it to be ran locally. Otherwise,
possible to corrected and be ran on a server and use a curl command to access it.
"""

"""
Used Flask to be able to create the REST API and used some in built functions to be able to execute
certain calculations more efficiently.
"""

from flask import Flask, request
from flask_restful import Resource, Api, reqparse, abort
import pandas as pd
import ast
import random, json, requests, re, bisect

app = Flask(__name__)
api = Api(app)
NUMBERLIST = {"list":[]}

# Class Declaration that contains all 3 different methods
class ListData(Resource):
    # Retrieve Data, sort before returning
    def get(self):
        NUMBERLIST['list'].sort()
        return NUMBERLIST

    # Create Data, make sure all entries are numbers and size is of 500
    def post(self):
        numlist = []
        templist = str(request.data).split(',')
        for i in templist:
            temp = re.sub("[^0-9]", "", i)
            numlist.append(int(temp))

        if(len(numlist) == 500):
            result = all(isinstance(x, int) for x in numlist)
            if (result == True):
                NUMBERLIST['list'] = numlist
                return NUMBERLIST
            else:
                abort(400,"Invalid Input. All entries have to be numbers.")
        else:
            abort(400, "Invalid input. Length is not 500.")

    # Insert one element in corresponding order
    def patch(self):
        entry = str(request.data).strip()
        temp = re.sub("[^0-9]", "", entry)

        # Check if the single entry is a number or not
        if(temp.isnumeric()):
            # Use built in function to insert entry in a sorted list
            bisect.insort(NUMBERLIST['list'], int(temp))
            return NUMBERLIST
        else:
            abort(400,"Invalid Input. All entries have to be numbers.")

api.add_resource(ListData, '/data/')  # '/data' is our entry point
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

# Testing Commands
payload = []
for i in range(0, 500):
    payload.append(random.randint(0,1000))

newPayload = json.dumps(payload)
newData = str(random.randint(0,1000))

r=requests.post("http://localhost:5000/data/",data=newPayload)
r=requests.get("http://localhost:5000/data/")
r=requests.patch("http://localhost:5000/data/",data=newData)


import pytest
from typing import Dict, Any, cast
from opentrons_shared_data.pipette import (
    load_data,
    pipette_load_name_conversions,
    dev_types,
)
from opentrons_shared_data.pipette.types import (
    PipetteChannelType,
    PipetteModelType,
    PipetteVersionType,
    PipetteTipType,
    Quirks,
    LiquidClasses,
)

def test_load_pipette_definition() -> None:
    pipette_config_one = load_data.load_definition(
        PipetteModelType.p50,
        PipetteChannelType.SINGLE_CHANNEL,
        PipetteVersionType(major=3, minor=3),
    )

    assert pipette_config_one.channels == 1
    assert pipette_config_one.pipette_type.value == "p50"
    assert pipette_config_one.nozzle_offset == [-8.0, -22.0, -259.15]

    assert (
        pipette_config_one.liquid_properties[LiquidClasses.default]
        .supported_tips[PipetteTipType.t50]
        .default_aspirate_flowrate.default
        == 8.0
    )

    assert pipette_config_one.quirks == []
    pipette_config_two = load_data.load_definition(
        PipetteModelType.p50,
        PipetteChannelType.SINGLE_CHANNEL,
        PipetteVersionType(major=1, minor=0),
    )

    assert pipette_config_two.channels == 1
    assert pipette_config_two.pipette_type.value == "p50"
    assert pipette_config_two.nozzle_offset == [0.0, 0.0, 25.0]
    assert (
        pipette_config_two.liquid_properties[LiquidClasses.default]
        .supported_tips[PipetteTipType.t200]
        .default_aspirate_flowrate.default
        == 25.0
    )
    assert pipette_config_two.quirks == [Quirks.dropTipShake]

@pytest.mark.parametrize(
    argnames=["key_spot_check", "value_spot_check"],
    argvalues=[
        ["P1KSV10", "p1000_single_v1.0"],
        ["P1KHV33", "p1000_96_v3.3"],
        ["P20MV21", "p20_multi_v2.1"],
        ["P300MV10", "p300_multi_v1.0"],
        ["P3HMV14", "p300_multi_v1.4"],
    ],
)
def test_build_serial_number_lookup(key_spot_check: str, value_spot_check: str) -> None:
    lookup_table = load_data.load_serial_lookup_table()
    assert lookup_table[key_spot_check] == value_spot_check

@pytest.mark.parametrize(
    argnames=["pipette_model", "v1_configuration_changes"],
    argvalues=[
        ["p300_multi_v1.4", {"max_volume": 200, "min_volume": 10}],
        ["p1000_96_v3.3", {"tip_length": 40, "min_volume": 300}],
    ],
)
def test_update_pipette_configuration(
    pipette_model: str, v1_configuration_changes: Dict[str, Any]
) -> None:

    liquid_class = LiquidClasses.default
    model_name = pipette_load_name_conversions.convert_pipette_model(
        cast(dev_types.PipetteModel, pipette_model)
    )
    base_configurations = load_data.load_definition(
        model_name.pipette_type, model_name.pipette_channels, model_name.pipette_version
    )
    updated_configurations = load_data.update_pipette_configuration(
        base_configurations, v1_configuration_changes, liquid_class
    )

    updated_configurations_dict = updated_configurations.dict()
    for k, v in v1_configuration_changes.items():
        if k == "tip_length":
            for i in updated_configurations_dict["liquid_properties"][liquid_class][
                "supported_tips"
            ].values():
                assert i["default_tip_length"] == v
        else:
            assert (
                updated_configurations_dict["liquid_properties"][liquid_class][k] == v
            )


"""Tests for SystemConfigurationModel class.

Note: Do not need to test matching module names because module names cannot be the same
by definition of dict.
"""
from typing import Any, Callable, Dict

import pytest
from pydantic import ValidationError
from pytest_lazyfixture import lazy_fixture  # type: ignore[import]

from emulation_system import SystemConfigurationModel
from emulation_system.compose_file_creator.errors import DuplicateHardwareNameError
from emulation_system.compose_file_creator.input.hardware_models import (
    HeaterShakerModuleInputModel,
    MagneticModuleInputModel,
    OT2InputModel,
    TemperatureModuleInputModel,
    ThermocyclerModuleInputModel,
)
from tests.conftest import (
    HEATER_SHAKER_MODULE_ID,
    MAGNETIC_MODULE_ID,
    OT2_ID,
    SYSTEM_UNIQUE_ID,
    TEMPERATURE_MODULE_ID,
    THERMOCYCLER_MODULE_ID,
)

@pytest.fixture
def matching_module_names(make_config: Callable) -> Dict[str, Any]:
    """Dict with matching moudle names."""
    config = make_config(modules={"magnetic-module": 1})
    config["modules"].extend(config["modules"])
    return config

@pytest.fixture
def matching_robot_and_module_names(make_config: Callable) -> Dict[str, Any]:
    """Dict with matching robot and module name."""
    config = make_config(robot="ot2", modules={"magnetic-module": 1})
    config["robot"]["id"] = config["modules"][0]["id"]
    return config

@pytest.fixture
def invalid_ot2_name(make_config: Callable) -> Dict[str, Any]:
    """Dict with ot2 that has invalid name."""
    config = make_config(robot="ot2")
    config["robot"]["id"] = "Invalid Name"
    return config

@pytest.fixture
def null_robot_with_modules(make_config: Callable) -> Dict[str, Any]:
    """Structure of SystemConfigurationModel with modules and null robot."""
    config = make_config(
        robot="ot2",
        modules={
            "magnetic-module": 1,
            "temperature-module": 1,
            "thermocycler-module": 1,
            "heater-shaker-module": 1,
        },
        system_unique_id="test",
    )
    config["robot"] = None
    config["system-unique-id"] = None
    return config

@pytest.fixture
def null_module_with_robot(make_config: Callable) -> Dict[str, Any]:
    """Structure of SystemConfigurationModel with modules and null robot."""
    config = make_config(
        robot="ot2",
        modules={
            "magnetic-module": 1,
            "temperature-module": 1,
            "thermocycler-module": 1,
            "heater-shaker-module": 1,
        },
        system_unique_id="test",
    )
    config["modules"] = None
    config["system-unique-id"] = None
    return config

@pytest.fixture
def null_everything() -> Dict[str, None]:
    """Structure of SystemConfigurationModel with all values null."""
    return {
        "robot": None,
        "modules": None,
        "system-unique-id": None,
    }

@pytest.fixture
def with_invalid_system_unique_id(make_config: Callable) -> Dict[str, Any]:
    """Structure of SystemConfigurationModel with robot, modules, and an invalid system-unique-id."""
    config = make_config(
        robot="ot2",
        modules={
            "magnetic-module": 1,
            "temperature-module": 1,
            "thermocycler-module": 1,
            "heater-shaker-module": 1,
        },
        system_unique_id="test",
    )
    config["system-unique-id"] = "I aM uNiQuE bUt InVaLiD"
    return config

@pytest.mark.parametrize(
    "config",
    [
        lazy_fixture("matching_robot_and_module_names"),
        lazy_fixture("matching_module_names"),
    ],
)
def test_duplicate_names(config: Dict[str, Any]) -> None:
    """Confirm that ValidationError is thrown when a robot and module have the same name."""
    with pytest.raises(DuplicateHardwareNameError) as err:
        SystemConfigurationModel.from_dict(config)
    expected_error_text = (
        "The following container names are duplicated in the "
        f"configuration file: {MAGNETIC_MODULE_ID}"
    )
    assert err.match(expected_error_text)

def test_invalid_container_name(invalid_ot2_name: Dict[str, Any]) -> None:
    """Confirm that ValidationError is thrown when a robot and module have the same name."""
    with pytest.raises(ValidationError) as err:
        SystemConfigurationModel.from_dict(invalid_ot2_name)
    expected_error_text = ".*string does not match regex.*"
    assert err.match(expected_error_text)

@pytest.mark.parametrize(
    "config",
    [
        lazy_fixture("modules_only"),
        lazy_fixture("ot2_and_modules"),
        lazy_fixture("null_robot_with_modules"),
    ],
)
def test_modules_exist_is_true(config: Dict[str, Any]) -> None:
    """Test that modules_exist property is true when it is supposed to be."""
    assert SystemConfigurationModel.from_dict(config).modules_exist  # type: ignore [truthy-function]

@pytest.mark.parametrize(
    "config", [lazy_fixture("ot2_only"), lazy_fixture("null_module_with_robot")]
)
def test_modules_exist_is_false(config: Dict[str, Any]) -> None:
    """Test that modules_exist property is false when it is supposed to be."""
    configuration_file = SystemConfigurationModel.from_dict(config)
    assert not configuration_file.modules_exist(configuration_file.modules)

@pytest.mark.parametrize(
    "config",
    [
        lazy_fixture("ot2_only"),
        lazy_fixture("ot2_and_modules"),
        lazy_fixture("null_module_with_robot"),
    ],
)
def test_robot_exists_is_true(config: Dict[str, Any]) -> None:
    """Test that robot_exists property is true when it is supposed to be."""
    configuration_model = SystemConfigurationModel.from_dict(config)
    assert configuration_model.robot_exists(configuration_model.robot)

@pytest.mark.parametrize(
    "config", [lazy_fixture("modules_only"), lazy_fixture("null_robot_with_modules")]
)
def test_robot_exists_is_false(config: Dict[str, Any]) -> None:
    """Test that robot_exists property is false when it is supposed to be."""
    configuration_model = SystemConfigurationModel.from_dict(config)
    assert not configuration_model.robot_exists(configuration_model.robot)

def test_containers_property(ot2_and_modules: Dict[str, Any]) -> None:
    """Test the containers property is constructed correctly."""
    containers = SystemConfigurationModel.from_dict(ot2_and_modules).containers
    magnetic_module_id = f"{MAGNETIC_MODULE_ID}-1"
    temperature_module_id = f"{TEMPERATURE_MODULE_ID}-1"
    thermocycler_module_id = f"{THERMOCYCLER_MODULE_ID}-1"
    heater_shaker_module_id = f"{HEATER_SHAKER_MODULE_ID}-1"

    assert set(containers.keys()) == {
        OT2_ID,
        magnetic_module_id,
        temperature_module_id,
        thermocycler_module_id,
        heater_shaker_module_id,
    }
    assert isinstance(containers[OT2_ID], OT2InputModel)
    assert isinstance(containers[magnetic_module_id], MagneticModuleInputModel)
    assert isinstance(containers[temperature_module_id], TemperatureModuleInputModel)
    assert isinstance(containers[thermocycler_module_id], ThermocyclerModuleInputModel)
    assert isinstance(containers[heater_shaker_module_id], HeaterShakerModuleInputModel)

def test_get_by_id(ot2_and_modules: Dict[str, Any]) -> None:
    """Test that loading containers by id works correctly."""
    system_config = SystemConfigurationModel.from_dict(ot2_and_modules)
    magnetic_module_id = f"{MAGNETIC_MODULE_ID}-1"
    temperature_module_id = f"{TEMPERATURE_MODULE_ID}-1"
    thermocycler_module_id = f"{THERMOCYCLER_MODULE_ID}-1"
    heater_shaker_module_id = f"{HEATER_SHAKER_MODULE_ID}-1"
    assert isinstance(system_config.get_by_id(OT2_ID), OT2InputModel)
    assert isinstance(
        system_config.get_by_id(heater_shaker_module_id), HeaterShakerModuleInputModel
    )
    assert isinstance(
        system_config.get_by_id(magnetic_module_id), MagneticModuleInputModel
    )
    assert isinstance(
        system_config.get_by_id(temperature_module_id), TemperatureModuleInputModel
    )
    assert isinstance(
        system_config.get_by_id(thermocycler_module_id), ThermocyclerModuleInputModel
    )

@pytest.mark.parametrize(
    "config", [lazy_fixture("ot2_and_modules"), lazy_fixture("null_everything")]
)
def test_no_system_unique_id(config: Dict[str, Any]) -> None:
    """Test that default network name is set correctly when field is not specified."""
    assert SystemConfigurationModel.from_dict(config).system_unique_id is None

def test_overriding_system_unique_id(with_system_unique_id: Dict[str, Any]) -> None:
    """Test that system network name is overridden correctly."""
    system_config = SystemConfigurationModel.from_dict(with_system_unique_id)
    assert system_config.system_unique_id == SYSTEM_UNIQUE_ID

def test_invalid_system_unique_id(
    with_invalid_system_unique_id: Dict[str, Any]
) -> None:
    """Verify exception is thrown when invalid system-unique-id is passed."""
    with pytest.raises(ValidationError):
        SystemConfigurationModel.from_dict(with_invalid_system_unique_id)


import pprint
from typing import List, Optional, Dict
from pydantic import BaseModel, Field
from unskript.thirdparty.pingdom import swagger_client as pingdom_client

pp = pprint.PrettyPrinter(indent=4)

class InputSchema(BaseModel):
    checkIds: Optional[List[str]] = Field(
        title='checkIds',
        description='List of check Ids to be modified. eg: ["1643815305","1643815323"].')
    pause: bool = Field(
        title="pause",
        description='True to pause the check Ids and false to unpause it.')
    resolution: int = Field(
        title="resolution",
        description='Interval time to test website (In Minutes). eg: 1 5 15 30 60.')

def pingdom_pause_or_unpause_checkids_printer(output):
    if output is None:
        return
    pprint.pprint(output)

def pingdom_pause_or_unpause_checkids(handle, pause: bool, resolution: int, checkIds=None) -> Dict:
    """pingdom_pause_or_unpause_checkids returns the results pause or unpause check ids

        :type handle: object
        :param handle: Object returned from task.validate(...).

        :type pause: bool
        :param pause: True to pause the check Ids and false to unpause it.

        :type resolution: int
        :param resolution: Interval time to test website (In Minutes). eg: 1 5 15 30 60.

        :type checkIds: List
        :param checkIds: List of check Ids to be modified.

        :rtype: Returns the list of result of all pause or unpause check ids
    """
    if checkIds is None:
        checkIds = []
    data = {"paused": pause, "resolution": resolution}
    if checkIds:
        data["checkids"] = ",".join(checkIds)
    check = pingdom_client.ChecksApi(api_client=handle)
    result = check.checks_put_with_http_info(body=data, _return_http_data_only=True)

    return result


#  coding utf-8
# @time      :2019/6/24 17:09
# @Author    :zjunbin
# @Email     :648060307@qq.com
# @File      :alarm_first.py
from common.basepage import BasePage
from PageLocator.Alarm_locator import Alarm as loc
from Testdatas import common_data

class AlarmPage(BasePage):

    def alarm_first(self, value):
        self.switch_iframe(iframe=loc.alarm_page)
        self.wait_element_Visible(locator=loc.idcard,model_name='接警第一步')
        self.input_text(model_name='接警第一步输入信息',locator=loc.idcard,value=value)
        self.click_element(model_name='接警第一步信息提交提交',locator=loc.btn)

    def get_message(self):
        self.switch_iframe(iframe=loc.alarm_page)
        self.wait_element_Visible(locator=loc.lblResult,model_name='')
        ele = self.get_element_text(locator=loc.lblResult,model_name='')
        return ele


import pygame
import random

# Constants
WIDTH = 800
HEIGHT = 600
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAVITY = 0.2

class Particle:
    def __init__(self, x, y, size, color):
        self.x = x
        self.y = y
        self.size = size
        self.color = color
        self.vx = random.uniform(-1, 1)
        self.vy = random.uniform(-1, 1)

    def update(self):
        self.vy += GRAVITY
        self.x += self.vx
        self.y += self.vy

        if self.y + self.size >= HEIGHT:
            self.y = HEIGHT - self.size
            self.vy *= -0.9

        if self.x <= 0 or self.x + self.size >= WIDTH:
            self.vx *= -1

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)

def main():
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Gravity Simulation")
    clock = pygame.time.Clock()

    particles = []
    for _ in range(100):
        x = random.randint(0, WIDTH)
        y = random.randint(0, HEIGHT)
        size = random.randint(5, 20)
        color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        particle = Particle(x, y, size, color)
        particles.append(particle)

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        screen.fill(BLACK)

        for particle in particles:
            particle.update()
            particle.draw(screen)

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

if __name__ == '__main__':
    main()


def index_to_column(data, indx_col, power_col):
    import pandas as pd
    data = data.reset_index()
    data[indx_col] = pd.to_datetime(data[indx_col])
    data = data.sort_values(indx_col)

    data = data.rename(columns={indx_col: 'ds', power_col: 'y'})
    return data

def xgboost_algorithm(file, checked_columns, col_names):
    import numpy as np
    import pandas as pd
    import matplotlib
    import matplotlib.pyplot as plt
    from prophet import Prophet
    from xgboost import XGBRegressor
    from sklearn.metrics import mean_squared_error
    from math import sqrt
    from sklearn.model_selection import GridSearchCV

    useColumns = []
    i = 0

    for column in checked_columns:
        if (checked_columns[i].get() == 1):
            useColumns.append(col_names[i])
        i = i + 1

    df = pd.read_csv(file, usecols=useColumns, index_col=useColumns[0], parse_dates=True)

    print(df)

    indx_col = useColumns[0]
    power_col = useColumns[1]

    df.sort_values(by=useColumns[0], ascending=True)
    df = df.fillna(method='ffill')
    df = df.sort_values(by=useColumns[0], ascending=True)
    df.dropna(axis=0, how='any', subset=None, inplace=True)

    df = df[~df.index.duplicated(keep='first')]

    df = df.asfreq('H')
    df = df.fillna(method='ffill')

    #############################################################################################
    #training section

    # Before building and training our model, let's split the data into training and testing
    #df_train, df_test = df[df.index < '2016-01-01'], df[df.index >= '2016-01-01']
    df_train, df_test = df[:], df[-22000:]

    X_train, y_train = date_transform(df_train, power_col)
    X_test, y_test = date_transform(df_test, power_col)

    print(X_train)
    print(y_train)

    print(X_test)
    print(y_test)

    xgb_model = XGBRegressor(n_estimators=1000, learning_rate=0.05, early_stopping_rounds=10)
    xgb_model.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)])
    xgb_pred = xgb_model.predict(X_test)

    rmse = sqrt(mean_squared_error(y_test, xgb_pred))  # root mean square error

    df_plot = pd.DataFrame({'y_test': y_test, 'xgb_pred': xgb_pred})

    plt.figure(figsize=(20, 8))

    df_plot['y_test'].plot(label='Actual')
    df_plot['xgb_pred'].plot(label='Predicted')
    plt.text(16770, 3250, 'RMSE: {}'.format(rmse), fontsize=20, color='red')
    plt.title('Testing Set Forecast', weight='bold', fontsize=25)
    plt.legend()

    #####################################################################################################
    #prediction section
    new_df = index_to_column(df, indx_col, power_col)
    new_df = remove_tz_from_dataframe(new_df)
    prophet_model2 = Prophet(interval_width=0.95)
    prophet_model2.fit(new_df)
    # 7 days to the future (7x24 = 168)
    future_dates = prophet_model2.make_future_dataframe(periods=168, freq='H')

    future_dates2 = future_dates.iloc[-168:, :].copy()

    future_dates2['ds'] = pd.to_datetime(future_dates2['ds'])
    future_dates2 = future_dates2.set_index('ds')

    future_dates2['Hour'] = future_dates2.index.hour
    future_dates2['Dayofweek'] = future_dates2.index.dayofweek
    future_dates2['Dayofmonth'] = future_dates2.index.day
    future_dates2['Dayofyear'] = future_dates2.index.dayofyear
    future_dates2['weekofyear'] = future_dates2.index.weekofyear
    future_dates2['Month'] = future_dates2.index.month
    future_dates2['Quarter'] = future_dates2.index.quarter
    future_dates2['Year'] = future_dates2.index.year

    X = pd.concat([X_train, X_test], ignore_index=True)
    y = pd.concat([y_train, y_test], ignore_index=True)

    xgb_model2 = XGBRegressor(n_estimators=1000, learning_rate=0.05)
    xgb_model2.fit(X, y)
    xgb_pred2 = xgb_model2.predict(future_dates2)

    df_plot2 = pd.DataFrame({'Hour': future_dates2['Hour'], 'xgb_pred2': xgb_pred2})

    #last_week = df['2018-07-01':'2018-08-15']
    last_week = df[-168:]

    plt.figure(figsize=(20, 8))

    last_week[power_col].plot()
    df_plot2['xgb_pred2'].plot()
    plt.title('7 Days Forecast', weight='bold', fontsize=25)
    plt.show()

def date_transform(data, power_col):
    df = data.copy()

    print(data)

    df['Hour'] = df.index.hour
    df['Dayofweek'] = df.index.dayofweek
    df['Dayofmonth'] = df.index.day
    df['Dayofyear'] = df.index.dayofyear
    df['weekofyear'] = df.index.weekofyear
    df['Month'] = df.index.month
    df['Quarter'] = df.index.quarter
    df['Year'] = df.index.year

    X = df.drop(power_col, axis=1)
    y = df[power_col]

    return X, y

def remove_tz_from_dataframe(df_in):
    import pandas as pd
    df = df_in.copy()
    col_times = [ col for col in df.columns if any([isinstance(x, pd.Timestamp) for x in df[col]])]
    for col in col_times:
        df[col] = pd.to_datetime(
            df[col], infer_datetime_format=True)
        df[col] = df[col].dt.tz_localize(None)
    return df


import urllib.request
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import sqlite3
import re
ignorewords = set(['the', 'of', 'to', 'and', 'a', 'in', 'is', 'it'])
class Crawler:
   # iniitialize the Crawler with the name database
    def __init__(self, dbname):
        self.con = sqlite3.connect(dbname)

    def __del__(self):
        self.con.close()

    def dbcommit(self):
        self.con.commit()

   # Auxilary function for getting an <entry: word_of_wordlist, url_of_urllist> id and adding
   # if it's not present
    def getentryid(self, table, field, value, createnew=True):
        # table = 'wordlist', field='word', value = word whose id is to be fetchd
        # table = 'urllist', field='url', value = url whose id is to be fetchd
        current_record = self.con.execute(
            "select rowid from %s where %s='%s'" % (table, field, value))
        res = current_record.fetchone()
        if res == None:
            current_record = self.con.execute(
                "insert into %s (%s) values ('%s')" % (table, field, value))
            return current_record.lastrowid
        else:
            return res[0]

    # index an individual page
    def addtoindex(self, url, soup):
        if self.isindexed(url):
            return
        print('Indexing ' + url)

        # Get the individual words
        text = self.gettextonly(soup)
        words = self.separatewords(text)

        # Get the URL id
        urlid = self.getentryid('urllist', 'url', url)

        # Link each word to this url
        for i in range(len(words)):
            word = words[i]
            if word in ignorewords:
                continue
            wordid = self.getentryid('wordlist', 'word', word)
            self.con.execute("insert into wordlocation(urlid,wordid,location) \
                values (%d,%d,%d)" % (urlid, wordid, i))
        self.storepdf(url, urlid)

    # Extract the text from an HTML page (no tags)
    def gettextonly(self, soup):
        # Finding the Words on a Page
        v = soup.string
        if v == None:
            c = soup.contents
            resulttext = ''
            for t in c:
                subtext = self.gettextonly(t)
                resulttext += subtext+'\n'
            return resulttext
        else:
            return v.strip()

    # Seperate the words by any non-whitespace character
    def separatewords(self, text):
        # source: https://www.pythontutorial.net/python-regex/python-regex-split/

        # splitter = re.compile('\W*')
        # return [s.lower() for s in splitter.split(text) if s != '']
        splitter = r'\W+'
        return [s.lower() for s in re.split(splitter, text) if s != '']

    # Return True if this url is already indexed
    def isindexed(self, url):
        u = self.con.execute(
            "select rowid from urllist where url='%s'" % url).fetchone()
        if u != None:
            # Check if it has actually been crawled
            v = self.con.execute(
                'select * from wordlocation where urlid=%d' % u[0]).fetchone()
            if v != None:
                return True
        return False

    # Add link between two pages
    def addlinkref(self, urlFrom, urlTo, linkText):
        words=self.separatewords(linkText)
        fromid=self.getentryid('urllist','url',urlFrom)
        toid=self.getentryid('urllist','url',urlTo)
        if fromid==toid: return  # i.e. fromurl_id == tourl_id
        cur=self.con.execute("insert into link(fromid,toid) values (%d,%d)" % (fromid,toid))
        linkid=cur.lastrowid
        for word in words:
            if word in ignorewords: continue
            wordid=self.getentryid('wordlist','word',word)
            self.con.execute("insert into linkwords(linkid,wordid) values (%d,%d)" % (linkid,wordid))

    # Starting with a list of pages, do breadth first search
    # to the given depth, indexing pages as we go
    def crawl(self, pages, depth=2):
        for i in range(depth):
            newpages = set()
            for page in pages:  # pages = url_list
                try:
                    # c = urllib.request.urlopen(page)
                    c = requests.get(page)
                except:
                    print(f"cound not open {page}")
                    continue
                # soup = BeautifulSoup(c.read())
                soup = BeautifulSoup(c.text)
                self.addtoindex(page, soup)

                links = soup('a')
                for link in links:
                    if('href' in dict(link.attrs)):
                        # if <a> has a link
                        url = urljoin(page, link['href'])
                        if url.find("\'") != -1:    # ignore url with quote
                            continue
                        url = url.split('#')[0]   # remove location portion
                        if url[:4] == 'http' and not self.isindexed(url):
                            newpages.add(url)
                        linkText = self.gettextonly(link)   # strange links e.g.
                        self.addlinkref(page, url, linkText)    # # Add link between two pages:
                self.dbcommit()
            pages = newpages

    # Create the database tables
    def createindextables(self):
        self.con.execute('create table urllist(url)')
        self.con.execute('create table wordlist(word)')
        self.con.execute('create table wordlocation(urlid,wordid,location)')
        self.con.execute('create table link(fromid integer,toid integer)')
        self.con.execute('create table linkwords(wordid,linkid)')
        self.con.execute('create table pdfs(urlid)')

        self.con.execute('create index wordidx on wordlist(word)')
        self.con.execute('create index urlidx on urllist(url)')
        self.con.execute('create index wordurlidx on wordlocation(wordid)')
        self.con.execute('create index urltoidx on link(toid)')
        self.con.execute('create index urlfromidx on link(fromid)')
        self.con.execute('create index pdfurl on pdfs(urlid)')  #  creating index of each  pdf_url
        self.dbcommit()

    def storepdf(self, url, url_id):
        # store to database: pdfs <if link is pdf>
        document_prefixes = ['https://drive.google.com/', ]
        document_suffixes = ['pdf', 'epub', 'doc', 'docx', 'odt', 'xls', 'xlsx', 'ppt', 'pptx']
        if True in [url.startswith(document_prefix) for document_prefix in document_prefixes] + [url.endswith(document_suffix) for document_suffix in document_suffixes]:
            print(f'pdf:{url}, {url_id}')
            self.conn.execute("insert into pdfs(urlid) values (%d)" % (url_id))
        else:
            print(f'not_: {url} {url_id}')


from __future__ import absolute_import
import os
from os.path import join

import flask

from . import helpers

class Flask(flask.Flask):
    def __init__(self, name, settings=None, *args, **kwargs):
        settings = helpers.import_settings(name, settings_module=settings)
        if not 'static_folder' in kwargs:
            ROOT_PATH = getattr(settings, 'ROOT_PATH', os.getcwd())
            kwargs.update({
                'static_folder': join(ROOT_PATH, 'build'),
                'static_url_path': '/static',
            })
        blueprints = kwargs.pop('blueprints', None)
        super(Flask, self).__init__(name, *args, **kwargs)
        self.setup_jinja_context_variables()

        helpers.setup_blueprints(self, blueprints)

    def setup_jinja_context_variables(self):
        self.jinja_env.globals.update({
            'static': helpers.static_helper
        })


import urllib
import random
import time
import pyperclip
filename = False
lineRn = 0 # For ordered mode
randomVal = 0
copyTime = False

def getFile():
    print("What is your file called?")
    x = input()
    if ".txt" in x:
        global filename;
        filename = x;
        try:
            f = open(filename, 'r')
        except:
            print("This file is not found, please try again\n")
            getFile()
    else:
        print("This is not a text file, please try again.\n")
        getFile()

def getDelay():
    print("Start time in seconds?\n")
    y = input()
    try:
        while int(y) > 0:
            print("Starting in: ", y)
            y = int(y) - 1
            time.sleep(1)
    except ValueError:
        print("That's not a number, try again. \n")
        getDelay()

def getCopyDelay():
    global copyTime
    if copyTime == False:
        print("Copy time (in seconds)?\n")
        copyTime = input()

        try:
            int(copyTime)
            copyTime = int(copyTime)
            print("Copy time: ", copyTime, "\n")
            getDelay()
        except ValueError:
            print("That's not a number, try again. \n")
            copyTime = False
            getCopyDelay()

def randomOrOrdered():
    global randomVal
    print("Random or ordered? \n")
    x = input()

    if x == "random" or x == "Random":
        randomVal = True
        getCopyDelay()
    elif  x == "ordered" or x == "Ordered":
        randomVal = False
        getCopyDelay()
    else:
        print("Invalid input: \nPossible inputs are: 'Random', 'random', 'Ordered, 'order' \nPlease try again. \n")
        randomOrOrdered()

# Gets lines, picks 10 random lines from the script
def getLines():
    global randomVal
    # pick random lines
    if randomVal == True:
        rand = random.randint(0, (len(lines)))
        numOfLines = random.randint(0, 8)
        choosenLines = lines[rand:(rand + numOfLines)]
    elif randomVal == False:
        global lineRn
        numOfLines = random.randint(1, 4)
        choosenLines = lines[lineRn:(lineRn + numOfLines)]
        lineRn = lineRn + numOfLines

        if lineRn == len(lines):
            lineRn = 0;

    text = ""
    # remove line breaks so it's ready for pasting
    for line in choosenLines:
        line = line.replace('\n', '')
        text = text + line
    # copies line to clipboard
    pyperclip.copy(text)
    print(text)

getFile()
randomOrOrdered()

# Open the file and read lines
with open(filename, 'r') as infile:
    # Read all lines into memory
    lines = infile.readlines()
    # Get rid of new lines
    # runs this function with a new line every 2 seconds
    while(True):
        getLines()
        time.sleep(copyTime)

# Changes:
    # Updated readme and more info there


from django.views.generic.edit          import CreateView       # For user registration, as per http://www.obeythetestinggoat.com/using-the-built-in-views-and-forms-for-new-user-registration-in-django.html
from django.contrib.auth.forms          import UserCreationForm # For user registration
from django.conf.urls                   import patterns, include, url
from django.contrib                     import admin
from django.contrib.staticfiles.urls    import staticfiles_urlpatterns
import contacts.views

urlpatterns = patterns('',
    url(r'^$',                      contacts.views.ListContactView.as_view(),   name='contacts-list',),
    url(r'^new$',                   contacts.views.CreateContactView.as_view(), name='contacts-new' ,),
    url(r'^edit/(?P<pk>\d+)/$',     contacts.views.UpdateContactView.as_view(), name='contacts-edit',),
    url(r'^delete/(?P<pk>\d+)/$',   contacts.views.DeleteContactView.as_view(), name='contacts-delete',),
    url(r'^(?P<pk>\d+)/$',          contacts.views.ContactView.as_view(),       name='contacts-view',),
    url(r'^edit/(?P<pk>\d+)/addresses$', contacts.views.EditContactAddressView.as_view(), name='contacts-edit-addresses',),
    url(r'^login/$',                'django.contrib.auth.views.login'),
    url(r'^logout/$',               'django.contrib.auth.views.logout'),
    url('^register/',               CreateView.as_view(                             # For user registration
                                        template_name='registration/register.html', # JM: Why do I need to specify the parent directory?
                                                                                # Is there a more elegant way? login and logout don't need this specification.
                                        form_class=UserCreationForm,
                                        success_url='/'
                                        )),
    url('^accounts/',               include('django.contrib.auth.urls')),           # For user registration
    url(r'^admin/', include(admin.site.urls)),
)

urlpatterns += staticfiles_urlpatterns()


from flask import Blueprint, render_template, request, flash, redirect, url_for
from flask_login import current_user

from blog_app import db
from blog_app.database.models import Review
from blog_app.forms.reviews import Review

reviews = Blueprint("reviews", __name__)

@reviews.route("/review-create", methods=['GET', 'POST'])
def review_create():
    form = Review()

    if request.method == "POST":
        if form.validate_on_submit():
            project_category = form.title.data
            review = form.description.data

            if not project_category and not review:
                flash('Post cannot be empty', category='error')
            else:
                complete_user_review = Review(
                    category=form.category.data,
                    title_name=form.title.data,
                    description=form.description.data,
                    mark=form.mark.data,
                    recommend=form.recommend.data,
                    author=current_user.id
                )
                try:
                    db.session.add(complete_user_review)
                    db.session.flush()
                    db.session.commit()
                    flash('Отзыв Создан !', category='success')
                except:
                    db.session.rollback()
                    flash('Ошибка добавления в БД', category='error')
                return redirect(url_for('start.home'))
    return render_template("posts/content_create.html", form=form)


import time
import pandas as pd
import pyclone2revolver as p2r
from pathlib import Path

def AssignCopyNumber(battenberg_ouput, patient_data):
    # Patient number
    mutation_id_begin = patient_data.split('.')[0].split('/')[1] + ":"
    # print(mutation_id_begin)
    # print(patient_data)
    # print(battenberg_ouput)

    copy_number_info = pd.read_csv(battenberg_ouput, sep="\t")
    copy_number_info.drop(copy_number_info.columns[[3, 4, 5, 9, 10, 11, 12, 13, 14, 15, 16]], axis=1, inplace=True)

    # rounding copy number
    copy_number_info['ntot'] = copy_number_info['ntot'].round()

    patient_df = pd.read_csv(patient_data, sep='\t', low_memory=False)

    patient_df['#CHROM'] = patient_df['#CHROM'].astype(str)

    mutation_id = []
    sample_id = []
    normal_cn = []
    minor_cn = []
    major_cn = []
    alt_counts = []
    ref_counts = []

    print("Data mapping")
    start_time = time.time()
    for i in range(len(patient_df)):

        from_tumour_file = patient_df.iloc[:, -1][i]
        from_control_file = patient_df.iloc[:, -2][i]

        # only needed 0/0 0/1
        if from_tumour_file == "./." or from_control_file == "./.":
            continue

        from_tumour_file = from_tumour_file.split(':')
        from_control_file = from_control_file.split(':')

        if from_tumour_file[0] == "0/0":
            continue

        if from_control_file[0] == from_tumour_file[0]:
            continue
        if from_control_file[0] != "0/0" and from_tumour_file[0] != "0/1":
            continue

        from_tumour_file = from_tumour_file[1]
        # getting referenced and altered genome counts
        to_counts = ''.join(from_tumour_file)
        to_counts = to_counts.split(',')

        if len(to_counts) == 1:
            continue

        if patient_df['#CHROM'][i] == 'Y':
            break
        if len(patient_df['#CHROM'][i]) < 3:
            for j in range(len(copy_number_info)):
                if patient_df['#CHROM'][i] == copy_number_info['chr'][j] and \
                        (copy_number_info['startpos'][j] <= patient_df['POS'][i] <= copy_number_info['endpos'][j]):
                    # nomral cn
                    normal_cn.append(int(copy_number_info['ntot'][j]))
                    # major cn
                    major_cn.append(int(copy_number_info['nMaj1_A'][j]))
                    # minor cn
                    minor_cn.append(int(copy_number_info['nMin1_A'][j]))
                    mutation_id.append(
                        mutation_id_begin + str(patient_df['#CHROM'][i]) + ":" + str(patient_df['POS'][i]) + ":" + str(
                            patient_df['REF'][i]))
                    sample_id.append("R1")
                    alt_counts.append(to_counts[0])
                    ref_counts.append(to_counts[1])

                    break

    output = {"mutation_id": mutation_id,
              "sample_id": sample_id,
              "ref_counts": ref_counts,
              "alt_counts": alt_counts,
              "normal_cn": normal_cn,
              "major_cn": major_cn,
              "minor_cn": minor_cn
              }
    PyClone_input = pd.DataFrame(output)

    PyClone_input.to_csv("PyClone" + patient_data.split('.')[0].split('/')[1], sep='\t')
    print("--- %s seconds ---" % (time.time() - start_time))

def EraseVCFHeader(vcf_file):
    with open(vcf_file, "r") as f:
        lines = f.readlines()

    with open(vcf_file, "w") as f:
        for line in lines:
            if line[0:2] != "##":
                f.write(line)

if __name__ == '__main__':
    list_of_files = []

    list_of_cn = []
    for path in Path('drive-download-20230509T123841Z-002').rglob('*'):
        list_of_files.append(path.name)

    for path in Path('CN_files').rglob('*'):
        list_of_cn.append(path.name)

    # remove header
    for file_name in list_of_files:
        print("Running header removal")
        EraseVCFHeader("drive-download-20230509T123841Z-002/"+file_name)
        print("Header removed")
        get_subclone_file_name = file_name.split('.')[1].split('-')[0]
        subclone : str
        for subclone_name in list_of_cn:
            get_sublone_name_1 = subclone_name.split('-')[0]
            subclone = get_sublone_name_1

            if subclone == get_subclone_file_name:
                print(f'subclone {subclone_name} VCF {file_name}')

                AssignCopyNumber("CN_files/"+subclone_name, "drive-download-20230509T123841Z-002/"+file_name)
                list_of_cn.remove(subclone_name)
                break

        # print(get_subclone_file_name)

    pyclone_files = []
    annovar_files = []
    for path in Path('pyclone-vi').rglob('PyClone.*.tsv'):
        pyclone_files.append(path.name)
    print(len(pyclone_files))

    for path in Path('annovar').rglob('*.cut.txt'):
        annovar_files.append(path.name)
    print(annovar_files)

    for file_name in pyclone_files:
        print("Running Revolver input creator")
        patient=file_name.split('.')[1]
        for anno_file_name in annovar_files:
            anno_patient = anno_file_name.split('.')[0]
            if patient == anno_patient:
                try:
                    p2r.SortOutputPyClone("/media/kovac/Resources1/MartinD/pyclone-vi/"+file_name)
                except:
                    print("Already done")
                p2r.MergeSortedWithAnnovar("/media/kovac/Resources1/MartinD/pyclone-vi/"+file_name,
                                           "/media/kovac/Resources1/MartinD/annovar/"+anno_file_name)
                annovar_files.remove(anno_file_name)
    # p2r.SortOutputPyClone("/media/kovac/Resources1/MartinD/pyclone-vi/PyClone.P2.tsv.h5.tsv")

    files_for_revolver = []
    for path in Path('/media/kovac/Resources1/MartinD/').glob('*.tsv'):
        print(path.name)
        p2r.RemoveDuplicates(path.name)
        files_for_revolver.append(path.name)
    p2r.CreateRevolverInput(files_for_revolver)

    p2r.DetermineDriver("revolver_input.tsv","IntOGen-DriverGenes.txt")

    revolver_inputDF = pd.read_csv("data.tsv", sep="\t")
    revolver_inputDF2 = pd.read_csv("revolver_input.tsv", sep="\t")
    print(len(revolver_inputDF2))
    df_subset = revolver_inputDF[revolver_inputDF['N_tot'] == 1]
    values = df_subset['variantID'].values.tolist()
    print(values)
    revolver_inputDF2 = revolver_inputDF2.query("variantID != @values")

    print(len(revolver_inputDF2))
    revolver_inputDF2.to_csv("revolver_input.tsv", sep='\t', index=False)


from datetime import datetime, timezone
from os import environ
from typing import Callable, List, Optional, Union
from uuid import uuid4

from boto3.dynamodb.conditions import Attr, Key

import dynamo.user
from dynamo.util import DYNAMODB_RESOURCE, convert_floats_to_decimals, format_time, get_request_time_expression

class QuotaError(Exception):
    """Raised when trying to submit more jobs that user has remaining"""

def _get_job_count_for_month(user) -> int:
    now = datetime.now(timezone.utc)
    start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
    job_count_for_month = count_jobs(user, format_time(start_of_month))
    return job_count_for_month

def get_quota_status(user) -> Union[tuple[int, int, int], tuple[None, None, None]]:
    max_jobs = dynamo.user.get_max_jobs_per_month(user)

    if max_jobs is not None:
        previous_jobs = _get_job_count_for_month(user)
        remaining_jobs = max(max_jobs - previous_jobs, 0)
    else:
        previous_jobs = None
        remaining_jobs = None

    return max_jobs, previous_jobs, remaining_jobs

def put_jobs(user_id: str, jobs: List[dict], fail_when_over_quota=True) -> List[dict]:
    table = DYNAMODB_RESOURCE.Table(environ['JOBS_TABLE_NAME'])
    request_time = format_time(datetime.now(timezone.utc))

    max_jobs, previous_jobs, remaining_jobs = get_quota_status(user_id)
    has_quota = max_jobs is not None
    if has_quota:
        assert previous_jobs is not None
        assert remaining_jobs is not None

    if has_quota and len(jobs) > remaining_jobs:
        if fail_when_over_quota:
            raise QuotaError(f'Your monthly quota is {max_jobs} jobs. You have {remaining_jobs} jobs remaining.')
        jobs = jobs[:remaining_jobs]

    priority_override = dynamo.user.get_priority(user_id)
    priority = _get_job_priority(priority_override, has_quota)

    prepared_jobs = [
        {
            'job_id': str(uuid4()),
            'user_id': user_id,
            'status_code': 'PENDING',
            'execution_started': False,
            'request_time': request_time,
            'priority': priority(previous_jobs, index),
            **job,
        } for index, job in enumerate(jobs)
    ]

    for prepared_job in prepared_jobs:
        table.put_item(Item=convert_floats_to_decimals(prepared_job))
    return prepared_jobs

def _get_job_priority(priority_override: Optional[int], has_quota: bool) -> Callable[[Optional[int], int], int]:
    if priority_override is not None:
        priority = lambda _, __: priority_override
    elif has_quota:
        priority = lambda previous_jobs, job_index: max(9999 - previous_jobs - job_index, 0)
    else:
        priority = lambda _, __: 0
    return priority

def count_jobs(user, start=None, end=None):
    table = DYNAMODB_RESOURCE.Table(environ['JOBS_TABLE_NAME'])
    key_expression = Key('user_id').eq(user)
    if start is not None or end is not None:
        key_expression &= get_request_time_expression(start, end)

    params = {
        'IndexName': 'user_id',
        'KeyConditionExpression': key_expression,
        'Select': 'COUNT',
    }
    response = table.query(**params)
    job_count = response['Count']
    while 'LastEvaluatedKey' in response:
        params['ExclusiveStartKey'] = response['LastEvaluatedKey']
        response = table.query(**params)
        job_count += response['Count']
    return job_count

def query_jobs(user, start=None, end=None, status_code=None, name=None, job_type=None, start_key=None):
    table = DYNAMODB_RESOURCE.Table(environ['JOBS_TABLE_NAME'])

    key_expression = Key('user_id').eq(user)
    if start is not None or end is not None:
        key_expression &= get_request_time_expression(start, end)

    filter_expression = Attr('job_id').exists()
    if status_code is not None:
        filter_expression &= Attr('status_code').eq(status_code)
    if name is not None:
        filter_expression &= Attr('name').eq(name)
    if job_type is not None:
        filter_expression &= Attr('job_type').eq(job_type)

    params = {
        'IndexName': 'user_id',
        'KeyConditionExpression': key_expression,
        'FilterExpression': filter_expression,
        'ScanIndexForward': False,
    }
    if start_key is not None:
        params['ExclusiveStartKey'] = start_key

    response = table.query(**params)
    jobs = response['Items']
    return jobs, response.get('LastEvaluatedKey')

def get_job(job_id):
    table = DYNAMODB_RESOURCE.Table(environ['JOBS_TABLE_NAME'])
    response = table.get_item(Key={'job_id': job_id})
    return response.get('Item')

def update_job(job):
    table = DYNAMODB_RESOURCE.Table(environ['JOBS_TABLE_NAME'])
    primary_key = 'job_id'
    key = {'job_id': job[primary_key]}

    prepared_job = convert_floats_to_decimals(job)
    update_expression = 'SET {}'.format(','.join(f'{k}=:{k}' for k in prepared_job if k != primary_key))
    expression_attribute_values = {f':{k}': v for k, v in prepared_job.items() if k != primary_key}

    table.update_item(
        Key=key,
        UpdateExpression=update_expression,
        ExpressionAttributeValues=expression_attribute_values,
    )

def get_jobs_waiting_for_execution(limit: int) -> list[dict]:
    table = DYNAMODB_RESOURCE.Table(environ['JOBS_TABLE_NAME'])

    params = {
        'IndexName': 'status_code',
        'KeyConditionExpression': Key('status_code').eq('PENDING'),
        'FilterExpression': Attr('execution_started').ne(True),
    }
    response = table.query(**params)
    jobs = response['Items']

    while 'LastEvaluatedKey' in response and len(jobs) < limit:
        params['ExclusiveStartKey'] = response['LastEvaluatedKey']
        response = table.query(**params)
        jobs.extend(response['Items'])

    return jobs[:limit]


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Data collection and Pre-Processing

raw_mail_data = pd.read_csv('./data/mail_data.csv')
# print(raw_mail_data)

# replace the null values with a null string
mail_data = raw_mail_data.where((pd.notnull(raw_mail_data)) , '')

# print the first five rows from the dataframe
# print(mail_data.head())

# checking the number of rows and columns in the dataframe
# print(mail_data.shape)

# Label spam mail as 0 and not spam mail (ham mail) as 1
# spam -> 0           ham -> 1
mail_data.loc[mail_data['Category'] == 'spam' , 'Category'] = 0
mail_data.loc[mail_data['Category'] == 'ham' , 'Category'] = 1

# Seprating the data as texts and label
X = mail_data['Message']
Y = mail_data['Category']

# print(X)
# print(Y)

# Splitting the data into training data and test data

X_train , X_test, Y_train, Y_test = train_test_split(X , Y, test_size = 0.2, random_state = 3)

# print(X.shape)
# print(X_train.shape)
# print(X_test.shape)

# Transform the text data to feature vectors(numerical values) that can be used to the logistic regression

feature_extraction = TfidfVectorizer(min_df= 1 , stop_words='english' , lowercase=True)

X_train_features = feature_extraction.fit_transform(X_train)
X_test_features = feature_extraction.transform(X_test)

# convert Y_train and Y_test as integers (dtype = objest - > dtype = integer)

Y_train = Y_train.astype('int')
Y_test = Y_test.astype('int')

# print(X_train_features )

#----------------Logistic Regression----------------

model = LogisticRegression()

# trainig the Logistic Regression model with the training data
model.fit(X_train_features , Y_train)

# Evaluating the trained model

# Prediction on trainig data
prediction_on_training_data = model.predict(X_train_features)
accuracy_on_training_data = accuracy_score(Y_train, prediction_on_training_data)

print("Accuracy on training data : " ,accuracy_on_training_data)

# Prediction on test data

prediction_on_test_data = model.predict(X_test_features)
accuracy_on_test_data = accuracy_score(Y_test, prediction_on_test_data)

print("Accuracy on test data : " ,accuracy_on_training_data)

#  Building a predictive system
print()
print("Enter an email content: ")
input_mail_content = input()
input_mail = [input_mail_content]

# convert text to feature vectors
input_data_features = feature_extraction.transform(input_mail)

# making predictions
prediction = model.predict(input_data_features)

if prediction[0] == 1 :
    print("It is Ham mail")
else:
    print("It is spam mail")


import sys
import pygame
import random
from settings import Settings
from catcher import Catcher
from ball import Ball

class Window:
    def __init__(self):
        pygame.init()
        self.settings = Settings()
        if self.settings.fullscreen == True:
            self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
            self.settings.screen_width = self.screen.get_rect().width
            self.settings.screen_height = self.screen.get_rect().height
        elif self.settings.fullscreen == False:
            self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Blue Window")

        self.catcher = Catcher(self)
        self.balls = pygame.sprite.Group()
        self.create_ball()

    def create_ball(self):
        ball = Ball(self)
        ball_width, ball_height = ball.rect.size
        ball.x = random.randint(ball_width, (self.settings.screen_width-ball_width))
        ball.rect.x = ball.x
        ball.rect.y = ball_height
        self.balls.add(ball)

    def check_ball_edges(self):
        for ball in self.balls.sprites():
            if ball.check_edges():
                ball.kill()
                self.create_ball()
                break
            else:
                return True

    def run_game(self):
        while True:
            self._check_events()
            self.catcher.update()
            self.update_ball()
            self._update_screen()

    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_keydown_events(self, event):
        if self.settings.movement_keys == "1":
            if event.key == pygame.K_RIGHT:
                self.catcher.moving_right = True
            elif event.key == pygame.K_LEFT:
                self.catcher.moving_left = True
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
        elif self.settings.movement_keys == "2":
            if event.key == pygame.K_d:
                self.catcher.moving_right = True
            elif event.key == pygame.K_a:
                self.catcher.moving_left = True
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

    def _check_keyup_events(self, event):
        if self.settings.movement_keys == "1":
            if event.key == pygame.K_RIGHT:
                self.catcher.moving_right = False
            elif event.key == pygame.K_LEFT:
                self.catcher.moving_left = False
        elif self.settings.movement_keys == "2":
            if event.key == pygame.K_d:
                self.catcher.moving_right = False
            elif event.key == pygame.K_a:
                self.catcher.moving_left = False

    def update_ball(self):
        self.check_ball_edges()
        self.balls.update()
        if self.check_ball_edges() == True:
            for ball in self.balls.sprites():
                if pygame.sprite.spritecollideany(self.catcher, self.balls):
                    ball.kill()
                    self.create_ball()
                ball.rect.y += self.settings.fleet_drop_speed

    def _update_screen(self):
        self.screen.fill(self.settings.bg_color)
        self.catcher.blitme()
        self.balls.draw(self.screen)
        pygame.display.flip()

if __name__ == '__main__':
    main = Window()
    main.run_game()


import functools
from rest_framework import status
from rest_framework.response import Response
import datetime
import pytz

TIME_OUTPUT_FORMAT = '%Y-%m-%d %H:%M:%S'

def validate_serializer(serializer):
    """
    如果符合序列化要求，序列化之后的数据放在request.serializer.data
    否则直接返回序列化失败
    @validate_serializer(TestSerializer)
    def post(self, request):
        return self.success(request.data)
    """

    def validate(view_method):
        @functools.wraps(view_method)
        def handle(*args, **kwargs):
            self = args[0]
            request = args[1]
            s = serializer(data=request.data)
            if s.is_valid():
                request.serializer = s
                return view_method(*args, **kwargs)
            else:
                return Response(s.errors, status=status.HTTP_400_BAD_REQUEST)

        return handle

    return validate

def convert_cn_tz(date):
    UTC = pytz.timezone('UTC')
    CN_TZ = pytz.timezone(pytz.country_timezones('cn')[0])
    return date.astimezone(CN_TZ)

def convert_utc(date):
    UTC = pytz.timezone('UTC')
    CN_TZ = pytz.timezone(pytz.country_timezones('cn')[0])
    return date.astimezone(UTC)


import numpy as np
import pandas as pd
import shap
from ALEScore import *
def get_shap(model, X):
  explainerShap = shap.Explainer(model.predict, X, )
  shap_values = explainerShap(X, feature_perturbation = "tree_path_dependent")
  vector = []
  for i in range(X.shape[1]):
    value = abs(shap_values.values[:, i]).mean()
    vector.append(value)
  #print(shap.summary_plot(shap_values, X, plot_type="bar"))
  return vector

def get_shapTreeRegressor(model, X):
  explainerTree = shap.TreeExplainer(model, feature_perturbation = "tree_path_dependent").shap_values(X)
  vector = []
  for i in range(X.shape[1]):
    value = abs(explainerTree[:, i]).mean()
    vector.append(value)
  #print(shap.summary_plot(explainerTree, X, plot_type="bar"))
  #print("shap",vector)
  return vector

def get_shapTreeClassifier(model, X):
  explainerTree = shap.TreeExplainer(model,feature_perturbation = "tree_path_dependent" ).shap_values(X)
  #vector = []
  #for i in range(X.shape[1]):
    #if name == "RandomForestClassifier":
      #value = abs(explainerTree[0][:, i]).mean()
      #vector.append(value)
    #else:
      #value = abs(explainerTree[0][i]).mean()
      #vector.append(value)
  vector = np.mean(np.abs(explainerTree[0]), axis=0)
  #print("shap",vector)
  #print(shap.summary_plot(explainerTree, X, plot_type="bar"))
  return  vector

def get_FeatImport(model, X):
  vector = model.feature_importances_
  #print(plt.bar(range(X.shape[1]),model.feature_importances_))
  #print("pfi",vector)
  return vector

def get_ALE_max(model, X, y, names):
  if np.unique(y).shape[0] > 2:
      proba_fun = model.predict
  else:
      proba_fun = model.predict_proba
  proba_ale = ALE(proba_fun, feature_names=names, target_names=["y"])
  proba_exp = proba_ale.explain(X)
  vector = []
  for i in range(X.shape[1]):
    value = abs(proba_exp.ale_values[i][:,0]).max()
    #value = np.mean(proba_exp.ale_values[i][:,0])
    vector.append(value)
  #print("ale_1", vector)
  return vector

def get_ale(model, X, y):
  #X = pd.DataFrame(X)
  if np.unique(y).shape[0] > 2:
      predict_fun = model.predict
      #print("predict")
  else:
      predict_fun = model.predict_proba
      #print("proba")
  vector = []
  for i in range(X.shape[1]):
    ale = ALE2(X, y,model, i)
    vector.append(ale)
 # print("ale_2",vector)
  return (vector)

def get_coef(model):
  coefficients = model.coef_[0]
  vector = np.array(coefficients)
  #print("coef",vector)
  return vector

def adjust_predictions(y_pred, epsilon=1e-15):
  return np.clip(y_pred, epsilon, 1 - epsilon)


from django.conf.urls import include,url,patterns
from . import views
import django.contrib.auth.views as auth_views

urlpatterns = [
	url(r'^$', views.index, name='index'),
	url(r'^about-us/$', views.about_us, name='about_us'),
	url(r'^members/$', views.members, name='members'),
	url(r'^achievements/$', views.achievements, name='achievements'),
	url(r'^projects/$', views.our_projects, name='our_projects'),
	url(r'^tutorials/$', views.tutorials, name='tutorials'),
	url(r'^support-us/$', views.support_us, name='support_us'),
	url(r'^members/alumni/$', views.alumni, name='alumni'),
	url(r'^members/active/$', views.active_members, name='active_members'),
	url(r'^projects/ongoing/$', views.ongoing_projects, name='ongoing_projects'),
	url(r'^projects/completed/$', views.completed_projects, name='completed_projects'),
    url(r'^login/',auth_views.login, name="auth_views.login"),
    url(r'^home/', views.index, name='index'),
    url(r'^logout/$', views.logout_page),
	url(r'^accounts/login/$',auth_views.login, name="auth_views.login"),
	url(r'^accounts/userprofile/$', views.userprofile, name='userprofile'),
    url(r'^register/$', views.register),
    url(r'^profile/$', views.userprofile, name='userprofile'),
    url(r'^profile/edit/$', views.editprofile, name='editprofile'),
    url(r'^profile/addproject/$', views.addproject, name='addproject'),
    url(r'^profile/editproject/(?P<proj>)/$', views.editproject, name='editproject'),
    url(r'^register/success/$', views.success, name='success'),
    url(r'^register/fill_info/$', views.fill_info, name='fill_info'),
    url(r'^expo/$', views.expo, name='expo'),
]


import pytest
from datetime import datetime
from src.utils.utils import view_list_generator, format_date

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

class TestUtils:
    def test_view_list_generator(self, my_config_loader):
        g = view_list_generator(sample_list)
        assert next(g) == sample_list[:2]

    def test_view_list_generator_next_page(self, my_config_loader):
        g = view_list_generator(sample_list)
        first_two = next(g)
        next_two = next(g)
        assert next_two == sample_list[2:4]

    @pytest.mark.parametrize("bad_dates", ['abc', '2023-10-15', 'lo', '  ', '2023-10-87 22:15:33.77777'])
    def test_format_date_with_bad_date(self, bad_dates, my_config_loader):
        with pytest.raises(ValueError):
            format_date(bad_dates)

    @pytest.mark.parametrize('inp_date, expected', [('2023-10-25 11:37:54.637963', "25/10/2023 at 11:37")])
    def test_format_date_with_good_date(self, inp_date, expected, my_config_loader):
        date: datetime = format_date(inp_date)
        assert date == expected


import ujson

env_file_name = 'env.json'

def get_all():
    file = open(env_file_name, 'r')
    contents = ujson.loads(file.read())
    file.close()

    return contents

def get(var_name):
    print('VAR NAME GET ' + var_name)

    contents = get_all()

    value = contents[var_name]

    print(value)
    print('---')

    return value

def set(var_name, value):
    print('VAR NAME SET ' + var_name + ' TO ' + value)

    contents = get_all()
    contents[var_name] = value

    file = open(env_file_name, 'w')
    file.write(ujson.dumps(contents))
    file.close()

    print('---')


import logging
import babel
import babel.dates

import weather
import drawing

EPD_WIDTH = 400
EPD_HEIGHT = 300

# only update once an hour within these ranges
DEAD_TIMES = [
    range(0,6),
    range(10,16)
]

class PaperClock( object ):

    def __init__( self, debug_mode=False ):

        self._debug_mode = debug_mode
        if not debug_mode:
            import epd4in2
            self._epd = epd4in2.EPD()
            self._epd.init()

        self._str_time = "XXXX"

    def display_buffer( self, buf ):

        if self._debug_mode:
            buf.save( "debug.bmp" )
            return

        self._epd.display_frame(
            self._epd.get_frame_buffer( buf )
        )

    def update_for_datetime( self, dt ):

        tz_display = babel.dates.get_timezone('Europe/London')
        formatted = babel.dates.format_time(
            dt,
            "HHmm",
            tzinfo=tz_display
        )

        # set blank minutes if time's hour is within dead ranges
        h = formatted[:2]
        for dead_range in DEAD_TIMES:
            if int(h) in dead_range:
                formatted = "{}  ".format( h )

        if formatted != self._str_time:

            w = weather.get_weather()

            frame = drawing.draw_frame(
                EPD_WIDTH, EPD_HEIGHT,
                formatted,
                w
            )
            self.display_buffer( frame )

            self._str_time = formatted


import os
import unittest
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
import logging
import VolumeClipWithRoi
#
# VolumeClip
#

class VolumeClip(ScriptedLoadableModule):
  """Uses ScriptedLoadableModule base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """

  def __init__(self, parent):
    ScriptedLoadableModule.__init__(self, parent)
    self.parent.title = "VolumeClip" # TODO make this more human readable by adding spaces
    self.parent.categories = ["Examples"]
    self.parent.dependencies = []
    self.parent.contributors = ["John Doe (AnyWare Corp.)"] # replace with "Firstname Lastname (Organization)"
    self.parent.helpText = """
This is an example of scripted loadable module bundled in an extension.
It performs a simple thresholding on the input volume and optionally captures a screenshot.
"""
    self.parent.helpText += self.getDefaultModuleDocumentationLink()
    self.parent.acknowledgementText = """
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
and Steve Pieper, Isomics, Inc. and was partially funded by NIH grant 3P41RR013218-12S1.
""" # replace with organization, grant and thanks.

#
# VolumeClipWidget
#

class VolumeClipWidget(ScriptedLoadableModuleWidget):
    """Uses ScriptedLoadableModuleWidget base class, available at:
    https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
    """

    def setup(self):
        self.developerMode = True
        ScriptedLoadableModuleWidget.setup(self)

        ############################DENSE CORRESPONDENCE TAB############################
        ############################DENSE CORRESPONDENCE TAB############################
        ############################DENSE CORRESPONDENCE TAB############################
        #Adds a main tab
        self.mainTab = qt.QTabWidget()
        self.layout.addWidget(self.mainTab)

        # Libraries and Case Loading Tab
        VolumeClip = qt.QTabWidget()
        self.mainTab.addTab(VolumeClip, "VolumeClip")

        # Layouts for Libraries and Case Loading Dropdown
        layout_main = qt.QFormLayout(VolumeClip)
        layout_vol = qt.QVBoxLayout()
        layout_roi = qt.QVBoxLayout()

        # Transforms
        self.volSelector = qt.QListWidget()
        self.volSelector.setSelectionMode(3) #Allows selection of multiple items in list by dragging mouse, holding ctrl, or shift
        self.volSelector.setDragDropMode(4) #Allows rearrangement of items in list via drag and drop

        # Buttons to Launch Dialog popup and delete selected models in medium resolution library list
        self.selectVol = qt.QPushButton("Select Volumes")
        self.deleteVol = qt.QPushButton("Delete Highlighted Volumes")

        # Add above widgets to layout
        layout_vol.addWidget(self.volSelector)
        layout_vol.addWidget(self.selectVol)
        layout_vol.addWidget(self.deleteVol)

        # Create surrounding box to have a title for this medium resolution section
        volBox = qt.QGroupBox("Volumes")
        volBox.setLayout(layout_vol)
        layout_main.addRow(volBox)

        # Fiducials
        self.roiSelector = qt.QListWidget()
        self.roiSelector.setSelectionMode(3) #Allows selection of multiple items in list by dragging mouse, holding ctrl, or shift
        self.roiSelector.setDragDropMode(4) #Allows rearrangement of items in list via drag and drop

        # Buttons to Launch Dialog popup and delete selected models in medium resolution library list
        self.selectRoi = qt.QPushButton("Select Roi")
        self.deleteRoi = qt.QPushButton("Delete Highlighted Roi")

        # Add above widgets to layout
        layout_roi.addWidget(self.roiSelector)
        layout_roi.addWidget(self.selectRoi)
        layout_roi.addWidget(self.deleteRoi)

        # Create surrounding box to have a title for this medium resolution section
        roiBox = qt.QGroupBox("ROIs")
        roiBox.setLayout(layout_roi)
        layout_main.addRow(roiBox)

        self.selectVol.connect('clicked(bool)', self.onSelectVol)
        self.selectRoi.connect('clicked(bool)', self.onSelectRoi)
        self.deleteVol.connect('clicked(bool)', self.onDeleteVol)
        self.deleteRoi.connect('clicked(bool)', self.onDeleteRoi)

        self.clipVol = qt.QPushButton("Clip Volume")
        self.clipVol.toolTip = "Clips volume with ROI boxes"
        self.clipVol.setMinimumWidth(300)

        layout_main.addRow(self.clipVol)
        self.clipVol.connect('clicked(bool)', self.onRunButton)

    def onRunButton(self):
        #runs DenseCorrespondence when run button is pressed
        logic = VolumeClipLogic(self.volSelector, self.roiSelector)
        logic.run()

    def onSelectVol(self):
        logic = ModelSelector()
        logic.runVolSelect(self.volSelector)

    def onSelectRoi(self):
        logic = ModelSelector()
        logic.runRoiSelect(self.roiSelector)

    def onDeleteVol(self):
        toDelete = self.volSelector.currentRow
        self.volSelector.takeItem(toDelete)

    def onDeleteRoi(self):
        toDelete = self.roiSelector.currentRow
        self.roiSelector.takeItem(toDelete)
        #
        # CreateROILogic
        #

#
# VolumeClipLogic
#

class VolumeClipLogic(ScriptedLoadableModuleLogic):
  """This class should implement all the actual
  computation done by your module.  The interface
  should be such that other python code can import
  this class and make use of the functionality without
  requiring an instance of the Widget.
  Uses ScriptedLoadableModuleLogic base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """
  def __init__(self, volListWidget = None, roiListWidget = None):
    self.volListWidget = volListWidget
    self.roiListWidget = roiListWidget
    self.volNameList = []
    self.roiNameList = []

    self.volList = []
    self.roiList = []
  def pair(self):
    if(self.volListWidget.count == 0 or self.roiListWidget.count ==0):
      print("No Nodes in one or both lists!")
      return 0,[]

    if(self.volListWidget.count != self.roiListWidget.count):
      print("Number of transform and fiducial points does not match")
      return 0,[]

    for node in range(self.volListWidget.count):
      self.volNameList.append(self.volListWidget.item(node).text())
      self.roiNameList.append(self.roiListWidget.item(node).text())

    paired = zip(self.volNameList, self.roiNameList)

    for vol, roi in paired:
      vol_node = slicer.util.getNode(vol)
      roi_node = slicer.util.getNode(roi)
      self.volList.append(vol_node)
      self.roiList.append(roi_node)

  def run(self):
    self.pair()

    nodePair = zip(self.volList, self.roiList)

    for volume, roi in nodePair:
      name = "clipped" + roi.GetName()[-3:]
      clippedNode = slicer.vtkMRMLScalarVolumeNode()
      clippedNode.SetName(name)
      slicer.mrmlScene.AddNode(clippedNode)

      vcr = VolumeClipWithRoi.VolumeClipWithRoiLogic()

      vcr.clipVolumeWithRoi(roi, volume, 0.00, "1", clippedNode)

class ModelSelector():

    # Handler for Select Med Res Library Models
    def runVolSelect(self, finalList):
        # Create list of models to choose from and select for dialog
        self.finalList = finalList
        self.list = qt.QListWidget()
        self.list.setSelectionMode(3)
        nodeDict = slicer.mrmlScene.GetNodesByClass('vtkMRMLVolumeNode')

        for i in range(0, nodeDict.GetNumberOfItems()):
            self.list.addItem(nodeDict.GetItemAsObject(i).GetName())

        self.addButton = qt.QPushButton('Add Volumes')
        self.addButton.connect('clicked(bool)', self.onAddButton)

        # Create and launch dialog with list of models to choose from and select
        self.dialog = qt.QDialog()
        dialogLayout = qt.QFormLayout()
        dialogLayout.addWidget(self.list)
        dialogLayout.addWidget(self.addButton)
        self.dialog.setLayout(dialogLayout)
        self.dialog.exec_()

    # Handler for Select High Res Library Models button
    def runRoiSelect(self, finalList):

        # Create list of models to choose from and select for dialog
        self.finalList = finalList
        self.list = qt.QListWidget()
        self.list.setSelectionMode(3)
        nodeDict = slicer.mrmlScene.GetNodesByClass('vtkMRMLAnnotationROINode')

        for i in range(0, nodeDict.GetNumberOfItems()):
            self.list.addItem(nodeDict.GetItemAsObject(i).GetName())

        self.addButton = qt.QPushButton('Add ROI Boxes')
        self.addButton.connect('clicked(bool)', self.onAddButton)

        # Create and launch dialog with list of models to choose from and select
        self.dialog = qt.QDialog()
        dialogLayout = qt.QFormLayout()
        dialogLayout.addWidget(self.list)
        dialogLayout.addWidget(self.addButton)
        self.dialog.setLayout(dialogLayout)
        self.dialog.exec_()

    def onAddButton(self):
        chosenOnes = self.list.selectedItems()
        for one in chosenOnes:
          self.finalList.addItem(one.text())
        self.dialog.close()

class VolumeClipTest(ScriptedLoadableModuleTest):
  """
  This is the test case for your scripted module.
  Uses ScriptedLoadableModuleTest base class, available at:
  https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  """

  def setUp(self):
    """ Do whatever is needed to reset the state - typically a scene clear will be enough.
    """
    slicer.mrmlScene.Clear(0)

  def runTest(self):
    """Run as few or as many tests as needed here.
    """
    self.setUp()
    self.test_VolumeClip1()

  def test_VolumeClip1(self):
    """ Ideally you should have several levels of tests.  At the lowest level
    tests should exercise the functionality of the logic with different inputs
    (both valid and invalid).  At higher levels your tests should emulate the
    way the user would interact with your code and confirm that it still works
    the way you intended.
    One of the most important features of the tests is that it should alert other
    developers when their changes will have an impact on the behavior of your
    module.  For example, if a developer removes a feature that you depend on,
    your test should break so they know that the feature is needed.
    """

    self.delayDisplay("Starting the test")
    #
    # first, get some data
    #
    import urllib
    downloads = (
        ('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume),
        )

    for url,name,loader in downloads:
      filePath = slicer.app.temporaryPath + '/' + name
      if not os.path.exists(filePath) or os.stat(filePath).st_size == 0:
        logging.info('Requesting download %s from %s...\n' % (name, url))
        urllib.urlretrieve(url, filePath)
      if loader:
        logging.info('Loading %s...' % (name,))
        loader(filePath)
    self.delayDisplay('Finished with download and loading')

    volumeNode = slicer.util.getNode(pattern="FA")
    logic = VolumeClipLogic()
    self.assertIsNotNone( logic.hasImageData(volumeNode) )
    self.delayDisplay('Test passed!')


import argparse
import logging

import numpy as np
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from datasets import load_dataset

logging.basicConfig(
    format='%(asctime)s %(levelname)-8s %(message)s',
    level=logging.INFO,
    datefmt='%Y-%m-%d %H:%M:%S')

label_dict = {"P":0,
              "C":1,
              "S":2,
              "O":3}

label_idx = {v:k for k,v in label_dict.items()}

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--model', default='bert-base-german-cased', help='Model to use') # check: https://huggingface.co/bert-base-german-cased
parser.add_argument('--checkpoint', default='models/model.pt', help='Model checkpoints')
args = parser.parse_args()

tokenizer = AutoTokenizer.from_pretrained(args.model)

# Example prediction
test_sentence = "Ich brauche deine Hilfe!"
test_encodings = tokenizer([test_sentence], is_split_into_words=False, padding=True, truncation=True)

# Enable using a GPU if available
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
logging.info(f"Using {device}")

model = AutoModelForSequenceClassification.from_pretrained(args.model, num_labels=len(label_dict))

model.to(device)
model.load_state_dict(torch.load(args.checkpoint))

model.eval() #Drop gradients for evaluation (increases efficiency)

input_pred = {k: torch.tensor(v).to(device) for k, v in test_encodings.items()}

outputs = model(**input_pred)

prediction = np.argmax(outputs.logits.cpu().detach().numpy()) # Fetch predicted labels

dataset = load_dataset("UKPLab/TexPrax", "sentence_cl", split="test")
label = dataset.features['label'].int2str(int(prediction))
logging.info(f"Predicted label: {label}")


# 假设现有需求，将一个只包含数字的列表中的所有元素都自加1
# 函数式编程的思想实现如下
test = [3,6,4,5,7,8,9,12,3]

def add_one(x):
    return x+1

def squ(x):
    return x**2

def map_list(func,list):
    res = []
    for i in list:
        a = func(i)
        res.append(a)
    return res

result = map_list(add_one,test)
result_2 = map_list(squ,test)
print('原列表元素都自加1：',result,'\n原列表元素都平方：',result_2)

print('&&&&&其实python中自带了Map()函数可以实现此功能&&&&')

a = map(lambda x:x+1,test)
print('map函数默认返回的是一个迭代器',a,'\n用list函数将其转换为列表:',list(a))

# ****************************************************************************
print('-'*80)
# 假设现在有需求，将列表中后缀为del的字符串删除
test = ['cyx','tsts','zj_del','jch_del','pzl','hzc_del','zxf']

# def del_del(list):
#     res = []
#     for i in list:
#         if not str(i).endswith('del'):
#             res.append(i)
#     return res
#
# a = del_del(test)
def del_ends(x):
    if not str(x).endswith('del'):
        return x

def s_sta(x):
    if not str(x).startswith('z'):
        return x

def filter_list(func,list):
    res = []
    for i in list:
        a = func(i)
        if a is not None:
            res.append(a)
    return res

result = filter_list(del_ends,test)
result_2 = filter_list(s_sta,test)
print('原列表中删除del结尾的字符串:',result,'\n原列表中删除开头的字符串：',result_2)

print('&&&&&&&其实python中自带了filter函数&&&&&&&&')

result_3 = filter(del_ends,test)
result_4 = filter(lambda x:not str(x).startswith('z'),test)    # filter默认是将返回为true的结果输出，因此要删除时，在前面加not取反
print('用python自带的filter函数运行的结果也是一个迭代器：',result_3,'用list将其转换为列表：',list(result_3))
print('另一个用匿名函数的执行结果：',list(result_4))

from functools import reduce
print('-'*80)
# 假设有一个需求，将一个列表中所有的元素相加
test = [1,2,3,4,5]

def add_all(list):
    res = 0
    for i in list:
        res += i
    return res

result = add_all(test)
print('使用reduce函数，要调用functools模块')
print(result)

result = reduce(lambda x,y:x*y,test)
print(result)


from django.urls import path
from . import views

# app_name 변수는 주소에 라벨링을 합니다.
app_name="polls"

urlpatterns = [
        path('main', views.index, name='index'),
        path('test', views.test, name = 'test'),
        path('get', views.get, name ='get'),
        path('getform', views.getform, name = 'getform'),
        path('name', views.get_name, name = 'name'),
        path('nameform', views.nameform, name = 'nameform'),
        path('<int:temp>/ctof', views.temp),
        path('<int:height>/<int:weight>/bmi', views.bmi),
        path('<int:number>/content', views.get_number),
        # int가 들어왔을 때 number라는 변수에 저장
        # number라는 변수는 get_number라는 함수로 전달
        # 127.0.0.1:8000/polls/50
        #                         number
        path('<int:age>/check', views.get_adult_result),
        path('<int:num1>/<int:num2>/two', views.get_two),
        path('<str:name>/name', views.get_name_2),
        path('<str:money1>/<str:money2>/exchange', views.money),
        path('qtest', views.qtest),
        path('<int:price>/price', views.bus_price),
        path('<int:id>', views.detail, name='detail'),
        path('<int:question_id>/results/', views.results, name='results'),
        path('<int:question_id>/vote/', views.vote, name='vote'),

    ]
# http://www.127.0.0.1:8080/main


from httpx import AsyncClient
from pytest import mark
from csv import writer, QUOTE_NONNUMERIC
from io import StringIO
from datetime import datetime
from schemas_mock import *
from app.services.auth import AuthHandler

auth = AuthHandler()

@mark.anyio
async def test_healthcheck(client: AsyncClient, refresh_db):
    response = await client.get('/')
    assert response.status_code == 200
    assert response.json() == {'status': 'Working from CI/CD GitHub actions by Samandar Khodjiyev for Meduzzen!'}

@mark.anyio
async def test_worker_create(client: AsyncClient):
    await client.post('/users/add', json=user1.dict())
    await client.post('/users/add', json=user2.dict())
    await client.post('/users/add', json=user3.dict())
    response_location = await client.post('/locations/add',json=location1.dict())
    response_location.status_code == 200
    assert response_location.json() == db_location1
    token = await client.post('/users/login', json=login_user1.dict())
    token = token.json()
    admin_response = await client.post("/users/add_admin?id=1", headers={'Token':token})
    assert admin_response.status_code == 200
    response = await client.post('/workers/add', json=worker1.dict(), headers={'Token':token})
    assert response.json() == db_worker1
    assert response.status_code == 200

@mark.anyio
async def test_work_get_auth(client: AsyncClient):
    response = await client.get('/workers/all')
    assert response.status_code == 200
    assert response.json() == [db_worker1.dict()]

@mark.anyio
async def  test_worker_get_list(client: AsyncClient):
    token = await client.post('/users/login', json=login_user1.dict())
    token = token.json()
    response = await client.delete('workers/delete?worker_id=1&id=1',headers={'Token':token})
    assert response.status_code == 200
    assert response.json()['detail'] == 'Worker with id 1 is successfully deleted'


# 1. set, 리스트만으로
from itertools import combinations

def solution(relation):
    col_n = len(relation[0])
    entity_n = len(relation)

    all = []
    for n in range(1, col_n+1):
        for tmp in combinations(range(col_n), n):
            all.append(tmp)

    # 유일성
    answer = []
    for case in all:
        tmp_set = set()
        for entity in relation:
            tmp = []
            for i in case:
                tmp.append(entity[i])
            tmp = tuple(tmp)
            tmp_set.add(tmp) # tmp_set에 중복은 한번만 더해짐
        if len(tmp_set) == entity_n:
            answer.append(case)

    # 최소성
    new_answer = set(answer)
    for i in range(len(answer)):
        for j in range(i+1, len(answer)):
            if len(answer[i]) == len( set(answer[i]) & set(answer[j])):
                new_answer.discard(answer[j])

    return len(new_answer)

# 2. 비트 마스킹
# 2-1) 엔티티 구분용 - 튜플로
def solution(relation):
    col_n = len(relation[0])
    entity_n = len(relation)

    all = list(range(1, 1 << col_n))

    answer = []
    # 1) 전체 케이스
    for case in all:
        # 2) 유일성 검사
        tmp_set = set()
        for entity in relation:  # 한 엔티티의 해당 키조합
            tmp = []
            for i in range(col_n):
                if case & (1 << i):
                    tmp.append(entity[i])
            tmp = tuple(tmp)
            tmp_set.add(tmp)

        # 3) 최소성 검사
        if len(tmp_set) == entity_n:
            flag = True
            for num in answer:
                if (num & case) == num:
                    flag = False
                    break
            if flag:
                answer.append(case)

    return len(answer)

# 2-2) 엔티티 구분용 str
def solution(relation):
    answer = []
    col_n = len(relation[0])
    entity_n = len(relation)

    all = list(range(1, 1 << col_n))
    for case in all: #모든 조합에 대한 검사
            tmp_set = set()
            for e in range(entity_n): # entity 마다
                tmp = ""
                for col in range(col_n): # col 마다
                    if case & (1<<col):
                        tmp += str(relation[e][col]) # 엔티티 구분용

                tmp_set.add(tmp)

                if len(tmp_set) == entity_n:
                    answer.append(case)

    answer_set = set(answer)
    for i in answer:
        for j in range(i, len(answer)):
            if i & answer[j] == i:
                answer_set.discard(answer[j])

    return len(answer_set)


import random

import matplotlib.pyplot as plt

class Point2D:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Line2D:
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2

def check_turn_right(p1, p2, p3):
    vec1 = (p2.x - p1.x, p2.y - p1.y)
    vec2 = (p3.x - p2.x, p3.y - p2.y)
    return vec1[0] * vec2[1] < vec2[0] * vec1[1]

def slow_convex_hull(p):
    e = []
    for i in range(len(p)):
        for j in range(len(p)):
            if i != j:
                valid = True
                for k in range(len(p)):
                    if k != i and k != j:
                        if check_turn_right(p[i], p[j], p[k]):
                            valid = False
                            break
                if valid:
                    e.append(Line2D(p[i], p[j]))
    l = []
    for edge in e:
        l.append(edge.p1)
    l.append(e[-1].p2)
    return l

# 设置随机种子
random.seed(42)

# 生成随机点集
num_points = 30
points = [Point2D(random.uniform(0, 10), random.uniform(0, 10)) for _ in range(num_points)]

points[1].x = 0.26535969683863625+10
points[1].y = 1.988376506866485 + 2.0

points[8].x = 0.26535969683863625+5
points[8].y = 1.988376506866485 + 4.0

# 计算凸包
hull = slow_convex_hull(points)

# 提取点集的坐标
x = [point.x for point in points]
y = [point.y for point in points]

# 提取凸包的坐标
hull_x = [point.x for point in hull]
hull_y = [point.y for point in hull]

# 绘制点集和凸包
plt.scatter(x, y, color='blue', label='Points')
plt.plot(hull_x, hull_y, color='red', label='Convex Hull')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Points and Convex Hull')
plt.legend()
plt.show()