from logging import Logger

import numpy as np
import pandas as pd
from sqlalchemy import Table, select
from sqlalchemy.sql import Select

from src.pipeline.processing import get_unused_col_name, join_on_multiple_keys

def make_add_vessels_columns_query(
    vessel_ids: list,
    vessels_table: Table,
    vessels_columns_to_add: list = None,
    districts_table: Table = None,
    districts_columns_to_add: list = None,
) -> Select:
    """
    Creates a `sqlalchemy.select` statement representing a query to fetch the
    designated columns from the `vessels` and / or `districts` tables for the
    indicated `vessel_ids`.

    Args:
        vessel_ids (list): List of vessels `id` to fetch data for.
        vessels_table (Table): vessels table.
        vessels_columns_to_add (list, optional): List of columns to get from the
          `vessels` table. Defaults to None.
        districts_table (Table, optional): districts table. Must be supplied if
          `districts_columns_to_get` is given. Defaults to None.
        districts_columns_to_add (list, optional): List of columns to get from the
          `districts` table. Defaults to None.

    Returns:
        Select: select statement to execute to get the indicated data.
    """

    from_table = vessels_table

    if districts_columns_to_add:
        assert isinstance(districts_table, Table)
        from_table = from_table.join(
            districts_table,
            from_table.c.district_code == districts_table.c.district_code,
            isouter=True,
        )

    columns = [vessels_table.c.id.label("vessel_id")]

    if vessels_columns_to_add:
        columns += [vessels_table.c.get(col) for col in vessels_columns_to_add]

    if districts_columns_to_add:
        columns += [districts_table.c.get(col) for col in districts_columns_to_add]

    q = (
        select(*columns)
        .select_from(from_table)
        .where(vessels_table.c.id.in_(vessel_ids))
    )

    return q

def make_find_vessels_query(
    vessels: pd.DataFrame,
    vessels_table: Table,
) -> Select:
    """
    Creates a `sqlalchemy.select` object representing a query to find `vessels` in
    the `vessels` table that match any of the lines in the input `DataFrame` on any of
    `cfr`, `ircs` or `external_immatriculation`.

    Args:
        vessels (pd.DataFrame): `DataFrame`. Must have columns `cfr`, `ircs` and
          `external_immatriculation`. If any other columns are present they are
          ignored.
        vessels_table (Table): `sqlalchemy.Table` object representing the `vessels`
          table. Must have columns `cfr`, `ircs` and `external_immatriculation`. If any
          other columns are present they are ignored.

    Returns:
        Select: query object with columns `vessel_id`, `cfr`, `ircs` and
          `external_immatriculation`.
    """
    assert "cfr" in vessels
    assert "ircs" in vessels
    assert "external_immatriculation" in vessels

    q = select(
        vessels_table.c.id.label("vessel_id"),
        vessels_table.c.cfr,
        vessels_table.c.ircs,
        vessels_table.c.external_immatriculation,
    ).where(
        vessels_table.c.cfr.in_(vessels.cfr.dropna().drop_duplicates().to_list())
        | vessels_table.c.external_immatriculation.in_(
            vessels.external_immatriculation.dropna().drop_duplicates().to_list()
        )
        | vessels_table.c.ircs.in_(vessels.ircs.dropna().drop_duplicates().to_list())
    )

    return q

def merge_vessel_id(
    vessels: pd.DataFrame, found_vessels: pd.DataFrame, logger: Logger
) -> pd.DataFrame:
    """
    The two input DataFrames are assumed to be:

      - a list of vessels with `cfr`, `ircs` and `external_immatriculation` identifiers
        (plus potential other columns) without a `vessel_id` column
      - a list of vessels with `cfr`, `ircs` and `external_immatriculation` and
        `vessel_id` columns (and no other columns). Typically these are the vessels
        that are found in the `vessels` table that match one of the identifiers of the
        `vessels` DataFrame by the `make_find_vessels_query` query.

    The idea is to add the `vessel_id` from the second DataFrame as a new column in the
    first DataFrame, by matching the right lines in both DataFrame.

    This is done by perfoming a left join of the input DataFrames using
    join_on_multiple_keys on ["cfr", "ircs", "external_immatriculation"].

    Additionnally, the returned `vessel_id` for each line in the first DataFrame is
    `None` if the following conditions are not met :

      - there is no ambiguity: only one vessel in the second DataFrame can be matched
        to a given line in the first DataFrame
      - there is no conflict: at most one vessel in the first DataFrame can be matched
        to a given line in the second DataFrame

    Lines in the second DataFrame that do not match a line in the first DataFrame are
    absent from the result.

    Lines in the first DataFrame that do not match a line in the second DataFrame are
    present in the result with a `vessel_id` of `None`.

    The result always has exactly the same lines as the first input DataFrame.

    Args:
        vessels (pd.DataFrame): Vessels to match to a found_vessel
        found_vessels (pd.DataFrame): found_vessels to match to a vessel
        logger (Logger): Logger instance

    Returns:
        pd.DataFrame: Same as vessels with an added `vessel_id` column.
    """
    initial_length = len(vessels)
    vessels = vessels.copy(deep=True)

    # Number rows of input DataFrame
    input_id = get_unused_col_name("input_row_number", vessels)
    vessels[input_id] = range(len(vessels))

    # Join
    vessels = join_on_multiple_keys(
        vessels,
        found_vessels,
        or_join_keys=["cfr", "ircs", "external_immatriculation"],
        how="left",
        coalesce_common_columns=False,
    )

    vessels["is_ambiguous"] = vessels.duplicated(subset=input_id, keep=False)
    vessels["is_in_conflict"] = vessels.duplicated(subset="vessel_id", keep=False)

    if vessels.is_ambiguous.any():
        ambiguous_vessels = vessels.loc[
            vessels.is_ambiguous,
            [input_id, "cfr", "ircs", "external_immatriculation", "vessel_id"],
        ].sort_values(input_id)

        warning_message = (
            "The following identifiers are ambiguous as they could correspond to "
            "more than one vessel:\n" + str(ambiguous_vessels.to_markdown(index=False))
        )

        logger.warning(warning_message)

    if vessels.is_in_conflict.any():
        vessels_in_conflict = vessels.loc[
            vessels.is_in_conflict,
            [input_id, "cfr", "ircs", "external_immatriculation", "vessel_id"],
        ].sort_values("vessel_id")

        warning_message = (
            "The following identifiers conflict with one another - "
            "more than one match the same vessel:\n"
            + str(vessels_in_conflict.to_markdown(index=False))
        )

        logger.warning(warning_message)

    vessels = vessels.drop_duplicates(subset=input_id)

    assert len(vessels) == initial_length

    vessels["vessel_id"] = vessels.vessel_id.where(
        ~(vessels[["is_ambiguous", "is_in_conflict"]].any(axis=1)), np.nan
    )

    return (
        vessels.sort_values(input_id)
        .drop(columns=[input_id, "is_ambiguous", "is_in_conflict"])
        .reset_index(drop=True)
    )


"""
Retrieve and process data from PRB — https://www.prb.org/
"""

# Standard library imports
from pathlib import Path
import csv
import os
import sys

# Third-party library imports
import requests
from dataflows import (
    Flow,
    PackageWrapper,
    ResourceWrapper,
    add_field,
    delete_fields,
    dump_to_path,
    load,
    set_type,
    validate,
)

# Local imports
from scripts import settings

# Hack to send data to change_path function without using additional parameters
FILE_NAME = None

def change_path(package: PackageWrapper):
    """Change the path to nest data in the subdirectory data/."""
    package.pkg.descriptor["resources"][0]["path"] = f"data/{FILE_NAME}.csv"
    yield package.pkg
    res_iter = iter(package)
    try:
        first: ResourceWrapper = next(res_iter)
    except StopIteration:
        pass
    yield first.it
    yield from package

def clean_data(filename: str, location: str) -> None:
    """Clean and validate data with `dataflows`, creating data packages in the
    process, one for each file."""
    global FILE_NAME
    FILE_NAME = f"{location}-{filename}"
    clean_directory, _, processing_directory = set_location_dirs(location)
    exported_file = f"{clean_directory}/{filename}"
    _ = Flow(
        load(
            f"{processing_directory}/{filename}.csv",
            name=FILE_NAME,
        ),
        change_path,
        add_field("NameFIPS", "string"),
        concat_name_columns,
        delete_fields(["Name", "FIPS"]),
        set_type("Data", type="any"),
        validate(),
        dump_to_path(exported_file),
    ).process()[1]

def concat_name_columns(row: dict) -> None:
    """Merge content of columns 'FIPS' and 'Name' into new column 'NameFIPS'."""
    row["NameFIPS"] = f"{row['Name']} {row['FIPS']}"

def delete_empty_rows(filename: str, location: str) -> None:
    """Delete empty lines in file located at `filepath`."""
    _, _, processing_directory = set_location_dirs(location)
    filepath = f"{processing_directory}/{filename}.csv"
    temp_file = f"{settings.DIRS['temp_dir']}/{filename}.csv"
    try:
        with open(filepath) as in_file, open(temp_file, "w") as out_file:
            writer = csv.writer(out_file)
            for row in csv.reader(in_file):
                if row:
                    writer.writerow(row)
        os.replace(temp_file, filepath)
    except FileNotFoundError:
        print(f"[Deleting empty lines] '{filepath}' not found. Next.")

def download_data(filename: str, url_bit: str) -> None:
    """Download given file from `url_bit` based on the required `filename`."""
    url = f"https://datacenter.prb.org/download/{url_bit}/indicator/{filename}/csv/"
    save_file = f"{filename}.csv"
    if url_bit == settings.DATA_US["url_bit"]:
        save_dir = f"{settings.DIRS['original_data_us']}/"
    else:
        save_dir = f"{settings.DIRS['original_data_inter']}/"
    save_path = f"{save_dir}{save_file}"

    if not os.path.isfile(save_path):
        request = requests.get(url, allow_redirects=True)
        with open(save_path, "wb") as data_file:
            data_file.write(request.content)

        # delete the file if the retrieved dataset doesn't exist
        # (file size < 1000 bytes)
        if Path(save_path).stat().st_size < 1000:
            # printed on the same line containing the dataset name
            print("DATA NOT FOUND.\n")
            os.remove(save_path)

def execute_on_all(location: str, func) -> None:
    """Take a `location` and do `func` on all items in that location."""
    func_arg = location
    if func is download_data:
        if location_is_us(location):
            func_arg = settings.DATA_US["url_bit"]
        else:
            func_arg = settings.DATA_INTER["url_bit"]

    if func is trim_file_header:
        if location_is_us(location):
            if folder_is_empty("original_data_us"):
                print("[Removing US header] No dataset found. Exiting.")
                sys.exit(0)
        else:
            if folder_is_empty("original_data_inter"):
                print(
                    "[Removing international header] No dataset found. Exiting."
                )
                sys.exit(0)

    if func is delete_empty_rows:
        if location_is_us(location):
            if folder_is_empty("processed_data_us"):
                print(
                    "[Deleting empty lines for US] No dataset found. Exiting."
                )
                sys.exit(0)
        else:
            if folder_is_empty("processed_data_inter"):
                print(
                    "[Deleting empty lines for international] No dataset found. Exiting."
                )
                sys.exit(0)

    for item in settings.LOCATIONS[location]:
        func(item, func_arg)

