# YOLOv5 ðŸš€ by Ultralytics, GPL-3.0 license
"""
Run YOLOv5 detection inference on images, videos, directories, globs, YouTube, webcam, streams, etc.

Usage - sources:
    $ python detect.py --weights yolov5s.pt --source 0                               # webcam
                                                     img.jpg                         # image
                                                     vid.mp4                         # video
                                                     screen                          # screenshot
                                                     path/                           # directory
                                                     'path/*.jpg'                    # glob
                                                     'https://youtu.be/Zgi9g1ksQHc'  # YouTube
                                                     'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream

Usage - formats:
    $ python detect.py --weights yolov5s.pt                 # PyTorch
                                 yolov5s.torchscript        # TorchScript
                                 yolov5s.onnx               # ONNX Runtime or OpenCV DNN with --dnn
                                 yolov5s_openvino_model     # OpenVINO
                                 yolov5s.engine             # TensorRT
                                 yolov5s.mlmodel            # CoreML (macOS-only)
                                 yolov5s_saved_model        # TensorFlow SavedModel
                                 yolov5s.pb                 # TensorFlow GraphDef
                                 yolov5s.tflite             # TensorFlow Lite
                                 yolov5s_edgetpu.tflite     # TensorFlow Edge TPU
                                 yolov5s_paddle_model       # PaddlePaddle
"""

import argparse
import os
import platform
import sys
from pathlib import Path

import torch

FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]  # YOLOv5 root directory
if str(ROOT) not in sys.path:
    sys.path.append(str(ROOT))  # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd()))  # relative

from models.common import DetectMultiBackend
from utils.dataloaders import (
    IMG_FORMATS,
    VID_FORMATS,
    LoadImages,
    LoadScreenshots,
    LoadStreams,
)
from utils.general import (
    LOGGER,
    Profile,
    check_file,
    check_img_size,
    check_imshow,
    check_requirements,
    colorstr,
    cv2,
    increment_path,
    non_max_suppression,
    print_args,
    scale_boxes,
    strip_optimizer,
    xyxy2xywh,
)
from utils.plots import Annotator, colors, save_one_box
from utils.torch_utils import select_device, smart_inference_mode

@smart_inference_mode()
def run(
    weights=ROOT / "yolov5s.pt",  # model path or triton URL
    source=ROOT / "data/images",  # file/dir/URL/glob/screen/0(webcam)
    data=ROOT / "data/coco128.yaml",  # dataset.yaml path
    imgsz=(640, 640),  # inference size (height, width)
    conf_thres=0.25,  # confidence threshold
    iou_thres=0.45,  # NMS IOU threshold
    max_det=1000,  # maximum detections per image
    device="",  # cuda device, i.e. 0 or 0,1,2,3 or cpu
    view_img=False,  # show results
    save_txt=False,  # save results to *.txt
    save_conf=False,  # save confidences in --save-txt labels
    save_crop=False,  # save cropped prediction boxes
    nosave=False,  # do not save images/videos
    classes=None,  # filter by class: --class 0, or --class 0 2 3
    agnostic_nms=False,  # class-agnostic NMS
    augment=False,  # augmented inference
    visualize=False,  # visualize features
    update=False,  # update all models
    project=ROOT / "runs/detect",  # save results to project/name
    name="exp",  # save results to project/name
    exist_ok=False,  # existing project/name ok, do not increment
    line_thickness=3,  # bounding box thickness (pixels)
    hide_labels=False,  # hide labels
    hide_conf=False,  # hide confidences
    half=False,  # use FP16 half-precision inference
    dnn=False,  # use OpenCV DNN for ONNX inference
    vid_stride=1,  # video frame-rate stride
):
    source = str(source)
    save_img = not nosave and not source.endswith(".txt")  # save inference images
    is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
    is_url = source.lower().startswith(("rtsp://", "rtmp://", "http://", "https://"))
    webcam = source.isnumeric() or source.endswith(".txt") or (is_url and not is_file)
    screenshot = source.lower().startswith("screen")
    if is_url and is_file:
        source = check_file(source)  # download

    # Directories
    save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
    (save_dir / "labels" if save_txt else save_dir).mkdir(
        parents=True, exist_ok=True
    )  # make dir

    # Load model
    device = select_device(device)
    model = torch.load(weights, map_location=torch.device("cpu"))

    # Manual fixes for FasterRCNN model object loaded from weights
    if not str(weights).endswith(".pt"):
        os.rename(
            str(weights), str(weights).replace(os.path.split(str(weights))[-1], ".pt")
        )
        weights = str(weights).replace(os.path.split(str(weights))[-1], ".pt")
    if not isinstance(model, dict):
        model.get = dict.get
        model.names = ["fish_1", "fish_2", "fish_3", "fish_4"]
        model_dict = {"model": model}
        torch.save(model_dict, weights)
    model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
    stride, names, pt = model.stride, model.names, model.pt
    imgsz = check_img_size(imgsz, s=stride)  # check image size

    # Dataloader
    bs = 1  # batch_size
    if webcam:
        view_img = check_imshow(warn=True)
        dataset = LoadStreams(
            source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride
        )
        bs = len(dataset)
    elif screenshot:
        dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt)
    else:
        dataset = LoadImages(
            source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride
        )
    vid_path, vid_writer = [None] * bs, [None] * bs

    # Run inference
    model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz))  # warmup
    seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
    c = 0
    for path, im, im0s, vid_cap, s in dataset:
        c += 1
        with dt[0]:
            im = torch.from_numpy(im).to(model.device)
            im = im.half() if model.fp16 else im.float()  # uint8 to fp16/32
            im /= 255  # 0 - 255 to 0.0 - 1.0
            if len(im.shape) == 3:
                im = im[None]  # expand for batch dim

        # Inference
        with dt[1]:
            visualize = (
                increment_path(save_dir / Path(path).stem, mkdir=True)
                if visualize
                else False
            )
            pred = model(im, augment=augment, visualize=visualize)

            pred = {
                "boxes": pred["boxes"].detach().cpu(),
                "labels": pred["labels"].cpu(),
                "scores": pred["scores"].detach().cpu(),
            }

            # targets = {
            #    'boxes': targets[0]['boxes'].detach().cpu(),
            #    'labels': targets[0]['labels'].cpu(),
            # }

        # NMS
        with dt[2]:

            from torchvision.ops import box_iou, batched_nms
            from collections import Counter
            import numpy as np
            from PIL import Image

            def apply_mns(results, applyAll=False, iou_thresh=0.3):
                """
                Returns the tensors after applying mns on the boxes and classifications.
                :param applyAll: boolean indicate whether to apply all
                :param iou_thresh: The iou threshold for the nms
                :param results: results\targets object
                """
                results_scores = results["scores"]
                results_boxes = results["boxes"]
                results_labels = results["labels"]

                results_labels_modify = (
                    results_labels
                    if not applyAll
                    else torch.zeros(len(results_boxes), dtype=torch.int32)
                )

                mns_ind = batched_nms(
                    results_boxes, results_scores, results_labels_modify, iou_thresh
                )
                labels_filtered = results_labels[mns_ind]
                scores_filtered = results_scores[mns_ind]
                boxes_filtered = results_boxes[mns_ind]

                return {
                    "boxes": boxes_filtered,
                    "labels": labels_filtered,
                    "scores": scores_filtered,
                }

            def add_bounding_boxes(
                img,
                pred_cls,
                boxes,
                pred_score=None,
                thresh=0.35,
                rect_th=2,
                text_size=0.5,
                text_th=1,
                color_box=(1, 1, 1),
                return_pil=True,
            ):
                """
                Returns the image with boxes and labels as PIL
                :param thresh:
                :param img: The image
                :param pred_cls: Array of classes (as numbers)
                :param pred_score: array of probabilities
                :param boxes: Array of boxes
                :param rect_th: thickness of the rectangle
                :param text_size: Text size
                :param text_th: thickness of the text
                :param color_box: Box and text color
                :param return_pil: Boolean indicate whether to return a pil
                :return: Returns the image with boxes and labels as PIL
                """
                # turn classification tensors to numpy
                pred_cls = pred_cls.numpy()

                if pred_score is not None:
                    pred_score = pred_score.numpy()

                for i in range(len(boxes)):
                    if pred_score is not None and pred_score[i] < thresh:
                        continue
                    cv2.rectangle(
                        img,
                        (int(boxes[i][0]), int(boxes[i][1])),
                        (int(boxes[i][2]), int(boxes[i][3])),
                        color=color_box,
                        thickness=rect_th,
                    )
                    print_text = f"fish{pred_cls[i]}"
                    print_text = (
                        print_text
                        if pred_score is None
                        else print_text + ":{}".format(str(round(pred_score[i], 3)))
                    )
                    cv2.putText(
                        img,
                        print_text,
                        (int(boxes[i][0] + rect_th), int(boxes[i][1]) + 20),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        text_size,
                        color_box,
                        thickness=text_th,
                    )

                # turn to PIL
                if return_pil:
                    return Image.fromarray((img * 255).astype(np.uint8))
                else:
                    return (img * 255).astype(np.uint8)

                return

            # apply NMS on prediction
            pred = apply_mns(pred, iou_thres)
            pred = apply_mns(pred, applyAll=True, iou_thresh=0.8)

            # Update mAp
            # metric.update([pred], [targets])

            # Update IOU
            # iou = box_iou(targets['boxes'].cpu(), pred['boxes'].cpu())

            # max_iou = torch.max(iou, dim=1).values if iou.shape[1] != 0 else torch.Tensor([0])
            # total_iou += torch.mean(max_iou)

            # len_targets = len(targets['boxes'].cpu())

            # if the shape of the iou is zero then it means that the model didn't predict nothing
            # so we need to ignore these cases
            # no_boxes_predicted = iou.shape[1] != 0

            # if no_boxes_predicted and len_targets != 0:
            #    # Calculate miss classification - counting the number of IOU less than 0.3
            #    miss_detections_rate += 0 if len_targets == 0\
            #        else torch.count_nonzero(max_iou < 0.3) / len_targets

            # false_positive_classification
            #    matching_index_iou = torch.argmax(iou, dim=1)
            #    count_miss_class = np.count_nonzero(targets['labels'] != pred['labels'][matching_index_iou])
            #    false_positive_classification += count_miss_class / len_targets

            # log images here

        for i in range(len(dataset)):
            if webcam:  # batch_size >= 1
                p, im0, frame = (
                    path[i],
                    im.squeeze(0).numpy().transpose(1, 2, 0).copy(),
                    dataset.count,
                )
                s += f'{dict(Counter(pred["labels"].cpu().numpy()))}:'
            else:
                p, im0, frame = (
                    path,
                    im.squeeze(0).numpy().transpose(1, 2, 0).copy(),
                    getattr(dataset, "frame", 0),
                )
            # .cpu().numpy()
            # image_to_log = im0.transpose(1, 2, 0)
            proc_img = cv2.cvtColor(im0, cv2.COLOR_BGR2RGB)

            proc_img = add_bounding_boxes(
                proc_img,
                pred["labels"],
                pred["boxes"],
                pred_score=pred["scores"],
                color_box=(1, 1, 1),
                thresh=0.001,
                return_pil=False,
            )
            # image_to_log.save(os.path.join(output_path, "test_" + dataset_test.get_image_name(cur_index)))

            # print("Average IOU all over the images: {}".format(total_iou / len(data_loader_test)))
            # print("Average miss classification all over the images: {}".format(false_positive_classification / len(data_loader_test)))
            # print("Average miss detection all over the images: {}".format(miss_detections_rate / len(data_loader_test)))
            # print("mAP score over all test images:{}".format(metric.compute()))

            # Save results (image with detections)
            p = Path(p)  # to Path
            save_path = str(save_dir / p.name.split(".")[0])  # im.jpg
            txt_path = str(save_dir / "labels" / p.stem) + (
                "" if dataset.mode == "image" else f"_{frame}"
            )  # im.txt
            gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh
            if True:
                if dataset.mode == "image":
                    cv2.imwrite(save_path, proc_img)
                else:  # 'video' or 'stream'
                    if vid_path[i] != save_path:  # new video
                        vid_path[i] = save_path
                        if isinstance(vid_writer[i], cv2.VideoWriter):
                            vid_writer[i].release()  # release previous video writer
                        if vid_cap:  # video
                            fps = vid_cap.get(cv2.CAP_PROP_FPS)
                            w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                            h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                        else:  # stream
                            fps, w, h = 30, im0.shape[1], im0.shape[0]
                        save_path = str(
                            Path(save_path).with_suffix(".mp4")
                        )  # force *.mp4 suffix on results videos
                        vid_writer[i] = cv2.VideoWriter(
                            save_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)
                        )
                    vid_writer[i].write(proc_img)

                # Write results
                for *xyxy, conf, cls in zip(
                    pred["boxes"], pred["scores"], pred["labels"]
                ):
                    if save_txt:
                        # Write to file
                        xywh = (
                            (xyxy2xywh(torch.tensor(xyxy[0]).view(1, 4)) / gn)
                            .view(-1)
                            .tolist()
                        )  # normalized xywh
                        line = (
                            (cls, *xywh, conf) if save_conf else (cls, *xywh)
                        )  # label format
                        with open(f"{txt_path}.txt", "a") as f:
                            f.write(("%g " * len(line)).rstrip() % line + "\n")

        # Print time (inference-only)
        LOGGER.info(
            f"{s}{'' if len(pred['boxes']) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms"
        )
        if c == 10:
            break

    # Print results
    t = tuple(x.t / c * 1e3 for x in dt)  # speeds per image
    LOGGER.info(
        f"Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}"
        % t
    )
    if save_txt or save_img:
        s = (
            f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}"
            if save_txt
            else ""
        )
        LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
    if update:
        strip_optimizer(weights[0])  # update model (to fix SourceChangeWarning)

