import os
import sys
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
import cv2
import argparse

# Root directory of the project
ROOT_DIR = os.path.abspath("../")
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "/content/drive/My Drive/logs")
# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log

sys.path.append(os.path.join(ROOT_DIR, "../"))

class CustomConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = "Demo Purpose"
    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 1
    GPU_COUNT = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 2  # Background + toy

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 1

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.6
config = CustomConfig()
config.display()

def apply_mask(image, mask, color, alpha=0.5):
    """apply mask to image"""
    for n, c in enumerate(color):
        image[:, :, n] = np.where(
            mask == 1,
            image[:, :, n] * (1 - alpha) + alpha * c,
            image[:, :, n]
        )
    return image

# based on https://github.com/matterport/Mask_RCNN/blob/master/mrcnn/visualize.py
# and https://github.com/markjay4k/Mask-RCNN-series/blob/887404d990695a7bf7f180e3ffaee939fbd9a1cf/visualize_cv.py
def display_instances(image, boxes, masks, class_ids, class_names, scores=None):
    assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]

    N = boxes.shape[0]

    colors = colors = [tuple(255 * np.random.rand(3)) for _ in range(N)]

    for i, c in enumerate(colors):
        if not np.any(boxes[i]):
            continue

        y1, x1, y2, x2 = boxes[i]
        label = class_names[class_ids[i]]
        score = scores[i] if scores is not None else None
        caption = "{} {:.3f}".format(label, score) if score else label

        # Mask
        mask = masks[:, :, i]
        image = apply_mask(image, mask, c)
        image = cv2.rectangle(image, (x1, y1), (x2, y2), c, 2)
        image = cv2.putText(image, caption, (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.7, c, 2)
    return image

class InferenceConfig(CustomConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

inference_config = InferenceConfig()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='MaskRCNN Video Object Detection/Instance Segmentation')
    parser.add_argument('-v', '--video_path', type=str, default='', help='Path to video. If None camera will be used')
    parser.add_argument('-sp', '--save_path', type=str, default='', help= 'Path to save the output. If None output won\'t be saved')
    parser.add_argument('-s', '--show', default=True, action="store_false", help='Show output')
    args = parser.parse_args()

    # Directory to save logs and trained model
    MODEL_DIR = os.path.join(ROOT_DIR, "/content/drive/My Drive/logs/sperm_detection20201024T1742/mask_rcnn_sperm_detection_0007.h5")

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "/content/drive/My Drive/logs/sperm_detection20201024T1742/mask_rcnn_sperm_detection_0007.h5")
    # Download COCO trained weights from Releases if needed
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    class_names = ['BG', 'not sperm','sperm']

    config = InferenceConfig()

    # Create model object in inference mode.
    model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir=DEFAULT_LOGS_DIR)

    # Load weights trained on MS-COCO
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    if args.video_path != '':
        cap = cv2.VideoCapture(args.video_path)
    else:
        cap = cv2.VideoCapture(0)

    if args.save_path:
        width = int(cap.get(3))
        height = int(cap.get(4))
        fps = cap.get(cv2.CAP_PROP_FPS)
        out = cv2.VideoWriter(args.save_path, cv2.VideoWriter_fourcc('M','J','P','G'), fps, (width, height))
    while cap.isOpened():
        ret, image = cap.read()
        results = model.detect([image], verbose=1)
        r = results[0]
        image = display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
        if args.show:
            #cv2.imshow('MaskRCNN Object Detection/Instance Segmentation', image)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        if args.save_path:
            out.write(image)
    cap.release()
    if args.save_path:
        out.release()
    #cv2.destroyAllWindows()


"""
Code compares the computation time needed for different kinds of colexification analyses.
"""
from cltoolkit import Wordlist
from pacs.colexifications import (
        full_colexifications,
        affix_colexifications,
        common_substring_colexifications,
        affix_colexifications_by_pairwise_comparison
        )
from pacs.util import write_gml
from pycldf import Dataset
from pyclts import CLTS

wl = Wordlist([Dataset.from_metadata("idssegmented/cldf/cldf-metadata.json")],
              ts=CLTS().bipa)

graphA = full_colexifications(wl)
graphB = affix_colexifications(wl, source_threshold=2, target_threshold=5)
graphC = common_substring_colexifications(wl, minimal_length_threshold=4,
        difference_threshold=3)

print("[i] writing graphs to file")
write_gml(graphA, "colexification-full.gml")
write_gml(graphB, "colexification-affix.gml", )
write_gml(graphC, "colexification-overlap.gml")


# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
import re, ast

with open('requirements.txt') as f:
	install_requires = f.read().strip().split('\n')

# get version from __version__ variable in frappe_teams_notifications/__init__.py
_version_re = re.compile(r'__version__\s+=\s+(.*)')

with open('frappe_teams_notifications/__init__.py', 'rb') as f:
	version = str(ast.literal_eval(_version_re.search(
		f.read().decode('utf-8')).group(1)))

setup(
	name='frappe_teams_notifications',
	version=version,
	description='This app will let us have push notificatios of Teams Channel for any doctype',
	author='Techlift',
	author_email='palash@techlift.in',
	packages=find_packages(),
	zip_safe=False,
	include_package_data=True,
	install_requires=install_requires
)


import pandas as pd
from pymongo import MongoClient
import numpy as np
import multiprocessing as mp
from time import perf_counter
import math

mongos1 = "mongodb://localhost:27019/"
mongos2 = "mongodb://localhost:27020/"
cpus = 10
chunksize = 100

def get_distinct_vessels():
    client = MongoClient(mongos1)
    db = client["ships"]
    collection = db["ship_positions"]
    query = {'mmsi': {'$ne': '0'}}
    vessels = collection.distinct('mmsi', query)
    client.close()

    return vessels

def remove_nas(vessels):
    columns = [
        'timestamp',
        'navigational_status',
        'latitude',
        'longitude',
        'sog',
        'cog'
    ]

    return [
        doc for doc in vessels if all(
            column in doc and doc[column] is not None and (
                isinstance(doc[column], (float, int)) and not math.isnan(doc[column])
                or isinstance(doc[column], str) and doc[column].lower() != 'nan'
            )
            for column in columns
        )
    ]

def filter(vessel_mmsi, chunkId):
    start_time = perf_counter()

    conn_string = mongos1 if chunkId % 2 == 0 else mongos2
    client = MongoClient(conn_string)
    db = client["ships"]
    query = [
        {
            '$match': {
                '$and': [
                    {'mmsi': {'$in': vessel_mmsi}},
                ]
            }
        },
        {
            '$group': {
                '_id': '$mmsi',
                'count': {'$sum': 1}
            }
        },
        {
            '$match': {
                'count': {'$gte': 100}
            }
        },
        {
            '$lookup': {
                'from': 'ship_positions',
                'localField': '_id',
                'foreignField': 'mmsi',
                'as': 'documents'
            }
        }
    ]

    vessels = list(db["ship_positions"].aggregate(query))
    vessels = sum([vessel['documents'] for vessel in vessels], [])
    before_filter = len(vessels)
    vessels = remove_nas(vessels)

    if len(vessels) > 0:
        db['filtered_positions'].insert_many(vessels)

    timing = perf_counter() - start_time

    print(f"chunk #{chunkId} - before: {before_filter}, after filter: {len(vessels)}. Elapsed: {round(timing, 2)}s")

    client.close()

def parallelize_filtering(vessels):
    pool = mp.Pool(cpus)
    vessels = [int(vessel) for vessel in vessels]
    n_chunks = math.ceil(len(vessels) / chunksize)
    chunks = np.array_split(vessels, n_chunks)
    temp = pool.starmap(filter, [(chunk.tolist(), i) for i, chunk in enumerate(chunks)])

if __name__ == '__main__':
    cpus_max = mp.cpu_count()
    distinct_values = get_distinct_vessels()

    start_time = perf_counter()
    parallelize_filtering(distinct_values)
    timing = perf_counter() - start_time

    print()
    print(f"all data filtered and migrated in {round(timing, 2)}s")


import numpy as np
from particle.base import Particle

class SuperEllipsoid(Particle):
    def __init__(self, a, b, c, p):
        super().__init__()
        self.dim = 3

        # semi-axis length (a: b: c) and shape parameter (by default p1=p2=p)
        self.a = a
        self.b = b
        self.c = c
        self.p = p

        self.rot_mat = np.diag(np.ones(self.dim))

    @property
    def semi_axis(self):
        return np.array([self.a, self.b, self.c])

    @property
    def volume(self):
        return 4./3.*np.pi*np.cumprod(self.semi_axis)[2]

    @property
    def inscribed_d(self):
        """
        diameter of the outscribed sphere
        """
        return np.min(self.semi_axis)

    @property
    def outscribed_d(self):
        """
        diameter of the outscribed sphere
        """
        # Hölder Inequality
        if (self.p > 1.):
            k = self.p/(self.p-1.)
            temp = self.a**(2.*k) + self.b**(2.*k) + self.c**(2.*k)
            return temp**(1./k)
        else: return np.max(self.semi_axis)

    def shapeFun(self, r_l: np.array):
        """ Superellipsoid function """
        y = 0.
        for i in range(self.dim):
            y += np.fabs(r_l[i] / self.semi_axis[i])**(2.*self.p)
        return y

    def shapeFund(self, r_l: np.array):
        """ The first-order derivative of superellipsoid function"""
        dx = np.zeros(self.dim)
        for i in range(self.dim):
            dx[i] = 2.*self.p/self.semi_axis[i]*np.fabs(r_l[i]/self.semi_axis[i])**(2.*self.p-1.)*np.sign(r_l[i])
        return dx

    def support_func(self, u: np.array):
        """
        Particle's support function: h(u) = max(K) u . x

        located in the origin, without rotation by default.
        """

        r_u = np.linalg.norm(u)
        # dxt = k*Ru/|u|
        dp = 2.*self.p - 1.

        normal = u / r_u
        r_c = np.zeros(self.dim)
        for i in range(self.dim):
            r_c[i] = self.semi_axis[i] * np.fabs(normal[i]*self.semi_axis[i]/2./self.p)**(1./dp) * np.sign(u[i])

        k = (1. / self.shapeFun(r_c))**(dp/2./self.p)
        r_c *= k**(1./dp)

        return np.dot(r_c, u)

    def support_funcs(self, u: np.array):
        """
        The support function of a specific particle:

        Particle's centroid & rotation matrix must be taken into consideration, i.e.,
        h_i(u) = h(R^T_i*u) + u*r_i
        """
        y = self.support_func(np.matmul(self.rot_mat.T, u.T).T)
        y += np.dot(u, self.centroid)
        return y

    def check(self, vector):

        r_v = np.linalg.norm(vector)

        # https://baike.baidu.com/item/球坐标系/8315363
        temp = np.sqrt(vector[0]**2. + vector[1]**2.)
        sin_cita = temp / r_v
        cos_cita = vector[2] / r_v

        if (temp == 0.):
            sin_fai, cos_fai = 1., 0.
        else:
            sin_fai, cos_fai = vector[1] / temp, vector[0] / temp

        point = np.array([sin_cita*cos_fai, sin_cita*sin_fai, cos_cita])
        r = (1. / self.shapeFun(point))**(0.5/self.p)
        point *= r

        u = self.shapeFund(point)

        tr = np.dot(point, u)

        return (tr-self.support_func(u))