def folder_is_empty(folder_name: str) -> bool:
    """Return bool as to whether `folder_name` is empty."""
    return os.listdir(settings.DIRS[folder_name]) == []

def location_is_us(source_name: str) -> bool:
    """Return bool as to whether `source_name` equals "us" or not."""
    return source_name == "us"

def set_location_dirs(location: str) -> tuple:
    """Return a tuple containing directory settings for a given `location`."""
    if location_is_us(location):
        clean_directory = settings.DIRS["clean_data_us"]
        original_directory = settings.DIRS["original_data_us"]
        processing_directory = settings.DIRS["processed_data_us"]
    else:
        clean_directory = settings.DIRS["clean_data_inter"]
        original_directory = settings.DIRS["original_data_inter"]
        processing_directory = settings.DIRS["processed_data_inter"]
    return (clean_directory, original_directory, processing_directory)

def trim_file_header(filename: str, location: str, lines: int = 4) -> None:
    """Remove the first `lines` in `filename` for `location`."""
    (_, original_directory, processing_directory,) = set_location_dirs(
        location
    )
    try:
        trimmed = False
        with open(f"{original_directory}/{filename}.csv") as input_file, open(
                f"{settings.DIRS['temp_dir']}/{filename}.csv", "w"
        ) as output_file:
            first_line = input_file.readline()
            if not first_line.startswith("FIPS"):  # file needs to be trimmed
                input_file.seek(0)  # reset position to first line
                for _ in range(lines):
                    next(input_file)  # discard header lines
                for line in input_file:  # save remaining lines
                    output_file.write(line)
                trimmed = True
        if trimmed:  # only replace if the file has been trimmed
            os.replace(
                f"{settings.DIRS['temp_dir']}/{filename}.csv",
                f"{processing_directory}/{filename}.csv",
            )
        else:
            print(f"No header found in {filename}.csv.")
    except FileNotFoundError:
        print(f"[Removing header] '{filename}.csv' not found. Next.")

def run() -> None:
    """Retrieve and clean everything in one go."""
    for location in settings.LOCATIONS:
        execute_on_all(location, download_data)
        execute_on_all(location, trim_file_header)
        execute_on_all(location, delete_empty_rows)
        execute_on_all(location, clean_data)

if __name__ == "__main__":
    run()


from dataclasses import dataclass, field

from dotmap import DotMap
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QDialog, QWidget, QFormLayout, QLabel, QDialogButtonBox

from gui.config_editor_widgets import MultiDropdownWidget, SingleDropdownWidget
from gui.config_editor_widgets import MultilineInputWidget, SingleLineInputWidget

@dataclass
class ConfigSetting:
    display_name: str
    object_name: str
    tooltip: str = ""
    has_multi_value: bool = False
    options: list[str] = field(default_factory=list)
    widget: object = None

class ProjectConfigDialog(QDialog):
    config_settings = [
        ConfigSetting(display_name="Moodle username:", object_name="username"),
        ConfigSetting(display_name="Moodle password:", object_name="password"),
        ConfigSetting(display_name="Moodle server URL:", object_name="server_url"),
        ConfigSetting(display_name="Moodle service:", object_name="moodle_service"),
        ConfigSetting(
            display_name="Moodle course shortname:", object_name="course_shortname"
        ),
        ConfigSetting(
            display_name="Assignment regexes:",
            object_name="assignment_regexes",
            tooltip="Info :: pattern",
            has_multi_value=True,
            options="assignment_regexes_presets",
        ),
        ConfigSetting(
            display_name="Username conversions:",
            object_name="username_conversions",
            tooltip="Info :: from-pattern :: to-pattern",
            has_multi_value=True,
            options="username_conversions_presets",
        ),
        ConfigSetting(
            display_name="Assignment conversions:",
            object_name="assignment_conversions",
            tooltip="Info :: from-pattern :: to-pattern",
            has_multi_value=True,
            options="assignment_conversions_presets",
        ),
        ConfigSetting(
            display_name="Archive directories:",
            object_name="archive_dirs",
            has_multi_value=True,
        ),
        ConfigSetting(display_name="Basecode directory:", object_name="template_dir"),
        ConfigSetting(
            display_name="Language parser:",
            object_name="language",
            has_multi_value=False,
            options="language_presets",
        ),
        ConfigSetting(display_name="Min sim to report:", object_name="min_sim"),
        ConfigSetting(display_name="Java path:", object_name="java_path"),
    ]

    def _makeWidget(self, setting):
        multivalue = setting.has_multi_value
        options = setting.options
        obj = setting.object_name
        hint = setting.tooltip
        opts = [] if not setting.options else self._config[setting.options]

        if multivalue and options:  # a group of combo boxes
            return MultiDropdownWidget(object_name=obj, tooltip=hint, options=opts)
        elif not multivalue and options:  # a single combo box
            return SingleDropdownWidget(object_name=obj, tooltip=hint, options=opts)
        elif multivalue:  # a group of single-line inputs
            return MultilineInputWidget(object_name=obj, tooltip=hint)
        else:  # a single-line input box
            return SingleLineInputWidget(object_name=obj, tooltip=hint)

    def __init__(self, parent: QWidget, config: DotMap) -> None:
        super().__init__(parent)

        self._config = config
        self.setWindowTitle("Project Settings")
        self.form_layout = QFormLayout(verticalSpacing=15)

        for setting in self.config_settings:
            label = QLabel(setting.display_name)
            setting.widget = self._makeWidget(setting)
            self.form_layout.addRow(label, setting.widget)

        buttons = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self
        )
        self.form_layout.addRow(buttons)

        buttons.accepted.connect(self.accept)
        buttons.rejected.connect(self.reject)

        self.setLayout(self.form_layout)
        self._loadConfigValues()

    def _loadConfigValues(self) -> None:
        for widget, value in [(s.widget, s.object_name) for s in self.config_settings]:
            widget.display_value(self._config[value])

    def _saveConfigValues(self) -> None:
        for widget in [s.widget for s in self.config_settings]:
            self._config[widget.objectName()] = widget.get_value()

    @staticmethod
    def show(parent, config) -> tuple[bool, DotMap]:
        dialog = ProjectConfigDialog(parent, config)
        result = dialog.exec()
        if result == QDialog.Accepted:
            dialog._saveConfigValues()
        return result == QDialog.Accepted, dialog._config


import os
import json
import httpx
from babel.dates import format_timedelta
import dateparser
from flask import Flask, jsonify, request, send_file, render_template, Response

import google.auth
from googleapiclient.discovery import build

app = Flask(__name__, static_url_path='/static', static_folder='static')
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

_, project = google.auth.default()
region = os.environ.get("REGION", "us-central1")

def run_report():
    run = build("run", "v1")
    services = (
        run.projects()
        .locations()
        .services()
        .list(parent=f"projects/{project}/locations/{region}")
        .execute()
    )

    cb = build("cloudbuild", "v1")

    results = {}
    for srv in services["items"]:
        name = srv["metadata"]["name"]
        url = srv["status"]["url"]

        trigger = None; commit_id = None; finishTime = None; success = None

        if name == "admin":
            continue

        trigger_id = get_trigger_id(cb, name)
        if trigger_id:
            last_build = latest_build(cb, trigger_id)
            if last_build:
                commit_id = last_build["substitutions"]["SHORT_SHA"]
                finishTime = last_build["finishTime"]
                success = last_build["status"] == "SUCCESS"

        results[name] = {"commit_id": commit_id,
                        "finishTime": finishTime,
                         "url": url,
                         "success": success}
    return results

def get_trigger_id(cb, name):
    triggers = cb.projects().triggers().list(projectId=project).execute()
    for t in triggers["triggers"]:
        if "substitutions" not in t.keys():
            continue
        if "_SERVICE" not in t["substitutions"].keys():
            continue
        if t["substitutions"]["_SERVICE"] == name:
            return t["id"]
    return None

def latest_build(cb, trigger_id):
    kwargs = {
                "projectId": project,
                "filter": f'trigger_id="{trigger_id}"',
                "pageSize": 1
            }
    # presumes order
    builds = cb.projects().builds().list(**kwargs).execute()
    if builds:
        return builds["builds"][0]
    else:
        return None

@app.route("/api")
def report():
    return jsonify(run_report())

def relative_time(dstr):
    if not dstr:
        return "(no data)"
    delta = dateparser.parse("now Z") - dateparser.parse(dstr, settings={"TIMEZONE": "Z"})
    return f"{format_timedelta(delta)} ago"

@app.route("/status")
def status():
    data = run_report()

    # converting dict to list, for table formatting ease
    resp = []
    for key in data.keys():
        state = data[key]
        state["name"] = key
        state["relativeFinishTime"] = relative_time(state["finishTime"])
        resp.append(state)
    return render_template("index.html", data=resp)

@app.route("/status/<service>.svg")
def status_image(service):
    cb = build("cloudbuild", "v1")
    commit_id = "unknown"; success_color = "inactive"
    trigger_id = get_trigger_id(cb, service)
    if trigger_id:
        result = latest_build(cb, trigger_id)
        if result:
            if result["status"] == "SUCCESS":
                success_color = "success"
            else:
                success_color = "critical"
            if "substitutions" in result.keys() and "SHORT_SHA" in result["substitutions"].keys():
                commit_id = result["substitutions"]["SHORT_SHA"]
            else:
                commit_id = "unknown"

    shield_label = "Last sample deployed at commit".replace(" ", "%20")
    shield_url = (f"https://img.shields.io/badge/{shield_label}-{commit_id}-{success_color}"
                   "?style=flat-square&logo=google-cloud&logoColor=white")
    resp = httpx.get(shield_url)
    return Response(resp.content, mimetype="image/svg+xml")

@app.route("/")
def home():
    return """

    """

if __name__ == "__main__":
    app.run(host="localhost", port=8080, debug=True)


import argparse