def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--weights",
        nargs="+",
        type=str,
        default=ROOT / "yolov5s.pt",
        help="model path or triton URL",
    )
    parser.add_argument(
        "--source",
        type=str,
        default=ROOT / "data/images",
        help="file/dir/URL/glob/screen/0(webcam)",
    )
    parser.add_argument(
        "--data",
        type=str,
        default=ROOT / "data/coco128.yaml",
        help="(optional) dataset.yaml path",
    )
    parser.add_argument(
        "--imgsz",
        "--img",
        "--img-size",
        nargs="+",
        type=int,
        default=[640],
        help="inference size h,w",
    )
    parser.add_argument(
        "--conf-thres", type=float, default=0.25, help="confidence threshold"
    )
    parser.add_argument(
        "--iou-thres", type=float, default=0.45, help="NMS IoU threshold"
    )
    parser.add_argument(
        "--max-det", type=int, default=1000, help="maximum detections per image"
    )
    parser.add_argument(
        "--device", default="", help="cuda device, i.e. 0 or 0,1,2,3 or cpu"
    )
    parser.add_argument("--view-img", action="store_true", help="show results")
    parser.add_argument("--save-txt", action="store_true", help="save results to *.txt")
    parser.add_argument(
        "--save-conf", action="store_true", help="save confidences in --save-txt labels"
    )
    parser.add_argument(
        "--save-crop", action="store_true", help="save cropped prediction boxes"
    )
    parser.add_argument(
        "--nosave", action="store_true", help="do not save images/videos"
    )
    parser.add_argument(
        "--classes",
        nargs="+",
        type=int,
        help="filter by class: --classes 0, or --classes 0 2 3",
    )
    parser.add_argument(
        "--agnostic-nms", action="store_true", help="class-agnostic NMS"
    )
    parser.add_argument("--augment", action="store_true", help="augmented inference")
    parser.add_argument("--visualize", action="store_true", help="visualize features")
    parser.add_argument("--update", action="store_true", help="update all models")
    parser.add_argument(
        "--project", default=ROOT / "runs/detect", help="save results to project/name"
    )
    parser.add_argument("--name", default="exp", help="save results to project/name")
    parser.add_argument(
        "--exist-ok",
        action="store_true",
        help="existing project/name ok, do not increment",
    )
    parser.add_argument(
        "--line-thickness", default=3, type=int, help="bounding box thickness (pixels)"
    )
    parser.add_argument(
        "--hide-labels", default=False, action="store_true", help="hide labels"
    )
    parser.add_argument(
        "--hide-conf", default=False, action="store_true", help="hide confidences"
    )
    parser.add_argument(
        "--half", action="store_true", help="use FP16 half-precision inference"
    )
    parser.add_argument(
        "--dnn", action="store_true", help="use OpenCV DNN for ONNX inference"
    )
    parser.add_argument(
        "--vid-stride", type=int, default=1, help="video frame-rate stride"
    )
    opt = parser.parse_args()
    opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1  # expand
    print_args(vars(opt))
    return opt

def main(opt):
    check_requirements(exclude=("tensorboard", "thop"))
    run(**vars(opt))

if __name__ == "__main__":
    opt = parse_opt()
    main(opt)


from faker import Faker
import random
import os
import csv
import spacy
import re
import email
import nltk
import pandas as pd
from spacy.training.example import Example
import warnings

# Ignore all user warnings globally
warnings.simplefilter("ignore", UserWarning)

fake = Faker('en_GB')  # Use the 'en_GB' locale for UK-specific data

email_files = []

def FindFilePaths(path):

    for root, dirs, files in os.walk(path):
        for file in files:
            if "." not in file:
                email_files.append(os.path.join(root, file))

def ProcessFiles(email_file):

    with open(email_file, 'r') as f:
        raw_email = f.read()
        email_message = email.message_from_string(raw_email)
        body = ""
        for part in email_message.walk():
            if part.get_content_type() == 'text/plain':
                body += part.get_payload()
    return body

def GenNewEmail():

    # Generate a random number of emails to use for the paragraph
    num_emails = random.randint(5, 10)
    email_bodies = []
    for i in range(num_emails):
        email_file = random.choice(email_files)
        email_body = ProcessFiles(email_file)
        email_bodies.append(email_body)

    # Concatenate the email bodies to form a single paragraph
    paragraph = "\n\n".join(email_bodies)

    # Tokenize the paragraph into sentences using NLTK
    sentences = nltk.sent_tokenize(paragraph)

    # Generate a random number of sentences to use for the final paragraph
    num_sentences = random.randint(3, 5)

    # Select random sentences from the original paragraph to form the final paragraph
    final_sentences = []
    for i in range(num_sentences):
        sentence = random.choice(sentences)
        final_sentences.append(sentence)

    # Concatenate the final sentences to form the final paragraph
    final_paragraph = " ".join(final_sentences)

    # define the regular expression pattern to match URLs
    url_pattern = re.compile(
        r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')

    # remove the URLs from the text paragraph
    final_paragraph = re.sub(url_pattern, '', final_paragraph)

    return final_paragraph

def GenName():

    # Randomly choose whether to generate a single name or full name
    rand = fake.boolean()

    if rand:
        # Generate a name
        name = fake.name()
    else:
        if rand:
            name = fake.first_name()
        else:
            name = fake.last_name()

    return name

def GenEmail():

    # Generate an email with a random domain name
    email = fake.email(domain=fake.domain_name())
    return email

def GenPhone():
    # Randomly choose to generate a phone number in different formats
    rand = fake.boolean()

    phone_number = ''

    if rand:
        mobile_prefix = '07'
        mobile_suffix = ''.join([str(random.randint(0, 9)) for i in range(8)])
        mobile_number = f'{mobile_prefix}{mobile_suffix}'

        # Randomly select a format for the mobile number
        mobile_format = random.choice(['07{} {}'.format(mobile_suffix[:3], mobile_suffix[3:]),
                                       '+44 7{} {}'.format(
                                           mobile_suffix[:3], mobile_suffix[3:]),
                                       '07{}-{}'.format(mobile_suffix[:3], mobile_suffix[3:])])
        phone_number = mobile_format

    else:
        # List of 10 UK area codes
        area_codes = ['01', '02', '03', '05', '07',
                      '08', '0161', '01642', '01752', '020']
        # Generate a random landline number within the selected area code
        area_code = random.choice(area_codes)
        landline_number = f'{area_code}{random.randint(10000, 999999)}'
        # Randomly select a format for the landline number
        landline_format = random.choice(['{} {}'.format(landline_number[:5], landline_number[5:]),
                                         '{}-{}'.format(
                                             landline_number[:5], landline_number[5:]),
                                         '+44 {} {}'.format(
                                             landline_number[:4], landline_number[4:]),
                                         '+44-{}-{}'.format(landline_number[:4], landline_number[4:])])
        phone_number = landline_format

    return str(phone_number)

def GenNHS():

    # Generate a random NHS number
    nhs_number = fake.numerify('##########')

    # Choose a random format
    formats = ['nnn nnn nnnn', 'nnnnnnnnnn', 'nnn-nnn-nnnn', 'nnn.nnn.nnnn']
    format_choice = random.choice(formats)

    # Format the NHS number accordingly
    if format_choice == 'nnn nnn nnnn':
        formatted_number = '{} {} {}'.format(
            nhs_number[:3], nhs_number[3:6], nhs_number[6:])
    elif format_choice == 'nnnnnnnnnn':
        formatted_number = nhs_number
    elif format_choice == 'nnn-nnn-nnnn':
        formatted_number = '{}-{}-{}'.format(
            nhs_number[:3], nhs_number[3:6], nhs_number[6:])
    else:
        formatted_number = '{}.{}.{}'.format(
            nhs_number[:3], nhs_number[3:6], nhs_number[6:])

    return str(formatted_number)

def GenAddress():

    # generate a random Building Number
    house_number = fake.building_number()
    # randomly choose either 'A' or 'a' ' '
    letter = random.choice(['A', 'a', ''])

    house_number = str(house_number)+letter

    uk_address = house_number + " " + fake.street_name() + " " + \
        fake.city() + " " + fake.postcode()

    return uk_address

def GenCard():

    # Choose a random format
    formats = ['XXXX XXXX XXXX XXXX',
               'XXXX-XXXX-XXXX-XXXX', 'XXXXXXXXXXXXXXXX']
    chosen_format = random.choice(formats)

    # Choose a random format
    cards = ['visa', 'mastercard', 'amex']
    card_choice = random.choice(cards)
    # Generate a credit card number
    credit_card_number = fake.credit_card_number(card_type=card_choice)

    # Format the credit card number according to the chosen format
    if chosen_format == 'XXXX XXXX XXXX XXXX':
        formatted_card_number = ' '.join(
            [credit_card_number[i:i+4] for i in range(0, 16, 4)])
    elif chosen_format == 'XXXX-XXXX-XXXX-XXXX':
        formatted_card_number = '-'.join([credit_card_number[i:i+4]
                                         for i in range(0, 16, 4)])
    else:
        formatted_card_number = credit_card_number.replace(' ', '')

    return str(formatted_card_number)

def GenBank():

    bank_account_number = fake.random_number(digits=8)

    return str(bank_account_number)

def GenSortCode():

    sort_code = str(fake.random_number(digits=6))

    # randomly choose one of the two formats
    format_choice = random.choice(["xxxxxx", "xx-xx-xx"])

    # format the sort code accordingly
    if format_choice == "xx-xx-xx":
        sort_code = '{}-{}-{}'.format(
            sort_code[:2], sort_code[2:4], sort_code[4:6])

    return str(sort_code)

def GenDOB():

    # Generate a random date of birth and format it as '01 Jan 1990'
    dob = fake.date_of_birth().strftime('%d %b %Y')

    return str(dob)

def GenKeywords():

    KeyWords = []
    # Generate a random number between 0 and 9
    random_number = random.randint(0, 10)

    # Generate a random number between 1 and 2
    random_number_2 = random.randint(1, 2)

    # Create an if-elseif statement based on the generated random number
    if random_number == 0:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 1:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 2:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['NHS', GenNHS()])
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 3:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['CreditCard', GenCard()])
            KeyWords.append(['NHS', GenNHS()])
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 4:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['PhoneNo', GenPhone()])
            KeyWords.append(['CreditCard', GenCard()])
            KeyWords.append(['NHS', GenNHS()])
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 5:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['Address', GenAddress()])
            KeyWords.append(['PhoneNo', GenPhone()])
            KeyWords.append(['CreditCard', GenCard()])
            KeyWords.append(['NHS', GenNHS()])
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 6:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['Name', GenName()])
            KeyWords.append(['Address', GenAddress()])
            KeyWords.append(['PhoneNo', GenPhone()])
            KeyWords.append(['CreditCard', GenCard()])
            KeyWords.append(['NHS', GenNHS()])
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 7:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['Email', GenEmail()])
            KeyWords.append(['Name', GenName()])
            KeyWords.append(['Address', GenAddress()])
            KeyWords.append(['PhoneNo', GenPhone()])
            KeyWords.append(['CreditCard', GenCard()])
            KeyWords.append(['NHS', GenNHS()])
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    elif random_number == 8:

        # Create a for loop based on the generated random number
        for i in range(random_number_2):
            KeyWords.append(['DOB', GenDOB()])
            KeyWords.append(['Email', GenEmail()])
            KeyWords.append(['Name', GenName()])
            KeyWords.append(['Address', GenAddress()])
            KeyWords.append(['PhoneNo', GenPhone()])
            KeyWords.append(['CreditCard', GenCard()])
            KeyWords.append(['NHS', GenNHS()])
            KeyWords.append(['BankAccount', GenBank()])
            KeyWords.append(['SortCode', GenSortCode()])

    else:

        KeyWords.append([' ', ' '])

    return KeyWords