#!/usr/bin/env python
import os

class Cofold:
    def __init__(self, b):
        seq_num, seq_id, seq, mfe_struct, mfe, p_struct, p_mfe, p_freq, deltag = list(
            map(str.strip, b))
        self.id = seq_id
        self.seq = seq.strip('"')
        self.mfe_struct = mfe_struct.strip('"')
        self.mfe = float(mfe)
        self.p_struct = p_struct.strip('"')
        self.p_mfe = float(p_mfe)
        self.p_freq = float(p_freq)
        self.deltag = float(deltag)

    def __str__(self):
        return "\n".join(self.info)

    @property
    def info(self):
        return [
            "\t".join(map(str, [self.id, self.seq, self.seq,  self.mfe])),
            "\t".join(map(str, [self.id, self.mfe_struct, self.p_struct, self.p_mfe]))]

def read_cofold(fcofoldp):
    d = {}
    i = 0
    for line in open(fcofoldp, encoding="ISO-8859-1"):
        i += 1
        if i == 1:
            idx = line.index("mfe ")
            line = line[idx+4:]
        b = line.rstrip().split(" ")
        b = [v for v in b if v != ""]
        cf = Cofold(b)
        d[cf.id] = cf
    return d

class JS:
    def __init__(self, b):
        chrom, start, end, name, _, strand, numevi, ntot, fs1, fs2, fs2rc = b
        self.chrom = chrom
        self.start = int(start)
        self.end = int(end)
        self.id = name
        self.strand = strand
        self.numevi = numevi
        self.ntot = ntot
        self.fs1 = fs1
        self.fs2 = fs2
        self.fs2rc = fs2rc

    @property
    def sig(self):
        return self.start, self.end

    def __str__(self):
        return "\t".join(map(str, [self.chrom, self.start, self.end, self.id, self.numevi, self.ntot]))

def read_js(fjs):
    assert os.path.exists(fjs), fjs
    d = {}
    for line in open(fjs):
        b = line.rstrip().split("\t")
        js = JS(b)
        d[js.id] = js
    return d

class JncTwoSide:
    def __init__(self, jslft, cflft, jsrgt, cfrgt):
        assert jslft.sig == jsrgt.sig
        assert jslft.id == jsrgt.id
        self.jslft = jslft
        self.cflft = cflft
        self.jsrgt = jsrgt
        self.cfrgt = cfrgt

    @property
    def sig(self):
        return self.jslft.sig

    def format_struct(self, side):
        if side == 'lft':
            return ["LFT\t" + v for v in self.cflft.info]
        elif side == 'rgt':
            return ["RGT\t" + v for v in self.cfrgt.info]
        else:
            return None

    @property
    def info(self):
        return "\n".join(map(str, [
            self.jslft] + self.format_struct('lft') + self.format_struct('rgt')))+"\n"

if __name__ == "__main__":
    f_lft_mfe = "../results/jspair/CoV_merged_lft_cofold.csv"
    f_rgt_mfe = "../results/jspair/CoV_merged_rgt_cofold.csv"
    f_lft_js = "../results/jspair/CoV_merged_lft.csv"
    f_rgt_js = "../results/jspair/CoV_merged_rgt.csv"

    lcfd = read_cofold(f_lft_mfe)
    rcfd = read_cofold(f_rgt_mfe)
    ljsd = read_js(f_lft_js)
    rjsd = read_js(f_rgt_js)

    sig2jts = {}
    for sid in ljsd:
        assert sid in rjsd and sid in lcfd and sid in rcfd
        jts = JncTwoSide(ljsd[sid], lcfd[sid], rjsd[sid], rcfd[sid])
        sig2jts[jts.sig] = jts

    print(len(sig2jts))

    fhotspot = "../results/jhotspot/NGS_Nanopore/all_consistent_gap.tsv" # NGS.enriched.consistent.tsv"
    assert os.path.exists(fhotspot)
    for line in open(fhotspot):
        if line.startswith("Start"):
            continue

        hs = line.rstrip().split("\t")
        start, end = hs[:2]
        #start, end, strand, numevt, ntot = hs[:5]
        #if strand != "+":
        #    continue
        sig = (int(start), int(end))
        jts = sig2jts[sig]
        print(jts.info)


#!/usr/bin/env python
#
#===- add_new_check.py - cpp-clippy check generator ---------*- python -*--===#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#===-----------------------------------------------------------------------===#

from __future__ import print_function
from __future__ import unicode_literals

import argparse
import io
import os
import re
import sys