def parse():
    """
	adds and parses arguments / hyperparameters
	"""
    p = argparse.ArgumentParser(description='TensorHyperGNNs', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    p.add_argument('--data_type', type=str, default='new', help='data type (coauthorship/cotation/3dObject/new)')
    p.add_argument('--dataset', type=str, default='House', help='dataset name (e.g.: cora/dblp for coauthorship, cora/citeseer/pubmed for cocitation, ModelNet40/NTU for 3dObject, House/Walmart for new)')
    p.add_argument('--hyperG_norm', type=bool, default=False, help='whether normalize hypergraph adjacency tensor')
    p.add_argument('--model', type=str, default='T-MPHN', help='T-HyperGNN models(T-Spectral, T-Spatial, T-MPHN)')
    p.add_argument('--self_loop', type=bool, default=False, help='whether add self loop to hypergraph')
    p.add_argument('--num_layers', type=int, default=1, help='number of HyperGNN layers')
    p.add_argument('--hid_dim', type=int, default=256, help='the dimension of embeddings at the hidden layer')
    p.add_argument('--dropout', type=float, default=0.3, help='dropout probability after each HyperGNN layer')
    p.add_argument('--layernorm', type=bool, default=True, help='whether use layer normalization')
    p.add_argument('--batchnorm', type=bool, default=True, help='whether use batch normalization')
    p.add_argument('--lr', type=float, default=0.001, help='learning rate')
    p.add_argument('--wd', type=float, default=0.0005, help='weight decay of learning rate')
    p.add_argument('--train_ratio', type=float, default=0.5, help='ratio of training data')
    p.add_argument('--valid_ratio', type=float, default=0.25, help='ratio of validation data')
    p.add_argument('--epochs', type=int, default=100, help='number of epochs to train')
    p.add_argument('--num_exps', type=int, default=2, help='number of repeated experiments')
    p.add_argument('--cuda', type=int, default=0, help='cuda id to use')
    p.add_argument('--seed', type=int, default=42, help='seed for randomness')
    p.add_argument('--early_stopping', type=bool, default=True, help='early stopping after convergence')
    p.add_argument('--combine', type=str, default='concat', help='the combine operation in T-MPHN (e.g., concat, sum))')
    p.add_argument('--M', type=int, default=5, help='the maximum cardinality of the hypergraph')
    p.add_argument('--Mlst', type=list, default=[3], help='the maximum cardinality of the hypergraph at each layer, max(Mlst) = M')
    # p.add_argument('-f') # for jupyter default
    return p.parse_args()


from itertools import product
import sys
input = sys.stdin.readline

# def cal_num(button, num):
#     if num not in button:
#         return [str(num)]
#     dif = 10
#     ret = []
#     for i in range(11):
#         if i not in button and abs(num - i) < dif:
#             dif = num - i
#             ret = [str(i)]
#         elif i not in button and abs(num - i) == dif:
#             ret.append(str(i))
#     return ret

# def dfs(ls, temp, cnt, n):
#     if cnt == n:
#         ans.append(temp)
#         return
#     for i in ls[cnt]:
#         temp += i
#         dfs(ls, temp, cnt+1, n)
#         temp = temp[:-1]

# ans = []
# N = input().rstrip()
# M = int(input().rstrip())
# if M == 0:
#     print(len(N))
# else:
#     button = list(map(int, input().rstrip().split()))

#     ls = []
#     for i in range(len(N)):
#         ls.append(cal_num(button, int(N[i])))
#     dfs(ls, "", 0, len(N))
#     print(ans)
#     diff = 1000000
#     diff_100 = 1000000
#     for answer in ans:
#         if abs(int(answer) - int(N)) < diff:
#             diff = abs(int(answer) - int(N))
#         if abs(int(N) - 100) < diff_100:
#             diff_100 = abs(int(N) - 100)
#     print(min(diff + len(N), diff_100))
bt_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
N = input().rstrip()
M = int(input().rstrip())
if M == 0:
    print(min(len(N), abs(int(N) - 100)))
elif M == 10:
    print(abs(int(N) - 100))
else:
    button = list(input().rstrip().split())
    bt_list = [x for x in bt_list if x not in button]
    bf = list(product(bt_list, repeat = len(N)))
    bf += list(product(bt_list, repeat = len(N)-1))
    bf += list(product(bt_list, repeat = len(N)+1))
    diff_100 = abs(int(N) - 100)
    diff = 1000000
    ans = 0
    for ks in bf:
        if len(ks) > 1 and diff > abs(int(''.join(ks)) - int(N)) + len(ks):
            diff = abs(int(''.join(ks)) - int(N)) + len(ks)
            if ks[0] == '0':
                diff -= 1
        elif len(ks) == 1 and diff > abs(int(ks[0]) - int(N)) + len(ks):
            diff = abs(int(ks[0]) - int(N)) + len(ks)
    print(min(diff, diff_100))

# 문제 접근 방법
# # 각 자리마다 가장 가까운 숫자를 선택해서 조합한 다음 계산하려 함
# # 자리수가 다를 때의 방법을 생각할 수 없었음
# # 최대 10^6이기 때문에 중복 순열로 모든 경우를 살펴봐도 시간 내에 가능하여 완탐으로 변경
# # product를 사용하여 자리수-1, 자리수, 자리수+1 모든 경우를 살펴봄
# # 한자리 수인 경우, 고장이 안난 경우, 전부 고장난 경우에 대해서는 예외 처리를 해줌


from gui.filtering_window import FilteringWindow
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout, QPushButton, QApplication, QFrame
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QFont
import sys
import os

class StartWindow(QWidget):
    """
    Starting window that welcomes user and opens the filtering window
    """

    def __init__(self):
        super().__init__()

        # Create filtering window
        self.filter_window = FilteringWindow()
        self.setWindowTitle("ClassMate")
        self.setStyleSheet("background-color: azure;")  # previous color: darkcyan
        layout = QHBoxLayout()

        layout_v = QVBoxLayout()
        lbl = QLabel('Welcome to Classmate')
        lbl.setFont(QFont('Bodoni MT', 55))
        lbl.setAlignment(Qt.AlignHCenter | Qt.AlignBottom)

        button1 = QPushButton("Get Started")
        button1.setStyleSheet("background-color: dodgerblue; color: white; font-weight: bold; ")
        button1.setFont(QFont('Bodoni MT', 15))  # set font family and size
        button1.clicked.connect(self.toggle_filter_window)
        layout_v.addWidget(lbl)
        layout_v.addWidget(button1, alignment=Qt.AlignTop)

        pic = QLabel()
        image = QPixmap(os.path.join(os.pardir, 'Ucla_Pic.jpg'))
        pixmap_resized = image.scaled(512, 512, Qt.KeepAspectRatio)
        pic.setPixmap(pixmap_resized)

        layout.addLayout(layout_v)
        layout.addWidget(pic, alignment=Qt.AlignHCenter)

        self.setLayout(layout)
        self.show()
        self.showFullScreen()

    def toggle_filter_window(self) -> None:
        """
        Toggles filtering window if it is not visible
        :return: None
        """
        if self.filter_window.isVisible():
            self.filter_window.hide()
        else:
            self.filter_window.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = StartWindow()
    w.show()
    app.exec()


import os
import json
import click
from flask import current_app
from flask.cli import with_appcontext

@click.command("init-settings")
@with_appcontext
def init_settings_command():
    init_settings()
    click.echo("Initialized Settings")

def init_settings():
    settings = dict(
        receipt_image="images/logo.jpeg"
    )
    save_settings(settings)

def save_settings(settings):
    settings_path = os.path.join(current_app.instance_path, "settings.json")
    with open(settings_path, "w+") as settings_file:
        json.dump(settings, settings_file, indent=4)

def get_settings():
    settings_path = os.path.join(current_app.instance_path, "settings.json")
    with open(settings_path, "r") as settings_file:
        settings = json.load(settings_file)

    return settings


import asyncio

import looptime

class MyEventLoop(looptime.LoopTimeEventLoop, asyncio.SelectorEventLoop):
    pass

def test_initial_time_is_zero_by_default():
    loop = MyEventLoop()
    assert loop.time() == 0

def test_initial_time_is_customized():
    loop = MyEventLoop(start=123)
    assert loop.time() == 123

def test_double_init_patches_once():
    original_loop = asyncio.SelectorEventLoop()
    original_select = original_loop._selector.select

    looptime_loop = looptime.patch_event_loop(original_loop)
    patched_select_1 = looptime_loop._selector.select
    assert patched_select_1 is not original_select

    looptime_loop.setup_looptime()
    patched_select_2 = looptime_loop._selector.select
    assert patched_select_2 is not original_select
    assert patched_select_2 is patched_select_1


from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Min

from home.models import Favorite, PinnedProduct

from generic.constants import (ACTIVITY_POINT,MAX_TRENDING_SPACE,
	MIN_RATE_FOR_SPACE_TRENDING)
from generic.crypto import hashing_into_int

from space.models import Product, ProductReact, Space, Status

def handle_pin(user, uid, req):
	req = req.strip()
	req = req.upper()
	if req =='ADD' or req == 'REMOVE':
		try:
			product = Product.objects.get(uid = uid)
			space = Space.objects.get(id=product.space_id)
			if space.owner_id == user.id:
				return False
			status = Status.objects.get(space_id = product.space_id)

			if req == 'ADD':
				try:
					pin = PinnedProduct.objects.get(user_id=user.id, product_id=product.uid)
					return False
				except ObjectDoesNotExist as e:
					pin = PinnedProduct(user_id=user.id, product_id=product.uid)
					pin.save()

					status.total_pinned += 1
					status.rating = _addition(status.rating, ACTIVITY_POINT['PIN'])
					status.save()

					return True
			else:
				try:
					pin = PinnedProduct.objects.get(user_id=user.id, product_id=product.uid)
					pin.delete()

					status.total_pinned -= 1
					status.rating = _addition(status.rating, -ACTIVITY_POINT['PIN'])
					status.save()

					return True

				except ObjectDoesNotExist as e:
					pass

		except ObjectDoesNotExist as e:
			pass
	return False

def handle_react(user, uid, what):
	_REACTS = ['GOOD', 'BAD', 'FAKE', 'NONE']

	what = what.strip()
	what = what.upper()

	if  what in _REACTS:
		try:
			product = Product.objects.get(uid=uid)
			space = Space.objects.get(id=product.space_id)
			if space.owner_id == user.id:
				return None

			status = Status.objects.get(space_id=product.space_id)

			react_obj = None

			try:
				react_obj = ProductReact.objects.get(product_id=product.uid, user_id=user.id)
			except ObjectDoesNotExist as e:
				react_obj = None

			if react_obj is None:
				if what != 'NONE':
					if what == 'GOOD':
						_increase_good_react(product, status)
						new_react_obj = ProductReact(user_id=user.id, product_id=product.uid, react='G')

					elif what == 'BAD':
						_increase_bad_react(product, status)
						new_react_obj = ProductReact(user_id=user.id, product_id=product.uid, react='B')

					elif what == 'FAKE':
						_increase_fake_react(product, status)
						new_react_obj = ProductReact(user_id=user.id, product_id=product.uid, react='F')

					new_react_obj.save()

			else:
				_decrease_current_react(react_obj, product, status)
				if what != 'NONE':
					if what == 'GOOD':
						_increase_good_react(product, status)
						react_obj.react = 'G'
					elif what == 'BAD':
						_increase_bad_react(product, status)
						react_obj.react = 'B'

					elif what == 'FAKE':
						_increase_fake_react(product, status)
						react_obj.react = 'F'

					react_obj.save()

				else:
					react_obj.delete()

			product.save()
			status.save()

			return product

		except ObjectDoesNotExist as e:
			pass
	return None

def handle_favorite(user, name, req):

	if not user.is_authenticated:
		return False
	try:
		space = Space.objects.get(code=hashing_into_int(name))
		if space.owner_id == user.id:
			return False

		req = req.strip()
		req = req.upper()

		if req == 'ADD' or req == 'REMOVE':
			try:
				row = Favorite.objects.get(user_id=user.id, space_id=space.id)
			except ObjectDoesNotExist as e:
				row = None

			status = Status.objects.get(space_id=space.id)

			if req == 'ADD':
				if row is None:
					row = Favorite(user_id=user.id, space_id=space.id)
					row.save()

					status.total_favorite += 1
					status.rating = _addition(status.rating, ACTIVITY_POINT['FAVORITE'])
					status.save()

					return True

			else:
				if row is not None:
					row.delete()

					status.total_favorite -= 1
					status.rating = _addition(status.rating, -ACTIVITY_POINT['FAVORITE'])
					status.save()

					return True

	except ObjectDoesNotExist as e:
		pass
	return False

def _decrease_current_react(react_obj, product, status):
	if react_obj.react == 'G':
		_decrease_good_react(product, status)

	elif react_obj.react == 'B':
		_decrease_bad_react(product, status)

	else:
		_decrease_fake_react(product, status)

def _addition(value1, value2):
	if value1 + value2 >= 0:
		return value1 + value2
	return value1

def _increase_good_react(product, status):
	product.react_good += 1
	status.total_good_react += 1
	status.rating = _addition(status.rating, ACTIVITY_POINT['GOOD'])

def _increase_bad_react(product, status):
	product.react_bad += 1
	status.total_bad_react += 1
	status.rating = _addition(status.rating, ACTIVITY_POINT['BAD'])

def _increase_fake_react(product, status):
	product.react_fake += 1
	status.total_fake_react += 1
	status.rating = _addition(status.rating, ACTIVITY_POINT['FAKE'])

def _decrease_good_react(product, status):
	product.react_good -= 1
	status.total_good_react -= 1
	status.rating = _addition(status.rating, -ACTIVITY_POINT['GOOD'])

def _decrease_bad_react(product, status):
	product.react_bad -= 1
	status.total_bad_react -= 1
	status.rating = _addition(status.rating, ACTIVITY_POINT['BAD'])

def _decrease_fake_react(product, status):
	product.react_fake -= 1
	status.total_fake_react -= 1
	status.rating = _addition(status.rating, ACTIVITY_POINT['FAKE'])


import sys
a = int(sys.stdin.readline())
def factorization(x):
    d = 2
    arr = []
    while d <= x:
        if x % d == 0:
            arr.append(d)
            x = x / d
        else:
            d = d + 1
    return arr
for i in factorization(a):
    print(i)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
print(tf.__version__)

(X_train, y_train), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

# type(tf_data)

# print(tf_data)

# type(X_train)

# X_train.shape

# y_train.shape

# X_test.shape

# y_test.shape

f, ax = plt.subplots()
ax.imshow(X_train[1])

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
    ])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