def GetIndices(keywords, paragraph):

    used_indices = set()

    for keyword in keywords:
        start_index = paragraph.find(keyword[1])
        if start_index != -1:
            end_index = start_index + len(keyword[1]) - 1

            used_indices.add((start_index, end_index))

    return used_indices

def IntroduceNewKeywords():

    KeyWords = GenKeywords()
    KeyWords2 = KeyWords.copy()
    final_paragraph = GenNewEmail()

# Insert keywords into random non-overlapping places in the paragraph
    used_indices = set()

    # Generate a random index that does not overlap with any previous keyword
    index = random.randint(0, len(final_paragraph) - 1)

    final_paragraph = final_paragraph[:index] + ' ' + \
        KeyWords[0][1] + ' ' + final_paragraph[index+len(KeyWords[0][1]):]

    used_indices = GetIndices(KeyWords2, final_paragraph)

    del KeyWords[0]

    for keyword in KeyWords:

        length = len(keyword[1])

        # Generate a random index that does not overlap with any previous keyword
        start = random.randint(0, len(final_paragraph) - 1)

        end = start + length

        for start, end in used_indices:
            start = random.randint(0, len(final_paragraph) - 1)
            end = start + length

        final_paragraph = final_paragraph[:start] + ' ' + \
            keyword[1] + ' ' + final_paragraph[start+len(keyword[1]):]

        used_indices = GetIndices(KeyWords2, final_paragraph)

    return FindIndices(KeyWords2, final_paragraph)

def FindIndices(keywords, paragraph):

    keyword_list = []

    for keyword in keywords:
        start_index = paragraph.find(keyword[1])
        if start_index != -1:
            end_index = start_index + len(keyword[1]) - 1

            item = {'Label': keyword[0], 'Keyword': keyword[1],
                    'Start': start_index, 'End': end_index}

            keyword_list.append(item)

    # append a new row
    return pd.Series({'Text': paragraph, 'Features': keyword_list})

FindFilePaths('maildir')

# create empty dataframe
df = pd.DataFrame(columns=["Text", "Features"])

for i in range(100):

    pd_series = IntroduceNewKeywords()
    delete_row = False
    # Keep track of seen start and end values
    seen_start = set()
    seen_end = set()

    for feat in pd_series['Features']:
        start = feat['Start']
        end = feat['End']
        # Check if start or end value has been seen before
        if start in seen_start or end in seen_end:
            delete_row = True
            break
        seen_start.add(start)
        seen_end.add(end)

    if not delete_row:
        # append the new row to the DataFrame
        df = pd.concat([df, pd_series.to_frame().T],
                       ignore_index=True)
    else:
        print('Delete')

    i = i+1

    # Clear seen sets for next row
    seen_start.clear()
    seen_end.clear()

# Load the spaCy model
nlp = spacy.blank('en')

# Define the new labels
LABELS = ['Name', 'Email', 'NHS', 'DOB', 'Address',
          'PhoneNo', 'CreditCard', 'BankAccount', 'SortCode']

# Add the new labels to the model
ner = nlp.create_pipe("ner")
for label in LABELS:
    ner.add_label(label)
nlp.add_pipe('ner', last=True)
nlp.replace_pipe("ner", "ner")
nlp.initialize()

# Load the training data from the Pandas DataFrame
train_data = []
for i, row in df.iterrows():
    text = row['Text']
    features = row['Features']

    ents = []

    for feat in features:
        label = feat['Label']
        start = feat['Start']
        end = feat['End']
        keyword = feat['Keyword']
        ents.append((start, end, label))

    doc = nlp(text)

    spans = []

    for start, end, label in ents:
        span = doc.char_span(start, end, label=label)
        if span is not None:
            spans.append(span)
    doc.spans['entities'] = spans
    train_data.append(Example.from_dict(doc, {'entities': spans}))

# Train the model

nlp.begin_training()

for i in range(20):
    losses = {}
    nlp.update(train_data, losses=losses, drop=0.2)
    print(losses)

score = nlp.evaluate(train_data)
# print(score)

# Save the trained model to disk
output_dir = "Save/"
nlp.to_disk(output_dir)


import numpy as np
import torch

joints = [[0,1], [1, 2], [2, 3], [3, 4], [4, 5], [6, 7], [7, 8], [8, 9],
            [9, 10], [10, 11], [12, 13], [2, 12]]
# sample_path = '3d_sample.npy'

# # should be 16 * 3 dim xyz
# prediction_3d = np.load(sample_path)
# limbs = [0] * len(mpii_edges)
# # apex for computing the angle
# relative_angles = []
# absolute_angles = np.zeros([16, 3])
# offset = prediction_3d[0]

def joint_angles(prediction_3d):
    jangles = torch.zeros([12, 3])
    for i, joint in enumerate(joints):
        e1, e2 = joint
        i1, i2 = mpii_edges[e1]
        i3, i4 = mpii_edges[e2]
        v1 = prediction_3d[i1] - prediction_3d[i2]
        v2 = prediction_3d[i4] - prediction_3d[i3]

        jangles[i] = torch.acos(torch.dot(v1, v2) / torch.norm(v1) / torch.norm(v2))
    return jangles

def absolute_angles(prediction_3d):
    absolute_angles = np.zeros([14, 3])
    offset = prediction_3d[8] ## offset !!!!!!!
    limbs = np.zeros([14, 1])
    for i in range(len(mpii_edges)):
        i1, i2 = mpii_edges[i]
        e1 = prediction_3d[i1] - prediction_3d[i2]
        l = np.linalg.norm(e1)
        limbs[i] = l
        absolute_angles[i] = np.arccos(e1/l)
    return absolute_angles, limbs, offset

def anglelimbtoxyz(offset, absolute_angles, limbs):
    limbs = limbs.float()
    res_3d = torch.zeros([4, 16, 3]).cuda()

    norm_direction = torch.cos(absolute_angles)
    limbs = limbs.repeat(1,1,3)
    direction = limbs * norm_direction

    res_3d[:, 0] = offset
    res_3d[:, 1] = res_3d[:, 0] - direction[:, 0]
    res_3d[:, 2] = res_3d[:, 1] - direction[:, 1]
    res_3d[:, 6] = res_3d[:, 2] - direction[:, 2]
    res_3d[:, 3] = res_3d[:, 6] - direction[:, 3]
    res_3d[:, 4] = res_3d[:, 3] - direction[:, 4]
    res_3d[:, 5] = res_3d[:, 4] - direction[:, 5]
    res_3d[:, 8] = res_3d[:, 6] - direction[:, 12]
    res_3d[:, 9] = res_3d[:, 8] - direction[:, 13]
    res_3d[:, 13] = res_3d[:, 8] - direction[:, 9]
    res_3d[:, 14] = res_3d[:, 13] - direction[:, 10]
    res_3d[:, 15] = res_3d[:, 14] - direction[:, 11]
    res_3d[:, 12] = res_3d[:, 8] + direction[:, 8]
    res_3d[:, 11] = res_3d[:, 12] + direction[:, 7]
    res_3d[:, 10] = res_3d[:, 11] + direction[:, 6]

    return res_3d

mpii_edges = [[0, 1], [1, 2], [2, 6], [6, 3], [3, 4], [4, 5],
                [10, 11], [11, 12], [12, 8], [8, 13], [13, 14], [14, 15],
              [6, 8], [8, 9]]