# Adapts the module's CMakelist file. Returns 'True' if it could add a new
# entry and 'False' if the entry already existed.
def adapt_cmake(module_path, check_name_camel):
  filename = os.path.join(module_path, 'CMakeLists.txt')

  # The documentation files are encoded using UTF-8, however on Windows the
  # default encoding might be different (e.g. CP-1252). To make sure UTF-8 is
  # always used, use `io.open(filename, mode, encoding='utf8')` for reading and
  # writing files here and elsewhere.
  with io.open(filename, 'r', encoding='utf8') as f:
    lines = f.readlines()

  cpp_file = check_name_camel + '.cpp'

  # Figure out whether this check already exists.
  for line in lines:
    if line.strip() == cpp_file:
      return False

  print('Updating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    cpp_found = False
    file_added = False
    for line in lines:
      cpp_line = line.strip().endswith('.cpp')
      if (not file_added) and (cpp_line or cpp_found):
        cpp_found = True
        if (line.strip() > cpp_file) or (not cpp_line):
          f.write('  ' + cpp_file + '\n')
          file_added = True
      f.write(line)

  return True

# Adds a header for the new check.
def write_header(module_path, module, namespace, check_name, check_name_camel):
  filename = os.path.join(module_path, check_name_camel) + '.h'
  print('Creating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    header_guard = ('CPP_CLIPPY_SRC_' + module.upper() + '_'
                    + check_name_camel.upper() + '_H_')
    f.write('//===--- ')
    f.write(os.path.basename(filename))
    f.write(' - cpp-clippy ')
    f.write('-' * max(0, 42 - len(os.path.basename(filename))))
    f.write('*- C++ -*-===//')
    f.write("""
//
// Part of the Cpp-Clippy Project, under the Apache License v2.0.
// See https://github.com/cpp-clippy/cpp-clippy/LICENSE.txt for
// license information.
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#ifndef %(header_guard)s
#define %(header_guard)s

#include "../ClangTidyCheck.h"

namespace clang {
namespace tidy {
namespace %(namespace)s {

/// FIXME: Write a short description.
///
/// For the user-facing documentation see:
/// https://cpp-clippy.gitbook.io/book/rules/%(module)s/%(check_name)s
class %(check_name_camel)s : public ClangTidyCheck {
public:
  %(check_name_camel)s(StringRef Name, ClangTidyContext *Context)
      : ClangTidyCheck(Name, Context) {}
  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};

} // namespace %(namespace)s
} // namespace tidy
} // namespace clang

#endif // %(header_guard)s
""" % {'header_guard': header_guard,
       'check_name_camel': check_name_camel,
       'check_name': check_name,
       'module': module,
       'namespace': namespace})

# Adds the implementation of the new check.
def write_implementation(module_path, module, namespace, check_name_camel):
  filename = os.path.join(module_path, check_name_camel) + '.cpp'
  print('Creating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    f.write('//===--- ')
    f.write(os.path.basename(filename))
    f.write(' - cpp-clippy ')
    f.write('-' * max(0, 51 - len(os.path.basename(filename))))
    f.write('-===//')
    f.write("""
//
// Part of the Cpp-Clippy Project, under the Apache License v2.0.
// See https://github.com/cpp-clippy/cpp-clippy/LICENSE.txt for
// license information.
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#include "%(check_name)s.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace %(namespace)s {

void %(check_name)s::registerMatchers(MatchFinder *Finder) {
  // FIXME: Add matchers.
  Finder->addMatcher(functionDecl().bind("x"), this);
}

void %(check_name)s::check(const MatchFinder::MatchResult &Result) {
  // FIXME: Add callback implementation.
  const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
  if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
    return;
  diag(MatchedDecl->getLocation(), "function %%0 is insufficiently awesome")
      << MatchedDecl;
  diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note)
      << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
}

} // namespace %(namespace)s
} // namespace tidy
} // namespace clang
""" % {'check_name': check_name_camel,
       'module': module,
       'namespace': namespace})

# Returns the source filename that implements the module.
def get_module_filename(module_path, module):
  modulecpp = list(filter(
      lambda p: p.lower() == module.lower() + 'clippymodule.cpp',
      os.listdir(module_path)))[0]
  return os.path.join(module_path, modulecpp)

# Modifies the module to include the new check.
def adapt_module(module_path, module, check_name, check_name_camel):
  filename = get_module_filename(module_path, module)
  with io.open(filename, 'r', encoding='utf8') as f:
    lines = f.readlines()

  print('Updating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    header_added = False
    header_found = False
    check_added = False
    check_fq_name = module + '-' + check_name
    check_decl = ('    CheckFactories.registerCheck<' + check_name_camel +
                  '>(\n        "' + check_fq_name + '");\n')

    lines = iter(lines)
    try:
      while True:
        line = next(lines)
        if not header_added:
          match = re.search('#include "(.*)"', line)
          if match:
            header_found = True
            if match.group(1) > check_name_camel:
              header_added = True
              f.write('#include "' + check_name_camel + '.h"\n')
          elif header_found:
            header_added = True
            f.write('#include "' + check_name_camel + '.h"\n')

        if not check_added:
          if line.strip() == '}':
            check_added = True
            f.write(check_decl)
          else:
            match = re.search('registerCheck<(.*)> *\( *(?:"([^"]*)")?', line)
            prev_line = None
            if match:
              current_check_name = match.group(2)
              if current_check_name is None:
                # If we didn't find the check name on this line, look on the
                # next one.
                prev_line = line
                line = next(lines)
                match = re.search(' *"([^"]*)"', line)
                if match:
                  current_check_name = match.group(1)
              if current_check_name > check_fq_name:
                check_added = True
                f.write(check_decl)
              if prev_line:
                f.write(prev_line)
        f.write(line)
    except StopIteration:
      pass

# Adds a release notes entry.
def add_release_notes(module_path, module, check_name):
  check_name_dashes = module + '-' + check_name
  filename = os.path.normpath(os.path.join(module_path,
                                           '../../docs/ReleaseNotes.rst'))
  with io.open(filename, 'r', encoding='utf8') as f:
    lines = f.readlines()

  lineMatcher = re.compile('New checks')
  nextSectionMatcher = re.compile('New check aliases')
  checkMatcher = re.compile('- New :doc:`(.*)')

  print('Updating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    note_added = False
    header_found = False
    add_note_here = False

    for line in lines:
      if not note_added:
        match = lineMatcher.match(line)
        match_next = nextSectionMatcher.match(line)
        match_check = checkMatcher.match(line)
        if match_check:
          last_check = match_check.group(1)
          if last_check > check_name_dashes:
            add_note_here = True

        if match_next:
          add_note_here = True

        if match:
          header_found = True
          f.write(line)
          continue

        if line.startswith('^^^^'):
          f.write(line)
          continue

        if header_found and add_note_here:
          if not line.startswith('^^^^'):
            f.write("""- New :doc:`%s
  <clang-tidy/checks/%s/%s>` check.

  FIXME: add release notes.

""" % (check_name_dashes, module, check_name))
            note_added = True

      f.write(line)

# Adds a test for the check.
def write_test(module_path, module, check_name, test_extension):
  check_name_dashes = module + '-' + check_name
  filename = os.path.normpath(os.path.join(
    module_path, '..', '..', 'test',
    module, check_name + '.' + test_extension))
  print('Creating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    f.write("""// RUN: %%check_cpp_clippy %%s %(check_name_dashes)s %%t

// FIXME: Add something that triggers the check here.
void f();
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [%(check_name_dashes)s]

// FIXME: Verify the applied fix.
//   * Make the CHECK patterns specific enough and try to make verified lines
//     unique to avoid incorrect matches.
//   * Use {{}} for regular expressions.
// CHECK-FIXES: {{^}}void awesome_f();{{$}}

// FIXME: Add something that doesn't trigger the check here.
void awesome_f2();
""" % {'check_name_dashes': check_name_dashes})

def get_actual_filename(dirname, filename):
  if not os.path.isdir(dirname):
    return ''
  name = os.path.join(dirname, filename)
  if (os.path.isfile(name)):
    return name
  caselessname = filename.lower()
  for file in os.listdir(dirname):
    if (file.lower() == caselessname):
      return os.path.join(dirname, file)
  return ''

# Recreates the list of checks in the book/src/SUMMARY.md.
def update_checks_list(clang_tidy_path):
  filename = os.path.normpath(os.path.join(clang_tidy_path, '../book/src/SUMMARY.md'))
  docs_dir = os.path.join(clang_tidy_path, '../book/src/rules')
  # Read the content of the current list.rst file
  with io.open(filename, 'r', encoding='utf8') as f:
    lines = f.readlines()
  # Get all existing docs
  doc_files = []
  a = os.listdir(docs_dir)
  for subdir in list(filter(lambda s: not s.endswith('.md') and not s.endswith('.py'),
                     os.listdir(os.path.join(docs_dir)))):
    for file in filter(lambda s: s.endswith('.md'), os.listdir(os.path.join(docs_dir, subdir))):
      doc_files.append([subdir, file])
  doc_files.sort()

  print('Updating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    for line in lines:
      if '(rules/' in line:
        continue

      f.write(line)
      if line.strip() == '## Rules':
        for doc_file in doc_files:
          module = doc_file[0]
          check_file = doc_file[1].replace('.md', '')

          if check_file == 'README':
            f.write('\n* [%(module_name)s](rules/%(module)s/%(check_file)s.md)' % {
              'module_name': module.capitalize(),
              'module': module,
              'check_file': check_file,
            })
          else:
            f.write('\n  * [Rule %(rule_name)s](rules/%(module)s/%(check_file)s.md)' % {
              'rule_name': check_file.upper(),
              'module': module,
              'check_file': check_file,
            })

# Adds a documentation for the check.
def write_docs(module_path, module, check_name):
  check_name_dashes = module + '-' + check_name
  filename = os.path.normpath(os.path.join(
      module_path, '../../book/src/rules/', module, check_name + '.md'))
  print('Creating %s...' % filename)
  with io.open(filename, 'w', encoding='utf8', newline='\n') as f:
    f.write("""---
description: FIXME
---

# Rule %(check_name_dashes)s

* **Classification:** FIXME [_Required_ / _Advisory_]
* **Offers Fixes:** FIXME [_Yes_ / _No_]

## What it does?

FIXME

## Why is this bad?

FIXME

## Example

```cpp
FIXME
```

### See also

FIXME
""" % {'check_name_dashes': check_name_dashes})

def get_camel_name(check_name):
  return ''.join(map(lambda elem: elem.capitalize(),
                     check_name.split('-')))

def get_camel_check_name(check_name):
  return get_camel_name(check_name) + 'Check'

def main():
  language_to_extension = {
      'c': 'c',
      'c++': 'cpp',
      'objc': 'm',
      'objc++': 'mm',
  }
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--update-docs',
      action='store_true',
      help='just update the list of documentation files, then exit')
  parser.add_argument(
      '--language',
      help='language to use for new check (defaults to c++)',
      choices=language_to_extension.keys(),
      default='c++',
      metavar='LANG')
  parser.add_argument(
      'module',
      nargs='?',
      help='module directory under which to place the new tidy check (e.g., misc)')
  parser.add_argument(
      'check',
      nargs='?',
      help='name of new tidy check to add (e.g. foo-do-the-stuff)')
  args = parser.parse_args()

  if args.update_docs:
    update_checks_list(os.path.dirname(sys.argv[0]))
    return

  if not args.module or not args.check:
    print('Module and check must be specified.')
    parser.print_usage()
    return

  module = args.module
  check_name = args.check
  check_name_camel = get_camel_check_name(check_name)
  if check_name.startswith(module):
    print('Check name "%s" must not start with the module "%s". Exiting.' % (
        check_name, module))
    return
  clang_tidy_path = os.path.dirname(sys.argv[0])
  module_path = os.path.join(clang_tidy_path, module)

  if not adapt_cmake(module_path, check_name_camel):
    return

  # Map module names to namespace names that don't conflict with widely used top-level namespaces.
  if module == 'llvm':
    namespace = module + '_check'
  else:
    namespace = module

  write_header(module_path, module, namespace, check_name, check_name_camel)
  write_implementation(module_path, module, namespace, check_name_camel)
  adapt_module(module_path, module, check_name, check_name_camel)
  # add_release_notes(module_path, module, check_name)
  test_extension = language_to_extension.get(args.language)
  write_test(module_path, module, check_name, test_extension)
  write_docs(module_path, module, check_name)
  update_checks_list(clang_tidy_path)
  print('Done. Now it\'s your turn!')

if __name__ == '__main__':
  main()


from tkinter import *

class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        self.btn1 = Button(frame, text='Exit', fg='red',
                           command=frame.quit)
        self.btn1.pack()

        self.btn2 = Button(frame, text='Msg', fg='green',
                           command=self.say_hi)
        self.btn2.pack()

    def say_hi(self):
        print('Hello world')

root = Tk()
app = App(root)
root.mainloop()
root.destroy()


import socket
import pickle

"""
NOTE: Mandatory Feature 1
Manager Node functionality
"""

# NOTE: GVM Static IP Address
SERVER_IP = "35.224.229.170"
SERVER_PORT = 4321
BUFFER_SIZE = 1024

def establishConnection():
    sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sc.connect((SERVER_IP, SERVER_PORT))
    return sc

def closeConnection(sc):
    print("Closing connection...")
    sc.close()

def sendState(sc, gameBoard):
    data = pickle.dumps(gameBoard)
    sc.send(data)

def receiveState(sc):
    try:
        outputValue = sc.recv(BUFFER_SIZE)
        nextBoard = pickle.loads(outputValue)
        return nextBoard
    except EOFError:
        print("Sorry! Connection with server refused. Try again later.")
        raise

Group B2 Tic-Tac-Toe Client/manager_node.py


# -*- coding: utf-8 -*-
"""WWDT Downloader.

Methods to download and format West Wide Drought Tracker datasets.

Notes:
    Watch for this resampling error:
        "failed to prevent overwriting existing key grid_mapping in attrs."
    Is only periodic, check for this before overwritting original.
"""
import os
import shutil
import time
import warnings

from multiprocessing.pool import ThreadPool
from pathlib import Path

import dask
import xarray as xr

from rasterio.enums import Resampling

from drip.downloaders.utilities import Downloader
from drip.loggers import init_logger, set_handler
from drip.app.options.options import INDEX_NAMES
from drip.downloaders.utilities import NetCDF

logger = init_logger(__name__)

PRISM_URL = "https://wrcc.dri.edu/wwdt/data/PRISM"

class WWDT_Builder(NetCDF):
    """Methods for downloading and formatting selected data from the WWDT."""

    def __init__(self, index, resolution=0.25, template=None,
                 std_limit=3):
        """Initialize DIBuilder object.

        Parameters
        ----------
        index : str
            Index key. Must be in prf.options.INDEX_NAMES.
        resolution : float
            Resolution of target drought index NetCDF4 file in decimal degrees.
        template : str
            Path to file to use as template for georeferencing.
        """
        super().__init__()
        self.url = PRISM_URL
        self.resolution = resolution
        self.template = template
        self.set_index(index)
        self._set_logger(index)

    def __repr__(self):
        """Print representation string."""
        argstr = ", ".join([f"{k}='{v}'" for k, v in self.__dict__.items()])
        return f"<DIBuilder object: {argstr}>"

    def build(self, overwrite=False):
        """Download and combine multiple NetCDF files from WWDT into one."""
        dst = self.final_path
        if dst.exists() and not overwrite:
            logger.info("%s exists, skipping.", dst)
        else:
            start = time.time()
            logger.info("Building %s...", dst)

            self.download_all(self.index_paths)
            self.resample_all()
            self.combine()

            tmp_folder = self.home.joinpath(f"{self.index}")
            logger.info("Removing %s", tmp_folder)
            shutil.rmtree(tmp_folder)

            end = time.time()
            duration = round((end - start) / 60, 2)
            logger.info("%s completed in %f minutes.", dst, duration)

    def combine(self):
        """Combine monthly WWDT files into one."""
        logger.info("Combining monthly %s files into singular timeseries...",
                    self.index)

        # Get all monthly file paths
        paths = [entry["dst"] for entry in self.index_paths]

        # Combine by day dimension
        try:
            chunk_option = "array.slicing.split_large_chunks"
            with dask.config.set(**{chunk_option: False}):
                data = xr.open_mfdataset(
                    paths,
                    combine="nested",
                    concat_dim="day"
                )

                # Resort by day
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    data = data.sortby("day")

                # Check that it was concatenated correctly
                single = xr.open_dataset(paths[0])
                lonshape = single["longitude"].shape[0]
                latshape = single["latitude"].shape[0]
                tshape = (lonshape, latshape)
                lonshape = data["longitude"].shape[0]
                latshape = data["latitude"].shape[0]
                nshape = (lonshape, latshape)
                assert tshape == nshape

                # Change dataset name to 'index'
                data = data.rename({"data": "index"})

                # Write to file
                data.to_netcdf(self.combined_path)

            logger.info("Build for %s complete.", self.combined_path)

        except AssertionError:
            print(f"Combination of {self.index} failed with incorrect shape: "
                  f"target spatial shape: {tshape}, generated spatial shape "
                  f"{nshape}")
            logger.error("Combination of %s failed with incorrect "
                         "shape: target spatial shape: %s, "
                         "generated spatial shape %s", self.index,
                         str(tshape), str(nshape))
            raise

        except Exception as error:
            print(error.__dict__)
            print(f"Combination of {self.index} failed with error "
                  f"message: {type(error)}, {error}")
            logger.error("Combination of %s failed. %s: %s", self.index,
                         type(error), error)
            raise

    @property
    def combined_path(self):
        """Return path for all year, 12-monthly combined dataset."""
        return self.home.joinpath(self.index + ".nc")

    @property
    def final_path(self):
        """Return final path for indexed NetCDF file."""
        return self.home.joinpath(self.index + ".nc")

    @property
    def home(self):
        """Return data home directory."""
        return self.paths["indices"]

    @property
    def index_paths(self):
        """Return list of remote urls and local destination paths."""
        paths = []
        for i in range(1, 13):
            url = os.path.join(
                self.url,
                f"{self.index}/{self.index}_{i}_PRISM.nc"
            )
            fname = f"{self.index}_{i:02d}_temp.nc"
            dst = self.home.joinpath(f"{self.index}/{fname}")
            os.makedirs(os.path.dirname(dst), exist_ok=True)
            entry = {"url": url, "dst": dst}
            paths.append(entry)

        return paths

    def resample(self, path):
        """Resample indices to target resolution."""
        dst = str(path).replace(".nc", "_2.nc")
        path = str(path)

        try:
            data = xr.open_dataset(path)
        except RuntimeError:
            try:
                data = xr.open_dataset(path)
                logger.warning("RuntimeError opening %s, trying again.", path)
            except RuntimeError as error:
                logger.error("RuntimeError opening %s: %s", path, error)

        data = set_georeferencing(self.index, data)

        if not self.template:
            if data.rio.resolution()[0] != self.resolution:
                logger.info("Resampling %s to %f decimal degrees...", path,
                            self.resolution)
                try:
                    ndata = data.rio.reproject(
                        data.rio.crs,
                        transform=self.resolution,
                        resampling=Resampling.bilinear
                    )
                    ndata.to_netcdf(dst)
                    logger.info("Resampling %s to %f complete.", path,
                                self.resolution)
                except Exception as error:
                    print(error.__dict__)
                    print(f"Resampling {path} failed with error message: "
                        f"{type(error)}, {error}")
                    logger.error("Resampling %s failed. %s: %s", path,
                                type(error), error)
            else:
                logger.info("%s already has %f resolution, skipping...",
                            path, self.resolution)

        else:
            logger.info("Reprojecting %s to match georeferencing of "
                        "%s ...", path, self.template)

            # Reproject Matching requires x and y dim names
            templ = xr.open_dataset(self.template)
            templ = set_georeferencing(self.index, templ, xy=True)
            data = set_georeferencing(self.index, data, xy=True)

            try:
                ndata = data.rio.reproject_match(
                    match_data_array=templ["index"],
                    resampling=Resampling.bilinear
                )
                ndata = set_georeferencing(self.index, ndata)
                ndata.to_netcdf(dst)
                templ.close()
            except Exception as error:
                print(error.__dict__)
                print(f"Resampling {path} failed with error message: "
                        f"{type(error)}, {error}")
                logger.error("Resampling %s failed. %s: %s", path,
                            type(error), error)

            data.close()

            path = Path(path)
            tmp = path.parent.joinpath("tmp", path.name)
            tmp.parent.mkdir(exist_ok=True)
            shutil.move(path, tmp)
            shutil.move(dst, path)
            logger.info("Reprojection of %s to match %s complete.", path,
                        self.template)

    def resample_all(self):
        """Resample all downloading monthly files to target resolution."""
        paths = [entry["dst"] for entry in self.index_paths]
        with ThreadPool(os.cpu_count() - 1) as pool:
            for _ in pool.imap(self.resample, paths):
                pass

    def set_index(self, index):
        """Set the index key and index name.

        Parameters
        ----------
        index : str
            Index key. Must be in prf.options.INDEX_NAMES.
        """
        if index not in INDEX_NAMES:
            available_keys = ", ".join(list(INDEX_NAMES.keys()))
            msg = (f"{index} key is not avaiable. Available keys: "
                   f"{available_keys}")
            logger.error(msg)
            raise KeyError(msg)
        self.index = index
        self.index_name = INDEX_NAMES[index]

    def _set_logger(self, index):
        """Create logging file handler for this process."""
        filename = self.home.joinpath("logs", index + ".log")
        set_handler(logger, filename)

if __name__ == "__main__":
    index = "pdsi"
    self = Data_Builder(index=index)


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

"""
    learned from others: math

    ref:
    - https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221939/C%2B%2B-with-picture-post-order-traversal
    - idea.png

    Time    O(n)
    Space   O(n)
    24 ms, faster than 81.56%
"""

class Solution(object):
    def distributeCoins(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.res = 0
        self.dfs(root)
        return self.res

    def dfs(self, node):
        if node == None:
            return 0
        left = self.dfs(node.left)
        right = self.dfs(node.right)
        self.res += abs(left) + abs(right)
        return node.val + left + right - 1


import torch
import torch.nn as nn
from network.resnetfpn_simple import *
from network.pooling import *
import MinkowskiEngine as ME

from network.general_minkfpn import GeneralMinkFPN

from models.minkloc import MinkLoc

from network.later_cloud_branch import LaterCloudBranch
import spconv.pytorch as spconv
from layers.pooling import MinkGeM as MinkGeM
try:
    from viz_lidar_mayavi_open3d import *
except:
    None
from network.general_imagefes import GeneralImageFE

from network.ffb import FFB
from network.gatt_block import GattBlock
# from network.pointnet_simple import PointNetSimple
# from pointnet2 import pointnet2_utils

from network.grids import Grids
from network.later_image_branch import LaterImageBranch

import torchvision

import matplotlib.pyplot as plt

from datasets.oxford import transform_pts_in_camsystem, project_onto_image

from network.ffb_local import FFBLocal
from network.ffb_glocal import FFBGLocal

from torch_geometric.nn import voxel_grid

from network.resnetfpn_simple import ImageGeM

from tools.options import Options
from tools.utils import set_seed
set_seed(7)
args = Options().parse()

def convert_ME_to_spconv_tensor(cloud_feat, tensor_stride):
    assert isinstance(cloud_feat, ME.SparseTensor)
    # test spconv
    spconv_feats = cloud_feat.F
    spconv_coords = cloud_feat.C
    spconv_coords[:,1:] = spconv_coords[:,1:] // tensor_stride
    spatial_shape = [
        torch.max(spconv_coords[:,1]) - torch.min(spconv_coords[:,1]),
        torch.max(spconv_coords[:,2]) - torch.min(spconv_coords[:,2]),
        torch.max(spconv_coords[:,3]) - torch.min(spconv_coords[:,3]),
    ]
    batch_size = len(cloud_feat.decomposed_coordinates)
    cloud_feat_spconv = spconv.SparseConvTensor(spconv_feats, spconv_coords, spatial_shape, batch_size=batch_size)

    return cloud_feat_spconv

def obtain_same_length_cloud_features(cloud_feat, sample_mode='rand', num_cloud_points=None):
    assert isinstance(cloud_feat, ME.SparseTensor) or isinstance(cloud_feat, spconv.SparseConvTensor)

    if isinstance(cloud_feat, ME.SparseTensor):
        # # -- obtain same length cloud features
        # -- fps ball query
        # clouds, cloud_feat = self.coordsclouds(cloud_feat, data_dict)
        # clouds, cloud_feat = self.pointnet2mlp(clouds, cloud_feat) # [b,npoint,3]  [b,c,npoint]
        # cloud_feat_gem = self.cloud_imagegem(cloud_feat.unsqueeze(-1))
        # --
        cloud_feat_list = cloud_feat.decomposed_features

    elif isinstance(cloud_feat, spconv.SparseConvTensor):
        # test spconv same length
        cloud_feat_list = []
        for i in range(cloud_feat.batch_size):
            rows_of_this_batch = cloud_feat.features[cloud_feat.indices[:,0]==i]
            cloud_feat_list.append(rows_of_this_batch)

    if sample_mode == 'rand':
        cloud_feat_avg = ME.MinkowskiGlobalAvgPooling()(cloud_feat)
        cloud_feat_avg = cloud_feat_avg.F.unsqueeze(1) # [b,1,c]
        # lengths_list = [len(row) for row in cloud_feat_list]
        # # # -- 1
        # cloud_feat = torch.nn.utils.rnn.pad_sequence(cloud_feat_list, batch_first=True)
        # # -- 2
        cloud_feat_same_length = []
        for i in range(len(cloud_feat_list)):
            this_x = cloud_feat_list[i]
            if len(this_x) < num_cloud_points:
                is_replace = True
            else:
                is_replace = False

            selected_indices = np.random.choice(len(this_x), size=num_cloud_points, replace=is_replace)
            new_x = this_x[selected_indices]
            cloud_feat_same_length.append(new_x)
        cloud_feat_same_length = torch.stack(cloud_feat_same_length) # [b,npts,c]
        # # -- 3, with map function
        # def _rand_sample(x):
        #     n = len(x)
        #     is_replace = True if n < num_cloud_points else False
        #     selected_indices = np.random.choice(len(x), size=num_cloud_points, replace=is_replace)
        #     output = x[selected_indices]
        #     return output
        # cloud_feat_same_length = map(_rand_sample, cloud_feat_list)
        # cloud_feat_same_length = list(cloud_feat_same_length)
        # cloud_feat_same_length = torch.stack(cloud_feat_same_length) # [b,npts,c]

        cloud_feat_same_length = torch.cat([cloud_feat_same_length, cloud_feat_avg], dim=1)
    elif sample_mode == 'globalavg':
        cloud_feat_same_length = ME.MinkowskiGlobalAvgPooling()(cloud_feat)
        cloud_feat_same_length = cloud_feat_same_length.F # [b,c]
        cloud_feat_same_length = cloud_feat_same_length.unsqueeze(1) # [b,1,c]
    elif sample_mode == 'globalmax':
        cloud_feat_same_length = ME.MinkowskiGlobalMaxPooling()(cloud_feat)
        cloud_feat_same_length = cloud_feat_same_length.F # [b,c]
        cloud_feat_same_length = cloud_feat_same_length.unsqueeze(1) # [b,1,c]
    elif sample_mode == 'globalavgmax':
        cloud_feat_avg = ME.MinkowskiGlobalAvgPooling()(cloud_feat)
        cloud_feat_max = ME.MinkowskiGlobalMaxPooling()(cloud_feat)
        cloud_feat_avg = cloud_feat_avg.F.unsqueeze(1) # [b,1,c]
        cloud_feat_max = cloud_feat_max.F.unsqueeze(1) # [b,1,c]
        cloud_feat_same_length = torch.cat([cloud_feat_avg, cloud_feat_max], dim=1) # [b,2,c]

    return cloud_feat_same_length

def obtain_same_length_image_features(image_feat, sample_mode, kernel_size=None, stride=None, num_image_points=None):
    '''
    sample image features, also have the same length
    input: image_feat: [b,c,h,w]
    output: [b,hw,c]
    '''
    assert len(image_feat.shape) == 4
    b,c,h,w = image_feat.shape

    if sample_mode == 'max':
        image_feat_same_length = F.max_pool2d(image_feat, kernel_size=kernel_size, stride=stride)
        image_feat_same_length = image_feat_same_length.flatten(2).permute(0,2,1) # [b,hw,c]
    elif sample_mode == 'avg':
        image_feat_same_length = F.avg_pool2d(image_feat, kernel_size=kernel_size, stride=stride)
        image_feat_same_length = image_feat_same_length.flatten(2).permute(0,2,1) # [b,hw,c]
    elif sample_mode == 'rand':
        image_feat_avg = F.adaptive_avg_pool2d(image_feat, output_size=(1,1))
        image_feat_avg = image_feat_avg.flatten(2).permute(0,2,1)
        image_feat = image_feat.flatten(2).permute(0,2,1) # [b,hw,c]
        # # --1
        selected_indices = np.random.randint(0, h*w, size=[b,num_image_points]) # [b,npts]
        image_feat_same_length = image_feat[torch.arange(b).unsqueeze(-1), selected_indices, :] # [b,npts,c]
        # image_feat_same_length = image_feat[torch.arange(b), selected_indices, :]
        # image_feat_same_length = image_feat[:, selected_indices, :]
        # # --2
        # image_feat_same_length = []
        # for _i in range(b):
        #     this_x = image_feat[_i]
        #     this_selected_indices = selected_indices[_i]
        #     this_x = this_x[this_selected_indices]
        #     image_feat_same_length.append(this_x)
        # image_feat_same_length = torch.stack(image_feat_same_length) # [b,npts,c]
        # -- 3
        # image_feat_same_length = []
        # for _i in range(b):

        image_feat_same_length = torch.cat([image_feat_same_length, image_feat_avg], dim=1)
    elif sample_mode == None:
        image_feat_same_length = image_feat.flatten(2).permute(0,2,1)
    elif sample_mode == 'globalavg':
        image_feat_same_length = F.adaptive_avg_pool2d(image_feat, output_size=(1,1))
        image_feat_same_length = image_feat_same_length.flatten(2).permute(0,2,1)
    elif sample_mode == 'globalmax':
        image_feat_same_length = F.adaptive_max_pool2d(image_feat, output_size=(1,1))
        image_feat_same_length = image_feat_same_length.flatten(2).permute(0,2,1)
    elif sample_mode == 'globalavgmax':
        image_feat_avg = F.adaptive_avg_pool2d(image_feat, output_size=(1,1))
        image_feat_max = F.adaptive_max_pool2d(image_feat, output_size=(1,1))
        image_feat_avg = image_feat_avg.flatten(2).permute(0,2,1)
        image_feat_max = image_feat_max.flatten(2).permute(0,2,1)
        image_feat_same_length = torch.cat([image_feat_avg, image_feat_max], dim=1)
    else:
        raise Exception

    return image_feat_same_length

class UniVPRV2(nn.Module):
    def __init__(self):
        super(UniVPRV2, self).__init__()

        self.image_fe = GeneralImageFE(image_fe=args.image_fe,
                                       num_other_stage_blocks=args.num_other_stage_blocks,
                                       num_stage3_blocks=args.num_stage3_blocks,
                                       input_type='image',
                                       image_fe_dim=args.image_fe_dim,
                                       )

        if args.cloud_fe == 'general_minkfpn':
            self.cloud_fe = GeneralMinkFPN(in_channels=1, out_channels=None, num_top_down=None, conv0_kernel_size=5,
                                        layers=[1,1,1], planes=[32,64,64])
        elif args.cloud_fe == 'minkloc':
            self.cloud_fe = MinkLoc(in_channels=1, feature_size=args.cloud_fe_dim, output_dim=args.cloud_fe_dim,
                            planes=[32, 64, 64], layers=[1, 1, 1], num_top_down=1,
                            conv0_kernel_size=5, block='ECABasicBlock', pooling_method='GeM')

        self.ffbs = nn.ModuleList()
        self.laterimagebranches = nn.ModuleList()
        self.latercloudbranches = nn.ModuleList()
        self.ffblocals = nn.ModuleList()
        self.gem = ImageGeM()

        for i in range(args.num_ffbs):
            ffb = FFB(in_features=args.fusion_dim, hidden_features=args.fusion_dim//8, n_heads=8,
                      num_blocks=args.num_blocks_in_ffb,
                        ffb_type=args.ffb_type,
                        num_image_points=args.num_image_points, num_cloud_points=args.num_cloud_points,
                        image_fe_last_dim=self.image_fe.last_dim)
            laterimagebranch = LaterImageBranch(block_type=args.later_image_branch_type,num_heads=8,
                                            in_features=self.image_fe.last_dim,
                                            num_blocks=args.num_blocks_in_later_image,
                                            window_size=[7,7], shift_size=[0,0])
            latercloudbranch = LaterCloudBranch(in_features=128,
                                                num_blocks=args.num_blocks_in_later_cloud)
            # ffblocal = FFBLocal(in_image_feat_dim=self.image_fe.last_dim, embed_dim=args.fusion_dim, num_heads=8, num_blocks=1, window_size=4)
            ffblocal = FFBLocal(in_image_feat_dim=self.image_fe.last_dim, embed_dim=args.fusion_dim, num_heads=8, num_blocks=1, window_size=args.ffblocal_windowsize)

            self.ffbs.append(ffb)
            self.laterimagebranches.append(laterimagebranch)
            self.latercloudbranches.append(latercloudbranch)
            self.ffblocals.append(ffblocal)

    def forward(self, data_dict):
        # feed_dict{
        #     'positives_mask(train)',
        #     'negatives_mask(train)',
        #     'coords',
        #     'clouds',
        #     'features',
        #     'uv',
        #     'colors',
        #     'maskselect',
        #     'images',
        #     'P0_camera',
        #     'T_camera_lidar_basedon_pose',
        # }

        # -- image part
        image_feat_128, image_feat_gem_org, image_feat_256, _ = self.image_fe(data_dict)

        # -- cloud part
        cloud_feat_dict = self.cloud_fe(data_dict)
        cloud_feat = cloud_feat_dict['x_feat']
        cloud_feat_gem_org = cloud_feat_dict['embedding']
        cloud_feat_gem_extra = cloud_feat_dict['embedding_extra']

        # _coords = cloud_feat.coordinates[cloud_feat.coordinates[:,0]==0][:,1:]
        # T_camera_lidar_basedon_pose = data_dict['T_camera_lidar_basedon_pose'][0]
        # P0_camera = data_dict['P0_camera'][0]
        # _pts_in_image, _pts_uv_in_image, _pts_colors_in_image, _pts_out_image = convert_pts_to_uv(_coords, T_camera_lidar_basedon_pose, P0_camera)
        # _image = data_dict['images'][0]
        # _pts_uv_in_image = _pts_uv_in_image * 0.125 / 1 # 16 is the downsample ratio of the image feature
        # _pts_window_indices, _image, _image_window_indices = get_window_indices(_pts_uv_in_image, _image, window_size=[64,64])

        # _pts_in_image = _pts_in_image.cpu().numpy()
        # _pts_uv_in_image = _pts_uv_in_image.cpu().numpy()
        # _pts_colors_in_image = _pts_colors_in_image.cpu().numpy()

        # # _pts_all = torch.cat([_pts_in_image, _pts_out_image], dim=0).cpu().numpy()
        # # viz_lidar_open3d(_pts_all)
        # # _coords = cloud_feat.coordinates[cloud_feat.coordinates[:,0]==0][:,1:]
        # # _coords = _coords.cpu().numpy()
        # # # _clouds = _clouds.cpu().numpy()
        # # # viz_lidar_open3d(_clouds)
        # # _T = data_dict['T_camera_lidar_basedon_pose'][0].cpu().numpy()
        # # _P0 = data_dict['P0_camera'][0].cpu().numpy()
        # # _coordsincamsystem = transform_pts_in_camsystem(_coords,T=_T)
        # # # viz_lidar_open3d(_coordsincamsystem)
        # # _uv,_colors,_mask = project_onto_image(_coordsincamsystem,P=_P0)
        # # _selected_indices = (_coords[:,0]>0) & (_coords[:,0]<1000) \
        # #             & (_coords[:,1]>0) & (_coords[:,1]<1000) \
        # #             & (_coords[:,2]>-10) & (_coords[:,2]<50)
        # # _maskselect = _mask & _selected_indices
        # # # _uv = _uv[_maskselect]
        # # _uv = _uv * 0.125 / 16
        # # viz projected points
        # plt.figure(dpi=200)
        # # _image = image_feat_128[0,:3].cpu()
        # # _image = image_feat_256[0,:3].cpu()
        # _image = data_dict['images'][0].cpu()
        # _mean = torch.tensor([0.485, 0.456, 0.406]).unsqueeze(-1).unsqueeze(-1)
        # _std = torch.tensor([0.229, 0.224, 0.225]).unsqueeze(-1).unsqueeze(-1)
        # _image = _image*_std + _mean
        # _image = torchvision.transforms.ToPILImage()(_image)
        # _image = np.array(_image)
        # # _uv = _uv[_maskselect]
        # # _colors = _colors[_maskselect]
        # _uv = _pts_uv_in_image  * 0.125
        # h_max = _uv[:,1].max()
        # w_max = _uv[:,0].max()

        # _colors = _pts_colors_in_image
        # plt.scatter(_uv[:,0], _uv[:,1], c=_colors, s=1)
        # plt.imshow(_image)
        # plt.show()
        # # plt.close()

        a=1

        if args.num_ffbs > 0:

            for i in range(args.num_ffbs):
                ffb = self.ffbs[i]
                laterimagebranch = self.laterimagebranches[i]
                latercloudbranch = self.latercloudbranches[i]
                ffblocal = self.ffblocals[i]

                # GFM
                image_feat_same_length = obtain_same_length_image_features(image_feat_128, sample_mode=args.sample_mode, num_image_points=args.num_image_points)
                cloud_feat_same_length = obtain_same_length_cloud_features(cloud_feat, sample_mode=args.sample_mode, num_cloud_points=args.num_cloud_points)

                output_dict = ffb(in_image_feat=image_feat_same_length,  # image_feat_128
                                  in_cloud_feat=cloud_feat_same_length)
                ffb_output_gem_128 = output_dict['output_feat_gem_128']
                ffb_output_gem_256 = output_dict['output_feat_gem_256']

                image_feat_128, image_feat_256_gem_later, image_feat_256 = laterimagebranch(image_feat_256, ffb_output_gem_256)
                cloud_feat, cloud_feat_gem_later = latercloudbranch(cloud_feat, ffb_output_gem_128)

                # local_ffb + NDM
                if args.use_ffblocal and i<args.num_ffbs-1:
                    image_feat_256, cloud_feat = ffblocal(image_feat_256, cloud_feat, data_dict['T_camera_lidar_basedon_pose'], data_dict['P0_camera'],
                                                        downsample_ratio=16, image=data_dict['images'])

            if args.final_embedding_type == 'imageorg_cloudorg_ffb':
                embedding = torch.cat([image_feat_gem_org, cloud_feat_gem_org, ffb_output_gem_128], dim=1)
            elif args.final_embedding_type == 'imagelater_cloudlater_ffb':
                embedding = torch.cat([image_feat_256_gem_later, cloud_feat_gem_later, ffb_output_gem_128], dim=1)
            elif args.final_embedding_type == 'imagelater_cloudlater':
                embedding = torch.cat([image_feat_256_gem_later, cloud_feat_gem_later], dim=1)
            elif args.final_embedding_type == 'imageorg':
                embedding = image_feat_gem_org
            elif args.final_embedding_type == 'cloudorg':
                embedding = cloud_feat_gem_org
            # elif args.final_embedding_type == 'imagelater_cloudlater_ffb_sphcloud':
            #     embedding = torch.cat([image_feat_256_gem_later, cloud_feat_gem_later, ffb_output_gem_128, sph_cloud_feat_gem_org], dim=1)
            else:
                raise Exception

        elif args.num_ffbs == 0:
            image_feat_same_length = obtain_same_length_image_features(image_feat_128, sample_mode=args.sample_mode, num_image_points=args.num_image_points)
            cloud_feat_same_length = obtain_same_length_cloud_features(cloud_feat, sample_mode=args.sample_mode, num_cloud_points=args.num_cloud_points)
            fusion_feat = torch.cat([image_feat_same_length, cloud_feat_same_length], dim=1) # [b,n,c]
            fusion_feat = fusion_feat.permute(0,2,1) # [b,c,n]
            fusion_feat = fusion_feat.unsqueeze(-1) # [b,c,n,1]
            fusion_gem = self.gem(fusion_feat) # [b,c]

            if args.final_embedding_type == 'imageorg_cloudorg':
                embedding = torch.cat([image_feat_gem_org, cloud_feat_gem_org], dim=1)
            elif args.final_embedding_type == 'imageorg_cloudorg_ffb':
                embedding = torch.cat([image_feat_gem_org, cloud_feat_gem_org, fusion_gem], dim=1)

        # print(f'beforfusion_{args.use_l2norm_before_fusion}_outputfeat_{args.use_l2norm_for_output_feat}')

        output_dict = {
            'image_embedding':image_feat_gem_org,
            'cloud_embedding':cloud_feat_gem_org,
            'embedding':embedding,
        }

        return output_dict


from datetime import datetime

from pymongo import MongoClient

from configuration import CONFIG

class Persistence(object):
    CLIENT = None
    DB = None

    @staticmethod
    def initialize():
        Persistence.CLIENT = MongoClient(CONFIG.MONGODB_SERVER_ADDR)
        Persistence.DB = Persistence.CLIENT.rps2

    @staticmethod
    def get_tokens():
        return [t['token'] for t in Persistence.DB.tokens.find()]

    @staticmethod
    def create_token(token, system_name):
        Persistence.DB.tokens.insert_one({
            'token': token,
            'system_name': system_name
        })

    @staticmethod
    def is_token_registered(token):
        return token in Persistence.get_tokens()

    @staticmethod
    def save_measure(token, buffer):
        resources = {}
        for data in buffer:
            timestamp = datetime.fromtimestamp(data['timestamp'])
            for type, measured in data['measured'].iteritems():
                if type not in resources:
                    resources[type] = []
                resources[type].append(
                        {'timestamp:': timestamp,
                         'measured:': measured})

        for type, measurements in resources.iteritems():
            collection_name = Persistence.get_collection_name(token, type)
            Persistence.DB[collection_name].insert_many(measurements)

    @staticmethod
    def get_system_name_by_token(token):
        system = Persistence.DB.tokens.find_one({
            'token': token
        })
        return system['system_name']

    @staticmethod
    def get_monitored_data(token, type, start_date, end_date):
        collection_name = Persistence.get_collection_name(token, type)
        collection = Persistence.DB[collection_name]
        if collection is not None:
            return {'data': [measure for measure in collection.find({'timestamp': {
                '$gt': start_date,
                '$lt': end_date
            }})]}
        else:
            return {}

    @classmethod
    def get_collection_name(cls, token, type):
        return token + '#' + type


class Solution1:
    def maxProfit(self, prices: list[int]) ->int:
        curPrice = prices[0]
        maxProf = 0
        n = len(prices)
        for i in range(1,n):
            if prices[i] <= curPrice:
                curPrice = prices[i]
            else:
                maxProf = max(maxProf, prices[i]-curPrice)
        return maxProf

class Solution2:
    def maxProfit(self, prices: list[int]) ->int:
        minPrice = 1e10
        maxProf = 0
        for price in prices:
            minPrice = min(minPrice, price)
            maxProf = max(maxProf, price - minPrice)
        return maxProf


"""
Test backend functions
"""
from io import BytesIO
from zipfile import is_zipfile

from django.core.exceptions import ValidationError
from django.conf import settings
from django.test import override_settings
from mock import MagicMock, patch

from sga.backend.authentication import get_role
from sga.backend.constants import Roles
from sga.backend.files import convert_illegal_S3_chars, submissions_zip_generator
from sga.backend.send_grades import send_grade, SendGradeFailure
from sga.backend.validators import validate_file_extension, validate_file_size
from sga.tests.common import SGATestCase

class TestBackend(SGATestCase):
    """
    Test that the backend functions work as expected
    """

    def test_validate_file_extension(self):
        """
        Verify that validators.validate_file_extension() works correctly
        """
        INVALID_FILE_UPLOAD_EXTENSIONS = [".ppt", ".txt", ".docx", ".jpg"]
        for ext in settings.VALID_FILE_UPLOAD_EXTENSIONS:
            file_obj = self.get_test_file("file{ext}".format(ext=ext))
            # Call to ensure it doesn't raise an exception
            validate_file_extension(file_obj)
        for ext in INVALID_FILE_UPLOAD_EXTENSIONS:
            file_obj = self.get_test_file("file{ext}".format(ext=ext))
            self.assertRaises(ValidationError, validate_file_extension, file_obj)

    def test_validate_file_size(self):
        """
        Verify that validators.validate_file_extension() works correctly
        """
        file_obj = MagicMock(file=MagicMock(size=100))
        # Call to ensure it doesn't raise an exception
        validate_file_size(file_obj)
        file_obj = MagicMock(file=MagicMock(size=10 * 1024 * 1024))
        self.assertRaises(ValidationError, validate_file_size, file_obj)

    def test_get_role(self):
        """
        Verify that authentication.get_role() returns the correct roles
        """
        course = self.get_test_course()
        admin_user = self.get_test_admin_user()
        student_user = self.get_test_student_user()
        grader_user = self.get_test_grader_user()
        user = self.get_test_user()
        self.assertEqual(get_role(admin_user, course.id), Roles.admin)
        self.assertEqual(get_role(student_user, course.id), Roles.student)
        self.assertEqual(get_role(grader_user, course.id), Roles.grader)
        self.assertEqual(get_role(user, course.id), Roles.none)

    def test_convert_illegal_S3_chars(self):
        """
        Verify that convert_illegal_S3_chars returns the correct conversions
        """
        CONVERSIONS = {
            "abc'defg/_(12.345)-AB*CD!": "abc'defg/_(12.345)-AB*CD!",
            "abc@#$123": "abc___123",
            "abc+<>?_": "abc_____"
        }
        for unconverted, converted in CONVERSIONS.items():
            self.assertEqual(convert_illegal_S3_chars(unconverted), converted)

    @patch(
        "oauth2.Client.request",
        MagicMock(return_value=("", "<imsx_codeMajor>success</imsx_codeMajor>"))
    )
    @override_settings(LTI_OAUTH_CREDENTIALS={"key": "secret"})
    def test_send_grade(self):  # pylint: disable=no-self-use
        """
        Tests the send_grade() function
        """
        self.assertRaises(SendGradeFailure, send_grade, "not_key", "url", "result_id", 1)
        # Call this to assert that it does NOT raise Exception
        send_grade("key", "url", "result_id", 1)

    @patch("oauth2.Client.request", MagicMock(return_value=(MagicMock(status="Bad"), "")))
    @override_settings(LTI_OAUTH_CREDENTIALS={"key": "secret"})
    def test_bad_send_grade(self):  # pylint: disable=no-self-use
        """
        Tests the failure of send_grade() function
        """
        self.assertRaises(SendGradeFailure, send_grade, "key", "url", "result_id", None)

    def test_submissions_zip_generator(self):
        """
        Tests submissions_zip_generator()
        """
        submissions = [MagicMock(student_document=self.get_test_file()) for _ in range(10)]
        # Since we're getting a stream, unpack streamed response
        zipfile = bytearray("", encoding="utf8").join(submissions_zip_generator(submissions))
        self.assertTrue(is_zipfile(BytesIO(zipfile)))


from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import cross_val_score
from numpy.random import randint
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier

s = 100
nx = np.linspace(-100,100,s)
ny = np.linspace(-100,100,s)
init_1 = 0
init_2 = 0

radius = 75

prob_inside = 0.9
prob_outside = 0.1

labels = np.zeros(s**2)
data = np.zeros((s**2,2))

counter1 = 0
for i in nx:
    counter2 = 0
    for j in ny:
        data[(counter1)*s + counter2] = np.array([[i,j]])
        number = randint(1,100,1)
        if(i**2 + j**2 > 75**2):
            if(number >= 15):
                labels[(counter1)*s + counter2] = -1
            else:
                labels[(counter1)*s + counter2] = 1
        else:
            if(number >= 85):
                labels[(counter1)*s + counter2] = -1
            else:
                labels[(counter1)*s + counter2] = 1

        counter2 += 1
    counter1 += 1

labels = np.array([labels])
labels = np.transpose(labels)

print(labels.shape)
print(data.shape)

data = np.concatenate((data,labels),axis=1)
np.savetxt("circle_noise.csv",data,delimiter=",")

plt.scatter(data[:,0],data[:,1],labels)
plt.show()


# -*- coding: utf-8 -*-
import json
import os
import pickle
import numpy
from extract import Extractor
from extract import RawData

class SecExtractor(Extractor):
    file_name_temp = "click_sec.json"
    bin_file = "click_sec.bin"

    def __init__(self):
        if os.path.exists(SecExtractor.file_name_temp):
            os.remove(SecExtractor.file_name_temp)
        if os.path.exists(SecExtractor.bin_file):
            os.remove(SecExtractor.bin_file)

    def get_total_click(self, ip):
        # type: (str) -> int
        """
        :rtype: numpy.float64
        """
        if os.path.exists("total_click_dict_file"):
            # has calculated
            total_click_dict_file = open("total_click_dict_file", "rb")
            total_click_dict = pickle.load(file=total_click_dict_file)
            if total_click_dict.has_key(ip):
                total_click_dict_file.close()
                return total_click_dict[ip]
            else:
                total_click_dict_file.close()
                raise Exception("wrong ip")
        else:
            # calculate total click for every ip
            total_click_dict_file = open("total_click_dict_file", "wb")
            total_click_dict = dict()
            bin_file = open(SecExtractor.bin_file, "rb")
            ip_click_sec = dict()
            pickle.load(ip_click_sec, bin_file)
            for k, v in ip_click_sec.items():
                total_click_dict[k] = sum(v)
            # save dict to binary file
            pickle.dump(total_click_dict, total_click_dict_file)
            total_click_dict_file.close()
        return total_click_dict[ip]

    def get_max_click_sec(self, ip):
        # type: (str) -> int
        """
        :rtype: numpy.float64
        """
        if os.path.exists("var_click_sec"):
            # has calculated
            max_click_sec_dict_file = open("var_click_sec", "rb")
            max_click_sec_dict = pickle.load(file=max_click_sec_dict_file)
            if max_click_sec_dict.has_key(ip):
                max_click_sec_dict_file.close()
                return max_click_sec_dict[ip]
            else:
                max_click_sec_dict_file.close()
                raise Exception("wrong ip")
        else:
            # calculate variance click for every ip by sec
            max_click_sec_dict_file = open("var_click_sec", "wb")
            max_click_sec_dict = dict()
            bin_file = open(SecExtractor.bin_file, "rb")
            ip_click_sec = dict()
            pickle.load(ip_click_sec, bin_file)
            for k, v in ip_click_sec.items():
                max_click_sec_dict[k] = numpy.max(v)
            # save dict to binary file
            pickle.dump(max_click_sec_dict, max_click_sec_dict_file)
            max_click_sec_dict_file.close()
        return max_click_sec_dict[ip]

    def get_var_click_sec(self, ip):
        # type: (str) -> int
        """
        :rtype: numpy.float64
        """
        if os.path.exists("var_click_sec"):
            # has calculated
            var_click_sec_dict_file = open("var_click_sec", "rb")
            var_click_sec_dict = pickle.load(file=var_click_sec_dict_file)
            if var_click_sec_dict.has_key(ip):
                var_click_sec_dict_file.close()
                return var_click_sec_dict[ip]
            else:
                var_click_sec_dict_file.close()
                raise Exception("wrong ip")
        else:
            # calculate variance click for every ip by sec
            var_click_sec_dict_file = open("var_click_sec", "wb")
            var_click_sec_dict = dict()
            bin_file = open(SecExtractor.bin_file, "rb")
            ip_click_sec = dict()
            pickle.load(ip_click_sec, bin_file)
            for k, v in ip_click_sec.items():
                var_click_sec_dict[k] = numpy.var(v)
            # save dict to binary file
            pickle.dump(var_click_sec_dict, var_click_sec_dict_file)
            var_click_sec_dict_file.close()
        return var_click_sec_dict[ip]

    def get_avg_click_sec(self, ip):
        # type: (str) -> float
        ip_click_sec = dict()
        bin_file = open(SecExtractor.bin_file, "rb")
        pickle.load(ip_click_sec, bin_file)
        # noinspection PyTypeChecker
        return numpy.float64(numpy.mean(ip_click_sec[ip])) / SecExtractor.get_total_click(ip)

    def extract(self,line):
        model = RawData(line)
        row = [model.ip, model.timestamps]
        if os.path.exists(SecExtractor.file_name_temp):
            _file = open(SecExtractor.file_name_temp, 'a+')
            _file.writelines(json.dumps(row))
            _file.write('\n')
            _file.flush()
        else:
            _file = open(SecExtractor.file_name_temp, 'w')
            _file.writelines(json.dumps(row))
            _file.write('\n')
            _file.flush()

    def merge(self):
        ip_click_sec = dict()
        _file = open(SecExtractor.file_name_temp, 'r')
        line = _file.readline()
        while line != "":
            line = line.replace("\n", "")
            row = json.loads(line)
            line = _file.readline()
            if not ip_click_sec.has_key(row[1]):
                ip_click_sec[row[0]] = (0 for x in range(60480))
            ip_click_sec[row[0]][row[2]] += 1
        bin_file = open(SecExtractor.bin_file, "wb")
        pickle.dump(ip_click_sec, bin_file)
        bin_file.close()


from __future__ import absolute_import, division, print_function

def run():
  from mmtbx.monomer_library import server
  from libtbx.utils import Sorry
  try :
    cif1 = server.mon_lib_list_cif()
    cif2 = server.geostd_list_cif()
    cif3 = server.mon_lib_ener_lib_cif()
    assert (not None in [cif1, cif2, cif3])
  except server.MonomerLibraryServerError :
    raise Sorry("The monomer library installation could not be found.  If "+
      "you are using a Phenix installer, we recommend downloading the "+
      "installer again and reinstalling.")
  else :
    print("OK")

if (__name__ == "__main__"):
  run()


import numpy as np
from gym_multirotor.utils.rotation_transformations import quat2rot

def get_ob_tiltrotor(mujoco_pos, mujoco_vel):
    """
    Convert mujoco observation with quaternion orientation to observation with orientation as
    a direction cosine matrix for tilt-rotor environments.
    This function returns the fully observed state of the tilt-rotor environment

    :param mujoco_pos: numpy array [shape (11,)] of position vector
    :param mujoco_vel: numpy array [shape (10,)] of velocity vector
    :return: numpy observation vector [shape (26,)]
    """

    pos = mujoco_pos[:3]
    quat = mujoco_pos[3:7]
    rot_mat = quat2rot(quat)
    rot_mat = rot_mat.flatten()
    tilt_angles = mujoco_pos[7:]
    return np.concatenate([pos, rot_mat, tilt_angles, mujoco_vel]).ravel()

def get_ob_quadrotor(mujoco_pos, mujoco_vel):
    """
    Function to convert mujoco observation with quaternion orientation to
    observation with orientation as direction cosine matrix for quad-copter environments

    :param mujoco_pos: numpy array [shape (7,)] of  position vector
    :param mujoco_vel: numpy array [shape (6,)] of velocity vector
    :return: numpy observation vector [shape (18,)]
    """

    pos = mujoco_pos[:3]                    # possibly this pos represents error

    quat = mujoco_pos[3:7]
    rot_mat = quat2rot(quat)
    rot_mat = rot_mat.flatten()

    return np.concatenate([pos, rot_mat, mujoco_vel]).ravel()

def get_partial_ob_tiltrotor(full_ob):
    """
    Function to evaluate partially observable state of the tilt-rotor uav environment

    :param full_ob: (numpy vector) complete observation vector of the environment
    :return observed_state: (numpy vector) partially observed state of the environment
    """

    e_pos = full_ob[:3]
    rot_mat = full_ob[3:12]
    vel = full_ob[16:19]
    angular_vel = full_ob[19:22]
    observed_state = np.concatenate([e_pos, rot_mat, vel, angular_vel]).ravel()
    return observed_state

def get_partial_observation(full_ob):
    """
    Function to evaluate partially observable state of the tilt-rotor uav environment
    Convenience function for `get_partial_ob_tiltrotor`

    :param full_ob: (numpy vector) complete observation vector of the environment
    :return observed_state: (numpy vector) partially observed state of the environment
    """
    return get_partial_ob_tiltrotor(full_ob)


#   This file is part of APUtils.
#   APUtils is a porting with addictions of some Embarcadero Delphi VCL Functions
#   made by APressato <apressato@gmail.com> in 2018 - 2019.
#
#   APUtils is free software: you can redistribute it and/or modify
#   it under the terms of the
#   Creative Commons Attribution ShareAlike 4.0 International
#   as published by Creative Commons.
#
#   APUtils is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   Creative Commons Attribution ShareAlike 4.0 International for more details.
#
#   You should have received a copy of the
#   Creative Commons Attribution ShareAlike 4.0 International
#   along with APUtils. If not,
#   see <https://creativecommons.org/licenses/by-sa/4.0/legalcode>.

import ast

ptLeft = 0
ptRight = 1
ptCenter = 2

def StringToStructure(aStr):
    """Transform a string into a python structure.
       Example:
       "{test: 1}" -> {test: 1}
    """
    return ast.literal_eval(aStr)

def CapitalizeToken(aToken):
    sResult = ''
    if len(aToken) > 0:
        sResult = str(aToken[0]).upper() + aToken[1:]
    return sResult

def Capitalize(aStr, RespectCase = False):
    sResult = aStr
    if RespectCase:
       MyStr = [(CapitalizeToken(txt)) for txt in aStr.split(' ')]
       sResult = ' '.join(MyStr)
    else:
       sResult = aStr.capitalize()
    return sResult

def APPad(aStr, PaddingChar, PaddingLenght, PaddingType = ptLeft):
    sResult = aStr
    if PaddingType == ptLeft:
       sResult = aStr.ljust(PaddingLenght, PaddingChar)
    elif PaddingType == ptRight:
       sResult = aStr.rjust(PaddingLenght, PaddingChar)
    elif PaddingType == ptCenter:
       PerSide = (PaddingLenght - len(aStr)) // 2
       sResult = aStr.rjust(len(aStr) + PerSide, PaddingChar)
       sResult = sResult.ljust(PaddingLenght, PaddingChar)
    return sResult

def APPadClipper(aStr, PaddingChar, PaddingLenght, PaddingType = ptLeft, TruncToLen = True):
    sResult = APPad(aStr, PaddingChar, PaddingLenght, PaddingType)
    if TruncToLen:
       return sResult[:PaddingLenght]
    else:
       return sResult

def SortString(aStr):
    return ''.join(sorted(aStr))

def DelChar(aStr, aChr):
    sResult = aStr
    if aStr != '':
       sResult = ''
       for ch in aStr:
          if ch != aChr:
            sResult += ch
    return sResult

def DelAllChar(aStr, aDelChrs):
    sResult = aStr
    for ch in aDelChrs:
       sResult = DelChar(sResult, ch)
    return sResult

def TrimRightChar (aStr, Ch):
    sResult = aStr
    if aStr != '':
       while (len(sResult)>0) and (sResult[-1] == Ch):
            sResult = sResult[:-1]
    return sResult

def TrimLeftChar (aStr, Ch):
    sResult = aStr
    if aStr != '':
       while (len(sResult)>0) and (sResult[0] == Ch):
            sResult = sResult[1:]
    return sResult

def TrimRightLeftChar (aStr, Ch):
    sResult = aStr
    if aStr != '':
       sResult = TrimRightChar(aStr, Ch)
       sResult = TrimLeftChar(sResult, Ch)
    return sResult

def ReduceCommonFactorChar(aStr, Ch):
   sResult = aStr
   if aStr != '':
      while sResult.find(Ch + Ch) > -1:
           sResult = sResult.replace(Ch + Ch, Ch)
   return sResult

def ReduceCommonFactorSpaces(aStr):
   return ReduceCommonFactorChar(aStr, ' ')

def TrimFuzzies(aStr):
   NonFuzzies = "' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
   sResult = aStr
   if aStr != '':
     sResult = ''
     for ch in aStr:
        if ch in NonFuzzies:
          sResult += ch
     sResult = ReduceCommonFactorSpaces(sResult)
     sResult = sResult.strip()
   return sResult

def ReplaceAllChars(aStr, OldChars, NewChars, RaiseErrors = False):
   sResult = aStr
   if len(OldChars) != len(NewChars):
      if RaiseErrors:
         raise ValueError("OldChars and NewChars must have the same lenght")
      else:
         return sResult
   for i in range(len(OldChars)):
      sResult = sResult.replace(OldChars[i], NewChars[i])
   return sResult

if __name__ == "__main__":
    print(StringToStructure('{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}'))
    print(Capitalize('Python is Awesome'))
    print(Capitalize('Python is AWesome', True))
    print(APPad(' Left Pad ', '=', 20))
    print(APPad(' Right Pad ', '=', 20, ptRight))
    print(APPad(' Center Pad ', '=', 20, ptCenter))
    print(APPadClipper(' Left Pad ', '=', 8))
    print(APPadClipper(' Right Pad ', '=', 8, ptRight))
    print(APPadClipper(' Center Pad ', '=', 8, ptCenter))
    print(SortString('425361'))
    print(DelChar('11123111451116111', '1'))
    print(DelAllChar('1234567890', '13579'))
    print(TrimRightChar('1234567890---------------', '-'))
    print(TrimLeftChar('------1234567890', '-'))
    print(TrimRightLeftChar('------------1234567890----------', '-'))
    print(ReduceCommonFactorChar('12--34--56--78---90-', '-'))
    print(ReduceCommonFactorSpaces('Python     is      Awesome'))
    print(TrimFuzzies('Python### is @@@@@@@Awesome'))
    print(ReplaceAllChars('We#Are-Legion', '#-', '  ', True))


import sys
class Calculadora:
    def __init__(self):
        print ("""************
                Calculadora
                ************
                """
                )
    def suma(self,x,y):
        print ("La suma es: ", x+y)
    def resta(self, x, y):
        print ("La resta es: ", x-y)
    def multi(self, x, y):
        print ("La multiplicacion es: ", x*y)
    def divi(self, x, y):
        try:
            print ("La division es: ", x/y)
        except ZeroDivisionError:
            print ("Error, división entre cero")

def Menu():
    while True:
        resultado = Calculadora()
        print ("Digite dos números")
        try:
            a = int(input("Numero 1: "))
            b = int(input("Numero 2: "))
            print ("""
                    Menu
                    1) Suma
                    2) Resta
                    3) Multiplicacion
                    4) Division
                    5) Salir""")
            opc = int(input("Seleccione una opcion: "))
            if(opc == 1):
                resultado.suma(a,b)
            elif (opc == 2):
                resultado.resta(a, b)
            elif (opc == 3):
                resultado.multi(a,b)
            elif (opc == 4):
                resultado.divi(a,b)
            elif (opc == 5):
                break
        except:
            print ("Error, tipo de dato invalido")
Menu()


import tensorflow as tf
from reader import get_csv_dataset

sess = tf.Session()
ds = get_csv_dataset("iris_train.csv", 1)
iterator = tf.data.make_initializable_iterator(ds)
labels, features = iterator.get_next()
for _ in range(10):
  sess.run(iterator.initializer)
  sess.run([labels, features])


import sys
from PyQt4 import QtGui,QtCore
import time
import random
from PyQt4.QtCore import QTimer
class MyThread(QtCore.QThread):
    trigger = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)

    def run_(self, message):
        #time.sleep(random.random() * 5)
        self.trigger.emit(message)

class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.text_area = QtGui.QTextBrowser()
        self.thread_button = QtGui.QPushButton('Start threads')
        self.thread_button.clicked.connect(self.start_threads)

        central_widget = QtGui.QWidget()
        central_layout = QtGui.QHBoxLayout()
        central_layout.addWidget(self.text_area)
        central_layout.addWidget(self.thread_button)
        central_widget.setLayout(central_layout)
        self.setCentralWidget(central_widget)

        #self.threads = MyThread(self)
       # self.threads.trigger.connect(self.update_text)

        self.thread_no = 0

    def start_threads(self):
        # self.thread_no += 1
        # message = "cnt:{0}".format(self.thread_no)
        # self.threads.run_(message)  # start the thread
        # print(message)
        self.timer = QTimer(self) #初始化一个定时器
        self.timer.timeout.connect(self.oprate) #计时结束调用operate()方法
        self.timer.start(2000) #设置计时间隔并启动
    def oprate(self):
        print(1111)
    def update_text(self, message):
        # self.text_area.append('thread # %d finished'%thread_no)
        # print('thread # %d finished'%thread_no)
        self.text_area.clear()
        self.text_area.append(message)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    mainWindow = Main()
    mainWindow.show()

    sys.exit(app.exec_())


# -*- coding: utf-8 -*-
"""
Created on Tue Feb 26 22:45:43 2019

