"""
This module exists to make conversions of positions from/to the client.

By default the language server spec expects characters above the utf-16 range
to occupy 2 chars, so, something as:

aðŸ˜ƒ

logically (in python unicode) has 2 chars, but for the spec it needs to have
3 chars, so, we need to convert back and forth all positions to account for
this discrepancy.
"""

from robocorp_ls_core.protocols import (
    IDocument,
    IWorkspace,
    ITestInfoTypedDict,
    EvaluatableExpressionTypedDict,
)
from typing import List, Optional, Union, Iterable, Set
from robocorp_ls_core.lsp import (
    TextEditTypedDict,
    PositionTypedDict,
    DiagnosticsTypedDict,
    CompletionItemTypedDict,
    WorkspaceEditTypedDict,
    RangeTypedDict,
    LocationTypedDict,
    LocationLinkTypedDict,
    SelectionRangeTypedDict,
    CodeLensTypedDict,
    HoverTypedDict,
    DocumentHighlightTypedDict,
    CodeActionTypedDict,
    ShowDocumentParamsTypedDict,
)
import typing

def compute_utf16_code_units_len(s: str) -> int:
    if s.isascii():
        return len(s)

    tot = 0
    for c in s:
        tot += 1 if ord(c) < 65536 else 2
    return tot

def get_range_considering_utf16_code_units(s: str, start_col: int, end_col: int) -> str:
    if s.isascii():
        return s[start_col:end_col]

    if start_col == end_col:
        return ""

    assert end_col > start_col

    chars = []
    i = 0
    iter_in = iter(s)
    while start_col > i:
        c = next(iter_in)
        i += 1
        if ord(c) < 65536:
            continue
        i += 1

    while end_col > i:
        c = next(iter_in)
        chars.append(c)
        i += 1
        if ord(c) < 65536:
            continue
        i += 1

    return "".join(chars)

def convert_utf16_code_unit_to_python(s: str, col: int) -> int:
    if s.isascii():
        return col

    if col == 0:
        return 0

    tot = 0
    i = 0

    for c in s:
        tot += 1 if ord(c) < 65536 else 2
        i += 1
        if col == tot:
            return i

    return i

def convert_python_col_to_utf16_code_unit(
    d: IDocument,
    line,
    col,
    memo: Optional[dict] = None,
) -> int:
    if memo is not None:
        key = (line, col)
        try:
            return memo[key]
        except KeyError:
            pass

    line_contents = d.get_line(line)
    if line_contents.isascii():
        if memo is not None:
            memo[key] = col
        return col

    start_col = 0
    for i, c in enumerate(line_contents):
        if col == i:
            if memo is not None:
                memo[key] = start_col
            return start_col
        start_col += 1 if ord(c) < 65536 else 2

    if memo is not None:
        memo[key] = start_col
    return start_col

def _convert_start_end_range_python_code_unit_to_utf16_inplace(
    d: IDocument,
    start_pos: PositionTypedDict,
    end_pos: PositionTypedDict,
    memo: Optional[dict] = None,
) -> None:
    start_pos["character"] = convert_python_col_to_utf16_code_unit(
        d, start_pos["line"], start_pos["character"], memo=memo
    )
    end_pos["character"] = convert_python_col_to_utf16_code_unit(
        d, end_pos["line"], end_pos["character"], memo=memo
    )

def convert_range_pos_to_client_inplace(
    d: IDocument,
    r: RangeTypedDict,
    memo: Optional[dict] = None,
) -> RangeTypedDict:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    start_pos = r["start"]
    end_pos = r["end"]
    start_pos["character"] = convert_python_col_to_utf16_code_unit(
        d, start_pos["line"], start_pos["character"], memo=memo
    )
    end_pos["character"] = convert_python_col_to_utf16_code_unit(
        d, end_pos["line"], end_pos["character"], memo=memo
    )
    return r