def anglelimbtoxyz2(offset, absolute_angles, limbs):
    limbs = limbs.float()
    b = limbs.shape[0]
    res_3d = torch.zeros([b, 16, 3]).cuda()

    norm_direction = torch.cos(absolute_angles)
    limbs = limbs.repeat(1,1,3)
    direction = limbs * norm_direction

    res_3d[:, 8] = offset
    res_3d[:, 13] = res_3d[:, 8] - direction[:, 9]
    res_3d[:, 14] = res_3d[:, 13] - direction[:, 10]
    res_3d[:, 15] = res_3d[:, 14] - direction[:, 11]
    res_3d[:, 12] = res_3d[:, 8] + direction[:, 8]
    res_3d[:, 11] = res_3d[:, 12] + direction[:, 7]
    res_3d[:, 10] = res_3d[:, 11] + direction[:, 6]
    res_3d[:, 9] = res_3d[:, 8] - direction[:, 13]
    res_3d[:, 6] = res_3d[:, 8] + direction[:, 12]
    res_3d[:, 3] = res_3d[:, 6] - direction[:, 3]
    res_3d[:, 4] = res_3d[:, 3] - direction[:, 4]
    res_3d[:, 5] = res_3d[:, 4] - direction[:, 5]
    res_3d[:, 2] = res_3d[:, 6] + direction[:, 2]
    res_3d[:, 1] = res_3d[:, 2] + direction[:, 1]
    res_3d[:, 0] = res_3d[:, 1] + direction[:, 0]

    return res_3d

corres = [10,9,8,11,12,13,17,17,1,17,4,3,2,5,6,7]

def cords_to_map(cords, img_size, sigma=6):
    result = torch.zeros([cords.size(0), 18, 256, 176])
    for i, points in enumerate(cords):
        for j in range(16):
            co = corres[j]
            point = points[j]
            xx, yy = torch.meshgrid(torch.arange(img_size[0], dtype=torch.int32).cuda(), torch.arange(img_size[1],dtype=torch.int32).cuda())
            xx = xx.float()
            yy = yy.float()
            res = torch.exp(-((yy - point[0]) ** 2 + (xx - point[1]) ** 2) / (2 * sigma ** 2))
            result[i, co] = res

    return result


def readInt(msg):
    """
    Função para a leitura de valores tipo int
    :param msg: valor a ser processado
    :return: valor convertido para int
    """
    try:
        number = int(input(msg))
    except (ValueError, TypeError, UnboundLocalError):
        print('\033[31mERRO! O valor digitado é inválido, digite um valor inteiro\033[m')
    except (KeyboardInterrupt):
        print('\033[31mO usúario preferiu não digitar o valor\033[m')
        number = 0
    else:
        return number

def line():
    print('\033[1m-\033[m'*42)

def title(msg):
    print('\033[1m-\033[m'*42)
    print(f'{msg:^42}')
    print('\033[1m-\033[m' * 42)

def menu(*option):
    title('MENU')
    count = 1
    for item in option:
        print(f'\033[94m{count}° - \033[m\033[93m{item}\033[m')
        count += 1

    answer = readInt('\nDigite sua opção: ')

    return answer

Exercício Python #115 - Cadastro de pessoas - C/lib/interface/__init__.py


#!/usr/bin/python3
"""
Matrix div
"""

def matrix_divided(matrix, div):
    """ matrix div definition """
    msg = "matrix must be a matrix (list of lists) of integers/floats"
    new_matrix = []
    if (type(div) is not int and type(div) is not float):
        raise TypeError("div must be a number")
    if (div == 0):
        raise ZeroDivisionError("division by zero")

    if (type(matrix) is list):
        line_len = len(matrix[0])
        for line in matrix:
            new_line = []
            if (type(line) is not list):
                raise TypeError(msg)
            if (len(line) != line_len):
                msg = "Each row of the matrix must have the same size"
                raise TypeError(msg)
            for item in line:
                if (type(item) is not int and type(item) is not float):
                    raise TypeError(msg)

                new_line.append(float("{0:.2f}".format((item/div))))

            new_matrix.append(new_line)
    else:
        raise TypeError(msg)

    return(new_matrix)


# -*- coding: utf-8 -*-
import sys
from pathlib import Path
from typing import Any, Optional, Tuple
from at.auth.client import AuthStatus, licensed
from at.gui import *
from at.gui.worker import run_thread
from at.logger import log
from at.result import Result
from at.io.utils import load_json, write_json
from atparser.app.utils.path import paths
from atparser.app.utils.state import state
# from PyQt5.QtCore import Qt, QThreadPool, pyqtSignal
# from PyQt5.QtGui import QFont
# from PyQt5.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QWidget

# When setting fixed width to QLineEdit ->
# -> add alignment=Qt.AlignmentFlag.AlignLeft when adding widget to layout

class SettingsTab(AtWidget):
    settingsChanged = pyqtSignal()
    serverStatusChanged = pyqtSignal(tuple)

    def __init__(self,
                 size: Tuple[Optional[int]] = (None, None),
                 parent: Optional[QWidget] = None,
                 *args,
                 **kwargs) -> None:
        super().__init__(parent=parent, *args, **kwargs)
        self.initUi(size)

    def setupUi(self, size):
        set_size(widget=self, size=size)

        layout = QVBoxLayout()
        layout.setContentsMargins(2, 4, 2, 0)
        driverLayout = QHBoxLayout()
        labelLayout = QHBoxLayout()
        buttonLayout = QHBoxLayout()

        self.app = Label(icon='app',
                         label=state.appname,
                         parent=self)
        self.version = Label(icon='hash',
                             label=state.version,
                             parent=self)
        self.driverSelect = ComboInput(label="WebDriver",
                                       labelsize=(140, 24),
                                       combosize=(100, 24),
                                       items=('Firefox', 'Chrome'),
                                       parent=self)
        self.driverExist = StatusLabel(icon='hdd-network-fill',
                                       statussize=(110, 24),
                                       parent=self)
        self.delayLaunch = IntInput(label='Launch delay',
                                    labelsize=(140, 24),
                                    editsize=(100, 24),
                                    parent=self)
        self.delayUrlChange = IntInput(label='URL change delay',
                                       labelsize=(140, 24),
                                       editsize=(100, 24),
                                       parent=self)
        self.delayPaginator = IntInput(label='Paginator delay',
                                       labelsize=(140, 24),
                                       editsize=(100, 24),
                                       parent=self)
        self.saveButton = Button(label="Αποθήκευση Αλλαγών",
                                 size=(160, 26),
                                 parent=self)
        self.status = StatusButton(parent=self)

        labelLayout.addWidget(self.app)
        labelLayout.addWidget(self.version, stretch=2, alignment=Qt.AlignmentFlag.AlignLeft)
        layout.addLayout(labelLayout)
        layout.addWidget(HLine())
        driverLayout.addWidget(self.driverSelect)
        driverLayout.addWidget(self.driverExist,
                               stretch=2,
                               alignment=Qt.AlignmentFlag.AlignLeft)
        layout.addLayout(driverLayout)
        layout.addWidget(self.delayLaunch, alignment=Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.delayUrlChange, alignment=Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.delayPaginator, alignment=Qt.AlignmentFlag.AlignLeft)

        buttonLayout.addWidget(self.saveButton, alignment=Qt.AlignmentFlag.AlignRight)
        layout.addLayout(buttonLayout)

        layout.addWidget(HLine())
        layout.addWidget(self.status, stretch=2, alignment=Qt.AlignmentFlag.AlignBottom)

        self.setLayout(layout)

    def initUi(self, size: tuple):
        self.setupUi(size)
        self.driverSelect.setCurrentText(state.webdriver)
        self.delayLaunch.setText(str(state.launch_delay))
        self.delayUrlChange.setText(str(state.urlchange_delay))
        self.delayPaginator.setText(str(state.paginator_delay))
        self.checkDrivers()
        self.driverSelect.subscribe(self.onDriverChange)
        self.saveButton.subscribe(self.onSave)

    def checkDrivers(self):
        if self.driverSelect.getCurrentText() == 'chrome':
            if paths.get_chromedriver(True).exists():
                self.driverExist.changeStatus("Driver exists", 'statusOk')
            else:
                self.driverExist.changeStatus("No Driver", 'statusError')
        else:
            if paths.get_firefoxdriver(True).exists():
                self.driverExist.changeStatus("Driver exists", 'statusOk')
            else:
                self.driverExist.changeStatus("No Driver", 'statusError')

    def onDriverChange(self):
        self.pickedDriver = self.driverSelect.getCurrentText()
        self.checkDrivers()

    def onSave(self):
        state.webdriver = self.driverSelect.getCurrentText()

        state.launch_delay = self.delayLaunch.getInt()
        state.urlchange_delay = self.delayUrlChange.getInt()
        state.paginator_delay = self.delayPaginator.getInt()

        self.settingsChanged.emit()

        self.popup.info("Οι ρυθμίσεις αποθηκεύτηκαν")

if __name__ == '__main__':
    cssGuide = paths.get_css(obj=True).joinpath("_style.css").read_text()
    SEGOE = QFont("Segoe UI", 9)

    app = QApplication(sys.argv)
    app.setFont(SEGOE)
    app.setStyle('Fusion')

    ui = SettingsTab(size=(600, None))
    ui.setStyleSheet(cssGuide)
    ui.show()

    sys.exit(app.exec_())


import tensorflow.contrib as tf_contrib
import tensorflow as tf
import tensorflow.contrib.slim as slim

def layer_norm(x, scope='layer_norm'):
    return tf_contrib.layers.layer_norm(x, center=True, scale=True, scope=scope)

def lrelu(x, alpha=0.2):
    return tf.nn.leaky_relu(x, alpha)

def Conv2D(inputs, filters, kernel_size=3, strides=1, scope='Conv2d'):
    if kernel_size == 3 and strides == 1:
        inputs = tf.pad(inputs, [[0, 0], [1, 1], [1, 1], [0, 0]], mode="REFLECT")
    if kernel_size == 7 and strides == 1:
        inputs = tf.pad(inputs, [[0, 0], [3, 3], [3, 3], [0, 0]], mode="REFLECT")
    if kernel_size == 3 and strides == 2:
        inputs = tf.pad(inputs, [[0, 0], [0, 1], [0, 1], [0, 0]], mode="REFLECT")
    return tf_contrib.layers.conv2d(
        inputs,
        num_outputs=filters,
        kernel_size=kernel_size,
        stride=strides,
        weights_initializer=tf.contrib.layers.variance_scaling_initializer(),
        biases_initializer=None,
        normalizer_fn=None,
        activation_fn=None,
        padding='VALID',
        scope=scope)

def Conv2DNormLReLU(inputs, filters, kernel_size=3, strides=1, scope='conv_blk'):
    with tf.variable_scope(name_or_scope=scope):
        x = Conv2D(inputs, filters, kernel_size, strides)
        x = layer_norm(x)
        return lrelu(x)

def dwise_conv(input, k_h=3, k_w=3, channel_multiplier=1, name='dwise_conv'):
    input = tf.pad(input, [[0, 0], [1, 1], [1, 1], [0, 0]], mode="REFLECT")
    with tf.variable_scope(name):
        in_channel = input.get_shape().as_list()[-1]
        w = tf.get_variable('w', [k_h, k_w, in_channel, channel_multiplier],
                            initializer=tf_contrib.layers.variance_scaling_initializer())
        conv = tf.nn.depthwise_conv2d(input=input, filter=w, strides=[1, 1, 1, 1], padding='VALID', name=name)
        return conv