model.summary()

model.fit(X_train, y_train, epochs=5)

loss, acc = model.evaluate(X_test, y_test, verbose=2)
model.predict(X_test)

norm = layers.Normalization(input_shape=[1,], axis=None)
norm.adapt(X_train)
model = keras.Sequential([
      norm,
      layers.Dense(units=1)
])
# model.summary()
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.1), loss='mse')
hist = model.fit(X_train, y_train, epochs=100, verbose=0)

f, ax = plt.subplots()
ax.scatter(X, y)
ax.plot(np.linspace(-0.1, 0.15, 20), model.predict(np.linspace(-0.1, 0.15, 20)))

norm = layers.Normalization(input_shape=[1,], axis=None)
norm.adapt(X_train)
model = keras.Sequential([
      norm,
      layers.Dense(units=64, activation='relu'),
      layers.Dense(units=64, activation='relu'),
      layers.Dense(units=1)
])
# model.summary()
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.1), loss='mse')
hist = model.fit(X_train, y_train, epochs=1000, verbose=0)

f, ax = plt.subplots()
ax.scatter(X, y)
ax.plot(np.linspace(-0.1, 0.15, 20), model.predict(np.linspace(-0.1, 0.15, 20)), c='red')

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import google

pd_data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data',header=None,sep='\t', skipinitialspace=True)
data = []
for i in pd_data[0]:
    data.append(i.strip().split())
# print(data)
pd_data2 = pd.DataFrame(data).dropna()
print(pd_data2.isna().sum())
print(pd_data2)

X_train, X_test, y_train, y_test = train_test_split(pd_data2[0].astype('float64'), pd_data2[7].astype('float64'))
norm = layers.Normalization(input_shape=[1,], axis=None)
norm.adapt(X_train)
model = keras.Sequential([
    norm,
    layers.Dense(units=64, activation='relu'),
    layers.Dense(units=32, activation='relu'),
    layers.Dense(3)
])
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.1), loss='mse')
hist = model.fit(X_train, y_train, epochs=1000, verbose=0)

f, ax = plt.subplots()
ax.plot(hist.history['loss'])

f, ax = plt.subplots()
ax.scatter(X_test, y_test)
ax.plot(X_test, model.predict(X_test))


import playsound
import pyautogui as pt
import keyboard
from time import sleep
import os
# import pyttsx3 as tts
from gtts import gTTS

def speak(text):
    tts = gTTS(text=text, lang='en-in')
    filename = "voices.mp3"
    tts.save(filename)
    playsound.playsound(filename)
    os.remove(filename)

def Spotify(name):
    speak("Playing it sir")
    os.startfile("C:\\Users\\surya\\AppData\\Roaming\\Spotify\\Spotify.exe")
    sleep(7)
    pt.click(x=82, y=123)
    sleep(1)
    pt.click(x=595, y=36)
    keyboard.write(name)
    sleep(4)
    pt.click(x=867, y=449)
    pt.click(x=867, y=449)


import os, sys
import postTomo as pt

from skimage import io
from scipy import ndimage

__author__ = 'Vitaliy Starchenko'

class CorrectCapillaryTiltXZ(pt.AbstractBaseTool):
    '''
    This tool corrects the tilt of the capillary in the direction defined
    by axis.
    '''

    def __init__(self):
        self.__toolName__ = 'correctTilt'

        # an array of parameters for current generator
        self.parameters = []

        # the format for parameters: [name, initial_value, description
        self.parameters.append(
            ['inputDir', 'fullDomain', 'directory with initial files']
        )
        self.parameters.append(
            ['outputDir', 'fullDomain_corTilt', 'final directory with files '
                                             'with corrected tilt']
        )
        self.parameters.append(
            ['rotAxis', 'y', 'the axis of rotation: x, y, z']
        )
        self.parameters.append(
            ['angle', '0.5', 'the angle to correct the tilt']
        )

    def execute(self, dictFileName):
        print('Starting {0:s} tool'.format(self.__toolName__))

        lines = self.read_dict(dictFileName)
        empty, inputDir, description = self.check_a_parameter('inputDir', lines)
        empty, outputDir, description = self.check_a_parameter('outputDir', lines)
        empty, axis, description = self.check_a_parameter('rotAxis', lines)
        empty, angle, description = self.check_a_parameter('angle', lines)

        # image orientation as usual ZYX
        if axis == "x":
            rotationAxis = (0, 1)
        elif axis == "y":
            rotationAxis = (0, 2)
        elif axis == "z":
            rotationAxis = (1, 2)
        else:
            print("Wrong value of rotational axis: {}\n"
                  "Please check the dictionary file.")
            exit(2)

        angle = float(angle)
        os.makedirs(outputDir)

        tif_files = sorted([f for f in os.listdir(inputDir)
                            if (os.path.isfile(
                os.path.join(inputDir, f)) and ".tif" in f)])

        for j, filename in enumerate(tif_files):
            print('\n*********************************************')
            print('  Processing file {}'.format(filename))
            print('*********************************************')

            filen = os.path.join(inputDir, filename)
            stack_tif = io.imread(filen, plugin='tifffile')

            stack_tif = ndimage.interpolation.rotate(stack_tif, angle,
                            axes=rotationAxis, reshape=False, output=None, order=3,
                            mode='constant', cval=0.0, prefilter=True)

            resultFilename = os.path.splitext(filename)[0]
            savef = os.path.join(outputDir, '{}_corTlt.tif'.
                                 format(resultFilename))
            io.imsave(savef, stack_tif, plugin='tifffile')

        return True


from bson import ObjectId

from src.model import Nypd
from ..contracts import nypd_contract as c

_NYPD_DOCUMENT_BINDINGS = {
    'id': c.ID,
    'date': c.DATE,
    'time': c.TIME,
    'precinct': c.PRECINCT,
    'offense_code': c.OFFENSE_CODE,
    'offense_description': c.OFFENSE_DESCRIPTION,
    'crime_outcome': c.CRIME_OUTCOME,
    'level_offense': c.LEVEL_OFFENSE,
    'borough': c.BOROUGH,
    'latitude': c.LATITUDE,
    'longitude': c.LONGITUDE,
    'age': c.AGE,
    'race': c.RACE,
    'sex': c.SEX
}

_UNSAFE_NYPD_FIELDS = [c.ID]

def nypd_from_document(doc: dict) -> Nypd:
    if doc is None:
        doc = {}

    def get_prop(k):
        if k == c.ID:
            return str(doc[c.ID]) \
                if c.ID in doc else None
        return doc.get(k)

    return Nypd(**{model_property: get_prop(doc_field)
                     for model_property, doc_field in _NYPD_DOCUMENT_BINDINGS.items()})

def nypd_to_document(nypd: Nypd, strip_unsafe=True) -> dict:
    doc = {doc_field: getattr(nypd, model_property)
           for model_property, doc_field in _NYPD_DOCUMENT_BINDINGS.items()
           if doc_field not in _UNSAFE_NYPD_FIELDS}

    if not strip_unsafe:
        doc[c.ID] = ObjectId(nypd.id)

    return doc


#!/usr/bin/env python

import numpy
import matplotlib.pyplot as plt

eV_per_Ha = 27.211396641

file = open('energies.txt', 'r')
data = file.readlines()
file.close()

T1s = []
S1s = []

# Compute T1 and S1 relative energies
for i in range(1,len(data)):
   t = data[i].split()
   abs_S0 = float(t[1])
   abs_T1 = float(t[2])
   abs_S1 = float(t[3])
   T1s.append( (abs_T1 - abs_S0)*eV_per_Ha )
   S1s.append( (abs_S1 - abs_S0)*eV_per_Ha )

T1s = numpy.asarray(T1s)
T1_average = numpy.mean(T1s)
T1_SD = numpy.std(T1s)
S1s = numpy.asarray(S1s)
S1_average = numpy.mean(S1s)
S1_SD = numpy.std(S1s)

# Calculate and plot T1 histogram
n, bins, patches = plt.hist(T1s, 20, facecolor='red')
plt.title('T1 energy distribution over G09 functional')
plt.xlabel('T1 relative energy (eV)')
plt.ylabel('Number')
plt.text(1.1, 50, 'E = %6.3f $\pm$ %6.3f eV' % (T1_average, T1_SD))
plt.savefig('T1s.png')
plt.close()