def convert_location_or_location_link_pos_to_client_inplace(
    d: IDocument,
    location: Union[LocationTypedDict, LocationLinkTypedDict],
) -> Union[LocationTypedDict, LocationLinkTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    memo: dict = {}

    if "range" in location:
        loc = typing.cast(LocationTypedDict, location)
        r = loc["range"]
        convert_range_pos_to_client_inplace(d, r, memo)
    else:
        loc_link = typing.cast(LocationLinkTypedDict, location)

        r = loc_link["originSelectionRange"]
        convert_range_pos_to_client_inplace(d, r, memo)

        r = loc_link["targetRange"]
        convert_range_pos_to_client_inplace(d, r, memo)

        r = loc_link["targetSelectionRange"]
        convert_range_pos_to_client_inplace(d, r, memo)

    return location

def _iter_ranges_from_selection_range(
    selection_range: SelectionRangeTypedDict, visited: Set[int]
) -> Iterable[RangeTypedDict]:
    key = id(selection_range)
    if key not in visited:
        visited.add(key)
        yield selection_range["range"]

    parent = selection_range.get("parent")
    if parent:
        yield from _iter_ranges_from_selection_range(parent, visited)

def convert_selection_range_pos_to_client_inplace(
    d: IDocument,
    selection_ranges: List[SelectionRangeTypedDict],
    memo: Optional[dict] = None,
) -> List[SelectionRangeTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    visited: Set[int] = set()

    for selection_range in selection_ranges:
        for text_range in _iter_ranges_from_selection_range(selection_range, visited):
            _convert_start_end_range_python_code_unit_to_utf16_inplace(
                d, text_range["start"], text_range["end"], memo=memo
            )
    return selection_ranges

def convert_text_edits_pos_to_client_inplace(
    d: IDocument, text_edits: List[TextEditTypedDict], memo: Optional[dict] = None
) -> List[TextEditTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    for text_edit in text_edits:
        text_range = text_edit["range"]
        _convert_start_end_range_python_code_unit_to_utf16_inplace(
            d, text_range["start"], text_range["end"], memo=memo
        )
    return text_edits

def convert_code_lens_pos_to_client_inplace(
    d: IDocument, code_lens: List[CodeLensTypedDict], memo: Optional[dict] = None
) -> List[CodeLensTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    for code_len in code_lens:
        text_range = code_len["range"]
        _convert_start_end_range_python_code_unit_to_utf16_inplace(
            d, text_range["start"], text_range["end"], memo=memo
        )
    return code_lens

def convert_tests_pos_to_client_inplace(
    d: IDocument, tests: List[ITestInfoTypedDict], memo: Optional[dict] = None
) -> List[ITestInfoTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    for test in tests:
        text_range = test["range"]
        _convert_start_end_range_python_code_unit_to_utf16_inplace(
            d, text_range["start"], text_range["end"], memo=memo
        )
    return tests

def convert_evaluatable_expression_pos_to_client_inplace(
    d: IDocument,
    evaluatable_expr: Optional[EvaluatableExpressionTypedDict],
    memo: Optional[dict] = None,
) -> Optional[EvaluatableExpressionTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    if evaluatable_expr:
        text_range = evaluatable_expr["range"]
        _convert_start_end_range_python_code_unit_to_utf16_inplace(
            d, text_range["start"], text_range["end"], memo=memo
        )
    return evaluatable_expr

def convert_hover_pos_to_client_inplace(
    d: IDocument,
    hover: Optional[HoverTypedDict],
    memo: Optional[dict] = None,
) -> Optional[HoverTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    if hover:
        text_range = hover.get("range")
        if text_range:
            _convert_start_end_range_python_code_unit_to_utf16_inplace(
                d, text_range["start"], text_range["end"], memo=memo
            )
    return hover

def convert_document_highlight_pos_to_client_inplace(
    d: IDocument,
    doc_highlight_list: Optional[List[DocumentHighlightTypedDict]],
    memo: Optional[dict] = None,
) -> Optional[List[DocumentHighlightTypedDict]]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    if memo is None:
        memo = {}

    if doc_highlight_list:
        for doc_highlight in doc_highlight_list:
            text_range = doc_highlight.get("range")
            if text_range:
                _convert_start_end_range_python_code_unit_to_utf16_inplace(
                    d, text_range["start"], text_range["end"], memo=memo
                )
    return doc_highlight_list

def convert_diagnostics_pos_to_client_inplace(
    d: IDocument, diagnostics: List[DiagnosticsTypedDict]
) -> List[DiagnosticsTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    memo: dict = {}
    for diagnostic in diagnostics:
        text_range = diagnostic["range"]
        _convert_start_end_range_python_code_unit_to_utf16_inplace(
            d, text_range["start"], text_range["end"], memo=memo
        )
    return diagnostics

def convert_completions_pos_to_client_inplace(
    d: IDocument, completion_items: List[CompletionItemTypedDict]
) -> List[CompletionItemTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    memo: dict = {}
    for completion_item in completion_items:
        text_edit = completion_item.get("textEdit")
        if text_edit:
            text_range = text_edit["range"]
            _convert_start_end_range_python_code_unit_to_utf16_inplace(
                d, text_range["start"], text_range["end"], memo=memo
            )
        additional_text_edits = completion_item.get("additionalTextEdits")
        if additional_text_edits:
            convert_text_edits_pos_to_client_inplace(
                d, additional_text_edits, memo=memo
            )
    return completion_items

def convert_workspace_edit_pos_to_client_inplace(
    workspace: IWorkspace, workspace_edit: WorkspaceEditTypedDict
) -> WorkspaceEditTypedDict:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    changes = workspace_edit.get("changes")
    if changes:
        for doc_uri, text_edits in changes.items():
            doc = workspace.get_document(doc_uri, accept_from_file=True)
            if doc:
                convert_text_edits_pos_to_client_inplace(doc, text_edits)

    return workspace_edit

def convert_references_pos_to_client_inplace(
    workspace: IWorkspace, curr_doc: IDocument, references: List[LocationTypedDict]
) -> List[LocationTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    for reference in references:
        uri = reference["uri"]
        if uri == curr_doc.uri:
            convert_location_or_location_link_pos_to_client_inplace(curr_doc, reference)
        else:
            doc = workspace.get_document(uri, accept_from_file=True)
            if doc is not None:
                convert_location_or_location_link_pos_to_client_inplace(doc, reference)

    return references

def convert_code_action_pos_to_client_inplace(
    workspace: IWorkspace, code_action_list: List[CodeActionTypedDict]
) -> List[CodeActionTypedDict]:
    """
    Note: changes contents in-place. Returns the same input to help on composability.
    """
    for code_action in code_action_list:
        command = code_action.get("command")
        if not command:
            continue
        arguments = command.get("arguments")
        if not arguments:
            continue

        if not isinstance(arguments, list):
            continue

        for argument in arguments:
            if isinstance(argument, dict):
                apply_snippet = argument.get("apply_snippet")
                if apply_snippet:
                    workspace_edit = apply_snippet.get("edit")
                    if workspace_edit:
                        convert_workspace_edit_pos_to_client_inplace(
                            workspace, workspace_edit
                        )
                apply_edit = argument.get("apply_edit")
                if apply_edit:
                    workspace_edit = apply_edit.get("edit")
                    if workspace_edit:
                        convert_workspace_edit_pos_to_client_inplace(
                            workspace, workspace_edit
                        )

                # The show document depends on the contents written to the disk as
                # it'll usually reference the language server edit, so, it has to
                # be built with the proper calculated offsets.
                # show_document: Optional[ShowDocumentParamsTypedDict] = argument.get(
                #     "show_document"
                # )
                # if show_document:
                #     uri = show_document.get("uri")
                #     selection = show_document.get("selection")
                #     if selection and uri:
                #         doc = workspace.get_document(uri, accept_from_file=True)
                #         if doc:
                #             convert_range_pos_to_client_inplace(doc, selection)

    return code_action_list


import joblib
filename = 'finalized_model.sav'
# loaded_model = joblib.load(filename)
# result = loaded_model.predict([[-0.0010777]])
# print(result)

#MODEL TRAIN IN GOOGLE COLAB

# WITH LINEAR REGRESSION ALGORITHM
############################################################################################
############################################################################################
# from sklearn.datasets import load_diabetes
# from sklearn.linear_model import LinearRegression
# from sklearn.metrics import mean_squared_error,accuracy_score

# import numpy as np
# import matplotlib.pyplot as plt
# import joblib
# data = load_diabetes()
# print(data.DESCR)
# diabetes_x = data.data[:,np.newaxis,9]
# diabetes_x_train =  diabetes_x[:-420]
# diabetes_x_test = diabetes_x[-30:]
# diabetes_y_train = data.target[:-420]
# diabetes_y_test =  data.target[-30:]

# print(diabetes_x_test)

# model = LinearRegression().fit(diabetes_x_train,diabetes_y_train)
# p=model.predict(diabetes_x_test)
# print(p)
# print(diabetes_y_test)
# print(mean_squared_error(diabetes_y_test,p))
# filename = 'finalized_model.sav'
# joblib.dump(model, filename)

import tkinter
from tkinter import messagebox

win = tkinter.Tk()
win.title("SUGAR LEVEL")
win.geometry('440x440')
win.configure(bg='#333333')
def sig():
    loaded_model = joblib.load(filename)
    result = loaded_model.predict([[float(username_e.get())]])
    label.config(text=""+str(result))

username_label = tkinter.Label(
        win, text="ENTER THE SUGAR LEVEL", bg='#333333', fg="#FFFFFF", font=("Arial", 16))
username_e = tkinter.Entry(win,font=("Arial", 16))

button1=tkinter.Button(win,text="Enter", command=sig)
username_label.pack()
username_e.pack()
button1.pack()
label=tkinter.Label(win, text="", font=('Calibri 15'))
label.pack()

win.mainloop()


import torch
import numpy as np
import cv2

from projects.mmdet3d_plugin.uniad.dense_heads.occ_head_plugin import calculate_birds_eye_view_parameters

from mmdet.datasets.builder import PIPELINES
import os

@PIPELINES.register_module()
class GenerateOccFlowLabels(object):
    def __init__(self, grid_conf, ignore_index=255, only_vehicle=True, filter_invisible=True, deal_instance_255=False):
        self.grid_conf = grid_conf
        self.bev_resolution, self.bev_start_position, self.bev_dimension = calculate_birds_eye_view_parameters(
            grid_conf['xbound'], grid_conf['ybound'], grid_conf['zbound'],
        )
        # convert numpy
        self.bev_resolution = self.bev_resolution.numpy()
        self.bev_start_position = self.bev_start_position.numpy()
        self.bev_dimension = self.bev_dimension.numpy()
        self.spatial_extent = (grid_conf['xbound'][1], grid_conf['ybound'][1])
        self.ignore_index = ignore_index
        self.only_vehicle = only_vehicle
        self.filter_invisible = filter_invisible
        self.deal_instance_255 = deal_instance_255
        assert self.deal_instance_255 is False

        nusc_classes = ['car', 'truck', 'construction_vehicle', 'bus', 'trailer',
                        'barrier', 'motorcycle', 'bicycle', 'pedestrian', 'traffic_cone']
        vehicle_classes = ['car', 'bus', 'construction_vehicle',
                           'bicycle', 'motorcycle', 'truck', 'trailer']
        plan_classes = vehicle_classes + ['pedestrian']

        self.vehicle_cls_ids = np.array([nusc_classes.index(
            cls_name) for cls_name in vehicle_classes])

        self.plan_cls_ids = np.array([nusc_classes.index(
            cls_name) for cls_name in plan_classes])

        if only_vehicle:
            self.filter_cls_ids = self.vehicle_cls_ids
        else:
            self.filter_cls_ids = self.plan_cls_ids

    def reframe_boxes(self, boxes, t_init, t_curr):
        l2e_r_mat_curr = t_curr['l2e_r']
        l2e_t_curr = t_curr['l2e_t']
        e2g_r_mat_curr = t_curr['e2g_r']
        e2g_t_curr = t_curr['e2g_t']

        l2e_r_mat_init = t_init['l2e_r']
        l2e_t_init = t_init['l2e_t']
        e2g_r_mat_init = t_init['e2g_r']
        e2g_t_init = t_init['e2g_t']

        # to bbox under curr ego frame  # TODO: Uncomment
        boxes.rotate(l2e_r_mat_curr.T)
        boxes.translate(l2e_t_curr)

        # to bbox under world frame
        boxes.rotate(e2g_r_mat_curr.T)
        boxes.translate(e2g_t_curr)

        # to bbox under initial ego frame, first inverse translate, then inverse rotate
        boxes.translate(- e2g_t_init)
        m1 = np.linalg.inv(e2g_r_mat_init)
        boxes.rotate(m1.T)

        # to bbox under curr ego frame, first inverse translate, then inverse rotate
        boxes.translate(- l2e_t_init)
        m2 = np.linalg.inv(l2e_r_mat_init)
        boxes.rotate(m2.T)

        return boxes

    def __call__(self, results):
        """
        # Given lidar frame bboxes for curr frame and each future frame,
        # generate segmentation, instance, centerness, offset, and fwd flow map
        """
        # Avoid ignoring obj with index = self.ignore_index
        SPECIAL_INDEX = -20

        all_gt_bboxes_3d = results['future_gt_bboxes_3d']
        all_gt_labels_3d = results['future_gt_labels_3d']
        all_gt_inds = results['future_gt_inds']
        all_vis_tokens = results['future_gt_vis_tokens']
        num_frame = len(all_gt_bboxes_3d)

        # motion related transforms, of seq lengths
        l2e_r_mats = results['occ_l2e_r_mats']
        l2e_t_vecs = results['occ_l2e_t_vecs']
        e2g_r_mats = results['occ_e2g_r_mats']
        e2g_t_vecs = results['occ_e2g_t_vecs']

        # reference frame transform
        t_ref = dict(l2e_r=l2e_r_mats[0], l2e_t=l2e_t_vecs[0], e2g_r=e2g_r_mats[0], e2g_t=e2g_t_vecs[0])

        segmentations = []
        instances = []
        gt_future_boxes = []
        gt_future_labels = []

        # num_frame is 5
        for i in range(num_frame):
            # bbox, label, index of curr frame
            gt_bboxes_3d, gt_labels_3d = all_gt_bboxes_3d[i], all_gt_labels_3d[i]
            ins_inds = all_gt_inds[i]
            vis_tokens = all_vis_tokens[i]

            if gt_bboxes_3d is None:
                # for invalid samples, no loss calculated
                segmentation = np.ones(
                    (self.bev_dimension[1], self.bev_dimension[0])) * self.ignore_index
                instance = np.ones(
                    (self.bev_dimension[1], self.bev_dimension[0])) * self.ignore_index
            else:
                # reframe bboxes to reference frame
                t_curr = dict(l2e_r=l2e_r_mats[i], l2e_t=l2e_t_vecs[i], e2g_r=e2g_r_mats[i], e2g_t=e2g_t_vecs[i])
                ref_bboxes_3d = self.reframe_boxes(gt_bboxes_3d, t_ref, t_curr)
                gt_future_boxes.append(ref_bboxes_3d)
                gt_future_labels.append(gt_labels_3d)

                # for valid samples
                segmentation = np.zeros(
                    (self.bev_dimension[1], self.bev_dimension[0]))
                instance = np.zeros(
                    (self.bev_dimension[1], self.bev_dimension[0]))

                if self.only_vehicle:
                    vehicle_mask = np.isin(gt_labels_3d, self.filter_cls_ids)
                    ref_bboxes_3d = ref_bboxes_3d[vehicle_mask]
                    gt_labels_3d = gt_labels_3d[vehicle_mask]
                    ins_inds      = ins_inds[vehicle_mask]
                    if vis_tokens is not None:
                        vis_tokens = vis_tokens[vehicle_mask]

                if self.filter_invisible:
                    assert vis_tokens is not None
                    visible_mask = (vis_tokens != 1)   # obj are filtered out with visibility(1) between 0 and 40%
                    ref_bboxes_3d = ref_bboxes_3d[visible_mask]
                    gt_labels_3d = gt_labels_3d[visible_mask]
                    ins_inds = ins_inds[visible_mask]

                # valid sample and has objects
                if len(ref_bboxes_3d.tensor) > 0:
                    bbox_corners = ref_bboxes_3d.corners[:, [
                        0, 3, 7, 4], :2].numpy()
                    bbox_corners = np.round(
                        (bbox_corners - self.bev_start_position[:2] + self.bev_resolution[:2] / 2.0) / self.bev_resolution[:2]).astype(np.int32)

                    for index, gt_ind in enumerate(ins_inds):
                        if gt_ind == self.ignore_index:
                            gt_ind = SPECIAL_INDEX   # 255 -> -20
                        poly_region = bbox_corners[index]

                        cv2.fillPoly(segmentation, [poly_region], 1.0)
                        cv2.fillPoly(instance, [poly_region], int(gt_ind))

            segmentations.append(segmentation)
            instances.append(instance)

        # segmentation = 1 where objects are located
        segmentations = torch.from_numpy(
            np.stack(segmentations, axis=0)).long()
        instances = torch.from_numpy(np.stack(instances, axis=0)).long()

        # generate heatmap & offset from segmentation & instance
        instance_centerness, instance_offset, instance_flow, instance_backward_flow = self.center_offset_flow(
            instances,
            all_gt_inds,
            ignore_index=255,
            )

        invalid_mask = (segmentations[:, 0, 0] == self.ignore_index)
        instance_centerness[invalid_mask] = self.ignore_index

        results['gt_occ_has_invalid_frame'] = results.pop('occ_has_invalid_frame')
        results['gt_occ_img_is_valid'] = results.pop('occ_img_is_valid')
        results.update({
            'gt_segmentation': segmentations,
            'gt_instance': instances,
            'gt_centerness': instance_centerness,
            'gt_offset': instance_offset,
            'gt_flow': instance_flow,
            'gt_backward_flow': instance_backward_flow,
            'gt_future_boxes': gt_future_boxes,
            'gt_future_labels': gt_future_labels
        })
        return results

    def center_offset_flow(self, instance_img, all_gt_inds, ignore_index=255, sigma=3.0):
        seq_len, h, w = instance_img.shape
        # heatmap
        center_label = torch.zeros(seq_len, 1, h, w)
        # offset from parts to centers
        offset_label = ignore_index * torch.ones(seq_len, 2, h, w)
        # future flow
        future_displacement_label = ignore_index * torch.ones(seq_len, 2, h, w)

        # backward flow
        backward_flow = ignore_index * torch.ones(seq_len, 2, h, w)

        # x is vertical displacement, y is horizontal displacement
        x, y = torch.meshgrid(torch.arange(h, dtype=torch.float),
                            torch.arange(w, dtype=torch.float))

        gt_inds_all = []
        for ins_inds_per_frame in all_gt_inds:
            if ins_inds_per_frame is None:
                continue
            for ins_ind in ins_inds_per_frame:
                gt_inds_all.append(ins_ind)
        gt_inds_unique = np.unique(np.array(gt_inds_all))

        # iterate over all instances across this sequence
        for instance_id in gt_inds_unique:
            instance_id = int(instance_id)
            prev_xc = None
            prev_yc = None
            prev_mask = None
            for t in range(seq_len):
                instance_mask = (instance_img[t] == instance_id)
                if instance_mask.sum() == 0:
                    # this instance is not in this frame
                    prev_xc = None
                    prev_yc = None
                    prev_mask = None
                    continue

                # the Bird-Eye-View center of the instance
                xc = x[instance_mask].mean()
                yc = y[instance_mask].mean()

                off_x = xc - x
                off_y = yc - y
                g = torch.exp(-(off_x ** 2 + off_y ** 2) / sigma ** 2)
                center_label[t, 0] = torch.maximum(center_label[t, 0], g)
                offset_label[t, 0, instance_mask] = off_x[instance_mask]
                offset_label[t, 1, instance_mask] = off_y[instance_mask]

                if prev_xc is not None and instance_mask.sum() > 0:
                    delta_x = xc - prev_xc
                    delta_y = yc - prev_yc
                    future_displacement_label[t-1, 0, prev_mask] = delta_x
                    future_displacement_label[t-1, 1, prev_mask] = delta_y
                    backward_flow[t-1, 0, instance_mask] = -1 * delta_x
                    backward_flow[t-1, 1, instance_mask] = -1 * delta_y

                prev_xc = xc
                prev_yc = yc
                prev_mask = instance_mask

        return center_label, offset_label, future_displacement_label, backward_flow

    def visualize_instances(self, instances, vis_root=''):
        if vis_root is not None and vis_root != '':
            os.makedirs(vis_root, exist_ok=True)

        for i, ins in enumerate(instances):
            ins_c = ins.astype(np.uint8)
            ins_c = cv2.applyColorMap(ins_c, cv2.COLORMAP_JET)
            save_path = os.path.join(vis_root, '{}.png'.format(i))
            cv2.imwrite(save_path, ins_c)

        vid_path = os.path.join(vis_root, 'vid_ins.avi')
        height, width = instances[0].shape
        size = (height, width)
        v_out = cv2.VideoWriter(vid_path, cv2.VideoWriter_fourcc(*'DIVX'), 4, size)
        for i in range(len(instances)):
            ins_c = instances[i].astype(np.uint8)
            ins_c = cv2.applyColorMap(ins_c, cv2.COLORMAP_JET)
            v_out.write(ins_c)
        v_out.release()
        return


"""[EX] PointInTriangle"""

def main():
    """find for finish 0 score"""
    var_c, var_f, var_x = float(input()), float(input()), float(input())
    cookie = 2
    total, total_1, total_2 = 0, var_x/cookie, var_c/cookie+var_x/(cookie+var_f)
    while total_1 > total_2:
        total += var_c/cookie
        cookie += var_f
        total_1 = total+var_x/(cookie)
        total_2 = total+var_c/(cookie)+var_x/(cookie+var_f)
    total += var_x/cookie
    print(int(total) if total == float(int(total)) else "%.4f" %total)

main()


class Song:
    """ class to represent a song """  # should always be triple quotes

    def __init__(self, title, artist, duration=None):
        self.title = title
        self.artist = artist
        self.duration = duration

    def get_title(self):
        return self.title  # this is a getter

    name = property(get_title)  # existing code will now work, as we have made name the same as the title
    # note we don't use parenthesis for the function
    # Python property() function returns the object of the property class and it is used to create property of a class.
    # Properties represent an intermediate functionality between a plain attribute (or field) and a method.
    # In other words, they allow you to create methods that behave like attributes.
    # With properties, you can change how you compute the target attribute whenever you need to do so.

# a_string = "this is a \n a string that has been split and \t tabbed"
# print(a_string)
# raw_string = r" the split \n and tabbing \t chars dont work in a raw string however"  # useful for regex or writing filepaths
# print(raw_string)
#
# help(Song)  #  help functions provide information on classes, including the docstrings
# Song.__init__.__doc__ = """ initialise the variables for the object"""   # we can add docstrings after declaration of the class
# # above is added docstring to the class's init function
#
# help(Song.__init__)

class Album:
    """ Class to represent a collection of songs"""
    def __init__(self, name, year, artist=None): # add a default value to the artist
        self.name = name
        self.year = year

        if artist is None:  # we can have logic branches in init method, artist here references the argument literal, not the member variable, hence no self
            self.artist = "Various"  # just a string, not an object
        else:
            self.artist = artist
        self.tracks = []  # create an empty list as a member variable

    def add_song(self, song, position=None):

        song_found = find_object(song, self.tracks)
        if song_found is None:
            song_found = Song(self, self.artist)

            if position is None:
                self.tracks.append(song_found)  # add it to the end of the list
            else:
                self.tracks.insert(position,song_found) # otherwise insert at the desired position

class Artist:
    """ a basic class to store artist details"""
    def __init__(self, name ):
        self.name = name
        self.albums = []  # empty list

    def add_album(self, album):
        if album not in self.albums:  # if it doesnt already exist in the list
            self.albums.append(album)
    # so currently the artist object will have a reference to the album objects
    # and the album objects will have a reference to an artist object
    # this can cause an issue with garbage collection, the runtime system removed memory for objects when they're no longer being referred to
    # as an example, if the program is no longer using the album associated with an artist, it cant reclaim the memory used by the album object
    # because it's still being referred to by the artists list of albums, this is circular referencing
    # we can avoid this by only storing strings of artists in the song class, and the name of the artist in the album class
    # then there is no need to store the objects themselves, since we can make use of the find_object() function

    def add_song(self, name, year, title):
        """ Add a new song to the collection of albums
        This method adds a song to an album in the collection
        The album should be created if it doesn't already exist"""
        album_found = find_object(name, self.albums)
        if album_found is None:  # if the album does not exist
            print(name + " not found")
            album_found = Album(name, year, self.name)
            self.add_album(album_found)
        else:
            print("Found album " + name)
        album_found.add_song(title)  # implement into the album class's add_song method

def find_object(field, object_list):
    """
    check object list to see if an object with a 'name' attribute that matches exists, and returns it if it does
    """
    for object in object_list:
        if object.name == field:
            return object
    return None  # if it doesn't exist in the list

def load_data():
    new_artist = None
    new_album = None
    artist_list = []

    with open("albums.txt","r") as albums:
        for line in albums:
            artist_field, album_field, year_field, song_field = tuple(line.strip('\n').split('\t'))
            year_field = int(year_field) # change this to an int
            print(artist_field,album_field,year_field,song_field)

            if new_artist is None:
                new_artist = Artist(artist_field)  # we have read up a new artist, so create an Artist object
                artist_list.append(new_artist)  #  add it to the list
            elif new_artist.name != artist_field: # or if the current new artist is not equal to the current artist field value
                # retrieve the artist object if there is one, if not then create a new object and add it to the artist list
                new_artist = find_object(artist_field,artist_list)
                if new_artist is None:
                    new_artist = Artist(artist_field)
                    artist_list.append(new_artist)  # append the object
                new_album = None

            if new_album is None:
               new_album = Album(album_field, year_field, new_artist)  # create a new album object now that we've created a new_artist object
               new_artist.add_album(new_album)  # add it at the point you create the object
            elif new_album.name != album_field:  # we've read a new album of the current artist
                # retrieve the album object if there is one
                # otherwise create a new album object and store it in the artists collection
                new_album = find_object(album_field, new_artist.albums)
                if new_album is None:
                    new_album = Album(album_field, year_field, new_artist)
                    new_artist.add_album(new_album)

            # now create a new songs object and add it to the current album collection
            new_song = Song(song_field, new_artist)
            new_album.add_song(new_song)

    return artist_list

def create_checkfile(artist_list):
    """ create a check file from object data for comparison with original file"""
    with open("checkfile.txt", 'w') as checkfile:
        for new_artist in artist_list:
            for new_album in new_artist.albums:
                for new_song in new_album.tracks:
                    print("{0.name}\t{1.name}\t{1.year}\t{2.name}".format(new_artist,new_album,new_song), file=checkfile)

# at the moment, the current implementation of load_data has disadvantages, including needing to check the last line of the file every time
# and that fact that we dont check for existing artists objects when we encounter a new one
# we can change this so new objects are stored in their respective lists as soon as they're created

# the above approach uses classes but is not OOP, as we havent used inheritance, encapsulation, etc...

# remember that encapsulation involves encapsulating methods and data, not just methods

# lets rewrite load_data to delegate most of the work to the appropriate classes

def OOP_load_data():

    artist_list = []

    with open("albums.txt","r") as albums:
        for line in albums:
            artist_field, album_field, year_field, song_field = tuple(line.strip('\n').split('\t'))
            year_field = int(year_field) # change this to an int
            print(artist_field,album_field,year_field,song_field)

            new_artist = find_object(artist_field, artist_list)
            if new_artist is None: # we could not find the artist in the existing list
                new_artist = Artist(artist_field) # create the new object
                artist_list.append(new_artist)

            new_artist.add_song(album_field,year_field,song_field)

    return artist_list

if __name__ == '__main__':  # if the file is run as a script, then the following code executes
   artists = load_data()  # save the result
   print(len(artists))  # there are this many artists
   print(artists)

   create_checkfile(artists)


import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from rfpimp import importances
from rfpimp import dropcol_importances

df_genes = pd.read_csv('all_genes_distance_and_gc.csv')

## convert true/false to int

df_genes["te_nearby"] = df_genes["te_nearby"].astype(int)
df_genes["gene_nearby"] = df_genes["gene_nearby"].astype(int)
df_genes["lineage_conserved"] = df_genes["lineage_conserved"].astype(int)

df_genes = df_genes[['te_nearby','gene_nearby','lineage_conserved', 'gene_gc', 'flanking_1kb_gc', 'flanking_5kb_gc']]

y = df_genes['lineage_conserved']
x = df_genes.drop('lineage_conserved', axis=1)

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.1,random_state=101)

model = RandomForestClassifier()
model.fit(x_train,y_train)

print('score')
print(model.score(x_train,y_train))
print(model.score(x_test,y_test))

predictions = model.predict(x_test)

TP = len(predictions[(predictions == 1) & (y_test == 1)])
FN = len(predictions[(predictions == 0) & (y_test == 1)])
TN = len(predictions[(predictions == 0) & (y_test == 0)])
FP = len(predictions[(predictions == 1) & (y_test == 0)])

# specificity, how specific is the test? TN/TN+FP
print('specificity')
print(TN/(TN+FP))

# sensitivity, how sensitive is the test? TP/TP+FN
print('sensitivity')
print(TP/(TP+FN))

## PPV, how powerful is a positive? TP/TP+FP
print('PPV')
print(TP/(TP+FP))

## NPV, how powerful is a negative? TN/TN+FN
print('NPV')
print(TN/(TN+FN))

# if we called everything positive
print('if we just called everything positive')
TP = len(y_test[y_test == 1])
FN = 0
TN = 0
FP = len(y_test[y_test == 0])

print('specificity')
print(TN/(TN+FP))
print('sensitivity')
print(TP/(TP+FN))
print('PPV')
print(TP/(TP+FP))
# print('NPV')
# print(TN/(TN+FN))

## default importance
I = pd.DataFrame()
I['Feature'] = x_train.columns
I['Importance'] = model.feature_importances_
I = I.sort_values('Importance', ascending=False)
I = I.set_index('Feature')
I.to_csv('default_importances.txt',sep='\t')

# permutation importances
I = importances(model, x_test, y_test)
I.to_csv('permutation_importances.txt',sep='\t')

# # drop col importances
# I = dropcol_importances(model, x_train, y_train, x_test, y_test)
# I.to_csv('dropcol_importances.txt', sep='\t')


import flask.json as json
from datetime import date
from flask import url_for
from flask_testing import TestCase
from app import create_app, db, seed

class FlaskClientTestCase(TestCase):
    app_context = None

    @classmethod
    def create_app(cls):
        return create_app('testing')

    @classmethod
    def setUpClass(cls):
        cls.app = cls.create_app()
        cls.app_context = cls.app.app_context()
        cls.app_context.push()
        seed.seed()
        # db.drop_all()
        # db.create_all()
        # Role.insert_role()
        # User.insert_users()
        # Account.insert_account()
        # Year.insert_year()
        # Day.insert_day()
        # Operation.insert_operations()
        cls.client = cls.app.test_client()

    @classmethod
    def tearDownClass(cls):
        db.session.remove()
        db.drop_all()
        cls.app_context.pop()

    def get_api_headers(self):
        return {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    def test_get_operations(self):
        response = self.client.get(url_for('api.get_operations'))
        # Get the JSON
        json_response = json.loads(response.get_data())
        self.assertTrue(len(json_response['operations']) > 0)
        first_operation = json_response['operations'][0]
        self.assertEqual(first_operation['descr'], 'operation 1')
        self.assertEqual(first_operation['frequency'], 1)
        self.assertEqual(first_operation['value'], 1)

    def test_post_new_operation(self):
        response = self.client.get(url_for('api.get_accounts'))
        json_account = json.loads(response.get_data())
        self.assertGreaterEqual(len(json_account['accounts']), 1, 'No account in the database')
        account_id = json_account['accounts'][0]['id']
        operation = {
            'descr': 'New operation',
            'date': date.today(),
            'value': 123,
            'frequency': 1,
            'start_date': '',
            'end_date': '',
            'account_id': account_id
        }
        response = self.client.post(url_for('api.new_operation'),
                                    headers=self.get_api_headers(),
                                    data=json.dumps(operation))
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertEqual(json_response['descr'], operation['descr'])


import serial
import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.GPIO as GPIO
import time
import datetime
import Adafruit_BBIO.ADC as ADC
import random
from SBE_functions import *

# user settings
debug_mode = False
enable_output = True
set_RH_w_pot = False # use a potentiometer to set the ride height
add_noise_to_RH = True # add varying amounts of constant disturbances to the RH

# gains
# P_gain = 0.225#0.130#0.0625#0.25 # proportional gain
# I_gain = 0.0002#0.00015#0.0002 # integral gain
# D_gain = 5.5#2.85#135.0 # derivative gain

# Need to test control loop speed effects, maybe run everything on timed intervals to make independent of faster loops

# Competition settings, sensitive to measurement noise but strong fast control
# P_gain = 0.34#0.24
# I_gain = 0.0005#0.00030
# D_gain = 7.9#5.5#5.5

# Good set of gains below
# handle static SP well and analog noise decently
# encounters an area of unstable oscillations in the D term between SP 400 - 700 sometimes
P_gain = 0.24
I_gain = 0.0002 #.0002
D_gain = 1.75 #1.75#3.00 # was 3.75

error = 0 # 10x the error in mm
last_error = 0 # for storing errors from previous loop
sum_error = 0 # integral term of error

D_read_Hz = 20 # Read derivative changes at 100x per second. This can be tuned to roughly match the dynamics of the system.
D_read_ms = 1.0/D_read_Hz*1000 # time in ms between each derivative read
last_derivative_read = datetime.datetime.now()
last_derivate_error = 0
rolling_avg_D_errors = [0]*50 # 50 seems good

# initialization
P_term = 0
I_term = 0
D_term = 0

target_RH_init = 647 # mm
target_RH = target_RH_init # mm
output_angle = 90 # initial servo output angle
target_aoa = output_angle

# Analog reading
time.sleep(15) # attempt to solve bootup problem
ADC.setup()
poten_pin = "P9_33"
poten_value = 0 # input range 0 - 1.0
poten_values = [target_RH]*1000 # rolling average the analog read to smooth it out

# Noise
noise_range = 50 # mm - looking to get to 200 mm
wave_freq = 250 # ms
wave_noise_timer = datetime.datetime.now()
temp_wave = 0 # wave size parameter

# timing setup
delta = 0
last_time = datetime.datetime.now()
loop_time_last = 0.0 # ms
loop_time_average = 0.0 # ms
loop_times = [] # list of last n loop times
num_loops_avg = 100
oneshot = True # flag to do something once in the code

# setup to run for t seconds
start_time = datetime.datetime.now()
run_for_fixed_time = False
duration = 5

# test logging
enable_logging = False
dist_log = []

# servo output setup
# 1-HK 15138, 2-HS-815BB (ball tilt servo), 3-DS3218mg (wing)"soft" limits, 4-Moth servo
Servo_Type = 2
duty_min, duty_max = servo_parameters(Servo_Type) # parameters for various kroova servos stored in the function
duty_span = duty_max - duty_min

if enable_output:
    servo_pin = "P8_13"
    PWM.start(servo_pin, (duty_max-duty_min)/2.0+duty_min, 60)

output_angle = 90 # initial servo output angle

# serial input setup
ser = setupSerial()

rval = 0.0

sen_gain = 0.003384*25.4 # converts sensor reading to mm

gained_val = 0.0 # sensor reading in mm

while True:
    try:
        # ------------- pot code --------------
        if set_RH_w_pot:
            print(ADC.read(poten_pin))
        # poten_values.append(ADC.read("P9_40"))
        # poten_values.pop(0)
        # poten_value = sum(poten_values)/len(poten_values)
        # target_RH = int(poten_value/0.4*800+300)
        # print(target_RH)

        # ------------- Ride Height Noise Code -----
        # adding noise to the RH should provide similar disturbances to the type of noise the system will experience due
        # to wave chop
        if add_noise_to_RH:
            if (datetime.datetime.now() - wave_noise_timer).microseconds/1000 > wave_freq:
                wave_noise_timer = datetime.datetime.now()
                # generate an incoming wave
                temp_wave = random.randint(0,noise_range)
                if target_RH == target_RH_init: target_RH = target_RH + temp_wave
                else: target_RH = target_RH_init

        # ------------- timing code -------------
        delta = datetime.datetime.now() - last_time
        last_time = datetime.datetime.now()
        loop_time_last = float(delta.microseconds)/float(1000)
        if len(loop_times) < num_loops_avg:
            loop_times.insert(0,loop_time_last)
        elif oneshot == True:
            oneshot = False
            print("queue filled")
            loop_times.pop(num_loops_avg-1)
            loop_times.insert(0,loop_time_last)
        else:
            loop_times.pop(num_loops_avg-1)
            loop_times.insert(0,loop_time_last)

##        print(loop_time_last," milliseconds")

        if run_for_fixed_time:
            if (datetime.datetime.now() - start_time).seconds > duration:
                print("run duration reached")
                break
        # ------------- END OF timing code -------------
        # ------------- logging code -------------------
        dist_log.append(gained_val)

        # ------ END OF logging code -------------------
        rval = ToughSonicRead(ser)

        gained_val = rval * sen_gain # sensor reading in mm
##        print(gained_val)
##        print(round(gained_val,2), " inches")

        error = int(gained_val)-target_RH
        # print(error)

        P_term =  error*P_gain

        # I_term = (error + I_term)*I_gain
        if (target_aoa < 60 and target_aoa > -60): sum_error = error + sum_error
        else: sum_error = sum_error
        I_term = sum_error*I_gain

        # if (datetime.datetime.now() - last_derivative_read).microseconds/1000 >= D_read_ms:
        #     D_term = (error - last_derivate_error)*D_gain
        #     last_derivate_error = error # update the derivate error
        #     last_derivative_read = datetime.datetime.now() # update the derivative read time

        rolling_avg_D_errors.append(error)
        rolling_avg_D_errors.pop(0)
        # D_term = (error - float(sum(rolling_avg_D_errors))/len(rolling_avg_D_errors))*D_gain

        D_term = (error - float(sum(rolling_avg_D_errors))/len(rolling_avg_D_errors))*D_gain
        # if P_term > 60: D_term = (error - float(sum(rolling_avg_D_errors))/len(rolling_avg_D_errors))*D_gain*1.25
        # else: D_term = (error - float(sum(rolling_avg_D_errors))/len(rolling_avg_D_errors))*D_gain

        target_aoa = P_term +  I_term + D_term # control equation
        if enable_output: print("P term:", int(P_term)," D term:",int(D_term)," I term:",int(I_term),"target aoa:",int(target_aoa)," error:",error," sum error: ",sum_error," target RH:",target_RH)
        target_aoa = -target_aoa # flip if needed
        output_angle = target_aoa + 90 # convert aoa to absolute angle for servo

        # update error terms
        last_error = error

        # threshold servo commands in case of errors
        if output_angle > servo_max: output_angle = servo_max
        elif output_angle < servo_min: output_angle = servo_min

        # print(output_angle)

        if enable_output:
            duty = float(output_angle)
            duty = ((duty / 180) * duty_span + duty_min)
            PWM.set_duty_cycle(servo_pin, duty)

    except KeyboardInterrupt: # allows for easy program stop by tester
        break

# clean up
ser.close()
if enable_output:
    PWM.stop(servo_pin)
    PWM.cleanup()

# timing wrap up
total = 0 # total time for n loops
for lap in loop_times:
    total = total + lap
loop_time_average = total/num_loops_avg
print("average loop time: ",loop_time_average," ms")

# plot logs
if enable_logging:
    plt.plot(dist_log)
    plt.plot([0,len(dist_log)-1],[target_RH,target_RH])
    plt.xlabel('code loop iteration')
    plt.ylabel('distance [mm]')
    plt.grid(True)
    axis1 = plt.gca()
    axis1.set_ylim([150,1350])
    plt.show()
    plt.pause(0.0001)


import random
from methods import read_csv, write_dict_to_csv

def split_dataset_category(input_file):
    input_data = read_csv(input_file, True)

    category_to_questions = {}      # Dict where key = category, value = [list of real questions in that category, list of generated questions in that category]

     # Output dictionaries where key is question, value is category
    train_dict = {}
    testA_dict = {}
    testB_dict = {}

    # Add questions to categories
    for row in input_data:
        category = row[0].split(' - ')[0]
        question = row[2]
        source = row[3]

        if category in category_to_questions.keys():
            if source == 'Author Generated':
                category_to_questions.get(category)[1].append(question)
            else:
                category_to_questions.get(category)[0].append(question)
        else:
            category_to_questions[category] = [[], []]

            if source == 'Author Generated':
                category_to_questions.get(category)[1].append(question)
            else:
                category_to_questions.get(category)[0].append(question)

    for category in category_to_questions.keys():
        if category == '' or category == 'Other':
            continue

        real_questions = category_to_questions.get(category)[0]
        generated_questions = category_to_questions.get(category)[1]

        print(category, len(real_questions))

        random.shuffle(real_questions)
        random.shuffle(generated_questions)

        for question in generated_questions:
            testB_dict[question] = category

        counter = 0
        for i in range(len(real_questions)):
            question = real_questions[i]

            if counter < 20:
                if question not in train_dict:
                    counter += 1
                    train_dict[question] = category
            else:
                if question not in testA_dict:
                    counter += 1
                    testA_dict[question] = category

    return [train_dict, testA_dict, testB_dict]

#       MAIN        #
output_names = 'train20.csv', 'testA.csv', 'testB.csv'
input_file = 'data/final_master_dataset.csv'

sub_datasets = split_dataset_category(input_file)

for i in range(3):
    write_dict_to_csv(output_names[i], sub_datasets[i])


"""
This script makes updates to virus_properties when a new clade is added
New clades require new characteristic mutations
But we need to make sure we don't throw out other important information in virus properties
Hence this script loads both old and new virus_properties.json and makes the necessary updates
"""
#%%
import copy
import json

#%%
path_old = "/Users/corneliusromer/code/nextclade_data/data/datasets/sars-cov-2-21L/references/BA.2/versions/2023-08-09T12:00:00Z/files/virus_properties.json"
# path_old = "/Users/corneliusromer/code/nextclade_data/data/datasets/sars-cov-2/references/MN908947/versions/2023-08-09T12:00:00Z/files/virus_properties.json"
path_new = "/Users/corneliusromer/code/nextclade_data_workflows/sars-cov-2/virus_properties.json"
# path_out = "virus_properties_updated.json"
path_out = path_old
# Load old virus_properties.json
old = json.load(open(path_old, "r"))
new = json.load(open(path_new, "r"))
old_copy = copy.deepcopy(old)

# Need to keep the extra data
# Pull in "rev" data
# Replace those mutations that are now rev
#%%
#%%
for k,v in new["nucMutLabelMap"].items():
    old["nucMutLabelMap"][k] = copy.deepcopy(v) #list(filter(lambda x: x != "rec", v))
    print(k)

#%%
# Sort old["nucMutLabelMap"]
old["nucMutLabelMap"] = dict(sorted(old["nucMutLabelMap"].items(), key=lambda item: int(item[0][:-1])))
#%%
# Don't use these mutations as "characteristic" as they are flip floppy
for k in ["21T", "44T"]:
    try:
        old["nucMutLabelMap"].pop(k)
    except KeyError:
        pass

def reverse_a_dict(dict_of_lists):
    result = {}
    for k, v in dict_of_lists.items():
        for x in v:
            result.setdefault(x, []).append(k)
    return result

old["nucMutLabelMapReverse"] = dict(sorted(reverse_a_dict(old["nucMutLabelMap"]).items()))

with open(path_out, "w") as f_out:
    json.dump(old, f_out, indent=None, sort_keys=False)

# %%


import pandas as pd
import numpy as np
import os
import pdb

metadata = pd.read_csv("../../metadata/lake_metadata.csv")
metadata = metadata[metadata['num_obs'] > 0]
site_ids = metadata['site_id'].values

sites = []
rmses = []
final_df = pd.DataFrame()
rmse_per_site = np.empty((len(site_ids)))
def calc_rmse(predictions, targets):
    return np.sqrt(((predictions - targets) ** 2).mean())
for site_ct,site_id in enumerate(site_ids):
    print(site_ct,"/",len(site_ids), " site")
    if not metadata[metadata['site_id'] == site_id]['num_obs'].values[0] > 0:
        print("unobserved")
        continue
    else:
        df = pd.read_feather("../../results/SWT_results/outputs_"+site_id+".feather")
        loss_outputs = df['temp_pred'].values[np.isfinite(df['temp_actual'].values)]
        loss_actual = df['temp_actual'].values[np.isfinite(df['temp_actual'].values)]
        loss_dates = df['index'].values[np.isfinite(df['temp_actual'].values)]
        sites.append(site_id)
        rmse = calc_rmse(loss_outputs, loss_actual)
        print("rmse: ",rmse)
        rmses.append(rmse)
    #    spring runs from March 1 to May 31;
        # summer runs from June 1 to August 31;
        # fall (autumn) runs from September 1 to November 30; and.
        # winter runs from December 1 to February 28 (February 29 in a leap year).

        df = pd.DataFrame(data={'site_id':site_id, 'date':loss_dates, 'pred':loss_outputs, 'actual':loss_actual})
        final_df = pd.concat([final_df,df])

final_df.reset_index(inplace=True)
final_df.to_feather("../../results/final_all_obs.feather")
final_df.to_csv("../../results/final_all_obs.csv")

obs_sites = metadata[metadata['num_obs'] > 0]['site_id'].values
rmse_per_site = np.array([calc_rmse(final_df[final_df['site_id']==i_d]['pred'], final_df[final_df['site_id']==i_d]['actual']) for i_d in obs_sites])

pdb.set_trace()


import random
print("\n")
print("=" * 32)
print("Bem vindo ao jogo da advinhação")
print("=" * 32)
advinhar = """
          **
        *     *
       *       *
        *     *
             *
            *
            *

            *
"""
print(advinhar)
print("Nesse jogo você deve descobrir o número secreto, que pode ser qualquer número de 1 a 100")
jogar = 1

while jogar == 1:

    x = random.randint(1, 100)
    print(x) #esse print vai sair
    palpite = 0
    chances = 1

    print("\n\n**********  vamos começar!  **********")
    while palpite != x :
        palpite = int(input("\nQual o número secreto?   "))

        if palpite > x:
             print("Seu palpite foi muito alto")
             chances += 1
        elif palpite < x:
            print("Seu palpite foi muito baixo")
            chances += 1

    else:
        print("\n\nParabéns! Você descobriu o número secreto! Você usou ", chances, "tentativas")

    jogar = int(input("\nVocê deseja jogar novamente? Digite 1 para novo jogo ou qualquer outro caracter para sair:  "))

else:
    exit(0)


from vehicle import Vehicle

class Car(Vehicle):  # ereditarietà

    def brag(self):
        print('Look how cool my car is!')

car1 = Car()
car1.drive()

car1.add_warning('ciao')
# car1.__warnings.append([])

#  stampa l'oggetto come un dictionary (oppure possiamo aggiungere un dunder method repr nella classe)
print(car1.__dict__)

print(car1)  # usando __repr__ ora stampiamo qualcosa di leggibile anche così

car2 = Car(200)
car2.drive()

car3 = Car(200)
car3.drive()


class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        stack = []
        for c in s:
            if c in ('(', '[', '{'):
                stack.append(c)
            elif c in (')', ']', '}'):
                if len(stack) == 0 or not 0 < ord(c) - ord(stack.pop()) <= 2:
                    return False
            else:
                return False
        return len(stack) == 0


from fastapi import FastAPI, APIRouter
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware

from . import models
from .database import engine

from .routers.posts import router as posts_router
from .routers.users import router as users_router
from .routers.auth import router as auth_router
from .routers.vote import router as vote_router

models.Base.metadata.create_all(bind=engine)

api_router = APIRouter()

api_router.include_router(router=posts_router)
api_router.include_router(router=users_router)
api_router.include_router(router=auth_router)
api_router.include_router(router=vote_router)

tags_metadata = [
    {
        'name': 'authentication',
        'description': 'Login authentication - operations'
    },
    {
        'name': 'posts',
        'description': 'Post details - operations'
    },
    {
        'name': 'users',
        'description': 'User details - operations'
    },
    {
        'name': 'vote',
        'description': 'Voting details - operations'
    }
]

middleware = [
    Middleware(
        CORSMiddleware,
        allow_origins=['*'],
        allow_credentials=True,
        allow_methods=['*'],
        allow_headers=['*']
    )
]

app = FastAPI(
    title='FastAPI Course - freeCodeCamp',
    openapi_tags=tags_metadata,
    swagger_ui_parameters={
        'docExpansion': 'none'
    },
    middleware=middleware
)

app.include_router(router=api_router)

@app.get('/', tags=['home'])
async def root():
    return {
        'message': 'Hello World'
    }


import linecache

DATA = "/home/v-jiaya/unilm-moe/data/wikilingual/download-split/train_spm/"
LANGS = "ar cs de en es fr hi it id ja ko nl pt ru th tr vi zh".split()
for lg in LANGS:
    src_lines = linecache.getlines(f"{DATA}/train.{lg}_src")
    tgt_lines = linecache.getlines(f"{DATA}/train.{lg}_tgt")
    src_tokens = []
    tgt_tokens = []
    for i in range(len(src_lines)):
        src_len = len(src_lines[i].split())
        tgt_len = len(tgt_lines[i].split())
        src_tokens.append(src_len)
        tgt_tokens.append(tgt_len)

    print(f"{lg}-SRC: Avg: {sum(src_tokens) // len(src_tokens)} | Max: {max(src_tokens)}")
    print(f"{lg}-TGT: Avg: {sum(tgt_tokens) // len(tgt_tokens)} | Max: {max(tgt_tokens)}")


import random

HEIGHT = 22
WIDTH = 10

pieces = ['I', 'J', 'L', 'O', 'S', 'T', 'Z']
buffer = 3

global boardState

""" Rotation system """
with open("SRSrotation.txt") as SRSFile:
    data = [line.rstrip() for line in SRSFile.readlines()]
    tetrominos = {}
    mino = []
    rotation = []

    for line in data:
        if line != "" and line not in pieces:
            rotation.append(line)
        elif line == "":
            mino.append(rotation)
            rotation = []
        elif line in pieces:
            tetrominos[line] = mino
            mino = []
    # print(tetrominos)

class Tetromino:

    def __init__(self, piece: str):
        self.piece = piece
        self.rot = 0
        self.x = 3
        self.y = 0
        self.lockCounter = 0

    def draw(self, boardState):  # draws tetromino in board and returns updated boardState
        # . is empty, X is falling piece, O is locked piece
        rotationArray = tetrominos[self.piece][self.rot]
        length = len(rotationArray)
        toBeFilled = []
        toBeRemoved = []

        for i in range(HEIGHT):
            for j in range(buffer, WIDTH + buffer):
                if boardState[i][j] == "X":
                    # searches for Xs in board to be removed
                    toBeRemoved.append((i, j))
                if i < length and j < length + buffer:
                    newI = i + self.y
                    newJ = j + self.x
                    if rotationArray[i][j-buffer] == "X":
                        # looks at piece rotation template to find Xs
                        if boardState[newI][newJ] in [".", "X"]:
                            # if relative position of X on the board is empty or has temp block, fill
                            toBeFilled.append((newI, newJ))
                        else:
                            # if filled with locked piece or out of bounds, stop drawing attempt
                            return False, boardState  # to signify failure in order to reverse fields

        if toBeRemoved == toBeFilled:
            # print("did not move")
            return False, boardState

        for i, j in toBeRemoved:
            boardState[i] = boardState[i][:j] + "." + boardState[i][j+1:]
        # print("removed: {}".format(toBeRemoved))

        for i, j in toBeFilled:
            boardState[i] = boardState[i][:j] + "X" + boardState[i][j+1:]
        # print("filled: {}".format(toBeFilled))

        return True, boardState  # to signify success

    def rotateCW(self, boardState):
        self.rot = (self.rot + 1) % 4
        if not self.draw(boardState)[0]:
            self.rot = (self.rot - 1) % 4
        return boardState

    def rotateCCW(self, boardState):
        self.rot = (self.rot - 1) % 4
        if not self.draw(boardState)[0]:
            self.rot = (self.rot + 1) % 4
        return boardState

    def moveRight(self, boardState):
        self.x += 1
        if not self.draw(boardState)[0]:
            self.x -= 1
        return boardState

    def moveLeft(self, boardState):
        self.x -= 1
        if not self.draw(boardState)[0]:
            self.x += 1
        return boardState

    def fall(self, boardState):  # remember to add collison testing
        self.y += 1
        attemptDraw = self.draw(boardState)[0]
        if not attemptDraw:
            self.y -= 1
        return boardState, attemptDraw

    def hardDrop(self, boardState):
        print("harddrop")
        attemptDraw = self.draw(boardState)[0]
        while attemptDraw:
            print("R")
            self.y += 1
        return boardState, attemptDraw

    def incrementLock(self):
        self.lockCounter += 1

    def getPiece(self):
        return self.piece

    def getRot(self):
        return self.rot

    def getLockState(self):
        return self.lockCounter

    def getPos(self):
        print("({}, {})".format(self.x, self.y))
        return self.x, self.y


import inspect
import random

import piclang

ATOM_MUTATION_RATE = 1.0 # Atom mutations per organism
OP_MUTATION_RATE = 0.5 # Operator mutations per organism
CHANGE_TYPE_PROBABILITY = 0.1 # Chance an atom will change type

def crossbreed(g1, g2):
    """Crossbreeds two genomes and returns any viable children."""
    children = cut_and_splice(g1, g2)
    return [mutate(x) for x in children]

def cut_and_splice(g1, g2):
    p1 = random.randrange(len(g1))
    p2 = random.randrange(len(g2))
    return g1[:p1] + g2[p2:], g2[:p2] + g1[p1:]

def mutate(genome):
    atom_probability = ATOM_MUTATION_RATE / len(genome)
    op_probability = OP_MUTATION_RATE / len(genome)
    for i in range(len(genome)):
        if piclang.is_atom(genome[i]):
            if random.random() < atom_probability:
                genome[i] = mutate_atom(genome[i])
        else:
            if random.random() < op_probability:
                genome[i] = mutate_op(genome[i])
    return genome

def mutate_atom(instruction):
    if random.random() < CHANGE_TYPE_PROBABILITY:
        return random_atom(instruction)
    if isinstance(instruction, (int, float)):
        return mutate_number(instruction)
    if isinstance(instruction, tuple):
        if random.random < 0.5:
            return (instruction[0], mutate_number(instruction[1]))
        else:
            return (mutate_number(instruction[0]), instruction[1])
    atom_curves = (piclang.PlatonicCircle, piclang.PlatonicLine)
    if isinstance(instruction, atom_curves):
        return random.choice(atom_curves)()
    return instruction

def random_atom(old_atom=None):
    typ = random.choice([float, tuple, piclang.PlatonicCircle, piclang.PlatonicLine])
    if typ == float:
        return random.random()
    if typ == tuple:
        return (random.random(), random.random())
    return typ()

def mutate_number(num):
    if random.random() < 0.5:
        # Smaller
        return num * (1 - random.random() * 0.5)
    else:
        # Bigger
        return num * (1 + random.random())

def mutate_op(instruction):
    ops = [piclang.translate, piclang.scale, piclang.rotate, piclang.reverse, piclang.concat, piclang.repeat, piclang.step]
    return random.choice(ops)


from flask import request, g, jsonify

from application import app, db
from application.models.account import User
from application.models.smscenter import SMSAccount
from application.account.lib.gloxon import gloxonAuth
from application.utils.auth import generate_token

@app.route('/', methods=['GET'])
def home():
    return jsonify({}), 200

@app.route('/account/v1/token', methods=['POST'])
def get_token():
    data = request.get_json()
    authorization_code = data.get('authorization_code')
    referrer_id = data.get('rid', None)

    res = gloxonAuth.exchangeAuthorizationCode(authorization_code)
    if res['success']:
        gx_user = res['user']  # gloxon_user
        public_id = gx_user.get('public_id')
        email = gx_user.get('email')
        token = res.get('token').get('token')
        refresh = res.get('token').get('refresh')

        # Create user account if it does not exist.
        created = False
        user = User.get_user_using_public_id(public_id)
        if user is None:
            user = User(public_id=public_id, email=email)
            created = True

            if referrer_id:
                referrer = User.get_user_using_public_id(referrer_id)
                if referrer is not None:
                    user.referrer_id = referrer.id
        user.gloxon_token = token
        user.gloxon_refresh = refresh
        db.session.add(user)
        db.session.commit()

        if created: # Create an SMS default account.
            name = gx_user.get('first_name')[:11]
            account = SMSAccount(owner_id=user.id, name=name, is_default=True)
            account.is_floataccount=False
            account.set_public_id()
            db.session.add(account)
            db.session.commit()

        # request for a user token.
        token = generate_token(user)
        user_info = res.get('user')

        # Add some valuable information to the existing user info.
        user_info['is_affiliate'] = user.is_affiliate
        user_info['is_admin'] = user.is_admin
        response = jsonify({"success": True, "token": token, "user": user_info}), 200
    else:
        response = jsonify({"success": False, "token": "", "message": "Invalid Authorization Code"}), 404

    return response


# split the data
# import sys
# add my lib dir to the sys path
# sys.path.insert(0, '../lib/')

# import ../lib/reddit_corpus.py
import json
import re
import json
import concurrent.futures
import glob
import bz2
import lzma

# r/the_Donald, r/Democrats, r/republican, r/libertarian, r/books
the_donald = 't5_38unr'
democrats = 't5_2qn70'
republican = 't5_2qndt'
libertarian = 't5_2qh63'
books = 't5_2qh4i'

subreddit_ids = [the_donald, democrats, republican, libertarian, books]
# testfile = '/l/research/social-media-mining/public/RC_201-01-random-sample-1000000.jsonlines'

savedatadir = '/nobackup/ejblom/reddit'
# test_dir = '/nobackup/ejblom/reddit/test'

indatadir = '/l/research/social-media-mining/public/reddit'
commentdir = '/comments'
submissionsdir = '/submissions'
glob_end = '/RC_2017-*.'
# sub_glob_end = '/RS_2017-*'

glob_str = indatadir + commentdir + glob_end
# glob_str = indatadir + submissionsdir + sub_glob_end
filenames = sorted(glob.glob(glob_str))

# didn't catch decemeber 2017 because it was an xz, redoing that one
redo = ['/l/research/social-media-mining/public/reddit/comments/RC_2017-12.xz']

def filter_subs_comments(filename):
	match = re.search(r"(RC_2017-.{2})", filename)
	newfile = savedatadir + commentdir + '/filtered-' + match.group(1) + '.jsonlines'
	if 'bz2' in filename:
		with open(newfile, 'a+') as outfile, bz2.open(filename, 'rt') as infile:
			for line in infile:
				test_json = json.loads(line)
				if (test_json['subreddit_id'] in subreddit_ids):
					outfile.write(line)
	else:
		with open(newfile, 'a+') as outfile, lzma.open(filename, 'rt') as infile:
			for line in infile:
				test_json = json.loads(line)
				if (test_json['subreddit_id'] in subreddit_ids):
					outfile.write(line)

def filter_subs_submisisons(filename):
	match = re.search(r"(RS_2017-.{2})", filename)
	newfile = savedatadir + submissionsdir + '/filtered-' + match.group(1) + '.jsonlines'
	if 'bz2' in filename:
		with open(newfile, 'a+') as outfile, bz2.open(filename, 'rt') as infile:
			for line in infile:
				test_json = json.loads(line)
				if (test_json['subreddit_id'] in subreddit_ids):
					outfile.write(line)
	else:
		with open(newfile, 'a+') as outfile, lzma.open(filename, 'rt') as infile:
			for line in infile:
				test_json = json.loads(line)
				if (test_json['subreddit_id'] in subreddit_ids):
					outfile.write(line)

# def filter_subs_compressed(filename):
# 	match = re.search(r"(RC_2017-.*)", filename)
# 	newfile = savedatadir + commentdir + '/filtered-' + match.group(1) + '.jsonlines'
# 	with bz2.open(newfile, 'a+') as outfile, bz2.open(filename, 'rt') as infile:
# 		for line in infile:
# 			test_json = json.loads(line)
# 			if (test_json['subreddit_id'] in subreddit_ids):
# 				outfile.write(line)

with concurrent.futures.ProcessPoolExecutor(max_workers=48) as executor:
	results = list(executor.map(filter_subs_comments, redo))

# with concurrent.futures.ProcessPoolExecutor(max_workers=48) as executor:
# 	results = list(executor.map(filter_subs_compressed, filenames))
# timeit.timeit('match = re.search(r"subreddit_id\":\"(.{8})", testcomment)', number=10000)
# reg_str = r'subreddit_id":"(.{8})'

# timeit.timeit("re.search(re.search(r'subreddit_id\":\"(.{8})', testcomment)", setup='import re', number=10000)

# timeit.timeit('test_json = json.loads(testcomment)', number=10000)


from rest_framework import routers
from django.urls import path, include
from .views import (TransactionViewSet, CategoryViewSet,
                    monthly_summary_report,current_balance)

router = routers.DefaultRouter()
router.register(r'transactions', TransactionViewSet, basename='transaction')
router.register(r'categories', CategoryViewSet)

urlpatterns = [
    path("", include(router.urls)),
    path("report/monthly", monthly_summary_report,name="monthly_summary_report"),
    path("report/current_balance", current_balance, name="current_balance")
]


import dagger
from typing import Optional, Set, List
from pathlib import Path

from ci_connector_ops.pipelines.bases import Step, StepStatus, TestReport
from ci_connector_ops.pipelines.actions.environments import with_poetry_module, with_pipx_module, DEFAULT_PYTHON_EXCLUDE
from ci_connector_ops.pipelines.contexts import PipelineContext
from ci_connector_ops.pipelines.utils import (
    DAGGER_CONFIG,
    execute_concurrently,
)

METADATA_DIR = "airbyte-ci/connectors/metadata_service"
METADATA_LIB_MODULE_PATH = "lib"
METADATA_ORCHESTRATOR_MODULE_PATH = "orchestrator"

class TestPoetryModule(Step):
    def __init__(self, context: PipelineContext, title: str, parent_dir_path: str, module_path: str):
        self.title = title
        self.parent_dir_path = parent_dir_path
        self.module_path = module_path
        super().__init__(context)

    async def _run(self) -> StepStatus:
        # TODO (ben): Use the GlobalExclusion when merged (https://github.com/airbytehq/airbyte/pull/24225/files#diff-c86417158894333350d986efd59ffada645270c1c65b4eec7645a0b63ac2d915R46)
        parent_dir = self.context.get_repo_dir(self.parent_dir_path, exclude=DEFAULT_PYTHON_EXCLUDE)
        metadata_lib_module = with_poetry_module(self.context, parent_dir, self.module_path)
        run_test = metadata_lib_module.with_exec(["poetry", "run", "pytest"])
        return await self.get_step_result(run_test)

class SimpleExecStep(Step):
    def __init__(self, context: PipelineContext, title: str, args: List[str], parent_container: dagger.Container):
        self.title = title
        self.args = args
        self.parent_container = parent_container
        super().__init__(context)

    async def _run(self) -> StepStatus:
        run_command = self.parent_container.with_exec(self.args)
        return await self.get_step_result(run_command)

def metadata_validation_step(metadata_pipeline_context: PipelineContext, metadata_path: Path, parent_container: dagger.Container) -> Step:
    return SimpleExecStep(
        context=metadata_pipeline_context,
        title=f"Validate Connector Metadata Manifest: {metadata_path}",
        args=["metadata_service", "validate", str(metadata_path)],
        parent_container=parent_container,
    )

async def run_metadata_validation_pipeline(
    is_local: bool,
    git_branch: str,
    git_revision: str,
    gha_workflow_run_url: Optional[str],
    pipeline_start_timestamp: Optional[int],
    ci_context: Optional[str],
    metadata_source_paths: Set[Path],
) -> bool:
    metadata_pipeline_context = PipelineContext(
        pipeline_name="Metadata Service Validation Pipeline",
        is_local=is_local,
        git_branch=git_branch,
        git_revision=git_revision,
        gha_workflow_run_url=gha_workflow_run_url,
        pipeline_start_timestamp=pipeline_start_timestamp,
        ci_context=ci_context,
    )

    async with dagger.Connection(DAGGER_CONFIG) as dagger_client:
        metadata_pipeline_context.dagger_client = dagger_client.pipeline(metadata_pipeline_context.pipeline_name)

        async with metadata_pipeline_context:
            parent_container = with_pipx_module(
                metadata_pipeline_context,
                ".",
                f"{METADATA_DIR}/{METADATA_LIB_MODULE_PATH}",
                include=["airbyte-integrations/connectors/*"],
            )

            validation_steps = [
                metadata_validation_step(metadata_pipeline_context, metadata_path, parent_container).run
                for metadata_path in metadata_source_paths
            ]

            results = await execute_concurrently(validation_steps)
            metadata_pipeline_context.test_report = TestReport(pipeline_context=metadata_pipeline_context, steps_results=results)

        return metadata_pipeline_context.test_report.success

async def run_metadata_lib_test_pipeline(
    is_local: bool,
    git_branch: str,
    git_revision: str,
    gha_workflow_run_url: Optional[str],
    pipeline_start_timestamp: Optional[int],
    ci_context: Optional[str],
) -> bool:
    metadata_pipeline_context = PipelineContext(
        pipeline_name="Metadata Service Lib Unit Test Pipeline",
        is_local=is_local,
        git_branch=git_branch,
        git_revision=git_revision,
        gha_workflow_run_url=gha_workflow_run_url,
        pipeline_start_timestamp=pipeline_start_timestamp,
        ci_context=ci_context,
    )

    async with dagger.Connection(DAGGER_CONFIG) as dagger_client:
        metadata_pipeline_context.dagger_client = dagger_client.pipeline(metadata_pipeline_context.pipeline_name)
        async with metadata_pipeline_context:
            test_lib_step = TestPoetryModule(
                context=metadata_pipeline_context,
                title="Test Metadata Service Lib",
                parent_dir_path=METADATA_DIR,
                module_path=METADATA_LIB_MODULE_PATH,
            )
            result = await test_lib_step.run()
            metadata_pipeline_context.test_report = TestReport(pipeline_context=metadata_pipeline_context, steps_results=[result])

    return metadata_pipeline_context.test_report.success

async def run_metadata_orchestrator_test_pipeline(
    is_local: bool,
    git_branch: str,
    git_revision: str,
    gha_workflow_run_url: Optional[str],
    pipeline_start_timestamp: Optional[int],
    ci_context: Optional[str],
) -> bool:
    metadata_pipeline_context = PipelineContext(
        pipeline_name="Metadata Service Orchestrator Unit Test Pipeline",
        is_local=is_local,
        git_branch=git_branch,
        git_revision=git_revision,
        gha_workflow_run_url=gha_workflow_run_url,
        pipeline_start_timestamp=pipeline_start_timestamp,
        ci_context=ci_context,
    )

    async with dagger.Connection(DAGGER_CONFIG) as dagger_client:
        metadata_pipeline_context.dagger_client = dagger_client.pipeline(metadata_pipeline_context.pipeline_name)
        async with metadata_pipeline_context:
            test_orch_step = TestPoetryModule(
                context=metadata_pipeline_context,
                title="Test Metadata Service Orchestrator",
                parent_dir_path=METADATA_DIR,
                module_path=METADATA_ORCHESTRATOR_MODULE_PATH,
            )
            result = await test_orch_step.run()
            metadata_pipeline_context.test_report = TestReport(pipeline_context=metadata_pipeline_context, steps_results=[result])

    return metadata_pipeline_context.test_report.success


def arrayReplace(inputArray, elemToReplace, substitutionElem):

    for x in range(len(inputArray)):
        if inputArray[x] == elemToReplace:
            inputArray[x] = substitutionElem
    return inputArray

print(arrayReplace([1,2,3,4,1,5],1,10))

# if x == elemToReplace


import pathlib
import sys
import re

class Node:
    def __init__(self):
        self.parent = None
        self.children = {}
        self.fileAmount = 0

def parse(puzzle_input):
    fs = {}
    parent = Node()
    current = parent

    n = 0
    lines = puzzle_input.split("\n")

    while n < len(lines):
        line = lines[n].split(" ")
        if line[0] == "$":
            if line[1] == "cd":
                if line[2] == "/":
                    current = parent
                elif line[2] == "..":
                    current = current.parent
                else:
                    if line[2] in current.children:
                        current = current.children[line[2]]
                    else:
                        new = Node()
                        new.parent = current
                        current.children[line[2]] = new
                        current = new
        else:
            if line[0] == "dir":
                if line[1] not in current.children:
                    current.children[line[1]] = Node()
                    current.children[line[1]].parent = current
            else:
                current.fileAmount += int(line[0])

        n += 1

    return parent

def calculateDirSums(node, sums):
    total = node.fileAmount
    for child in node.children.values():
        total += calculateDirSums(child, sums)

    sums.append(total)
    return total

def calculateTotalSize(node):
    total = node.fileAmount
    for child in node.children.values():
        total += calculateTotalSize(child)
    return total

def part1(data):
    sums = []
    calculateDirSums(data, sums)

    return sum(filter(lambda x: x <= 100000, sums))

def part2(data):
    sums = []
    calculateDirSums(data, sums)
    unused = 70000000 - calculateTotalSize(data)

    for dirSize in sorted(sums):
        if unused + dirSize >= 30000000:
            return dirSize

def solve(puzzle_input):
    solution1 = part1(parse(puzzle_input))
    solution2 = part2(parse(puzzle_input))

    return solution1, solution2

if __name__ == "__main__":
    for path in sys.argv[1:]:
        print(f"{path}:")
        puzzle_input = pathlib.Path(path).read_text()
        solutions = solve(puzzle_input)
        print("\n".join(str(solution) for solution in solutions))


import pytest
from unittest.mock import patch

import pandas as pd
import pandas.util.testing as pdt
from flask import Flask

from pyaddepar.flask_addepar import addepar
from pyaddepar.addeparrequest import AddeparRequest, PortfolioType, AttrDict

@pytest.fixture
def addepar_request():
    # there's no need to expose the entire client...
    app = Flask(__name__)
    # initialize the config of the app object
    app.config.from_pyfile('config/settings.cfg')

    # move into the app context and initialize the amberdata project
    with app.app_context():
        addepar.init_app(app)
        yield addepar.request

def test_version(addepar_request, requests_mock):
    requests_mock.get("https://maffay.addepar.com/api/v1/api_version", json={'a': 'b'})
    y = addepar_request.version
    assert y == {'a': 'b'}

def test_auth(addepar_request):
    assert addepar_request.auth == ("A", "B")

def test_dicturl(addepar_request):
    x = addepar_request.dicturl({"a": 200, "b": "cc"})
    assert x == "a=200&b=cc"

def test_view_csv(addepar_request, requests_mock):
    xxx = pd.DataFrame(index=["x", "y"], columns=["a"], data=[[4], [5]])
    xxx.index.name = "Peter"

    requests_mock.get(
        "https://maffay.addepar.com/api/v1/portfolio/views/10/results?portfolio_id=20&portfolio_type=entity&output_type=csv&start_date=1978-11-12&end_date=1978-11-12",
        text=xxx.to_csv())
    x = addepar_request.view_csv(view_id=10, portfolio_id=20, portfolio_type=PortfolioType.ENTITY,
                           start_date=pd.Timestamp("12-Nov-1978"), end_date=pd.Timestamp("12-Nov-1978"))

    pdt.assert_frame_equal(x, xxx.reset_index())

def test_users(addepar_request,requests_mock):
    requests_mock.get("https://maffay.addepar.com/api/v1/users",
                      json={"data": [{"id": "1", "attributes": {"a": 2.0, "b": 3.0}}], "links": {"next": None}})
    x = {key: attr for key, attr in addepar_request.users}
    assert x == {"1": {"a": 2.0, "b": 3.0}}

def test_user_id(addepar_request,requests_mock):
    requests_mock.get("https://maffay.addepar.com/api/v1/users/1",
                      json={"data": {"id": "1", "attributes": {"a": 2.0, "b": 3.0}}})
    x = addepar_request.user(id=1)
    assert x == {"a": 2.0, "b": 3.0}

def test_entities(addepar_request,requests_mock):
    requests_mock.get("https://maffay.addepar.com/api/v1/entities",
                      json={"data": [{"id": "1", "attributes": {"a": 2.0, "b": 3.0}}], "links": {"next": None}})
    x = {key: attr for key, attr in addepar_request.entities()}
    assert x == {"1": {"a": 2.0, "b": 3.0}}

def test_entity_id(addepar_request,requests_mock):
    requests_mock.get("https://maffay.addepar.com/api/v1/entities/1",
                      json={"data": {"id": "1", "attributes": {"a": 2.0, "b": 3.0}}})
    x = addepar_request.entity(id=1)
    assert x == {"a": 2.0, "b": 3.0}

def test_group_id(addepar_request,requests_mock):
    requests_mock.get("https://maffay.addepar.com/api/v1/groups/1",
                      json={"data": {"id": "1", "attributes": {"a": 2.0, "b": 3.0}}})
    x = addepar_request.group(id=1)
    assert x == {"a": 2.0, "b": 3.0}

def test_options(addepar_request):
    with patch.object(AddeparRequest, "entities") as mock:
        addepar_request.options
        mock.assert_called_once_with(link="/v1/entities", modeltype="OPTION")

def test_persons(addepar_request):
    with patch.object(AddeparRequest, "entities") as mock:
        addepar_request.persons
        mock.assert_called_once_with(link="/v1/entities", modeltype="PERSON_NODE")

def test_groups(addepar_request):
    with patch.object(AddeparRequest, "entities") as mock:
        addepar_request.groups
        mock.assert_called_once_with(link="/v1/groups")

def test_users(addepar_request):
    with patch.object(AddeparRequest, "entities") as mock:
        addepar_request.users
        mock.assert_called_once_with(link="/v1/users")

def test_delete(addepar_request):
    with patch("requests.delete") as mock:
        mock.return_value = True
        assert addepar_request.delete(entity="1")

def test_post(addepar_request):
    with patch("requests.post") as mock:
        mock.return_value = AttrDict({"ok": True, "status_code": 200})
        assert addepar_request.post(data=1)


import math, os

# os.chdir("C:\Users\Ahmad Walid Naimi\Desktop\")

os.chdir(r"C:\Users\Ahmad Walid Naimi\Desktop")
print(os.getcwd())

family = [["mother", "45"], ["sister1", "27"], ["sister2","26"], ["brother", "25"]]

family2 = []
family1= []

for i in family:
    for j in i:
        family1.append(j)

#sorted function sorts from largest to smallest
print(sorted(family2))
print(family1.index("sister2")) #gives me the index of the list
print(family.count("sister1"))
family.reverse()
print(family)

dist = math.radians(23)*32
print("The distance is:", str(round(dist,2))+"mm")

with open("testing.txt","w") as testing:
    testing.write("Hello")

Python Documents/Practice/prac002 - Lists.py


import pickle
from tkinter import *
from tkinter.font import Font
from tkinter import filedialog

root = Tk()
root.title('To-Do-List-App')
root.geometry("500x500")

# Define our font
my_font = Font(
    family="Brush Script MT",
    size=30,
    weight="bold")
# Create frame
my_frame = Frame(root)
my_frame.pack(pady=10)

# Create listbox
my_list = Listbox(my_frame,
                  font=my_font,
                  width=25,
                  height=5,
                  bg="#EBEDEF",
                  bd=0,
                  fg="#464646", borderwidth=5
                  , highlightthickness=0,
                  selectbackground="#a6a6a6",
                  activestyle="none"
                  )  # systemButtonFace

my_list.pack()
# Create dummy list
stuff = ["Go to Mandir", "Collect news paper", "Buy notebook", "Call home", "Complete the test", "Complete task2",
         "Take medicines"]
# Add dummy list to the list box
for item in stuff:
    my_list.insert(END, item)
# Add scroll bar
my_scrollbar = Scrollbar(my_frame)
my_scrollbar.pack(side=RIGHT, fill=BOTH)

# add scrollbar
my_list.config(yscrollcommand=my_scrollbar.set)
my_scrollbar.config(command=my_list.yview)

# create an entry box to add items to the list
my_entry = Entry(root, font=("Helvetica", 24), width=26, borderwidth=5)
my_entry.pack(pady=20)

# create button frame
button_frame = Frame(root)
button_frame.pack(pady=20)

# Functions
def delete_item():
    my_list.delete(ANCHOR)

def add_item():
    my_list.insert(END, my_entry.get())
    my_entry.delete(0, END)

def cross_off_item():
    # cross off item
    my_list.itemconfig(
        my_list.curselection(),
        fg="#dedede")
    # get rid of selection bar
    my_list.select_clear(0, END)

def uncross_item():
    # cross off item
    my_list.itemconfig(
        my_list.curselection(),
        fg="#464646")
    # get rid of selection bar
    my_list.select_clear(0, END)

def delete_crossed():
    count = 0
    while count < my_list.size():
        if my_list.itemcget(count, "fg") == "#dedede":
            my_list.delete(my_list.index(count))
        else:
            count += 1

def save_list():
    file_name = filedialog.asksaveasfilename(
        initialdir="D:\Python_Projects\info_aidtech_task\data"
        , title="Save File", filetypes=(("Dat Files", "*.dat")
                                        , ("All Files", "*.*")))
    if file_name:
        if file_name.endswith(".dat"):
            pass
        else:
            file_name = f'{file_name}.dat'
        # Delete crossed off items before saving
        count = 0
        while count < my_list.size():
            if my_list.itemcget(count, "fg") == "#dedede":
                my_list.delete(my_list.index(count))
            else:
                count += 1
            # Grab all the stuff from the list
        my_list.get(0, END)

        # Open the file
        output_file = open(file_name, 'wb')

        # Actually add the stuff to the file
        pickle.dump(stuff, output_file)

def open_list():
    file_name = filedialog.askopenfilename(
        initialdir="D:\Python_Projects\info_aidtech_task\data"
        , title="Save File", filetypes=(("Dat Files", "*.dat")
                                        , ("All Files", "*.*")))
    if file_name:
        # Delete currently open list
        my_list.delete(0, END)
        input_file = open(file_name, 'rb')
        # Load the data the data from the file
        stuff = pickle.load(input_file)

        # Output the stuff to the screen
        for item in stuff:
            my_list.insert(END, item)

def clear_list():
    my_list.delete(0, END)

# Create Menu
my_menu = Menu(root)
root.config(menu=my_menu, bg="#808B96")

# Add items to the menu
file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)

# Add drop down items
file_menu.add_command(label="Save List", command=save_list)
file_menu.add_command(label="Open List", command=open_list)
file_menu.add_separator()
file_menu.add_command(label="Clear List", command=clear_list)

# Add some buttons
delete_button = Button(button_frame, text="Delete Item", command=delete_item)
add_button = Button(button_frame, text="Add Item", command=add_item)
cross_off_button = Button(button_frame, text="Cross off Item", command=cross_off_item)
uncross_button = Button(button_frame, text="Uncross Item", command=uncross_item)
delete_crossed_button = Button(button_frame, text="Delete Crossed", command=delete_crossed)

delete_button.grid(row=0, column=0)
add_button.grid(row=0, column=1, padx=20)
cross_off_button.grid(row=0, column=2)
uncross_button.grid(row=0, column=3, padx=20)
delete_crossed_button.grid(row=0, column=4)

root.mainloop()


import sys, serial, argparse
import numpy as np
from time import sleep
from collections import deque

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math

def is_number(st):
	try:
		float(st)
		return True
	except ValueError:
		return False
	except:
		print("ERROR in is_number()")
		sys.exit(0)

def line_to_int(utf8_line):
	value = 0
	try:
		for n in utf8_line:
			if is_number(n):
				value = 10*value +int(n)
		return value
	except:
		print("line_to_int failed.\n")
		return -1

temp_data = 0

class AnalogPlot:
	def __init__(self, strPort, maxLen):
		self.ser = serial.Serial(port=strPort, baudrate=115200)
		print('Reading from serial port %s...'%self.ser.name)
		print("HERE")
		self.ax = deque([0.0]*maxLen)
		self.ay = deque([0.0]*maxLen)
		self.maxLen = maxLen
		print("HERE")
	def addToBuf(self, buf, val):
		if len(buf) < self.maxLen:
			buf.append(val)
		else:
			buf.pop()
			buf.appendleft(val)
	def add(self, data):
		assert(len(data) == 2)
		self.addToBuf(self.ax, data[0])
		self.addToBuf(self.ay, data[1])
	def update(self, frameNum, a0, a1):
		global temp_data

		try:
			data = [float(line_to_int(self.ser.readline().decode('utf-8'))), 0]
			if(len(data) == 2):
				self.add(data)
				a0.set_data(range(self.maxLen), self.ax)
				a1.set_data(range(self.maxLen), self.ay)
		except KeyboardInterrupt:
			print('Exiting...')

		return a0,
	def close(self):
		self.ser.flush()
		self.ser.close()

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description="LDR serial")
	parser.add_argument('--port', dest='port', required=True)
	args = parser.parse_args()
	strPort = args.port

	#strPort = input("COM Port:  ")
	#print("HERE %s\n"%strPort)
	#strPort = int(strPort)-1
	#print("HERE\n")
	analogPlot = AnalogPlot(strPort, 100)

	print('Plotting data...')

	fig = plt.figure()
	ax = plt.axes(xlim=(0, 100), ylim=(0, 1023))
	a0, = ax.plot([], [])
	a1, = ax.plot([], [])
	anim = animation.FuncAnimation(fig, analogPlot.update,
								fargs=(a0, a1),
								interval=50)

	plt.show()
	analogPlot.close()

	print('Exiting...')


#!/usr/bin/env python3

with open('05.input','r') as f:
    part_1=[int(line) for line in f]

LEFT=0
RIGHT=len(part_1)

spot=0
steps=0

while LEFT <= spot < RIGHT:
    steps+=1
    part_1[spot]+=1
    spot+=part_1[spot]-1

print(steps)

with open('05.input','r') as f:
    part_2=[int(line) for line in f]

LEFT=0
RIGHT=len(part_2)

spot=0
steps=0

while LEFT <= spot < RIGHT:
    steps+=1
    offset = part_2[spot]
    if(offset >=3):
        part_2[spot]-=1
    else:
        part_2[spot]+=1
    spot+=offset

print(steps)


import sys
import os

from numpy.lib.function_base import select

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
ROOT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

import time
import json
import numpy as np
import torch
from torch.utils.data import DataLoader

from lib.options import BaseOptions
from lib.mesh_util import *
from lib.sample_util import *
from lib.train_util import *
from lib.model import *

from PIL import Image
import torchvision.transforms as transforms
import glob
import tqdm

# get options
opt = BaseOptions().parse()

class Evaluator:
    def __init__(self, opt, projection_mode='orthogonal'):
        self.opt = opt
        self.load_size = self.opt.loadSize
        self.to_tensor = transforms.Compose([
            transforms.Resize(self.load_size),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])
        # set cuda
        cuda = torch.device('cuda:%d' % opt.gpu_id) if torch.cuda.is_available() else torch.device('cpu')

        # create net
        netG = HGPIFuNet(opt, projection_mode).to(device=cuda)
        print('Using Network: ', netG.name)

        if opt.load_netG_checkpoint_path:
            netG.load_state_dict(torch.load(opt.load_netG_checkpoint_path, map_location=cuda))

        if opt.load_netC_checkpoint_path is not None:
            print('loading for net C ...', opt.load_netC_checkpoint_path)
            netC = ResBlkPIFuNet(opt).to(device=cuda)
            netC.load_state_dict(torch.load(opt.load_netC_checkpoint_path, map_location=cuda))
        else:
            netC = None

        os.makedirs(opt.results_path, exist_ok=True)
        os.makedirs('%s/%s' % (opt.results_path, opt.name), exist_ok=True)

        opt_log = os.path.join(opt.results_path, opt.name, 'opt.txt')
        with open(opt_log, 'w') as outfile:
            outfile.write(json.dumps(vars(opt), indent=2))

        self.cuda = cuda
        self.netG = netG
        self.netC = netC

    def load_image(self, images, masks):
        # Name
        img_name = os.path.splitext(os.path.basename(images[0]))[0]
        # Calib
        B_MIN = np.array([-1, -1, -1])
        B_MAX = np.array([1, 1, 1])
        projection_matrix = np.identity(4)
        projection_matrix[1, 1] = -1

        # modify: multi-view setting
        calibList = []
        if self.opt.num_views == 1:
            calibList.append(torch.Tensor(projection_matrix).float())
        elif self.opt.num_views == 3:
            extrin_60 = np.array([
                [-0.5, 0, 0.866, 0],
                [0, 1, 0, 0],
                [-0.866, 0, -0.5, 0],
                [0, 0, 0, 1]
            ])
            extrin_120 = np.array([
                [-0.5, 0, -0.866, 0],
                [0, 1, 0, 0],
                [0.866, 0, -0.5, 0],
                [0, 0, 0, 1]
            ])
            calibList.append(torch.Tensor(projection_matrix).float())
            calibList.append(torch.Tensor(np.matmul(projection_matrix, extrin_60)).float())
            calibList.append(torch.Tensor(np.matmul(projection_matrix, extrin_120)).float())
        elif self.opt.num_views == 4:
            extrin_90 = np.array([
                [0, 0, 1, 0],
                [0, 1, 0, 0],
                [-1, 0, 0, 0],
                [0, 0, 0, 1]
            ])
            extrin_180 = np.array([
                [-1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, -1, 0],
                [0, 0, 0, 1]
            ])
            extrin_270 = np.array([
                [0, 0, -1, 0],
                [0, 1, 0, 0],
                [1, 0, 0, 0],
                [0, 0, 0, 1]
            ])
            calibList.append(torch.Tensor(projection_matrix).float())
            calibList.append(torch.Tensor(np.matmul(projection_matrix, extrin_90)).float())
            calibList.append(torch.Tensor(np.matmul(projection_matrix, extrin_180)).float())
            calibList.append(torch.Tensor(np.matmul(projection_matrix, extrin_270)).float())
        # Mask
        maskList = []
        imageList = []
        for mask, image in zip(masks, images):
            mask = Image.open(mask).convert('L')
            mask = transforms.Resize(self.load_size)(mask)
            mask = transforms.ToTensor()(mask).float()
            maskList.append(mask)
            image = Image.open(image).convert('RGB')
            image = self.to_tensor(image)
            image = mask.expand_as(image) * image
            imageList.append(image)
        return {
            'name': img_name,
            'img': torch.stack(imageList, dim=0),
            'calib': torch.stack(calibList, dim=0),
            'mask': torch.stack(maskList, dim=0),
            'b_min': B_MIN,
            'b_max': B_MAX,
        }
        #--------------------------------------#

    def eval(self, data, use_octree=False):
        '''
        Evaluate a data point
        :param data: a dict containing at least ['name'], ['image'], ['calib'], ['b_min'] and ['b_max'] tensors.
        :return:
        '''
        opt = self.opt
        with torch.no_grad():
            self.netG.eval()
            if self.netC:
                self.netC.eval()
            save_path = '%s/%s/result_%s.obj' % (opt.results_path, opt.name, data['name'])
            if self.netC:
                gen_mesh_color(opt, self.netG, self.netC, self.cuda, data, save_path, use_octree=use_octree)
            else:
                gen_mesh(opt, self.netG, self.cuda, data, save_path, use_octree=use_octree)

if __name__ == '__main__':
    evaluator = Evaluator(opt)

    #test_images = glob.glob(os.path.join(opt.test_folder_path, '*'))
    #test_images = [f for f in test_images if ('png' in f or 'jpg' in f) and (not 'mask' in f)]

    # modify: multi-view setting #
    test_images = [opt.test_folder_path+'/0_0_00.jpg', opt.test_folder_path+'/90_0_00.jpg', opt.test_folder_path+'/180_0_00.jpg', opt.test_folder_path+'/270_0_00.jpg']
    test_masks = [opt.test_folder_path+'/0_0_00_mask.png', opt.test_folder_path+'/90_0_00_mask.png', opt.test_folder_path+'/180_0_00_mask.png', opt.test_folder_path+'/270_0_00_mask.png']
    #--------------------------#

    #test_images = [opt.test_folder_path+'/0_0_00.jpg']
    #test_masks = [opt.test_folder_path+'/0_0_00.png']
    print("Use view:", opt.num_views)

    #for image_path, mask_path in tqdm.tqdm(zip(test_images, test_masks)):
    try:
        data = evaluator.load_image(test_images, test_masks)
        evaluator.eval(data, True)
    except Exception as e:
        print("error:", e.args)


class Solution:
    def subarraySum(self, nums, k) -> int:
        count = 0
        sums = 0
        d = {0: 1}
        for i in nums:
            sums += i
            if sums - k in d:
                count += d[sums - k]
            if sums not in d:
                d[sums] = 1
            else:
                d[sums] += 1
        return count

# driver code
nums=[1,1,1]
k=2
sol=Solution()
print(sol.subarraySum(nums,k))