def upsample(inputs, filters, scope='upsample'):
    '''
        An alternative to transposed convolution where we first resize, then convolve.
        See http://distill.pub/2016/deconv-checkerboard/
        For some reason the shape needs to be statically known for gradient propagation
        through tf.image.resize_images, but we only know that for fixed image size, so we
        plumb through a "training" argument
    '''
    new_H, new_W = 2 * tf.shape(inputs)[1], 2 * tf.shape(inputs)[2]
    inputs = tf.image.resize_images(inputs, [new_H, new_W])
    return Conv2DNormLReLU(inputs=inputs, filters=filters, scope=scope)

def InvertedRes_block(inputs, expansion_ratio, output_dim, scope='InvertedRes_block'):
    with tf.variable_scope(scope):
        bottleneck_dim = round(expansion_ratio * inputs.get_shape().as_list()[-1])
        net = Conv2DNormLReLU(inputs=inputs, filters=bottleneck_dim, kernel_size=1)

        net = dwise_conv(net)
        net = layer_norm(net, scope='layer_norm_1')
        net = lrelu(net)

        net = Conv2D(inputs=net, filters=output_dim, kernel_size=1)
        net = layer_norm(net, scope='layer_norm_2')

        if (int(inputs.get_shape().as_list()[-1]) == output_dim):
            net = inputs + net
        return net

def G_net(inputs):

    with tf.variable_scope('A'):
        x1 = Conv2DNormLReLU(inputs=inputs, filters=32, kernel_size=7, scope='conv_blk_1')              # 256，256，32
        x2 = Conv2DNormLReLU(inputs=x1, filters=64, kernel_size=3, strides=2, scope='conv_blk_2')   # 128，128，64
        x3 = Conv2DNormLReLU(inputs=x2, filters=64, kernel_size=3, strides=1, scope='conv_blk_3')   # 128，128，64

    with tf.variable_scope('B'):
        x4 = Conv2DNormLReLU(inputs=x3, filters=128, kernel_size=3, strides=2, scope='conv_blk_1')  # 64，64，128
        x5 = Conv2DNormLReLU(inputs=x4, filters=128, kernel_size=3, strides=1, scope='conv_blk_2')  # 64，64，128

    with tf.variable_scope('C'):
        x6 = Conv2DNormLReLU(inputs=x5, filters=128, kernel_size=3, strides=1, scope='conv_blk_1')  # 64，64，128
        x7 = InvertedRes_block(inputs=x6, expansion_ratio=2, output_dim=256, scope='r1')            # 64，64，256
        x8 = InvertedRes_block(inputs=x7, expansion_ratio=2, output_dim=256, scope='r2')            # 64，64，256
        x9 = InvertedRes_block(inputs=x8, expansion_ratio=2, output_dim=256, scope='r3')            # 64，64，256
        x10 = InvertedRes_block(inputs=x9, expansion_ratio=2, output_dim=256, scope='r4')            # 64，64，256
        x11 = Conv2DNormLReLU(inputs=x10, filters=128, kernel_size=3, strides=1, scope='conv_blk_2')  # 64，64，128

    with tf.variable_scope('D'):
        x12 = upsample(inputs=x11, filters=128, scope='upsample')                                     # 128，128，128

        x13 = Conv2DNormLReLU(inputs=x12, filters=128, scope='conv_blk')                              # 128，128，128

    with tf.variable_scope('E'):
        x14 = upsample(inputs=x13, filters=64, scope='upsample')                                      # 256，256，64
        x15 = Conv2DNormLReLU(inputs=x14, filters=64, scope='conv_blk_1')                             # 256，256，64
        x16 = Conv2DNormLReLU(inputs=x15, filters=32, kernel_size=7, scope='conv_blk_2')              # 256，256，32

    with tf.variable_scope('out_layer'):
        out = Conv2D(inputs=x16, filters=3, kernel_size=1)                                               # 256，256，3
        fake = tf.tanh(out)

    return fake

def G_net_unet(inputs):

    with tf.variable_scope('A'):
        x1 = Conv2DNormLReLU(inputs=inputs, filters=32, kernel_size=7, scope='conv_blk_1')              # 256，256，32
        x2 = Conv2DNormLReLU(inputs=x1, filters=64, kernel_size=3, strides=2, scope='conv_blk_2')   # 128，128，64
        x3 = Conv2DNormLReLU(inputs=x2, filters=64, kernel_size=3, strides=1, scope='conv_blk_3')   # 128，128，64

    with tf.variable_scope('B'):
        x4 = Conv2DNormLReLU(inputs=x3, filters=128, kernel_size=3, strides=2, scope='conv_blk_1')  # 64，64，128
        x5 = Conv2DNormLReLU(inputs=x4, filters=128, kernel_size=3, strides=1, scope='conv_blk_2')  # 64，64，128

    with tf.variable_scope('C'):
        x6 = Conv2DNormLReLU(inputs=x5, filters=128, kernel_size=3, strides=1, scope='conv_blk_1')  # 64，64，128
        x7 = InvertedRes_block(inputs=x6, expansion_ratio=2, output_dim=256, scope='r1')            # 64，64，256
        x8 = InvertedRes_block(inputs=x7, expansion_ratio=2, output_dim=256, scope='r2')            # 64，64，256
        x9 = InvertedRes_block(inputs=x8, expansion_ratio=2, output_dim=256, scope='r3')            # 64，64，256
        x10 = InvertedRes_block(inputs=x9, expansion_ratio=2, output_dim=256, scope='r4')            # 64，64，256
        x11 = Conv2DNormLReLU(inputs=x10, filters=128, kernel_size=3, strides=1, scope='conv_blk_2')  # 64，64，128

    with tf.variable_scope('D'):
        x12 = upsample(inputs=x11, filters=128, scope='upsample')                                     # 128，128，128
        x12 = tf.concat([x12, x3], axis=-1)
        x13 = Conv2DNormLReLU(inputs=x12, filters=128, scope='conv_blk')                              # 128，128，128

    with tf.variable_scope('E'):
        x14 = upsample(inputs=x13, filters=64, scope='upsample')                                      # 256，256，64
        x14 = tf.concat([x14, x1], axis=-1)
        x15 = Conv2DNormLReLU(inputs=x14, filters=64, scope='conv_blk_1')                             # 256，256，64
        x16 = Conv2DNormLReLU(inputs=x15, filters=32, kernel_size=7, scope='conv_blk_2')              # 256，256，32

    with tf.variable_scope('out_layer'):
        out = Conv2D(inputs=x16, filters=3, kernel_size=1)                                               # 256，256，3
        fake = tf.tanh(out)

    return fake

def unet_generator(inputs, channel=32, num_blocks=4):

    x0 = slim.convolution2d(inputs, channel, [7, 7], activation_fn=None)
    x0 = tf.nn.leaky_relu(x0)

    x1 = slim.convolution2d(x0, channel, [3, 3], stride=2, activation_fn=None)
    x1 = tf.nn.leaky_relu(x1)
    x1 = slim.convolution2d(x1, channel * 2, [3, 3], activation_fn=None)
    x1 = tf.nn.leaky_relu(x1)

    x2 = slim.convolution2d(x1, channel * 2, [3, 3], stride=2, activation_fn=None)
    x2 = tf.nn.leaky_relu(x2)
    x2 = slim.convolution2d(x2, channel * 4, [3, 3], activation_fn=None)
    x2 = tf.nn.leaky_relu(x2)

    for idx in range(num_blocks):
        x2 = resblock(x2, out_channel=channel * 4, name='block_{}'.format(idx))

    x2 = slim.convolution2d(x2, channel * 2, [3, 3], activation_fn=None)
    x2 = tf.nn.leaky_relu(x2)

    h1, w1 = tf.shape(x2)[1], tf.shape(x2)[2]
    x3 = tf.image.resize_bilinear(x2, (h1 * 2, w1 * 2))
    x3 = slim.convolution2d(x3 + x1, channel * 2, [3, 3], activation_fn=None)
    x3 = tf.nn.leaky_relu(x3)
    x3 = slim.convolution2d(x3, channel, [3, 3], activation_fn=None)
    x3 = tf.nn.leaky_relu(x3)

    h2, w2 = tf.shape(x3)[1], tf.shape(x3)[2]
    x4 = tf.image.resize_bilinear(x3, (h2 * 2, w2 * 2))
    x4 = slim.convolution2d(x4 + x0, channel, [3, 3], activation_fn=None)
    x4 = tf.nn.leaky_relu(x4)
    x4 = slim.convolution2d(x4, 3, [7, 7], activation_fn=None)
    output = tf.tanh(x4)
    return output

def resblock(inputs, out_channel=32, name='resblock'):
    with tf.variable_scope(name):
        x = slim.convolution2d(inputs, out_channel, [3, 3],
                               activation_fn=None, scope='conv1')
        x = tf.nn.leaky_relu(x)
        x = slim.convolution2d(x, out_channel, [3, 3],
                               activation_fn=None, scope='conv2')

        return x + inputs


from django.conf.urls import url

from . import views

urlpatterns = [
        url(r'^questions/(?P<pk>[0-9]+)/$', views.QuestionList.as_view(), name='questions-list'),
        url(r'^question_answer_submit/$', views.QuestionAnswerSubmit.as_view(), name='question-answer-submit'),
        url(r'^tips_tricks/$', views.TipsAndTricks.as_view(), name='tips-tickets'),
        url(r'^articles/$', views.ArticleList.as_view(), name='articles')
]


#search_image_type.py

import cgi
import database_handler
import pymysql
import format

def main():

    print( """
    <!Doctype html>
    <html lang='en'>
    <head>
        <meta charset="UTF-8"/>
        <meta name='viewport' content='width=device-width, initial-scale=1.0' />
        <meta http-equiv='X-UA-Comptatible' content='ie=edge'/>

        <title>Gallery</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <link rel="stylesheet" href="css/style.py">
    </head>
    """)

    image_type = ''

    form = cgi.FieldStorage()

    if ( form.getvalue('image_type') ):
        image_type = form.getvalue('image_type')
        image_type = image_type.lower()

    if ( image_type == '' ):
        print("search_image_type.py main(): Error! Need to input field.")
        return 0
    print("""
        <body>
            <h1> Image Results with Type %s </h1>
        """ % image_type)

    database = database_handler.initialize()
    if ( database == -1 ):
        print("search_image_type.py main(): Error linking database.")
        return 0
    cursor = database.cursor()
    query = "SELECT image_id FROM detail WHERE type = '" + image_type + "'"
    cursor.execute(query)
    response = cursor.fetchall()
    image_ids = []
    if ( response == None ):
        print("<p>\nNo Results Found\n </p>")
    else:
        for row in response:
            image_ids.append(row[0])
        output = format.format_image_search(image_ids)
        print(output)
    print("""
            </body>
          """)
    return 0

if __name__ == '__main__':
    main()


import cv2
import os

# 이미지가 저장된 폴더 경로
origin_image_path = '0529/dataset/short_sleeve'
save_path = '0529/short_sleeve_resize/'

# 이미지 크기 조정 함수

def resize_img(folder_path, save_folder_path, size):
    cnt = 0
    for filename in os.listdir(folder_path):
        img_path = os.path.join(folder_path, filename)
        if os.path.isfile(img_path):
            img = cv2.imread(img_path)
            img = cv2.resize(img, size)
            save_folder = save_folder_path + \
                'short_sleeve_{0:04d}'.format(cnt) + '.jpg'
            cv2.imwrite(save_folder, img)
            cnt += 1