# Calculate and plot S1 histogram
n, bins, patches = plt.hist(S1s, 20, facecolor='blue')
plt.title('S1 energy distribution over G09 functional')
plt.xlabel('S1 relative energy (eV)')
plt.ylabel('Number')
plt.text(2.0, 50, 'E = %6.3f $\pm$ %6.3f eV' % (S1_average, S1_SD))
plt.savefig('S1s.png')


import os, sys
import matplotlib

import matplotlib.pyplot as plt
RESULTS_DIR = '../../../bin/execution_times/'
TABLES_DIR = './'

matplotlib.use("pgf")
matplotlib.rcParams.update({
    "pgf.texsystem": "pdflatex",
    'font.family': 'serif',
    'font.size' : 18,
    'text.usetex': True,
    'pgf.rcfonts': False,
})

def read_times(filename):
    sum_s = 0
    sum_b = 0
    n = 0
    with open(filename) as f:
        for r in f:
            s, b = map(float, r.split())
            sum_s += s
            sum_b += b
            n += 1

    return sum_s/n, sum_b/n

def plot_times(sizes, solution, baseline):

    plt.title("Execution time with different input sizes")

    fig, ax1 = plt.subplots()
    ax1.set_xlabel('Input size (number of tweets)')

    color = 'darkslategrey'
    ax1.set_ylabel('Basic A-Priori time (s)', color=color)
    ax1.plot(sizes, baseline, color=color, marker='o')
    ax1.tick_params(axis='y', labelcolor=color)

    ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

    color = 'midnightblue'
    ax2.set_ylabel('Efficient A-Priori time (s)', color=color)  # we already handled the x-label with ax1
    ax2.set_ylim([0, 5.9])
    ax2.plot(sizes, solution, color=color, marker='X')
    ax2.tick_params(axis='y', labelcolor=color)

    fig.tight_layout()  # otherwise the right y-label is slightly clipped
    plt.savefig(f"{TABLES_DIR}times.pgf")
    # plt.plot(sizes, solution)
    # plt.plot(sizes, baseline)

if __name__ == '__main__':
    base = ((sys.argv[0][:sys.argv[0].rfind('/') + 1]).rstrip('/') or '.') + '/'
    TABLES_DIR = f"{base}{TABLES_DIR}"

    sizes = []
    solution = []
    baseline = []
    for filename in sorted(os.listdir(f'{base}{RESULTS_DIR}'), key = int):
        sizes.append(int(filename))
        s, b = read_times(f"{base}{RESULTS_DIR}{filename}")
        solution.append(s)
        baseline.append(b)
    plot_times(sizes, solution, baseline)


from clases import SFRegion2D, SFRegion3D
import auxFuncts
import numpy as np
import argparse

#load and initialization
parser = argparse.ArgumentParser()
parser.add_argument('--fname', required=True)
args = parser.parse_args()
inp = auxFuncts.readInput(args.fname)
spl=args.fname.split('.')
#print(spl)
ofil='.'.join(spl[:-1])+'.out'
ofilplot='.'.join(spl[:-1])+'.pdf'
print(ofil)
auxFuncts.checkInput(inp)
headerOutput='x  y  cluster'
if(inp['coord']=='Ra Dec'):
    headerOutput='ra  dec  cluster'
pts=np.genfromtxt(inp['filename'], skip_header=1, delimiter='\t')
if(inp['dim']==2):
    sfR2D=SFRegion2D(pts, inp['coord'])
    print('2D region created')
    print('Q parameter',sfR2D.q)
    if(inp['eps']=='None'):
        assert sfR2D.q <inp['Qlim'], "Check input Q limit for structure searching."
        sfR2D.calculateRho()
        print('RHO',sfR2D.rho)
        sfR2D.calculateEps()
        print('EPS',sfR2D.eps)
        sfR2D.setSignif(inp['Signif'])
        print('SIGNIF', sfR2D.signif)
        sfR2D.calculateNmin()
        print('NMIN',sfR2D.Nmin)
        sfR2D.detectStructs()
        npts=pts.shape[0]
        salid = np.hstack((pts,sfR2D.db.labels_.reshape(npts,1)))
        np.savetxt(ofil,salid,header=headerOutput)
        sfR2D.plotStructs(ofilplot)
    elif(inp['eps']>0):
        assert inp['Nmin']>0, "please input also a minimum number of points for dbscan"
        sfR2D.calculateRho()
        sfR2D.setEps(inp['eps'])
        sfR2D.setNmin(inp['Nmin'])
        print(sfR2D.eps)
        print(sfR2D.Nmin)
        sfR2D.detectStructs()
        npts=pts.shape[0]
        salid = np.hstack((pts,sfR2D.db.labels_.reshape(npts,1)))
        np.savetxt(ofil,salid,header=headerOutput)
        sfR2D.plotStructs(ofilplot)
    else:
        print('invalid value of eps, check documentation')
elif(inp['dim']==3):
    sfR3=SFRegion3D(pts, inp['coord'])
    print('2D region created')
    print('Q parameter',sfR3.q)
    sfR3.calculateRho()
    print('RHO',sfR3.rho)
    sfR3.calculateEps()
    print('EPS',sfR3.eps)
    sfR3.setSignif(inp['Signif'])
    print('SIGNIF', sfR3.signif)
    sfR3.calculateNmin()
    print('NMIN',sfR3.Nmin)
    sfR3.detectStructs()
    npts=pts.shape[0]
    salid = np.hstack((pts,sfR3.db.labels_.reshape(npts,1)))
    np.savetxt(ofil,salid,header=headerOutput)
    sfR3.plotStructs()
else:
    print('Now, only 2 or 3 dimensions')


#!/usr/bin/python3
"""Print the first State object from the database hbtn_0e_6_usa."""
import sys
from model_state import Base, State
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

if __name__ == "__main__":
    # Check for the correct number of command-line arguments
    if len(sys.argv) != 4:
        print("Usage: {} <username> <password> <database>"
              .format(sys.argv[0]))
        sys.exit(1)

    # Create the database engine
    engine = create_engine(
    'mysql+mysqldb://{}:{}@localhost:3306/{}'.format(
        sys.argv[1], sys.argv[2], sys.argv[3]
    ),
    pool_pre_ping=True
    )

    # Create a session to interact with the database
    Session = sessionmaker(bind=engine)
    session = Session()

    # Query and retrieve the first State object
    first_state = session.query(State).order_by(State.id).first()

    # Check if a State object was found
    if first_state:
        print("{}: {}".format(first_state.id, first_state.name))
    else:
        print("Nothing")

    # Close the session
    session.close()


#!/usr/bin/env python3

import sys
import os
from argparse import ArgumentParser
from traintasticmanualbuilder.utils import detect_version

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: " + sys.argv[0] + " <format>", file=sys.stderr)
        sys.exit(1)

    args = sys.argv[2:]  # remove program name and format

    # Standard options:
    parser = ArgumentParser()
    parser.add_argument('--base-dir', default=os.path.join(os.path.dirname(__file__), 'traintasticmanual'))
    parser.add_argument('--output-dir')
    parser.add_argument('--language', default='en-us')
    parser.add_argument('--version', default=detect_version())

    if sys.argv[1] == 'html-single-page':
        from traintasticmanualbuilder.htmlsinglepage import HTMLSinglePageBuilder
        HTMLSinglePageBuilder(parser.parse_args(args)).build()
    else:
        print("Unknown format: " + sys.argv[1], file=sys.stderr)
        sys.exit(1)


# Import Tools
# Use Flask to render a template
from flask import Flask, render_template, redirect, url_for

# Use PyMongo to interact with our Mongo database
from flask_pymongo import PyMongo

# Convert from Jupyter notebook to Python to use scraping code
import scraping

# Setup Flask
app = Flask(__name__)

# Use flask_pymongo to set up mongo connection
app.config["MONGO_URI"] = "mongodb://localhost:27017/mars_app"
mongo = PyMongo(app)

# Define the route for the HTML page
@app.route("/")
def index():
   mars = mongo.db.mars.find_one()
   return render_template("index.html", mars=mars)

# Set up scraping route
# Define it with def scrape()
@app.route("/scrape")
def scrape():
   mars = mongo.db.mars
   mars_data = scraping.scrape_all()
   mars.update_one({}, {"$set":mars_data}, upsert=True)
   return redirect('/', code=302)
if __name__ == "__main__":
   app.run()


import re
import string
from collections import defaultdict

filename = 'CS210_Project_Three_Input_File.txt'
newfile = 'frequency.dat'

def ListFrequency():
    # Creates defaultdict named item_frequencies
    item_frequencies = defaultdict(int)

    # Opens file
    with open(filename, "r") as infile:
        # Reads each item without counting newline
	    lines = infile.read().splitlines()
	    for row in lines:
            # Adds frequency of item
		    item_frequencies[row] += 1

    # Prints each item and its frequency
    for item, freq in item_frequencies.items():
        print(item + ": " + str(freq))
    return 0

def ItemFrequency(v):
    count = 0

    # Opens file
    with open(filename, 'r') as infile:
        lines = infile.read().splitlines()
        for row in lines:
            # String must equal "row" to be added to "count"
            if v == row:
                count += 1

    # Prints user input and how many times the input appears in the list
    print(v, count)
    return 0

def FileTransfer():
    item_frequencies = defaultdict(int)

    # Opens file and reads each item
    with open(filename, "r") as infile:
	    lines = infile.read().splitlines()
	    for row in lines:
            # Adds frequency of item to defaultdict
		    item_frequencies[row] += 1

    # Writes item and its frequency onto the new file, frequency.dat
    with open(newfile, 'w') as outfile:
        for item, freq in item_frequencies.items():
            outfile.write(item + " " + str(freq) + "\n")

    return 0


# Importing Packages
import numpy as np
import matplotlib.pyplot as plt

from matplotlib import cm

def ploting(fx,bounds,step,plot_step):

    X = np.arange(bounds[0][0], bounds[0][1], plot_step)
    Y = np.arange(bounds[1][0], bounds[1][1], plot_step)
    X, Y = np.meshgrid(X, Y)
    Z=np.zeros_like(X)

    for i in range(len(X)):
       for j in range(len(Y)):
         matchig_pixcel_L,matchig_pixcel_L,z=fx([X[i,j],Y[i,j]],step)
         Z[i,j]=z

    # Plotting the Animation
    fig = plt.figure(figsize=(15, 7))
    ax1 = fig.add_subplot(121, projection='3d')
    mycmap = plt.get_cmap('gist_earth')
    surf=ax1.plot_surface(X, Y, Z, cmap=mycmap,linewidth=0.01)

    plt.colorbar(surf, shrink=0.5, aspect=5,ax=ax1)
    plt.show()


import cv2
import numpy as np

import Vision as vis

# get img from the webcam
cap_size  = (1200, 1000)
map_shape = [1200,  800]
cap       = cv2.VideoCapture(2)                       # put 1 if an external webcam is used
cap.set(cv2.CAP_PROP_FRAME_WIDTH, cap_size[0])        # width
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,cap_size[1])        # height
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE,1)                 # set auto exposure

robot_pos = np.array([])