@author: Alpha
"""

import pandas as pd
import numpy as np
import copy
p = [0.7,0.3]
df = pd.read_excel("C:/Users/Alpha/Desktop/planwork/df.xlsx")
#df['proActual'] = 0
#df['proStandard'] = 0
dfh = pd.read_excel("C:/Users/Alpha/Desktop/planwork/dfh.xlsx")

df = df.fillna(0)
#df = df[df['skill'] == '甲']
datafreq = ['often','some']
a1 = np.double(sum(df['often']))
a2 = np.double(sum(df['some']))
a = [a1,a2]
N = np.double(sum(df['date']))

if N == 0:
    for i in range(len(datafreq)):
        df.loc[df[datafreq[i]]==1,'proStandard'] =  p[i]/a[i]
    index = copy.deepcopy(df.index)
    index = np.random.permutation(index)
    data = dfh['name'].tolist()
    data.append(df['name'][index[0]])
    dfh = pd.DataFrame(data,columns=['name'])
    df.loc[index[0],'date'] = df['date'][index[0]] + 1
    N = sum(df['date'])
    df['proActual'] = df['date'] / N
    df['dateDiff'] = df['proActual'] - df['proStandard']

else:
    c = df['proActual'] < df['proStandard']
    dftemp = df[c]
    dftemp = dftemp.sort_values('dateDiff')
    index = copy.deepcopy(dftemp.index)
    index = np.random.permutation(index)
    data = dfh['name'].tolist()
    data.append(df['name'][index[0]])
    dfh = pd.DataFrame(data,columns=['name'])
    df.loc[index[0],'date'] = df['date'][index[0]] + 1
    N = sum(df['date'])
    df['proActual'] = df['date'] / N
    df['dateDiff'] = df['proActual'] - df['proStandard']
    print(df)

df.to_excel("C:/Users/Alpha/Desktop/planwork//df.xlsx",index=False)
dfh.to_excel("C:/Users/Alpha/Desktop/planwork/dfh.xlsx",index=False)


'''
1) 벽 정보 세팅
-

2) 성에 있는 방의 개수 : bfs 및 dfs를 통한 집단 분류

3) 가장 넓은 방의 넓이 --> 위에서 구한 것 중에서 최댓값

4) 하나 벽 --> 벽 부수고 이동하기

'''
n = int(input())

'''
1) 부분 수열의 개수 for 문 돌리기
2) 해당 부분 수열 갯수에 대한 dfs
'''

def DFS(v):
    if v == n+1:
        for i in range(1, n+1):
            if ch[i] == 1:
                print(i, end=' ')
        print()
    else:
        # 사용한다, 사용하지 않는다 -> ch 활용하기
        ch[v] = 1
        DFS(v+1)
        ch[v] = 0
        DFS(v+1)

ch = [0] * (n+1)
DFS(1)


from aqt import gui_hooks, mw
from aqt.addcards import AddCards

def init(add_cards):
    editor = add_cards.editor
    note = editor.note
    if note is None:
        return
    if mw.state != "review":
        return
    reviewer = mw.reviewer
    current_card = reviewer.card
    if current_card is None:
        return

    current_note = current_card.note()
    editor.note.tags = current_note.tags
    showInfo(" ".join(current_note.tags))
    current_did = current_card.odid or current_card.did
    add_cards.deckChooser.selected_deck_id = current_did

    # didn't found other way to update tags, as editor.updateTags() throws error
    mw.reset()

gui_hooks.add_cards_did_init.append(init)


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

import json
import sys

from BranchCode import App_punish
from PublicCode import config
from PublicCode import deal_html_code
from PublicCode.Public_code import Connect_to_DB
from PublicCode.Public_code import Send_Request

# 用于解决中文编码问题
reload(sys)
sys.setdefaultencoding('utf-8')
Type = sys.getfilesystemencoding()

HOST, USER, PASSWD, DB, PORT = config.HOST, config.USER, config.PASSWD, config.DB, config.PORT
connect, cursor = Connect_to_DB().ConnectDB(HOST, USER, PASSWD, DB, PORT)

code = sys.argv[1]
province = sys.argv[2]
gs_basic_id = sys.argv[3]
gs_py_id = sys.argv[4]

code = '9131000013221158XC'
# province = ''
gs_basic_id = 229418511
gs_py_id = 1

def get_index(code):
    province = deal_html_code.judge_province(code)
    first_url = config.url_list[province].format(code)
    result,status_code = Send_Request().send_requests(first_url)
    data = None
    if status_code ==200:
        info = json.loads(result)["info"][0]
        uuid = info["uuid"]
        second_url = config.detail_list[province].format(uuid)
        data = Send_Request().send_requests(second_url)[0]
    return data
def get_info_list(data):
    info = {}
    if data!= None:
        data = json.loads(data)
        info["basic"] = data
        info["branch"] = data["entBranchSet"]
        info["brand"] = data["entTmSet"]
        info["change"] = data["entChangeSet"]
        info["check"] = data["entSpotCheckSet"]
        info["clear"] = data["entClearSet"]
        info["except"] = data["entExceptListedSet"]
        info["freeze"] = data["entStockFreezeViSet"]
        info["mort"] = data["entMortgageSet"]
        info["permit"] = data["entShrPermitSet"]
        info["permit2"] = data["entOthPermitSet"]
        info["person"] = data["entMemberSet"]
        info["punish"] = data["entShrPunishSet"]
        info["punish2"] = data[""]
        info["share"] = data["entInvestorSet"]
        info["stock"] = data["entPledgeSet"]
    return info
def get_all_info(cursor,connect,gs_basic_id):
    province = deal_html_code.judge_province(code)
    data = get_index(code)
    data_list = get_info_list(data)
    # basicinfo = App_basic.name(data)
    # App_basic.update_to_db(cursor, connect, gs_basic_id, basicinfo)
    # branchinfo = data_list["branch"]
    # branchinfo = App_branch.name(branchinfo)
    # App_branch.update_to_db(cursor,connect,gs_basic_id,branchinfo)
    # brandinfo = data_list["brand"]
    # brandinfo = App_brand.name(brandinfo)
    # App_brand.update_to_db(cursor,connect,gs_basic_id,brandinfo)
    # changeinfo = data_list["change"]
    # changeinfo = App_change.name(changeinfo)
    # App_change.update_to_db(cursor,connect,gs_basic_id,changeinfo)
    # clearinfo = data_list["clear"]
    # clearinfo = App_clear.name(clearinfo)
    # App_clear.update_to_db(cursor,connect,gs_basic_id,clearinfo)
    # exceptinfo = data_list["except"]
    # exceptinfo = App_except.name(exceptinfo)
    # App_except.update_to_db(cursor,connect,gs_basic_id,exceptinfo)
    # freezeinfo = data_list["freeze"]
    # freezeinfo = App_freeze.name(freezeinfo,province)
    # App_freeze.update_to_db(cursor,connect,gs_basic_id,freezeinfo)
    # mortinfo = data_list["mort"]
    # mortinfo = App_mort.name(mortinfo,province)
    # App_mort.update_to_db(gs_py_id,cursor,connect,gs_basic_id,mortinfo)
    # permitinfo = data_list["permit"]
    # permitinfo = App_permit.name(permitinfo)
    # App_permit.update_to_db(cursor,connect,gs_basic_id,permitinfo)
    # personinfo = data_list["person"]
    # personinfo = App_person.name(personinfo)
    # App_person.update_to_db(cursor,connect,gs_basic_id,personinfo)
    punishinfo = data_list["punish"]

    punishinfo = App_punish.name(punishinfo)
    App_punish.update_to_db(cursor, connect, gs_basic_id, punishinfo)
    # shareinfo = data_list["share"]
    # shareinfo = App_shareholder.name(shareinfo)
    # App_shareholder.update_to_db(cursor,connect,gs_basic_id,shareinfo)
    # stockinfo = data_list["stock"]
    # stockinfo = App_stock.name(stockinfo)
    # App_stock.update_to_db(cursor,connect,gs_basic_id,stockinfo)

get_all_info(cursor,connect,gs_basic_id)