# 모든 이미지를 256x256 크기로 조정
resize_img(origin_image_path, save_path, (256, 256))
print(len(os.listdir(save_path)))


'''
THIS MODULE LOAD ALL THE NECCESSARY DATA FOR TRAINING

for mat data, it is a dict data which contains the following keys:
	timing
	names
	images
	drawings
	offsets
each of the keys contains a list
in each list, is numpy arrays

I rearranged this into a new data container: character
'''

import scipy.io

keys = ['timing', 'names', 'images', 'drawings', 'offsets']

def load_mat(file):
	mat = scipy.io.loadmat(file)
	return mat

mat_dict = load_mat("./data_background.mat")

# for key in keys:
# 	print(len(mat_dict[key]))

names = []
for i in range(len(mat_dict['names'])):
	names.append(str(mat_dict['names'][i].tolist()[0][0]))

# print(names)

# draw_dir = mat_dict['drawings'][0][0]
# draw_character = draw_dir[0][0]
# draw_img = draw_character[0][0]
# print(draw_img.shape)
# draw_stroke = draw_img[0][0]
# print(draw_stroke.shape)

character = {}
for index, (name, drawings_dir, images_dir, timing_dir) in enumerate(zip(names, mat_dict['drawings'], mat_dict['images'], mat_dict['timing'])):

	drawings_dir, images_dir, timing_dir = drawings_dir[0], images_dir[0], timing_dir[0]

	_dir = []
	for drawings_subdir, images_subdir, timing_subdir in zip(drawings_dir, images_dir, timing_dir):
		drawings_subdir, images_subdir, timing_subdir = drawings_subdir[0], images_subdir[0], timing_subdir[0]
		_subdir = []
		for drawings_img, images_img, timing_img in zip(drawings_subdir, images_subdir, timing_subdir):
			idrawings_img, images_img, timing_img = drawings_img[0], images_img[0], timing_img[0]

			_img = {}
			_img['data'] = images_img
			_img['time'] = []
			_img['strokes'] = []
			for drawings_stroke, timing_stroke in zip(drawings_img, timing_img):
				_img['strokes'].append(drawings_stroke[0])
				_img['time'].append(timing_stroke[0])
			_subdir.append(_img)
		_dir.append(_subdir)

	character[name] = _dir

# print(character['Greek'])


from json import dumps as json_dumps

from iso639.iso639 import _Language

from ..templating import Templator
from ..templating import TemplateType
from ..templating import TemplatedData
from ..templating import ResumeData
from ..templating import Address
from ..templating import WorkSummary
from ..templating import Education
from ..templating import EducationalOrganizationType

def convert_address(data: Address) -> dict:
    address_locality = data.city
    address_region = data.zone_code
    postal_code = data.postal_code
    street_address = f"{data.address_line_1}, {data.address_line_2}"
    return {
        "@type": "PostalAddress",
        "addressLocality": address_locality,
        "addressRegion": address_region,
        "postalCode": postal_code,
        "streetAddress": street_address
    }

def convert_work_summary(work_summary: WorkSummary) -> dict:
    return {
        "@type": "OrganizationRole",
        "alumniOf": {
            "@type": work_summary.type.value,
            "name": work_summary.employer,
            "sameAs": work_summary.url
        },
        "startDate": work_summary.start_date.isoformat(),
        "endDate": None if work_summary.end_date is None else work_summary.end_date.isoformat()
    }

def map_education_organization_type(education_type: EducationalOrganizationType) -> str:
    switch = {
        EducationalOrganizationType.UNIVERSITY: "CollegeOrUniversity",
        EducationalOrganizationType.HIGH_SCHOOL: "HighSchool"
    }
    return switch[education_type]

def convert_education_summary(education_summary: Education) -> dict:
    mapped_education_type = map_education_organization_type(education_summary.type)
    return {
        "@type": "OrganizationRole",
        "alumniOf": {
            "@type": mapped_education_type,
            "name": education_summary.name,
            "sameAs": education_summary.url
        },
        "startDate": education_summary.start_date.isoformat(),
        "endDate": None if education_summary.end_date is None else education_summary.end_date.isoformat()
    }

def convert_language(language: _Language) -> dict:
    return {
        "@type": "Language",
        "name": language.name,
        "alternateName": language.alpha2
    }

def convert_knowledge(programming: dict) -> list[str]:
    """
    See https://stackoverflow.com/a/13016200 for more info
    """
    return [x for v in programming.values() for x in v]

def convert(data: ResumeData) -> dict:
    address = convert_address(data.summaries.address)
    formatted_phone_number = data.summaries.personal_data.contact.phone.iso
    current_work_info = convert_work_summary(data.summaries.current_work_summary)

    previous_work_info = [convert_work_summary(previous_work_summary) for previous_work_summary in data.summaries.previous_work_summaries]
    education_info = [convert_education_summary(previous_education_institution) for previous_education_institution in data.summaries.education]
    organization_alumni = [*previous_work_info, *education_info]

    knowledge = convert_knowledge(data.summaries.skills.programming)
    languages_known = [convert_language(language) for language in data.summaries.skills.general.languages]

    return {
        "@context": "https://schema.org",
        "@type": "Person",
        "address": address,
        "name": data.summaries.personal_data.name,
        "description": data.summaries.personal_data.description,
        "email": f"mailto:{data.summaries.personal_data.contact.email}",
        "image": data.summaries.personal_data.image,
        "telephone": f"tel:{formatted_phone_number}",
        "url": data.summaries.personal_data.contact.url.full_url,
        "nationality": {
            "@type": "Country",
            "name": data.summaries.personal_data.nationality.alpha_3
        },
        "jobTitle": data.summaries.current_work_summary.job_title,
        "worksFor": current_work_info,
        "alumniOf": organization_alumni,
        "knowsAbout": knowledge,
        "knowsLanguage": languages_known
    }

class JSONLDTemplator(Templator):
    def template(self, data: ResumeData) -> TemplatedData:
        converted_data = convert(data)

        return TemplatedData(
            data=json_dumps(converted_data, indent=4),
            file_name="json-schema",
            type=self.type()
        )

    def type(self) -> TemplateType:
        return TemplateType.JSON

def load_templator() -> Templator:
    return JSONLDTemplator()


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

# author      : Tao Qitian
# email       : taoqt@mail2.sysu.edu.cn
# datetime    : 2021/5/11 00:02
# filename    : solution.py
# description : LC 100 相同的树

# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        """ DFS """
        # Case 1 两二叉树同一位置的结点为空
        if not p and not q:
            return True
        # Case 2 两二叉树同一位置的其中一个结点为空
        elif not p or not q:
            return False
        # Case 3 两二叉树的结点值不相同
        elif p.val != q.val:
            return False
        else:
            # Case 4 比较左右子树是否相同
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

    def isSameTreeBFS(self, p: TreeNode, q: TreeNode) -> bool:
        """ BFS """
        # 判断二叉树头结点是否相同
        if not p and not q:
            return True
        elif not p or not q:
            return False
        # 头结点入队
        queue1, queue2 = [p], [q]
        while queue1:
            # 结点出队
            node1, node2 = queue1.pop(), queue2.pop()
            # 首先判断同一位置的结点的值是否相同
            if node1.val != node2.val:
                return False
            # 判断左子结点是否为空，如果两左子结点为非空，入队
            left1, left2 = node1.left, node2.left
            if left1 and left2:
                queue1.append(left1)
                queue2.append(left2)
            # 否则不是相同的树
            elif (not left1 and left2) or (not left2 and left1):
                return False
            # 判断右子结点是否为空，如果两右子结点为非空，入队
            right1, right2 = node1.right, node2.right
            if right1 and right2:
                queue1.append(right1)
                queue2.append(right2)
            elif (not right1 and right2) or (not right2 and right1):
                return False
        # 两队必然相同为空，且对应结点的值都相同，所以是相同的树
        return True


import numpy as np

def AND(x1, x2):
    x = np.array([x1,x2])
    w = np.array([0.5,0.5])
    b = -0.7
    tmp = np.sum(w*x) + b

    if tmp <= 0 :
        return 0
    else :
        return 1

def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    tmp = np.sum(w * x) + b

    if tmp <= 0:
        return 0
    else:
        return 1

def OR(x1, x2):
    x = np.array([x1,x2])
    w = np.array([0.5,0.5])
    b = -0.2
    tmp = np.sum(w*x) + b
    if tmp <= 0 :
        return 0
    else :
        return 1

def XOR(x1, x2):
    s1 = NAND(x1,x2)
    s2 = OR(x1,x2)
    return AND(s1, s2)

def FullAdder(a,b,z):
    Sum = XOR(XOR(a,b),z)
    Carry = OR(AND(XOR(a,b),z),AND(a,b))
    result = np.array([Sum,Carry])
    return result

print('[a b z] = [s c]')
print('[0 0 0] =',FullAdder(0,0,0))
print('[0 0 1] =',FullAdder(0,0,1))
print('[0 1 0] =',FullAdder(0,1,0))
print('[0 1 1] =',FullAdder(0,1,1))
print('[1 0 0] =',FullAdder(1,0,0))
print('[1 0 1] =',FullAdder(1,0,1))
print('[1 1 0] =',FullAdder(1,1,0))
print('[1 1 1] =',FullAdder(1,1,1))


import argparse
import os
import subprocess
import sys

from ttb_utils import *

def make_arg_parser():
    p = argparse.ArgumentParser()
    return p

def commented_file_lines(f):
    for line in f:
        line = line.split('#', 1)[0]
        line = line.strip()

        if line:
            yield line

def entrypoint(argv):
    settings = make_arg_parser().parse_args(argv[1:])
    bundle = Bundle.open_default()
    install_dir = bundle.install_path()

    with open(bundle.path('tlpackages.txt')) as f:
        packages = list(commented_file_lines(f))

    # Validate that current repo, containers, and install are in sync

    git_hash, _ = get_repo_version()

    with open('/state/containers/GITHASH') as f:
        container_git_hash = f.readline().strip()

    if git_hash != container_git_hash:
        die(
            'refusing to proceed since current repo hash {git_hash} does not agree '
            'with that used to make containers; rerun `update-containers` step?'
        )

    with open(os.path.join(install_dir, 'GITHASH')) as f:
        install_git_hash = f.readline().strip()

    if git_hash != install_git_hash:
        die(
            'refusing to proceed since current repo hash {git_hash} does not agree '
            'with that used to make the installtion; recreate it?'
        )

    # OK, looks good

    try:
        # Note: the leading `./` in the exe path is vital so that the Perl code
        # can figure out its module search path.
        args = [
            './bin/x86_64-linux/tlmgr',
            '--repository', '/state/containers',
            'install',
        ]
        args += packages

        subprocess.check_call(
            args,
            shell = False,
            cwd = install_dir,
        )
    finally:
        chown_host(install_dir)

if __name__ == '__main__':
    sys.exit(entrypoint(sys.argv))


# coding:utf-8
#
# chatgpt/find_lora_module_name.py
#
# created by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on August 19, 2023
# updated by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on September 4, 2023
#
# 查看模型 lora 模块名.