while True :
    success, img = cap.read()
    if success :
        success2, true_map = vis.get_clean_map(img, map_shape)                  # get a clean map
        if success2 :
            robot_pos = vis.robot_detection(true_map)                           # find the robot, annotate on map
            goal_pos  = vis.goal_detection(true_map)                            # find the goal, annotate on map
            if robot_pos.size :
                obs_map = vis.get_global_search_map(true_map, robot_pos)        # find obstacles
                cv2.imshow("Obstacles", obs_map)

            true_map = vis.annotate_map(true_map, robot_pos, goal_pos)

        cv2.imshow("Map", true_map)
        cv2.imshow("Raw", img)                                    # display

    else:
        print("Couldn't capture image")
        break

    if (cv2.waitKey(1) & 0xFF) == ord('q'):                       # quit when 'Q' key is pressed
        break

cap.release()
cv2.destroyAllWindows()


from collections import deque

fireworks_effect = deque(int(x) for x in input().split(", "))
explosive_power = [int(x) for x in input().split(", ")]
fireworks_enough = False
palm = 0
willow = 0
crossette = 0

while fireworks_effect and explosive_power:
    current_effect = fireworks_effect.popleft()
    current_power = explosive_power.pop()
    if current_power <= 0:
        fireworks_effect.appendleft(current_effect)
        continue
    if current_effect <= 0:
        explosive_power.append(current_power)
        continue

    mix = current_effect + current_power

    if mix % 3 == 0 and mix % 5 != 0:
        palm += 1
    elif mix % 3 != 0 and mix % 5 == 0:
        willow += 1
    elif mix % 3 == 0 and mix % 5 == 0:
        crossette += 1
    else:
        fireworks_effect.append(current_effect - 1)
        explosive_power.append(current_power)
    if palm > 2 and willow > 2 and crossette > 2:
        fireworks_enough = True
        break

if fireworks_enough:
    print("Congrats! You made the perfect firework show!")
else:
    print("Sorry. You can't make the perfect firework show.")

if fireworks_effect:
    print(f"Firework Effects left: {', '.join(str(x) for x in fireworks_effect)}")

if explosive_power:
    print(f"Explosive Power left: {', '.join(str(x) for x in explosive_power)}")

print(f"Palm Fireworks: {palm}")
print(f"Willow Fireworks: {willow}")
print(f"Crossette Fireworks: {crossette}")


from sklearn.decomposition import PCA
import numpy as np
import matplotlib.pyplot as plt

class PriorFalsification:
    '''
    This class contains methods to perform and visualize prior falsification based on PCA
    '''

    def __init__(self, zprior, zobs, npc=None):
        '''
        This function constructs the prior falsification object and performs PCA on the data
        Inputs:
            zprior: an [NxD] matrix of prior data realizations
            zobs: a [QxD] array of observed data
            npc (optional): integer specifying number of principal components to compute. If not specified then N principal components are used.

        Output:
            PF: PosteriorFalsification object with the following attributes:
                - eigvecs: a [DxC] matrix of principal component eigenvectors
                - Sprior: a [NxC] matrix of prior data scores
                - Sobs: a [QxC] array of observed data scores
                - eigvals: a [1xC] array of eigenvalues
                - var: a [1xC] array of component-wise variance fraction
                - cumvar: a [1xC] array of cumulative variance fraction
                - npc: integer specifying the number of PCs calculated
                - pca: sklearn.decomposition.PCA object fitted to zprior

        Defintion of dimensions:
            D: data dimensionality
            Q: number of observed data samples
            N: number of prior realizations
            C: number of principal components to calculate
        '''
        self.pca = PCA(n_components=npc)
        self.pca.fit(zprior)
        self.Sprior = self.pca.transform(zprior)
        self.Sobs = self.pca.transform(zobs)
        self.eigvecs = self.pca.components_
        self.eigvals = self.pca.explained_variance_
        self.var = self.pca.explained_variance_ratio_
        self.cumvar = np.cumsum(self.var)
        self.npc = self.Sprior.shape[1]

    def plot_scores(self, c1=0, c2=1):
        '''
        This function plots a scatter plot of prior and observed PC scores

        Inputs:
            c1 (optional): integer of first PC to plot (starting from 0)
            c2 (optional): integer of second PC to plot (starting from 0)

        '''

        plt.scatter(
            self.Sprior[:,c1], self.Sprior[:,c2],
            label='Prior',
            edgecolor='k',
            c=(0.5,0.5,0.5)
            )

        plt.scatter(
            self.Sobs[:,c1], self.Sobs[:,c2], 500,
            label='Obs',
            marker='*',
            edgecolor='k',
            c=(225/255,223/255,0/255)
            )

        plt.xlabel('PC '+str(c1+1)+' Score ('+str(np.round(self.var[c1]*100,2))+'%)',fontsize=16)
        plt.ylabel('PC '+str(c2+1)+' Score ('+str(np.round(self.var[c2]*100,2))+'%)',fontsize=16)
        plt.legend(fontsize=14)

    def plot_scree(self, npcs=None):
        '''
        This function generates a scree plot

        Inputs:
            npcs (optional): integer specifying the number of PCs to plot
        '''

        if npcs is None:
            n = self.npc
        else:
            n = npcs

        pc = list(range(0,n+1))
        variance =  np.concatenate((np.array([0]),self.cumvar[:n]))

        plt.plot(pc,variance,'-k',linewidth=4)
        plt.ylim((0,1))
        plt.xlabel('Principal Component Number',fontsize=14)
        plt.ylabel('Cumulative Variance Fraction',fontsize=14)
        plt.xticks(fontsize=12)
        plt.yticks(fontsize=12)


from functools import cache
from pyinstrument import Profiler

from src.instance_loader import InstanceLoader
from src.config import Config
from src.optimizer import Optimizer
from src.main import formulations
from typing import Dict, Set, List

def minimal_formulation_analysis(formulation_name: str) -> None:
    """
    Find all minimal sets of constraints for a given formulation.
    Minimal in the sense that removing any constraint from the set makes the formulation invalid
    (optimal value is better than the real one).

    Args:
        formulation_name: name of the formulation to analyze.
    """

    assert not config.n_instances_main, f'Cannot run minimal_formulation_analysis with n_instances != None'
    instance_loader = InstanceLoader(config)
    all_instances = instance_loader.load_instances()
    instance_names = ['small_R12hom_imp3_K3T2', 'small_R12hom_alt2_K2T2', 'small_RC12het_altimpinc3_K3T4',
                      'small_R12het_altimpinc3_K2T3']
    instances = [v for k, v in all_instances.items() if any(name in k for name in instance_names)]
    assert len(instances) == len(instance_names), f'Something went wrong when filtering instances.'

    # Get set of all constraints
    temp_formulation = formulations.get(formulation_name)(instances[0])
    temp_formulation.formulate()
    all_constraints = set(temp_formulation.constraints.keys())

    @cache
    def minimal_set(current_constraints: str) -> List[str]:
        """
        Find all minimal sets of constraints given a set of current constraints.
        Algorithm: for each constraint, test if the formulation is valid without it. If it is, call minimal_set on the
        remaining constraints. If no constraint can be removed, return current_constraints is a minimal set.

        Args:
            current_constraints: set of current constraints (string representation). It is assumed to be valid.

        Returns:
            List of all minimal sets of constraints.
        """
        # Retrieve current constraints
        current_constraints = eval(current_constraints)

        # Build the activations dictionary
        activations = {c: True if c in current_constraints else False for c in all_constraints}

        ans = []
        for constraint in current_constraints:
            activations[constraint] = False
            if formulation_is_valid(str(activations)):
                ans += minimal_set(str(current_constraints - {constraint}))
            activations[constraint] = True

        if not ans:
            return [str(current_constraints)]
        return ans

    @cache
    def formulation_is_valid(activations: str) -> bool:
        """
        Checks if the formulation is valid for the given instances and activations.
        Valid means that the optimal value of the problem corresponds to the known one.

        Args:
            activations: dictionary of constraints to activate or not (string representation).

        Returns:
            True if the formulation is valid for the given instances and activations, False otherwise.
        """
        # Retrieve activations
        activations = eval(activations)

        assert config.exception_when_non_optimal, f'Cannot validate formulation without exception_when_non_optimal.'
        for instance in instances:
            formulation = formulations.get(formulation_name)(instance, activations)
            optimizer = Optimizer(formulation, config)
            try:
                optimizer.run()
            except ValueError as e:
                return False

        return True

    ans = set(minimal_set(str(all_constraints)))
    final_list = []
    for a in ans:
        final_list.append((len(eval(a)), eval(a)))
    print(f'All constraints: \n  {len(all_constraints)}: {str(all_constraints)}')
    print(f'Minimal sets:')
    for size, s in sorted(final_list):
        print(f'  {size}: {str(s)}')

    # Print the corresponding activations for the yaml file
    for i, a in enumerate(ans):
        print(f'Minimal set {i}\n{formulation_name}:')
        for c in all_constraints:
            print(f'  {c}: {"true" if c in eval(a) else "false"}')

if __name__ == '__main__':
    prof = Profiler()
    prof.start()

    config = Config()
    minimal_formulation_analysis(config.formulation)

    prof.stop()
    print(prof.output_text(unicode=True, color=True))


#! /usr/bin/python

import numpy as np
from numpy import cos, sin
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pcl
from pcl.registration import icp, gicp, icp_nl

theta = [0., 0., np.pi]
rot_x = [[1,              0,              0],
         [0,              cos(theta[0]), -sin(theta[0])],
         [0,              sin(theta[0]),  cos(theta[0])]]
rot_y = [[cos(theta[1]),  0,              sin(theta[1])],
         [0,              1,              0],
         [-sin(theta[1]),  0,             cos(theta[1])]]
rot_z = [[cos(theta[2]), -sin(theta[1]),  0],
         [sin(theta[2]),  cos(theta[1]),  0],
         [0,              0,              1]]
transform = np.dot(rot_x, np.dot(rot_y, rot_z))

source = np.random.randn(100, 3)
source_pc = pcl.PointCloud(source.astype(np.float32))
target = np.dot(transform, source.T).T
target_pc = pcl.PointCloud(target.astype(np.float32))

converged, transf, estimate, fitness = icp(source_pc, target_pc, max_iter=10000)

print(converged, transf, estimate, fitness)
result = estimate.to_array()

fig = plt.figure()
ax = fig.add_subplot(221, projection='3d')
ax.scatter(source[:,0], source[:,1], source[:,2])
ax = fig.add_subplot(222, projection='3d')
ax.scatter(target[:,0], target[:,1], target[:,2])
ax = fig.add_subplot(212, projection='3d')
ax.scatter(source[:,0], source[:,1], source[:,2], 'b')
ax.scatter(result[:,0], result[:,1], result[:,2], 'r')

plt.show()


import json
import os
import re
import sys

stopwords = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself',
             'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself',
             'they',
             'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those',
             'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does',
             'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at',
             'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after',
             'above',
             'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then',
             'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more',
             'most',
             'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't',
             'can', 'will', 'just', 'don', 'should', 'now', ]