from transformers import AutoModel

model = AutoModel.from_pretrained("chatgpt/Atom-7B")

for name, module in model.named_modules():
    print(name)


import numpy as np
from netCDF4 import Dataset
from enum import  Enum
from Libs.Scaling import ScalerListToArray
from datetime import datetime
from datetime import timedelta
import Libs.SmartOutput.julian
import matplotlib.pyplot as plt
import matplotlib.colors
import os
from sys import platform

import numpy as np
import matplotlib as mpl
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature
from A03_Hydraulics_Implementation.TRENDY.NetCDF_CVS import NetCDF_CVS_Reader
from A03_Hydraulics_Implementation.TRENDY.extraction import HalfDegreeExtractor
from A03_Hydraulics_Implementation.AutoAnalysis.PaperFigures_N2.Revisions2022.BiomassAmazonConverter \
    import BiomassAmazonAnomalyConverter

import os
rtpath = '../TRENDY/cVeg/conv'
files = [f for f in os.listdir(rtpath) if os.path.isfile(os.path.join(rtpath, f))]

file_str = rtpath + '/' + files[0]

ab_extractor = HalfDegreeExtractor('../Amazon_basin_05.tsv', '\t')
raisg_mask = r"../AB-SHAPE/Amazon_shape.shp"
mask = ShapelyFeature(Reader(raisg_mask).geometries(),
                      ccrs.PlateCarree())

normAGB = mpl.colors.Normalize(-5, 5)
colorsAGB = [[normAGB(-5), "#9C0200"],
             [normAGB(-2.5), "#ff9a00"],
             [normAGB(-0.5), "white"],
             [normAGB(0.0), "white"],
             [normAGB(0.5), "white"],
             [normAGB(5), "tab:blue"]]
cmapAGB = mpl.colors.LinearSegmentedColormap.from_list("", colorsAGB)

yois = [2005, 2007, 2009, 2010]

for yoi in yois:
    index = 1
    fig = plt.figure(figsize=(16, 10))

    yearbeginAGB = 1985
    yearEndAGB = 2010

    for file in files:
        filename = os.path.join(rtpath, file)
        reader = NetCDF_CVS_Reader(filename, incomplete=False)

        cVeg = reader.GetCompleteSpaceOverTimeRange('cVeg', yearbeginAGB, yearEndAGB)
        unit_str = reader.GetVarUnit('cVeg')

        converter = BiomassAmazonAnomalyConverter()
        diff_anomaly = converter.Convert_CVS(cVeg, yoi, yearbeginAGB, yearEndAGB, yearbeginAGB)

        ab_extractor.GetCVSCoords(reader.lons, reader.lats)
        img_ab = ab_extractor.CreateCVSImage(diff_anomaly)
        img_extent = ab_extractor.IMG_extent

        if (unit_str == 'kg m-2') | (unit_str == 'kg m^-2') | (unit_str == 'kg/m$^2$'):
            multiplier = 1.0
        elif (unit_str == 'kgC/m2') | (unit_str == 'kgC m-2') | (unit_str == 'kg C m-2'):
            multiplier = 1.0
        else:
            print("Invalid cVeg unit")
            exit(99)

        # To convert from kg AGB C m-2 to Mg C AGB Ha-1
        img_ab *= 10.0
        # Flip according to the latitude axis
        img_ab = img_ab[::-1, :]

        axGeo = fig.add_subplot(4, 4, index, projection=ccrs.PlateCarree())
        lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='g')
        lat_formatter = LatitudeFormatter()
        axGeo.xaxis.set_major_formatter(lon_formatter)
        axGeo.yaxis.set_major_formatter(lat_formatter)
        axGeo.add_feature(cfeature.BORDERS, edgecolor='tab:grey')
        axGeo.coastlines(resolution='110m', linewidth=1, color='tab:grey')
        axGeo.set_extent(np.array(img_extent), crs=ccrs.PlateCarree())
        axGeo.add_feature(mask, edgecolor='black', linewidth=1, facecolor="None")
        axGeo.text(0.97, 0.08, file[:len(file) - 11], horizontalalignment='right', verticalalignment='center',
                   transform=axGeo.transAxes, size=10,
                   bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))
        imsh = axGeo.imshow(img_ab, transform=ccrs.PlateCarree(),
                            extent=img_extent, cmap=cmapAGB, norm=normAGB)

        index += 1

    cax = plt.axes([0.12, 0.15, 0.78, 0.02])

    bar = plt.colorbar(imsh, cax=cax, orientation="horizontal")
    bar.set_label('Absolute AGB anomaly [Mg/ha]', fontsize=12)
    plt.subplots_adjust(wspace=0.1, hspace=0.1, bottom=0.2)
    plt.savefig('cVeg_AB_Anomaly_' + str(yoi) + '.png', dpi=300, bbox_inches='tight',
                    pad_inches=0)
    plt.close()


#https://lda.readthedocs.io/en/latest/autoapi/lda/index.html
import numpy as np
import pandas as pd
import lda
import re
import os

import gensim
from gensim.utils import simple_preprocess
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
import gensim.corpora as corpora
from pprint import pprint

import pickle
import pyLDAvis

#https://radimrehurek.com/gensim/models/ldamulticore.html
nw = 5
alpha = "auto"
eta = "auto"
passes = 200

def sent_to_words(sentences):
    for sentence in sentences:
        # deacc=True removes punctuations
        yield(gensim.utils.simple_preprocess(str(sentence), deacc=True))

def remove_stopwords(texts):
    return [[word for word in simple_preprocess(str(doc))
             if word not in stop_words] for doc in texts]

df = pd.read_csv("dataset.csv", dtype=object)
for index, row in df.iterrows():
    print("- Record {}/{}".format(str(index+1), str(len(df.index))))

    # Remove punctuation
    # Convert the titles to lowercase
    row['article_processed'] = str(row['article']).lower()
    row['article_processed'] = re.sub('[,!?]', '', row['article_processed'])

    #to list of sentences
    text = row['article_processed']
    new_data = pd.DataFrame()
    for index, t in enumerate(text.split('.')):
        new_data.loc[index,"article_processed"] = t.replace(".",'')

    #Prepare text for LDA analysis
    stop_words = stopwords.words('english')
    stop_words.extend(['from', 'subject', 're', 'edu', 'use'])

    data_ready = new_data.article_processed.values.tolist()
    #data_ready = data.title_processed.values.tolist() + data_ready
    data_words = list(sent_to_words(data_ready))
    # remove stop words
    data_words = remove_stopwords(data_words)

    #Create dictionary and terms frequencies
    id2word = corpora.Dictionary(data_words)
    # Create Corpus
    texts = data_words
    # Term Document Frequency
    corpus = [id2word.doc2bow(text) for text in texts]
    if corpus == [[]]:
        continue

    #LDA modeling
    # number of topics
    num_topics = 3
    # Build LDA model
    lda_model = gensim.models.LdaModel(corpus=corpus,id2word=id2word,num_topics=num_topics, eta=eta, alpha=alpha, passes=passes, random_state=30 )
    # Print the Keyword in the 3 topics
    for i in range(0,num_topics):
        df.loc[index,"topic "+str(i+1)] = str((lda_model.print_topics(num_words=nw))[1])

df.to_csv("results/res.csv")


'''
while 循环

猜数字游戏
计算机出一个1~100之间的随机数由人来猜
计算机根据人猜的数字分别给出提示大一点/小一点/猜对了

'''

from random import randint

answer = randint(1, 100)
couter = 0
while True:
    couter += 1
    number = int(input('请输入1~100以内的数字:'))
    if number < answer:
        print('小一点')
    elif number > answer:
        print('大一点')
    else:
        print('你猜对了')
        break
    print('你一共猜了%d次' % couter)
if couter > 7:
    print('你的智商余额明显不足，请及时充值')


from pprint import pprint

def get_cook_book(file_name):
    cook_book = {}
    dish_list = []
    with open(file_name) as f:
        for line in f:
            dish_name = line.strip()
            quantity_components = int(f.readline().strip())
            for component in range(quantity_components):
                components_dict = {'ingridient_name': None, 'quantity': None, 'measure': None}
                component = f.readline().strip().split('|')
                components_dict['ingridient_name'] =  component[0].strip()
                components_dict['quantity'] = int(component[1].strip())
                components_dict['measure'] = component[2].strip()
                dish_list.append(components_dict)
            f.readline()
            cook_book[dish_name] = dish_list
            dish_list = []
    return cook_book

def get_shop_list_by_dishes(dishes, person_count, cook_book):
    shop_list = {}
    for dish in dishes:
        if dish in cook_book.keys():
            for i in cook_book.get(dish):
                name_ingridient = i.get('ingridient_name')
                vol_ingridient = i.get('quantity')
                measure = i.get('measure')
                if name_ingridient in shop_list:
                    value = shop_list[name_ingridient]['quantity']
                    shop_list[name_ingridient]['quantity'] = value * 2
                else:
                    shop_list[name_ingridient] = {'measure': measure, 'quantity': vol_ingridient * person_count}
    return pprint(shop_list)

if __name__ == '__main__':
    file_name = input('Введите имя файла для чтения рецептов: ')
    cook_book = get_cook_book(file_name)
    get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2, cook_book)


def even_parameters(function):
    def decorator(*args):
        if all(isinstance(n, int) and n % 2 == 0 for n in args):
            result = function(*args)
            return result
        return "Please use only even numbers!"
    return decorator

@even_parameters
def add(a, b):
    return a + b

print(add(2, 4))
print(add("Peter", 1))

@even_parameters
def multiply(*nums):
    result = 1

    for num in nums:
        result *= num
    return result

print(multiply(2, 4, 6, 8))
print(multiply(2, 4, 9, 8))


# ---------------------------
# ------- Strings -----------
# ---------------------------

myStringOne = 'This is single qoute'
myStringtwo = "This is double qoutes"

print(myStringOne)
print(myStringtwo)

# --------------------------------

myStringThree = 'This is single qoute "test"'  # To print double qoutes => use scape sequence or other qoutes
myStringFour = "This is single qoute 'test'"  # To print single qoutes => use scape sequence or other qoutes

print(myStringThree)
print(myStringFour)

# -------------------------------

myStringFive = '''First
Second
Third '''
# myStringFive = 'First\nSecond\nThird '

myStringSix = """First
Second
Third """
# myStringSix = """First\nSecond\Third

print(myStringFive)
print(myStringSix)

# --------------------------------

myStringSeven = '''First
Second 'test' "test"
Third '''

myStringAight = """First
Second "test" 'test'
Third """

print(myStringSeven)
print(myStringAight)

# --------------------------------

myStringNighn = """First
Second "test" \ \\ 'test'  \
Third """

print(myStringNighn)


import numpy as np
import matplotlib
matplotlib.use('Agg')
matplotlib.rc('font', size=10)
import matplotlib.pyplot as plt

fn = '../expres/results_std.npy'
sep = 1
total = 10000
lw = 1.5
smf = 10
sep *= smf
results = np.load(fn, allow_pickle=True).item()
results = {
    'x': results['Proposed_64_r'],
    '0.5x': results['Proposed_64_r'] * 0.5,
    'k=2, F(x)': results['Proposed_64_Fr'],
    'k=2, F(G(z))': results['Proposed_64_Fg'],
    'k=1, F(x)': results['Proposed_64_k1_Fr'],
    'k=1, F(G(z))': results['Proposed_64_k1_Fg'],
}
t = np.arange(total // sep) * sep + sep
for key, value in results.items():
    value = np.reshape(value, [-1, total // smf, smf])
    value = np.mean(value, axis=2)
    v, e = np.mean(value, axis=0), np.std(value, axis=0)
    if len(v) != len(t):
        continue
#    plt.errorbar(t, v, yerr=e, label=key, linewidth=lw, fmt='.--', markersize='2', elinewidth=2)
    plt.plot(t, v, '--', label=key, linewidth=lw, markersize='5')
    plt.fill_between(t, v-e, v+e, alpha=0.4, antialiased=True)

plt.xlabel('Iterations')
plt.ylabel('Standard Deviation')
plt.xlim([sep, 2500])
plt.ylim([0, 0.8])
plt.legend(loc='upper right')

plt.savefig('../ggg.png')


import json

person_string = '{"name" : "Savaş" , "languages":["Python","Java"] }'

#JSON stirng to Dict

#result = json.loads(person_string)

#print(type(result))
#print(result)

#with open("jsonString",encoding="utf-8") as f :
#    data = json.load(f)
#    print(data)

person_dict = {

    "name": "Ali",
   "languages":"[Python,Java]"
}

#Dict to String
#result = json.dumps(person_dict)
#print(type(person_dict))

with open("jsonString","w") as file :
    json.dump(person_dict,file)


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

import redis
from flask import Flask
app = Flask(__name__)

"""
思路：
使用推技术，每个用户或仓库有变动的时候把消息推给粉丝的timelines里面

生产消息：
    1. 使用redis incr给每个消息生成唯一的msgID
    2. 变动的事件包括关注，fork，创建仓库，以及user_id, 变动对象的信息都写入到一个字典里面，然后序列化后存到redis的msgId里面
    3. 获取当前用户的粉丝列表，数据结构是set，用smember获取粉丝id
    4. 每个粉丝都维护一个timeline队列，遍历粉丝列表，给他们的timeline队列添加msgID

获取消息：
    1. 每个粉丝上来就获取自己的timeline队列

"""

def msg_produce():
    redis_ins = redis()
    # 使用Redis incr生成唯一的msgID
    msg_id = redis_ins.incr("global:msgID")

    # 消息内容
    user_id = 1
    msg_data = {
        "user_id": user_id,
        "timestamp": "1587601324",
        "action": "关注",
        "target_info": {
            "url": "xx.xx",
            "name": "xx",
            "user": ""
        },
        "from_info": {
            "url": "xx.xx",
            "name": "xx"
        }
    }

    # redis存储消息，msg_id->msg_data
    msg_data = json.dumps(msg_data)
    redis_ins.set(msg_id, msg_data)

    # 获取自己的粉丝列表, 并把消息写入他们的时间线
    follower_ids = redis_ins.smember("{user_id}:followers".format(user_id))
    for follower_id in follower_ids:
        redis_ins.lpush("{follower_id}:timeline".format(follower_id), msg_id)

@app.route('/')
def get_timelines(page, limit):
    start = (page-1) * limit
    redis_ins = redis()
    user_id = 2
    # 根据页数和limit逐页获取
    msg_ids = redis_ins.lrange("{user_id}:timeline".format(user_id), start, end+limit)
    result = []
    for msg_id in msg_ids:
        data = json.loads(redis.get(msg_id))
        result.append(data)

    return result


#!/usr/bin/python

"""
Harry Sarkis Terkanian

ROS Python topic subscriber and publisher

Subscribes to std_msgs.msg of type String on topic /chatter
Publishes received messages on topic /chatter2

see: wiki.ros.org/rospy/Overview/Publishers and Subscribers
"""

import rospy
from std_msgs.msg import String

pub = rospy.Publisher('chatter2', String, queue_size = 10)

def chatter_callback(msg, pub):
    """ROS callback function republishes message to topic '/chatter2'."""
    pub.publish(msg)

rospy.Subscriber("chatter", String, chatter_callback, pub)

if __name__ == '__main__':
    rospy.init_node('hst_chatter_subscriber')
    rospy.spin()


# Write a function that accepts two input lists and returns a
# new list which contains only the unique elements from both lists.
def list_eve9(lista,listb):
    newlist=[]
    for itema in lista:
        if itema not in newlist:
            newlist.append(itema)
    for itemb in listb:
        if itemb not in newlist:
            newlist.append(itemb)
    return newlist

print(list_eve9([1, 2, 3, 4, 5, 6], [5, 5, 6, 2, 8]))


# numCourses = 2
# prerequisites = [[1,0]]

numCourses = 2
prerequisites = [[1,0],[0,1]]

from collections import deque

# 위상 정렬 => 위상 정렬하려면 사이클이 없어야 함, 사이클 존재하면 false
# 모든 노드를 방문하기 전에 큐가 비어 있어 있을 때 사이클 존재 => 사이클이 존재하여 더 이상 진행 불가
# 강의가 노드, 전제 조건이 간선 정보가 된다.
'''
위상 정렬 구현 로직
1. 진입 차수가 0인 노드를 큐에 삽입
2. 큐에서 원소를 꺼내며 해당 원소와 연결된 노드의 진입 차수 - 1
3. 새롭게 진입 차수가 0이 된 노드를 큐에 삽입
사이클이 존재할 경우 모든 노드 방문 전에 큐가 비게 됨(진입 차수가 모두 0이 되기 전에 큐가 비게 됨)
'''

q = deque()
indegree = [0] * numCourses  # 진입 차수
graph = [[] for _ in range((numCourses))]  # 그래프

for i in range(len(prerequisites)):
    graph[prerequisites[i][1]].append(prerequisites[i][0])  # prerequisites[i][1]에서 prerequisites[i][0] 이동
    indegree[prerequisites[i][0]] += 1  # 진입 차수 + 1

# 위상 정렬 구현
# 진입 차수가 0인 노드 큐에 삽입
for i in range(numCourses):
    if indegree[i] == 0:
        q.append(i)

if len(q) == 0 and sum(indegree) != 0:
    print(False)
    exit(0)

while q:
    # 큐에서 원소 빼고 해당 노드와 연결된 노드의 진입 차수 - 1
    currNode = q.popleft()
    for i in graph[currNode]:
        indegree[i] -= 1

        # 진입 차수가 0이 된 노드 큐에 삽입
        if indegree[i] == 0:
            q.append(i)

    if len(q) == 0 and sum(indegree) != 0:
        print(False)
        exit(0)

print(True)

# 참고
# https://freedeveloper.tistory.com/390
# https://spongeb0b.tistory.com/215


# Waitrose online shopping webservice

__author__ = 'robdobsn'

from flask import Flask
from ShoppingManager import ShoppingManager
import logging
import time

app = Flask(__name__)

@app.route('/')
def index():
    return appName

@app.route('/waitrose/api/v1.0/<accountCode>/trolleyinfo')
def getTrolleyInfo(accountCode):
    return shoppingManager.getTrolleyInfo(accountCode)

if __name__ == '__main__':

    # Set logging level
    logging.basicConfig(level=logging.INFO)

    # App name
    appName = "Waitrose Service 0.1"
    logging.info(appName + " Starting")

    # Start headless display if required
    try:
        from pyvirtualdisplay import Display
        display = Display(visible=0, size=(800, 600))
        display.start()
        logging.info(appName + " is running headless")
    except ImportError:
        logging.info(appName + " is running on a GUI")

    # Start WaitroseManager
    shoppingManager = ShoppingManager()

    # Start server
    # use_reloader=False avoids initialising objects twice
    # note that server is single-threaded - and it doesn't seem to help to turn theading on - maybe due to GIL
    # so every request has to return quickly and not block
    # host=0.0.0.0 results in the machine's IP address being used
    # as opposed to localhost for the web server
    app.run(debug=True, use_reloader=False, host="0.0.0.0")


import random
import time

decoration = "----------------------------------"

DEPARTMENTS = [
	"Cauca",
	"Choco",
	"Nariño",
	"Putumayo",
	"Valle"
	]

MONTHS = [
	"January",
	"February",
	"March",
	"April",
	"May",
	"June"
	]

rain_statistics = []

def initialize_rains():
	for i in range(len(DEPARTMENTS)):
		rains = []

		for j in range(len(MONTHS)):
			random_number = round(random.random() * 100, 1)
			rains.append(random_number)

		rain_statistics.append(rains)

def month_average(month):
	my_sum = 0

	for i in rain_statistics:
		my_suma += i[month]

	average = my_sum / len(DEPARTMENTS) # my_sum / 5

	return average

def department_average(department):
	my_sum = sum(rain_statistics[department])
	average = my_sum / len(MONTHS) # my_sum / 6

	return average

def menu(mode):
	if mode == 1:
		print("Write the department:")
		department = int("1.Cauca \n2.Choco \n3.Nariño \n4.Putumayo \n5.Valle \n") - 1

		print(f"The average for the department {DEPARTMENTS[department]} is {department_average(department)}")
	else:
		print("Write the month:")
		month = int("1.January \n2.February \n3.March \n4.April \n5.May \n6.June \n") - 1

		print(f"The average for the month {MONTHS[month]} is {month_average(month)}")

def fill_out():
	initialize_rains()
	print("Filling rain matrix...")
	time.sleep(2)
	print("Matrix filled")

def print_matriz():
	print("{:^13}".format(""), end = " ")
	for month in MONTHS:
		print("{:^10}".format(month), end =" ")
	print("\n")

	for i, department in enumerate(DEPARTMENTS):
		print("{:<10}".format(department), end = " ")
		for rain in rain_statistics[i]:
			print("{:>10.1f}".format(rain), end = " ")
		print("")

# ------------------------------------- - ------------------------------------ #

option = 0

while option != 5:
	print(decoration)

	print("Write an option:")
	print("1. Fill the matrix \n2. Print the matrix \n3. Average rains by department \n4. Average rains by month \n5. Leave")
	option = int(input())

	print(decoration)

	if option == 1:
		fill_out()
	elif option == 2:
		print_matriz()
	elif option == 3:
		menu(1)
	elif option == 4:
		menu(2)


class Fridge:
    def __init__(self, color, price):
        self.color = color
        self.price = price
        self.__on = False

    def turn_on(self):
            self.__on = True

    def turn_of(self):
            self._on = False

    def __str__(self):
        return f"""
        - color: {self.color}
        - price: {self.price}
        - fridge on: {self.__on}
        """

class People:
    def __init__(self, name, money):
        self.name = name
        self.money = money
        self.fridge = None

    def buy_fridge(self, fridge: Fridge):
        if self.money >= fridge.price:
            self.money -= fridge.price
            self.fridge = fridge

    def __str__(self):
        return f"""
        - name: {self.name}
        - money: {self.money}
        - fridge: {self.fridge}
        """

davi = People("Davi", 1500)
print(davi)
fridge = Fridge('Blue', 1000)
print(fridge)
davi.buy_fridge(fridge)
print(davi)
fridge.turn_on()
print(fridge, davi)