punctuations = ['!', '"', '#', '$', '%', '&', '\'', ',', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=',
                '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']

dictionary = {}
spamCount = 0
spamFileno = 0
hamCount = 0
hamFileno = 0
uniquewords = set()
initialDict = {}
probabilityDict = {}

def appendfunc():
    for x in range(0, len(content)):
        if content[x] not in stopwords:
            if content[x] in punctuations:
                content[x] = content[x].replace(content[x], " ")
            bagofwords.append(content[x])

def initialList(initialDict):
    global spamCount, hamCount
    if (hamFlag == 1):
        initialDict['ham'] = initialDict['ham'] + 1

    if (spamFlag == 1):
        initialDict['spam'] = initialDict['spam'] + 1

    return initialDict

def assignZero():
    initialDict = {'spam': 0, 'ham': 0}

    return initialDict

def writeJSON(condProb):
    with open('nbmodel.txt', 'w+') as f:
        json.dump(condProb, f)

def calculatep():
    for x in range(0, len(bagofwords)):
        uniquewords.add(bagofwords[x])
        if bagofwords[x] not in dictionary:
            initialDict = assignZero()

        else:
            initialDict = dictionary[bagofwords[x]]
        initialDict = initialList(initialDict)
        dictionary[bagofwords[x]] = initialDict
        # print(initialDict)print the spam and ham count of a word
        # print(dictionary)
        # prints the word along with how many times it occours in spam and ham
        # print(len(uniqueSet))

def probdistribution():
    for key in dictionary:
        finalList = {}
        initialDict = dictionary[key]
        finalList['spam'] = (initialDict['spam'] + 1) / float(spamCount + len(uniquewords))
        finalList['ham'] = (initialDict['ham'] + 1) / float(hamCount + len(uniquewords))
        probabilityDict[key] = finalList
    writeJSON(probabilityDict)

for root, dirs, files in os.walk(sys.argv[1]):
    for i in range(0, len(files)):
        bagofwords = []
        hamFlag = 0
        spamFlag = 0
        if files[i] not in ['README.md', '.DS_Store', 'README.txt', 'LICENSE']:
            filename = root + '/' + files[i]
            # print(filename)
            f = open(filename, 'r', encoding="latin1")
            # print("hello")
            content = f.read()
            content = content.lower()

            content = content.split()
            # print contents
            appendfunc()

            # print(bagofwords)
            if (re.search(r'(.)*spam(.)*', root)):
                spamFlag = 1
                spamCount = spamCount + len(bagofwords)
                spamFileno = spamFileno + 1
            elif (re.search(r'(.)*ham(.)*', root)):
                hamFlag = 1
                hamCount = hamCount + len(bagofwords)
                hamFileno = hamFileno + 1
                # print(spamFileno)
                # print(hamFileno)
            calculatep()
probdistribution()
# print(probabilityDict)

# print("done")


#Imports:
import os
import glob
import argparse
import numpy as np
from pathlib import Path
from tqdm import tqdm
import warnings
import torch
import yaml

from src.Rect_to_seg_helper import *
from src.SAM_helper import *
#########################################################################################################################
def run(img_directory,annotation_directory,saving_path,sam_checkpoint_path,smooth):
    '''This code converts YOLO-style rectangle annotations to segmentation annotations.
       It takes the bounding box coordinates provided in the YOLO format and generates corresponding segmentation masks.
       The output of the script will be json files saved in the spacified path.
         Parameters:
             img_directory: The directory of the annotated images.
             annotation_directory:The directory of the annotations crosponding to the images.
             saving_path: The path where the results will be saved in.
             sam_checkpoint_path: The path to SAM checkpoint.
             '''
    torch.cuda.empty_cache()
    print('The process of converting rectangle annotations to segmentations has begun.')

    #load the model
    print('Loading The Model...........')
    predictor=model_loader_predictor(sam_checkpoint=sam_checkpoint_path)
    print('The Model is loaded')

    all_annotations=glob.glob(os.path.join(annotation_directory,'*.json'))
    print(f'Total number of JSON files: {len(all_annotations)}.')

    missing_images=[]
    files_with_exceptions=[]
    print('Code execution will start now.')
    for num, annotation_file in tqdm(enumerate(all_annotations), total=len(all_annotations), desc="Processing"):
        try:
    #for i,annotation_file in enumerate(all_annotations):

            #get image path:
            img_file= os.path.join(img_directory,Path(annotation_file).stem+'.png')
            #check if the image exists:
            if not os.path.exists(img_file):

                warnings.warn(f"This file does not  exist: {img_file}.", UserWarning)
                missing_images.append(img_file)
                continue
            #read image:
            image,img_dim=image_reader(img_file)

            #predict the image:
            sam_predictor(image,predictor)

            #read the json file and get the annotated objects:
            classes_dict=get_points_from_json(annotation_file)
            if not classes_dict:#check if the file is empty
                continue

            #initiate a json object to store the segmentation annotations:
            data_example, image_name= json_file_data_format(img_path=img_file,img_height=img_dim[0],img_width=img_dim[1])

            for key in classes_dict.keys():

                bboxes=np.vstack(classes_dict[key])
                #generate the masks of interest:
                masks=generate_masks_from_bboxes(bboxes,predictor,image)
                #get a list of segmentations:
                polygons=get_segmentations_from_mask(masks,smooth)

                #append to the annotations to the json file:
                for obj in polygons:

                    obj=np.vstack(obj).squeeze()
                    #elemenat files that have one dim
                    if np.ndim(obj)==1:
                        continue

                    #fill the annotation data
                    data_example['shapes'].append({'label':key,
                    'points':obj.tolist(), 'group_id': None,
                    'shape_type':"polygon",
                    "flags": {}})

            #write the annotations into json file:
            write_json(saving_path,image_name,data_example)
            print(f'This is file number: {num} | Progress: {(num/len(all_annotations))*100}%')
        except Exception as e:
            print(e)
            warnings.warn(f"This file has an Exception check it: {annotation_file}.", UserWarning)
            files_with_exceptions.append(annotation_file)
            continue

    os.mkdir(os.path.join(saving_path,'missing_and_exception_files'))
    f_missing_name=os.path.join(saving_path,'missing_and_exception_files','missing_images')
    f_with_exceptions_name=os.path.join(saving_path,'missing_and_exception_files','files_with_exceptions')

    text_writer(f_missing_name,missing_images)
    text_writer(f_with_exceptions_name,files_with_exceptions)

    print('The process has been successfully completed!! Thanks for using CSSRI tools....')

def parse_config(config_path):
    with open(config_path, 'r') as file:
        return yaml.safe_load(file)

if __name__ == "__main__":
    #parser = argparse.ArgumentParser(description="CSSRI tool for converting YOLO-style rectangle annotations to segmentation annotations.")
    # parser.add_argument("img_directory", type=str, help="path to the source directory where you have the images.")
    # parser.add_argument("annotation_directory", type=str, help="path to the source directory where you have the annotations.")
    # parser.add_argument("saving_path", type=str, help="Path where the json files will be saved in.")
    # parser.add_argument("sam_checkpoint_path", type=str, help="Path to sam checkpoint.")
    # parser.add_argument("--smooth", action='store_true', help="Optional argument to apply smoothening to the polygons.")

    parser = argparse.ArgumentParser(description="CSSRI tool for converting YOLO-style rectangle annotations to segmentation annotations.")
    parser.add_argument('--config', type=str, help='Path to the configuration file')
    args = parser.parse_args()

    config = parse_config(args.config)

    #run(args.img_directory, args.annotation_directory, args.saving_path, args.sam_checkpoint_path,args.smooth)
    run(config['img_directory'], config['annotation_directory'], config['saving_path'], config['sam_checkpoint_path'], config.get('smooth', False))


import discord
import time
import asyncio
import json
from discord.ext import commands
from rolecommands import roles_command, addrole_command, removerole_command
from apod import apodLoad, apodSend
from purge import purge
from iss import send_iss

intents = discord.Intents.all()
client = commands.Bot(intents=intents, command_prefix='!')
client.remove_command('help')

with open('config.json', 'r') as f:
    config = json.load(f)

@client.event
async def on_ready():
    print("AstroBot is online!")
    await client.wait_until_ready()

    try:
        synced = await client.tree.sync()
        print(f"Synced {len(synced)} commands")

    except Exception as e:
        print(f"Error syncing commands: {e}")

    isImg = None

    while not client.is_closed():
        t = time.localtime()
        current_time = time.strftime("%H:%M:%S", t)

        if current_time == "11:58:00":
            if isImg is None:
                isImg = apodLoad(config["NASA API KEY"])
        if current_time == "12:00:00":
            if isImg is not None:
                await apodSend(client, config, isImg)
                isImg = None

        await asyncio.sleep(1)

@client.tree.command(name='ping', description="Ping the bot! (Just to test)")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message(f"{interaction.user.mention} pong!", ephemeral=True)

@client.tree.command(name='iss', description="Find out where the ISS is currently overhead!")
async def iss(interaction: discord.Interaction):
    await send_iss(interaction, client, config["ISS CHANNEL ID"])

@client.tree.command(name='roles', description="View all available roles in the server.")
async def roles(interaction: discord.Interaction):
    await roles_command(interaction, client)

@client.tree.command(name='addrole', description="Assign yourself a role.")
async def roles(interaction: discord.Interaction, rolename: str):
    await addrole_command(interaction, rolename, client)

@client.tree.command(name='removerole', description="Remove a role from your account.")
async def roles(interaction: discord.Interaction, rolename: str):
    await removerole_command(interaction, rolename, client)

@client.event
async def on_message(message: discord.Message):
    try:
        print(f"User {message.author} just sent this message in {message.channel.name}: {message.content}")
    except AttributeError as e:
        print(f"Error displaying message sent by {message.author}: {e}")

    if message.author == client.user:
        return

    elif message.content.startswith('-apod-load'):
        if not message.author.guild_permissions.administrator:
            await message.reply('Sorry, you do not have permission to use this command!')
            return

        apodLoad(config["NASA API KEY"])

    elif message.content.startswith('-apod-send-img'):
        if not message.author.guild_permissions.administrator:
            await message.reply('Sorry, you do not have permission to use this command!')
            return

        await apodSend(client, config, isImg=True)

    elif message.content.startswith('-apod-send-vid'):
        if not message.author.guild_permissions.administrator:
            await message.reply('Sorry, you do not have permission to use this command!')
            return

        await apodSend(client, config, isImg=False)

    elif message.content.startswith('-purge'):
        if not message.author.guild_permissions.administrator:
            await message.reply('Sorry, you do not have permission to use this command!')
            return
        str = message.content[7:]
        await purge(message, str)

client.run(config["TOKEN"])


import logging

from flask import jsonify, request

from app import app, api
from app.resources.appointment import AppointmentsResource, AppointmentsItemResource
from app.resources.patient import PatientsResource, PatientsItemResource
from app.resources.provider import ProvidersResource, ProvidersItemResource

logger = logging.getLogger(__name__)

API_PREFIX = '/v1'

@app.route('/')
def index():
    return jsonify({'index': 'page'})

api.add_resource(AppointmentsResource, f'{API_PREFIX}/appointments')
api.add_resource(AppointmentsItemResource, f'{API_PREFIX}/appointments/<int:appointment_id>')

api.add_resource(PatientsResource, f'{API_PREFIX}/patients')
api.add_resource(PatientsItemResource, f'{API_PREFIX}/patients/<int:patient_id>')

api.add_resource(ProvidersResource, f'{API_PREFIX}/providers')
api.add_resource(ProvidersItemResource, f'{API_PREFIX}/providers/<int:provider_id>')

###############################
# Notification Webhook Endpoint
###############################

@app.route('/receive_notifications', methods=['POST'])
def webhook_notification_receiver():
    """
    Endpoint to receive notifications. Aware of separation of concern issues

    Only putting it here as this is a prototype and want to show we recieved
    a POST request from a webhook
    """
    logger.info(request.values)
    return jsonify({'all': 'good'})


import re
from dataclasses import dataclass, field
from ipaddress import IPv4Address

from .connection import CiscoConnection
from .models import *
from .base import CiscoBase
from ..ssh.error import *

class CiscoRouter(CiscoBase):

    def __init__(self, host, discover=True):
        super().__init__(host, discover)

        if discover:
            self.Connection.connect()

            self.OSPFNeighbors = []
            self._discover_ospf()

            self.Connection.close()

    def _discover_ospf(self):
        for i in self.Connection.command('sh ip ospf neigh | i Gi*').split('\r\n'):
            if len(i.split(' ')) > 1:
                res = []
                for s in i.split(' '):
                    if s != '':
                        res.append(s)
                NeighborID, Priority, State, DeadTime, IPv4Address, Interface = res
                self.OSPFNeighbors.append(OSPFNeighbor(self.Hostname, NeighborID, Priority, State, DeadTime, IPv4Address, Interface))


#%%
################################################
# K-FOLD CROSS VALIDATION
# WITH ALL THE PROPOSED MODELS
################################################
import warnings
from utils import *
from sklearn.model_selection import KFold, LeaveOneOut, cross_val_score
from termcolor import colored

warnings.filterwarnings("ignore")

#########################################
# Preparing data for the cross validation
#########################################

# Read authors and texts
print(colored("Reading all texts...",'blue'))
texts, authors = get_authors_and_texts(TEXT_PATH)

# Creation of the associated labels
print(colored("Creating authors labels...",'blue'))
classes, author_dict = create_labels_vector(authors, AUTHORS_INFORMATION_PATH)

# Bag of words representation
print(colored("Representing the text as vectors...",'blue'))
vectorizer, vectors = generate_text_vectors(texts,'bow')

# To get n-1 in training set and 1 in test set
# Note that it requires 30 min to execute
# cv = LeaveOneOut()

cv = KFold(n_splits=5, shuffle=True, random_state=0)
models = [
    RandomForestClassifier(n_estimators=100, max_depth=5, random_state=0),
    DecisionTreeClassifier(max_depth=2),
    LinearSVC(),
    MultinomialNB(),
    LogisticRegression(random_state=0),
]

classes, author_dict = create_labels_vector(authors, AUTHORS_INFORMATION_PATH)
# Bag of words representation
vectors = vectorizer.fit_transform(texts)

entries = []
from tqdm import tqdm

for model in tqdm(models):
    model_name = model.__class__.__name__
    accuracies = cross_val_score(model, vectors, classes, cv=cv, n_jobs=1)
    for fold_idx, accuracy in enumerate(accuracies):
        entries.append((model_name, fold_idx, accuracy))
cv_df = pd.DataFrame(entries, columns=["model_name", "fold_idx", "accuracy"])
#%%
# Mean results
cv_df.groupby(["model_name"]).mean().sort_values("accuracy", ascending=False)
# Median results
cv_df.groupby(["model_name"]).median().sort_values("accuracy", ascending=False)


from abc import ABC, abstractmethod
import cvxpy as cvx

__all__ = ['DefaultReturns', ]

class BaseReturns(ABC):
    """Estimated returns """

    def __init__(self,  w_benchmark=0., cash_ticker='_CASH', **kwargs):
        """Args:

            w_benchmark:  benchmark weight single column data frame
            cash_ticker: the cash_ticker
        """
        self.w_benchmark = w_benchmark
        self.cash_ticker = cash_ticker

    @abstractmethod
    def expr(self, t, w_plus, z, v, theta):
        """Returns the estimate at time t of alpha at time tau.

        Args:
            t: time
            w_plus: post-trade weights
            z: trade weights
            v: portfolio dollar value
            theta: int: how many extra step extra to predict, default to 0 for single period
        """
        # TO DO to put back benchmark
        pass

class DefaultReturns(BaseReturns):
    def __init__(self, returns, deltas, gamma_decay, **kwargs):
        """
        Args:
            returns: dictionary as_of_time: pd.DataFrame(forecast_time, ticker)
            deltas: dictionary as_of_time: pd.DataFrame(forecast_time, ticker)
        """
        super().__init__(**kwargs)
        self.returns = returns
        self.deltas = deltas
        self.gamma_decay = gamma_decay

    def expr(self, t, w_plus, z, v, theta=0):
        returns = self.returns[t].iloc[theta].values
        deltas = self.deltas[t].iloc[theta].values

        #####
        # alpha = cvx.mul_elemwise(returns, w_plus)
        # alpha -= cvx.mul_elemwise(deltas, cvx.abs(w_plus))
        # estimate = cvx.sum_entries(alpha)
        ######
        # estimate = returns.T * w_plus
        #####

        estimate = cvx.mul_elemwise(returns, w_plus)
        estimate -= cvx.mul_elemwise(deltas, cvx.abs(w_plus))
        estimate = cvx.sum_entries(estimate)
        if theta > 0 and self.gamma_decay is not None:
            estimate *= theta ** (-self.gamma_decay)
        return estimate


import cv2
import numpy as np
import read_stardict
import pytesseract
import spacy
import re
import string

class OCR:
    """
    Interface for the class:
    ocr = OCR(filename, hsv_lower, hsv_upper)
    ocr.recognize() returns a list of detected highlighted English words

    hsv_lower and hsv_upper is a list that defines specified hsv color range
    """

    def __init__(self, image_file, hsv_lower, hsv_upper):
        self.image = image_file #cv2.imread(image_file)
        self.hsv_lower = hsv_lower
        self.hsv_upper = hsv_upper
        self.dic = read_stardict.Dictionary('stardict-lazyworm-ec-2.4.2/')

        self.rgb = cv2.cvtColor(self.image.copy(), cv2.COLOR_BGR2RGB)

        # rgb to HSV color spave conversion
        hsv_img = cv2.cvtColor(self.image, cv2.COLOR_BGR2HSV)
        HSV_lower = np.array(hsv_lower, np.uint8)  # Lower HSV value
        HSV_upper = np.array(hsv_upper, np.uint8)  # Upper HSV value

        # Threshold
        frame_threshed = cv2.inRange(hsv_img, HSV_lower, HSV_upper)

        # find connected components
        self.contours, self.hierarchy, = cv2.findContours(frame_threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

        self.sp = spacy.load('en_core_web_sm')

    def find_and_format_word(self, word):
        if not word:
            return '', ''
        lemma_word = self.sp(word)
        if lemma_word[0].lemma_ == word:
            chinese_text = self.dic.lookup(word)
        else:
            chinese_text = self.dic.lookup(lemma_word[0].lemma_)
        if chinese_text:
            formated_chinese = chinese_text.replace('\n', ' ')
            return formated_chinese, chinese_text
        return '', ''

    # get the thresholded image
    def threshold_image(self, img, v):
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        ret, th = cv2.threshold(gray, v, 255, cv2.THRESH_BINARY)
        return th

    def calc_areas(self):
        areas = []
        for cnt in self.contours:
            area = cv2.contourArea(cnt)
            areas.append(area)
        self.areas = np.array(areas)

    def warp_image(self, rgb_image):
        warped_images = []
        for cnt in self.contours:
            area = cv2.contourArea(cnt)
            if area > np.mean(self.areas):

                rect = cv2.minAreaRect(cnt)
                big_rect = (rect[0], (1.5 * rect[1][0], 1.5 * rect[1][1]), rect[2])
                box = cv2.boxPoints(big_rect)
                box = np.int0(box)

                width = int(big_rect[1][0])
                height = int(big_rect[1][1])

                src_pts = box.astype("float32")
                # coordinate of the points in box points after the rectangle has been
                # straightened
                dst_pts = np.array([[0, height - 1],
                                    [0, 0],
                                    [width - 1, 0],
                                    [width - 1, height - 1]], dtype="float32")
                # the perspective transformation matrix
                M = cv2.getPerspectiveTransform(src_pts, dst_pts)

                # directly warp the rotated rectangle to get the straightened rectangle
                warped = cv2.warpPerspective(rgb_image, M, (width, height))
                if width < height:
                    warped = np.rot90(warped)
                warped_images.append(warped)
        return warped_images

    def remove_non_english(self, warped, v):
        '''
        Input:
        warped, single bounded image for a potential word
        v is the value for thresholding, can be 120, 150, or 180

        Return,
        a English word from a dictionary or None
        '''
        th = self.threshold_image(warped, v)
        candidate = pytesseract.image_to_string(th)
        regex = re.compile('[%s]' % re.escape(string.punctuation))
        candidate = regex.sub('', candidate)
        words = [] or re.split(r'\—|\s+|\n', candidate)
        print(words)
        longest_word = ''
        definition = ''
        for idx, word in enumerate(words):
            formatted_chinese, chinese = self.find_and_format_word(word)
            if chinese:
                if len(longest_word) < len(word):
                    longest_word = word
                    definition = formatted_chinese
        return longest_word, definition

    def recognize(self):
        v = [120, 150, 180]
        """
        :return: a list of list of English words and their definitions
        """
        ans = []
        self.calc_areas()
        warped_images = self.warp_image(self.rgb)
        for warped in warped_images:
            target_word, target_definition = '', ''
            for val in v:
                word, definition = self.remove_non_english(warped, val)
                if len(target_word) < len(word):
                    target_word = word
                    target_definition = definition
            if target_word != '' and target_definition != '':
                ans.append([target_word, target_definition])
        return ans


import logging

from community.models import Community
from bot.bin.friendship import create_friendship_members
from bot.onstatus import search_triage
from celery import shared_task

logger = logging.getLogger(__name__)

@shared_task
def handle_follow_member():
    communities = Community.objects.filter(
        active=True,
        twitter_follow_member=True
    )
    for community in communities:
        handle_follow_member_community.apply_async(
            args=(community.name,),
            ignore_result=True
        )

@shared_task
def handle_follow_member_community(community: str):
    try:
        community = Community.objects.get(name=community)
    except Community.DoesNotExist:
        return
    create_friendship_members(community)

@shared_task
def handle_search_triage(community: str):
    try:
        community = Community.objects.get(name=community)
    except Community.DoesNotExist:
        return
    search_triage(community)