from datetime import datetime
from skimage.metrics import structural_similarity
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import glob, cv2, os

root = "../Screenshots/"
report_dir = "../Reports/"
browsers = ['Chrome','Firefox']
condition = ["WebAssembly_Enabled","WebAssembly_Disabled"]
suffix = ["En", "Dis"]
hist_bin = 32
urls = []
images = {}
hists = {}

def search_urls(root):
    # If search reaches .jpeg without finding chrome and firefox, return
    if not os.path.isdir(root):
        return
    for sub in os.listdir(root):
        if sub in browsers:
            if root not in urls:
                urls.append(root)
            continue
        search_urls(root+sub+"/")

def read_img_and_hist():
    # Read the images and convert them to the histograms
    # Store in a dict with the url being the first-level key and browsername+condition being the second-level key
    for url in urls:
        images[url] = {}
        hists[url] = {}
        for b in browsers:
            for i in range(0,len(condition)):
                try:
                    # urls that don't have .wasm, thus no WebAssembly_Disabled directory
                    if i == 1 and (not os.path.exists(root+url+"/"+b+"/"+condition[i])):
                        images[url][b+suffix[i]] = None
                        hists[url][b+suffix[i]] = None
                        continue
                    img = cv2.imread(url+b+"/"+condition[i]+"/screenshot.jpeg")
                    images[url][b+suffix[i]] = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
                    hist = cv2.calcHist([img], [0, 1, 2], None, [hist_bin, hist_bin, hist_bin], [0, 256, 0, 256, 0, 256])
                    hists[url][b+suffix[i]] = cv2.normalize(hist, hist).flatten()
                except:
                    print(url+b+"/"+condition[i]+"/screenshot.jpeg")

def chi_sqr_similarity(hist1, hist2):
    if hist1 is None or hist2 is None:
        return None
    return cv2.compareHist(hist1, hist2, cv2.HISTCMP_CHISQR)

def structural_difference(img1, img2, url, pair_name):
    # Trim for cross browser size difference (unsolved)
    height = min(img1.shape[0],img2.shape[0])
    width = min(img1.shape[1],img2.shape[1])
    img1 = img1[:height,:width,:]
    img2 = img2[:height,:width,:]
    img1_grey = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
    img2_grey = cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY)
    (score, diff) = structural_similarity(img1_grey,img2_grey, full=True)
    # print("Image Similarity: {:.4f}%".format(score * 100))

    diff = (diff * 255).astype("uint8")
    diff_box = cv2.merge([diff, diff, diff])

    # Threshold the difference image, followed by finding contours to
    # obtain the regions of the two input images that differ
    thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if len(contours) == 2 else contours[1]

    mask = np.zeros(img1.shape, dtype='uint8')
    filled_img2 = img2.copy()

    for c in contours:
        area = cv2.contourArea(c)
        if area > 1000:
            x, y, w, h = cv2.boundingRect(c)
            cv2.rectangle(img1, (x, y), (x + w, y + h), (36, 255, 12), 2)
            cv2.rectangle(img2, (x, y), (x + w, y + h), (36, 255, 12), 2)
            cv2.rectangle(diff_box, (x, y), (x + w, y + h), (36, 255, 12), 2)
            cv2.drawContours(mask, [c], 0, (255, 255, 255), -1)
            cv2.drawContours(filled_img2, [c], 0, (0, 255, 0), -1)

    save_dir = report_dir+url+pair_name+"/"
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    cv2.imwrite(save_dir+'img1.jpeg', cv2.cvtColor(img1, cv2.COLOR_RGB2BGR))
    cv2.imwrite(save_dir+'img2.jpeg', cv2.cvtColor(img2, cv2.COLOR_RGB2BGR))
    cv2.imwrite(save_dir+'diff.jpeg', diff)
    cv2.imwrite(save_dir+'diff_box.jpeg', diff_box)
    cv2.imwrite(save_dir+'mask.jpeg', mask)
    cv2.imwrite(save_dir+'filled_img2.jpeg', cv2.cvtColor(filled_img2, cv2.COLOR_RGB2BGR))

    return score

if __name__ == "__main__":
    # img1 = cv2.imread("../Screenshots/sfs___biz/jobs/Chrome/WebAssembly_Enabled/screenshot.jpeg")
    # img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
    # img2 = cv2.imread("../Screenshots/sfs___biz/jobs/Firefox/WebAssembly_Enabled/screenshot.jpeg")
    # img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
    # structural_difference(img1,img2)

    search_urls(root)
    read_img_and_hist()

    # Generate Report in .csv
    # Prepare Calculation Pair
    col_names = ["path"]
    for b in browsers:
        col_names.append("chisqr_"+b+suffix[0]+"_"+b+suffix[1])
        col_names.append("ssim_" + b + suffix[0]+"_" + b + suffix[1])
    for i in range(0,len(browsers)):
        for j in range(i+1,len(browsers)):
            col_names.append("chisqr_"+browsers[i]+suffix[0]+"_"+browsers[j]+suffix[0])
            col_names.append("chisqr_"+browsers[i] +suffix[1]+"_" + browsers[j] + suffix[1])
            col_names.append("ssim_" + browsers[i] + suffix[0]+"_" + browsers[j] + suffix[0])
            col_names.append("ssim_" + browsers[i] + suffix[1]+"_" + browsers[j] + suffix[1])
    report = pd.DataFrame(columns=col_names)

    # A Subdirectory for current report
    report_dir += datetime.now().strftime("%Y%m%d_%H_%M_%S")+"/"
    if not os.path.exists(report_dir):
        os.makedirs((report_dir))

    # Calculate CHI-square Distance and SSIM similarity pairwise
    for url in urls:
        new_row = [url]
        for i in range(1,len(col_names)):
            [method, index1, index2] = col_names[i].split("_")
            if method == "chisqr":
                new_row.append(chi_sqr_similarity(hists[url][index1],hists[url][index2]))
            elif method == "ssim":
                new_row.append(structural_difference(images[url][index1],images[url][index2],url[15:],col_names[i]))
        report.loc[len(report.index)] = new_row

    # Output the report into a .csv

    report.to_csv(report_dir+"metrics.csv")
    print("Report "+report_dir+" generated!")


# coding: utf-8
"""
-------------------------------------------------
   File Name:      main
   Author :        Tiancheng Song
   E-mail :        tiancheng.song@mailbox.tu-dresden.de
   Date:           2022-01-15 12:10 AM
-------------------------------------------------
Description :

    Face Depth Measurement

"""
import cv2
import cvzone
from cvzone.FaceMeshModule import FaceMeshDetector

cap = cv2.VideoCapture('http://192.168.178.48:4747/video')
detector = FaceMeshDetector(maxFaces=1)

while True:
    success, img = cap.read()
    img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
    img, faces = detector.findFaceMesh(img, draw=False)

    if faces:
        face = faces[0]
        pointLeft = face[145]
        pointRight = face[374]
        # Drawing
        # cv2.line(img, pointLeft, pointRight, (0, 200, 0), 3)
        # cv2.circle(img, pointLeft, 5, (255, 0, 255), cv2.FILLED)
        # cv2.circle(img, pointRight, 5, (255, 0, 255), cv2.FILLED)
        w, _ = detector.findDistance(pointLeft, pointRight)
        W = 6.3

        # # Finding the Focal Length
        # d = 50
        # f = (w*d)/W
        # print(f)

        # Finding distance
        f = 840
        d = (W * f) / w
        print(d)

        cvzone.putTextRect(img, f'Depth: {int(d)}cm', (face[10][0] - 100, face[10][1] - 50), scale=2)

    cv2.imshow("Image", img)
    key = cv2.waitKey(1) & 0xFF
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

print("********************************************************")
print("***                   By Song T.C.                   ***")
print("***       tiancheng.song@mailbox.tu-dresden.de       ***")
print("********************************************************")

Module/Extern/OpenCV/BasicProject/44 Face Distance Measurement/FaceDepthMeasurement.py


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

# Configuration
import os
import sys
file_path = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.sep.join(file_path.split(os.path.sep)[:-1])
sys.path.append(config_path)

import itertools
import pickle as pk
from tqdm import tqdm
from collections import Counter

from util import *
scci_path = SCCIPath()

from connlp.preprocess import Normalizer, KoreanTokenizer
normalizer = Normalizer()
tokenizer = KoreanTokenizer(pre_trained=True, analyzer='Hannanum')

def data_import():
    flist = os.listdir(scci_path.fdir_article)
    docs = []
    for fname in flist:
        fpath = os.path.join(scci_path.fdir_article, fname)
        with open(fpath, 'rb') as f:
            docs.append(pk.load(f))
    return docs

def tokenize_docs(docs):
    normalized = [normalizer.normalize(a.content) for a in docs]

    tokenized = []
    with tqdm(total=len(normalized)) as pbar:
        for doc in normalized:
            tokenized.append(tokenizer.extract_noun(doc))
            pbar.update(1)
    return tokenized

def load_stoplist(fname):
    fpath = os.path.join(scci_path.fdir_stoplist, fname)
    with open(fpath, 'r', encoding='utf-8') as f:
        stoplist = list(set([w.strip() for w in f.read().strip().split('\n')]))

    with open(fpath, 'w', encoding='utf-8') as f:
        f.write('\n'.join(stoplist))

    return stoplist

def count_words(docs, fname_tokenized, do_tokenize, fname_stoplist):
    print('========================================')
    print('Tokenize articles')
    fpath_tokenized = os.path.join(scci_path.fdir_tokenized, fname_tokenized)
    if do_tokenize:
        tokenized = tokenize_docs(docs)
        makedir(fpath_tokenized)
        with open(fpath_tokenized, 'wb') as f:
            pk.dump(tokenized, f)
    else:
        with open(fpath_tokenized, 'rb') as f:
            tokenized = pk.dump(f)

    print('========================================')
    print('Stopword removal')
    stoplist = load_stoplist(fname_stoplist)
    stopword_removed = []
    with tqdm(total=len(tokenized)) as pbar:
        for doc in tokenized:
            stopword_removed.append([w.strip() for w in doc if w not in stoplist])
            pbar.update(1)

    counter_before = Counter(itertools.chain(*tokenized))
    counter_after = Counter(itertools.chain(*stopword_removed))
    return counter_before, counter_after

def update_stoplist(counter_before, counter_after, topn):
    words_before = [w for w, c in sorted(counter_before.items(), key=lambda x:x[1], reverse=True)]
    words_after = [w for w, c in sorted(counter_after.items(), key=lambda x:x[1], reverse=True)]

    print('========================================')
    for idx, word in enumerate(words_before[:topn]):
        if word in words_after:
            print('  | [{:3,}] BEFORE: {} -> AFTER: {}({:,})'.format((idx+1), word, word, counter_after[word]))
        else:
            print('  | [{:3,}] BEFORE: {}({:,}) -> '.format((idx+1), word, counter_before[word]))
    print('========================================')

if __name__ == '__main__':
    ## Data import
    docs = data_import()

    ## Update stopword list
    fname_tokenized = 'tokenized_corpus_20210715.pk'
    fname_stoplist = 'stoplist_20210715.txt'
    do_tokenize = True
    counter_before, counter_after = count_words(docs=docs, fname_tokenized=fname_tokenized, do_tokenize=do_tokenize, fname_stoplist=fname_stoplist)

    topn = 300
    update_stoplist(counter_before=counter_before, counter_after=counter_after, topn=topn)


from typing import Dict, Iterable, Optional

from crowdin_api.api_resources.abstract.resources import BaseResource
from crowdin_api.api_resources.enums import ExportProjectTranslationFormat
from crowdin_api.api_resources.translations.enums import (
    CharTransformation,
    PreTranslationApplyMethod,
    PreTranslationAutoApproveOption,
)

class TranslationsResource(BaseResource):
    """
    Resource for Translations.

    Translators can work with entirely untranslated project or you can pre-translate the files to
    ease the translations process.

    Use API to pre-translate files via Machine Translation (MT) or Translation Memory (TM), upload
    your existing translations, and download translations correspondingly. Pre-translate and build
    are asynchronous operations and shall be completed with sequence of API methods.

    Link to documentation:
    https://developer.crowdin.com/api/v2/#tag/Translations
    """

    def get_builds_path(self, projectId: int, buildId: Optional[int] = None):
        if buildId:
            return f"projects/{projectId}/translations/builds/{buildId}"

        return f"projects/{projectId}/translations/builds"

    def pre_translation_status(
        self, preTranslationId: str, projectId: Optional[int] = None
    ):
        """
        Pre-Translation Status.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#tag/Translations/paths/~1projects~1{projectId}~1pre-translations~1{preTranslationId}/get
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="get",
            path=f"projects/{projectId}/pre-translations/{preTranslationId}",
        )

    def apply_pre_translation(
        self,
        languageIds: Iterable[str],
        fileIds: Iterable[int],
        projectId: Optional[int] = None,
        method: Optional[PreTranslationApplyMethod] = None,
        engineId: Optional[int] = None,
        autoApproveOption: Optional[PreTranslationAutoApproveOption] = None,
        duplicateTranslations: Optional[bool] = None,
        translateUntranslatedOnly: Optional[bool] = None,
        translateWithPerfectMatchOnly: Optional[bool] = None,
        labelIds: Optional[Iterable[int]] = None,
        excludeLabelIds: Optional[Iterable[int]] = None,
    ):
        """
        Apply Pre-Translation.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.pre-translations.post
        """
        if labelIds is None:
            labelIds = []

        if excludeLabelIds is None:
            excludeLabelIds = []

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="post",
            path=f"projects/{projectId}/pre-translations",
            request_data={
                "languageIds": languageIds,
                "fileIds": fileIds,
                "method": method,
                "engineId": engineId,
                "autoApproveOption": autoApproveOption,
                "duplicateTranslations": duplicateTranslations,
                "translateUntranslatedOnly": translateUntranslatedOnly,
                "translateWithPerfectMatchOnly": translateWithPerfectMatchOnly,
                "labelIds": labelIds,
                "excludeLabelIds": excludeLabelIds,
            },
        )

    def build_project_directory_translation(
        self,
        directoryId: int,
        projectId: Optional[int] = None,
        targetLanguageIds: Optional[Iterable[str]] = None,
        skipUntranslatedStrings: Optional[bool] = None,
        skipUntranslatedFiles: Optional[bool] = None,
        exportApprovedOnly: Optional[bool] = None,
    ):
        """
        Build Project Directory Translation.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.directories.post
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="post",
            path=f"{self.get_builds_path(projectId=projectId)}/directories/{directoryId}",
            request_data={
                "targetLanguageIds": targetLanguageIds,
                "skipUntranslatedStrings": skipUntranslatedStrings,
                "skipUntranslatedFiles": skipUntranslatedFiles,
                "exportApprovedOnly": exportApprovedOnly,
            },
        )

    def build_project_file_translation(
        self,
        fileId: int,
        targetLanguageId: str,
        projectId: Optional[int] = None,
        skipUntranslatedStrings: Optional[bool] = None,
        skipUntranslatedFiles: Optional[bool] = None,
        exportApprovedOnly: Optional[bool] = None,
        eTag: Optional[str] = None,
    ):
        """
        Build Project File Translation.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.files.post
        """

        if eTag is not None:
            headers = {"If-None-Match": eTag}
        else:
            headers = None

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="post",
            headers=headers,
            path=f"{self.get_builds_path(projectId=projectId)}/files/{fileId}",
            request_data={
                "targetLanguageId": targetLanguageId,
                "skipUntranslatedStrings": skipUntranslatedStrings,
                "skipUntranslatedFiles": skipUntranslatedFiles,
                "exportApprovedOnly": exportApprovedOnly,
            },
        )

    def list_project_builds(
        self,
        projectId: Optional[int] = None,
        branchId: Optional[int] = None,
        page: Optional[int] = None,
        offset: Optional[int] = None,
        limit: Optional[int] = None,
    ):
        """
        List Project Builds.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.getMany
        """

        projectId = projectId or self.get_project_id()
        params = {"branchId": branchId}
        params.update(self.get_page_params(page=page, offset=offset, limit=limit))

        return self._get_entire_data(
            method="get",
            path=self.get_builds_path(projectId=projectId),
            params=params,
        )

    def build_project_translation(
        self, request_data: Dict, projectId: Optional[int] = None
    ):
        """
        Build Project Translation.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.post
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="post",
            path=self.get_builds_path(projectId=projectId),
            request_data=request_data,
        )

    def build_crowdin_project_translation(
        self,
        projectId: Optional[int] = None,
        branchId: Optional[int] = None,
        targetLanguageIds: Optional[Iterable[str]] = None,
        skipUntranslatedStrings: Optional[bool] = None,
        skipUntranslatedFiles: Optional[bool] = None,
        exportApprovedOnly: Optional[bool] = None,
        exportWithMinApprovalsCount: Optional[int] = None,
    ):
        """
        Build Project Translation(Crowdin Translation Create Project Build Form).

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.post
        """

        projectId = projectId or self.get_project_id()

        return self.build_project_translation(
            projectId=projectId,
            request_data={
                "branchId": branchId,
                "targetLanguageIds": targetLanguageIds,
                "skipUntranslatedStrings": skipUntranslatedStrings,
                "skipUntranslatedFiles": skipUntranslatedFiles,
                "exportApprovedOnly": exportApprovedOnly,
                "exportWithMinApprovalsCount": exportWithMinApprovalsCount,
            },
        )

    def build_pseudo_project_translation(
        self,
        pseudo: bool,
        projectId: Optional[int] = None,
        prefix: Optional[str] = None,
        suffix: Optional[str] = None,
        lengthTransformation: Optional[int] = None,
        charTransformation: Optional[CharTransformation] = None,
    ):
        """
        Build Project Translation(Translation Create Project Pseudo Build Form).

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.post
        """

        projectId = projectId or self.get_project_id()

        return self.build_project_translation(
            projectId=projectId,
            request_data={
                "pseudo": pseudo,
                "prefix": prefix,
                "suffix": suffix,
                "lengthTransformation": lengthTransformation,
                "charTransformation": charTransformation,
            },
        )

    def upload_translation(
        self,
        languageId: str,
        storageId: int,
        fileId: int,
        projectId: Optional[int] = None,
        importEqSuggestions: Optional[bool] = None,
        autoApproveImported: Optional[bool] = None,
        translateHidden: Optional[bool] = None,
    ):
        """
        Upload Translations.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.postOnLanguage
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="post",
            path=f"projects/{projectId}/translations/{languageId}",
            request_data={
                "storageId": storageId,
                "fileId": fileId,
                "importEqSuggestions": importEqSuggestions,
                "autoApproveImported": autoApproveImported,
                "translateHidden": translateHidden,
            },
        )

    def download_project_translations(
        self, buildId: int, projectId: Optional[int] = None
    ):
        """
        Download Project Translations.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.download.download
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="get",
            path=f"{self.get_builds_path(projectId=projectId, buildId=buildId)}/download",
        )

    def check_project_build_status(self, buildId: int, projectId: Optional[int] = None):
        """
        Check Project Build Status.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.get
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="get",
            path=self.get_builds_path(projectId=projectId, buildId=buildId),
        )

    def cancel_build(self, buildId: int, projectId: Optional[int] = None):
        """
        Cancel Build.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.builds.delete
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="delete",
            path=self.get_builds_path(projectId=projectId, buildId=buildId),
        )

    def export_project_translation(
        self,
        targetLanguageId: str,
        projectId: Optional[int] = None,
        format: Optional[ExportProjectTranslationFormat] = None,
        labelIds: Optional[Iterable[int]] = None,
        branchIds: Optional[Iterable[int]] = None,
        directoryIds: Optional[Iterable[int]] = None,
        fileIds: Optional[Iterable[int]] = None,
        skipUntranslatedStrings: Optional[bool] = None,
        skipUntranslatedFiles: Optional[bool] = None,
        exportApprovedOnly: Optional[bool] = None,
    ):
        """
        Export Project Translation.

        Link to documentation:
        https://developer.crowdin.com/api/v2/#operation/api.projects.translations.exports.post
        """

        projectId = projectId or self.get_project_id()

        return self.requester.request(
            method="post",
            path=f"projects/{projectId}/translations/exports",
            request_data={
                "targetLanguageId": targetLanguageId,
                "format": format,
                "labelIds": labelIds,
                "branchIds": branchIds,
                "directoryIds": directoryIds,
                "fileIds": fileIds,
                "skipUntranslatedStrings": skipUntranslatedStrings,
                "skipUntranslatedFiles": skipUntranslatedFiles,
                "exportApprovedOnly": exportApprovedOnly,
            },
        )


import json

from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer

from .game import DiceGame

class GameConsumer(WebsocketConsumer):
    player_list = []
    rooms = {}

    def connect(self):
        self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
        self.room_group_name = "room_%s" % self.room_name

        async_to_sync(self.channel_layer.group_add)(  # type:ignore
            self.room_group_name,
            self.channel_name
        )

        self.accept()

    def disconnect(self, _):
        GameConsumer.player_list.clear()
        GameConsumer.rooms.clear()

        async_to_sync(self.channel_layer.group_discard)(  # type:ignore
            self.room_group_name,
            self.channel_name
        )

    def checkPlayerStatus(self):
        length = len(GameConsumer.player_list)
        return False if length < 2 else True

    def receive(self, text_data):
        print(text_data)
        data = json.loads(text_data)
        action = data.get("action", "")
        status = action

        if action == "turn":
            if self.checkPlayerStatus():
                req_id = data.get("id", "")
                if self.nextTurn(req_id) is False:
                    return
            else:
                self.sendWaiting()
                return
        elif action == "lock":
            if self.checkPlayerStatus():
                req_id = data.get("id", "")
                lockList = data.get("lockList", "")
                if self.lockDice(req_id, lockList) is False:
                    return
            else:
                self.sendWaiting()
                return
        elif action == "magni":
            if self.checkPlayerStatus():
                if self.updateMagni(data.get("num", "")) is False:
                    return
            else:
                self.sendWaiting()
                return
        elif action == "cal_score":
            if self.checkPlayerStatus():
                self.calScore()
            else:
                self.sendWaiting()
                return
        elif action == "get_id":
            self.getId()
            return
        else:
            return

        async_to_sync(self.channel_layer.group_send)(  # type:ignore
            self.room_group_name,
            {
                "type": "sendMsg",
                "s": status,
            }
        )

    def sendWaiting(self):
        self.send(
            text_data=json.dumps({"status": "waiting"})
        )

    def getId(self):
        players = GameConsumer.player_list
        length = len(players)
        if length == 2:  # 已有两个玩家
            return

        players.append(str(length))
        length += 1

        if length >= 2:
            game = DiceGame(id1=players[0], id2=players[1], first=players[0])
            GameConsumer.rooms[self.room_name] = game

        self.send(
            text_data=json.dumps(
                {"status": "get_id", "id": GameConsumer.player_list[-1]}
            )
        )

    def calScore(self):
        self.game_driver = GameConsumer.rooms[self.room_name]
        self.game_driver.calScore()

    def updateMagni(self, num):
        self.game_driver = GameConsumer.rooms[self.room_name]
        turn = self.game_driver.turn

        k = num
        self.game_driver.incMagnification(k=int(k))
        self.game_driver.turn = int(not turn)
        return True

    def lockDice(self, req_id, lockList):
        self.game_driver = GameConsumer.rooms[self.room_name]
        turn = self.game_driver.turn
        id = self.game_driver.ord2id(turn)
        if id != req_id:
            return False

        if self.game_driver.curRound < self.game_driver.rounds:
            self.game_driver.players[id].lockDice(lockList)
        else:
            self.game_driver.players[id].lockDice([0, 1, 2, 3, 4])
        self.game_driver.turn = int(not turn)
        return True

    def nextTurn(self, req_id):
        self.game_driver = GameConsumer.rooms[self.room_name]
        turn = self.game_driver.turn
        id = self.game_driver.ord2id(turn)
        if id != req_id:
            return False

        k = self.game_driver.players[id].calAvail()
        self.game_driver.rollDice(k)
        self.game_driver.players[id].updateDiceStatus(
            self.game_driver.rollRes
        )

        if turn == int(self.game_driver.first):
            if self.game_driver.curRound < self.game_driver.rounds:
                self.game_driver.curRound += 1
            else:
                self.game_driver.curRound = 0
                self.game_driver.curInning += 1

        if self.game_driver.curRound == self.game_driver.rounds:
            self.game_driver.players[id].lockDice([0, 1, 2, 3, 4])
            self.game_driver.turn = int(not turn)

        return True

    def sendMsg(self, event):
        s = event["s"]
        self.game_driver = GameConsumer.rooms[self.room_name]
        obj = self.game_driver.getStatus()
        obj["status"] = s
        self.send(text_data=json.dumps(obj))


import os
from dotenv import load_dotenv

def load():

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    stage_config = {
        "local": ".env",
        "dev": "dev.env",
        "hmg": "hmg.env",
        "prd": "prd.env"
    }

    dotenv_config = {
        "local": True,
        "dev": True,
        "hmg": False,
        "prd": False
    }

    stage_setted = os.getenv("STAGE", 'local')

    dotenv_path = os.path.join(BASE_DIR, stage_config[stage_setted])

    override_environment_variables = dotenv_config[stage_setted]

    load_dotenv(dotenv_path=dotenv_path, override=override_environment_variables)


"""
    This module contains functions relevant to Animating solutions
"""

import matplotlib.pyplot as plt
from matplotlib import animation, rc

def animate_heatmap(weight_trajectory, weight_bounds=3):
    """ Calls the solution_trajectory function, then creates an animated line plot of the solutions

        training_run -- an object containing a 'model', and a 'weight_trajectory'
        model -- and ODE
        weight_trajectory -- a series of weight matrices that represent progress along the way from the initial weights to final weights
        inital_conds -- the initial values of each of the variables in the model
    """
    fig, ax = plt.subplots(figsize = (6,6))
    fig.subplots_adjust(left=0, bottom=0.1, right=1, top=.9, wspace=None, hspace=None)
    # ax.set_title('Weight Trajectory')
    # ax.set_xlabel('Connections In')
    # ax.set_ylabel('Connections Out')
    ax = plt.imshow(weight_trajectory[0], cmap="coolwarm", vmin = -weight_bounds, vmax = weight_bounds)

    class ani:
        n=0
    def update(frame):
        ax.set_array(weight_trajectory[ani.n-2])
        ani.n = ani.n+1
        return ax,

    ani = animation.FuncAnimation(fig, update, interval=200, blit=True, frames= len(weight_trajectory)-1 )
    plt.close()
    return ani


from abc import ABC
from typing import Any

from starkware.cairo.lang.compiler.program import Program

from protostar.cairo import HintLocalsDict
from protostar.cairo.cairo_function_executor import CairoFunctionExecutor, OffsetOrName
from protostar.cairo_testing.cairo_injectable_function_runner import (
    CairoInjectableFunctionRunner,
)
from protostar.cairo_testing.cairo_test_execution_state import CairoTestExecutionState

class CairoExecutionEnvironment(CairoFunctionExecutor, ABC):
    def __init__(
        self,
        state: CairoTestExecutionState,
        program: Program,
        hint_locals: HintLocalsDict,
    ):
        self.state = state
        self.program = program
        self.hint_locals = hint_locals

    async def run_cairo_function(
        self,
        function_identifier: OffsetOrName,
        *args: Any,
        **kwargs: Any,
    ):
        await CairoInjectableFunctionRunner(
            hint_locals=self.hint_locals, program=self.program
        ).run_cairo_function(function_identifier, *args, **kwargs)


from networkx.generators.random_graphs import fast_gnp_random_graph
import random
import networkx as nx
import matplotlib.pyplot as plt
import pylab

class Graph:
    def __init__(self, vertices):
        self.V = vertices
        self.graph = []

    def add_edge(self, start, end, weight):
        self.graph.append([start, end, weight])

    def find(self, parent, i):
        if parent[i] == i:
            return i
        return self.find(parent, parent[i])

    def apply_union(self, parent, rank, x, y):
        xroot = self.find(parent, x)
        yroot = self.find(parent, y)
        if rank[xroot] < rank[yroot]:
            parent[xroot] = yroot
        elif rank[xroot] > rank[yroot]:
            parent[yroot] = xroot
        else:
            parent[yroot] = xroot
            rank[xroot] += 1

    def kruskal_algo(self):
        result = []
        i, e = 0, 0
        self.graph = sorted(self.graph, key=lambda item: item[2])
        parent = []
        rank = []
        for node in range(self.V):
            parent.append(node)
            rank.append(0)
        while e < self.V - 1:
            start, end, weight = self.graph[i]
            i = i + 1
            x = self.find(parent, start)
            y = self.find(parent, end)
            if x != y:
                e = e + 1
                result.append([start, end, weight])
                self.apply_union(parent, rank, x, y)
        return result

def create_weighted_graph(n):
    p = 0.6
    w_edges = []
    l = [0, 0, 0]

    mode = int(input("Enter the mode: 1 - if you want random weights; 2 - if you want to input it yourself: "))

    gr = fast_gnp_random_graph(n, p)

    print("The list of all edges:", gr.edges)
    uw_edges = list(gr.edges())
    length = len(gr.edges)

    for i in range(0, length):
        l = [0, 0, 0]
        for j in range(0, 3):
            if j == 2:
                if mode == 1:
                    l[j] = random.randint(1, 7)
                if mode == 2:
                    l[j] = int(input("Enter the weight of the edge: "))
            else:
                l[j] = uw_edges[i][j]
        w_edges.append(l)

    return w_edges, length

def print_graph(w_edges, length, res):
    G = nx.Graph()
    for i in range(0, length):
        G.add_edges_from([(str(w_edges[i][0]), str(w_edges[i][1]))], weight = w_edges[i][2])

    edge_labels=dict([((u,v,),d['weight'])
                     for u,v,d in G.edges(data=True)])

    red_edges = []
    for i in range(0, len(res)):
        red_edges.append((str(res[i][0]), str(res[i][1])))

    edge_colors = ['r' if edge in red_edges else 'b' for edge in G.edges()]

    pos=nx.circular_layout(G)
    node_labels = {node:node for node in G.nodes()}
    nx.draw_networkx_labels(G, pos, labels=node_labels)
    nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
    nx.draw(G, pos, node_color='blue', node_size=1200, edge_color=edge_colors, edge_cmap=plt.cm.Reds)
    pylab.show()

def main():

    n = int(input("Enter the amount of nodes:"))
    g = Graph(n)

    w_edges, length = create_weighted_graph(n)

    for i in range(0, length):
        g.add_edge(w_edges[i][0], w_edges[i][1], w_edges[i][2])

    print("The minimal tree:\n")
    res = g.kruskal_algo()
    res_weight = 0
    res_len = len(res)
    for start, end, weight in res:
        res_weight += weight
        print("%d - %d: %d" % (start, end, weight))
    print('The weight of the tree:', res_weight)

    print_graph(w_edges, length, res)

if __name__ == '__main__':
    main()


import re
from orangecontrib.bio.ontology import OBOOntology
import itertools
import logging
from pprint import pprint

# The IDs from the ontology file
TECH_REP = "EFO:0002090"
BIOL_REP = "EFO:0002091"
HOUR_TERM = "UO:0000032"
MINUTE_TERM = "UO:0000031"
DAY_TERM = "UO:0000033"

def get_rex_value(regex, str):
    """
    Wrapper for regex parsing functions.

    Returns None if parsing fails.
    """
    try:
        return regex.search(str).group()
    except AttributeError:
        return None

def get_obo_id(str):
    """
    Get OBO id from column header.

    Throw exception if parsing fails.

    >>> get_obo_id("tpm of 293SLAM rinderpest infection, 00hr, biol_rep1.CNhs14406.13541-145H4")
    'FF:13541-145H4'
    """
    OBO_ID_REGEX = re.compile(r'CNhs\d+.(\w+)-(\w+)')
    return "FF:" + "-".join(OBO_ID_REGEX.search(str).groups())

def get_lib_id(str):
    """
    Get library id from column header

    Throw exception if parsing failes.

    >>> get_lib_id("tpm of 293SLAM rinderpest infection, 00hr, biol_rep1.CNhs14406.13541-145H4")
    'CNhs14406'
    """
    LIB_ID_REGEX = re.compile(r'CNhs(\d+)')
    return LIB_ID_REGEX.search(str).group()

def get_time(str):
    """
    Parse time from column header

    >>> get_time("tpm of 293SLAM rinderpestday01 infectionday01, 00hr, biol_rep1.CNhs14406.13541-145H4")
    Traceback (most recent call last):
    ...
    AssertionError: multiple times per unit day not allowed: tpm of 293SLAM rinderpestday01 infectionday01, 00hr, biol_rep1.CNhs14406.13541-145H4
    >>> get_time("tpm of 293SLAM rinderpestd infectionday01, 00hr, biol_rep1.CNhs14406.13541-145H4")
    'day01;00hr'
    >>> get_time("tpm of 293SLAM rinderpestd infectionday12, biol_rep1.CNhs14406.13541-145H4")
    'day12'
    >>> get_time("tpm of 293SLAM rinderpestdayd01 infectionddady01, 00hxr, biol_rep1.CNhs14406.13541-145H4")

    >>> get_time("tpm of 293SLAM rinderpestd infectiond, 01hr40min, biol_rep1.CNhs14406.13541-145H4")
    '01hr40min'
    """
    time = {"hr": None, "min": None, "day": None}
    TIME_REGEX = re.compile(r'(\d+)(hr|min)|day(\d+)')
    INT_REGEX = re.compile(r'(\d+)')
    for match_obj in TIME_REGEX.finditer(str):
        time_str = match_obj.group()
        for unit in time:
            if unit in time_str:
                assert time[unit] is None, 'multiple times per unit {} not allowed: {}'.format(unit, str)
                time[unit] = INT_REGEX.search(time_str).group()

    return time_dict_to_str(time)

def time_dict_to_str(time_dict):
    """
    Format a dictionary `time = {"hr": j, "min": k, "day": l}` to a string e.g. 02hr40min.

    >>> time_dict_to_str({"hr": None, "min": None, "day": None})

    >>> time_dict_to_str({"hr": None, "min": None, "day": 1})
    'day01'
    >>> time_dict_to_str({"hr": 12, "min": None, "day": "01"})
    'day01;12hr'
    >>> time_dict_to_str({"hr": 12, "min": 15, "day": None})
    '12hr15min'
    """
    out = []
    if time_dict["day"] is not None:
        out.append("day{:02d}".format(int(time_dict["day"])))
        if time_dict["hr"] is not None or time_dict['min'] is not None:
            out.append(";")  # we need a separator in that case
    if time_dict['hr'] is not None:
        out.append("{:02d}hr".format(int(time_dict['hr'])))
    if time_dict['min'] is not None:
        out.append("{:02d}min".format(int(time_dict['min'])))
    out = "".join(out)
    if out == "":
        return None
    else:
        return out

def get_donor(str):
    """
    Parse donor from column header.

    >>> get_donor("tpm of 293SLAM rinderpestdayd01 infection, 00hr, donor1, biol_rep1.CNhs14406.13541-145H4")
    'donor1'
    >>> get_donor("tpm of 293SLAM rinderpestdayd01 infection, 00hr, dondor1, biol_rep1.CNhs14406.13541-145H4")

    """
    DONOR_REGEX = re.compile(r'donor(\d+)')
    return get_rex_value(DONOR_REGEX, str)

def get_biol_replicate(str):
    """
    Parse biological replicate from column header.
    Both rep1 and biol_rep1 are considered as a biological replicate.

    >>> get_biol_replicate("tpm of 293SLAM rinderpestdayd01 infection, 00hr, donor1, biol_rep1.CNhs14406.13541-145H4")
    'biol_rep1'
    >>> get_biol_replicate("tpm of 293SLAM rinderpestdayd01 infection, 00hr, donor1, rep1.CNhs14406.13541-145H4")
    'rep1'
    >>> get_biol_replicate("tpm of 293SLAM rinderpestdayd01 infection, 00hr, donor1, CNhs14406.13541-145H4")

    >>> get_biol_replicate("tpm of 293SLAM rinderpestdayd01 infection, 00hr, donor1, some_rep1.CNhs14406.13541-145H4")

    """
    REPLICATE_REGEX = re.compile(r'([ ]+|biol_)rep(\d+)')
    rep = get_rex_value(REPLICATE_REGEX, str)
    if rep is not None:
        rep = rep.strip()
    return rep

def get_tech_replicate(str):
    """
    Parse replicate from column header

    >>> get_tech_replicate("tpm of 293SLAM rinderpestdayd01 infection, 00hr, donor1, tech_rep1.CNhs14406.13541-145H4")
    'tech_rep1'
    >>> get_tech_replicate("tpm of 293SLAM rinderpestdayd01 infection, 00hr, donor1, rep1.CNhs14406.13541-145H4")

    """
    REPLICATE_REGEX = re.compile(r'tech_rep(\d+)')
    return get_rex_value(REPLICATE_REGEX, str)

def contains_term(term, tag_name, tag_value):
    """Check if a set of tags (from a certain term) contains a certain annotation (e.g. is_a: 24-hour-sample)"""
    return any(a == tag_name and b == tag_value for a, b in term.tags())

def get_time_terms(obo):
    hour_terms = [term.id for term in obo.child_terms(HOUR_TERM)]
    minute_terms = [term.id for term in obo.child_terms(MINUTE_TERM)]
    day_terms = [term.id for term in obo.child_terms(DAY_TERM)]
    return hour_terms, minute_terms, day_terms

def is_time_term(obo, term_id):
    """
    Return true if the term_id is a time-ontology

    >>> obo = OBOOntology()
    >>> obo.load(open("test/testdata/ff-phase2-140729.obo"))
    >>> is_time_term(obo, "FF:0000357")
    True
    >>> is_time_term(obo, "FF:0350357")
    False
    """
    return True if term_id in itertools.chain(*get_time_terms(obo)) else False

def ontology_to_time(obo, obo_id):
    """
    Convert an ontology id to a readable time string.

    >>> obo = OBOOntology()
    >>> obo.load(open("test/testdata/ff-phase2-140729.obo"))
    >>> ontology_to_time(obo, "FF:0000357")
    '00hr'

    """
    hour_terms, minute_terms, day_terms = get_time_terms(obo)
    NO_REGEX = re.compile(r'(\d+)')
    term = obo.term(obo_id)
    number = NO_REGEX.search(term.name).group()
    time_dict = {"hr": None, "min": None, "day": None}
    if obo_id in hour_terms:
        time_dict['hr'] = number
    elif obo_id in minute_terms:
        time_dict['min'] = number
    elif obo_id in day_terms:
        time_dict['day'] = number
    return time_dict_to_str(time_dict)

def get_sample_type_from_ontology(obo, obo_term):
    """
    Get the sample_type from the ontology.

    There are three sample types defined in the ontology. The sample belongs to
    a certain type if it is a descendant of the respective type definition object.

    There are certain cases which are not unique. Thus a list of the associated types is returned.

    Args:
        obo: OBOOntology Object
        obo_term: OBOObject (term)

    Returns:
        List containing None/primary cell/tissue/cell line

    >>> obo = OBOOntology()
    >>> obo.load(open("test/testdata/ff-phase2-140729.obo"))
    >>> obo_term = obo.term("FF:10100-102D1")  # tpm of uterus, adult, pool1.CNhs1167...
    >>> get_sample_type_from_ontology(obo, obo_term)
    ['tissue']
    >>> obo_term = obo.term("FF:11931-125I5") # not unique
    >>> sorted(get_sample_type_from_ontology(obo, obo_term))
    ['primary cell', 'tissue']
    """
    mapping = {
        "FF:0000002": "primary cell",
        "FF:0000004": "tissue",
        "FF:0000003": "cell line",
    }
    super_terms = [term.id for term in obo.super_terms(obo_term.id)]
    sample_type = []  # cases like FF:11931-125I5 which are not unique
    for key, val in mapping.items():
        if key in super_terms:
            sample_type.append(val)
    return sample_type

def process_sample_name(sample_info):
    """
    Extract sample information from the sample name.

    >>> pprint(process_sample_name("tpm of 293SLAM rinderpest infection, 00hr, biol_rep1.CNhs14406.13541-145H4"))
    {'biol_rep': 'biol_rep1',
     'donor': None,
     'lib_id': 'CNhs14406',
     'name_orig': 'tpm of 293SLAM rinderpest infection, 00hr, '
                  'biol_rep1.CNhs14406.13541-145H4',
     'obo_id': 'FF:13541-145H4',
     'tech_rep': None,
     'time': '00hr'}

    """
    info_n = dict()
    info_n["obo_id"] = get_obo_id(sample_info)
    info_n["lib_id"] = get_lib_id(sample_info)
    info_n["name_orig"] = sample_info
    logging.info("{}: Parsing information from sample name".format(info_n["lib_id"]))

    # we use the 'original' name here, as some information e.g. tech_rep is not available
    # in the name retrieved from the ontology.
    info_n["donor"] = get_donor(sample_info)  # donor is some sort of biological replicate
    info_n["time"] = get_time(sample_info)
    info_n["tech_rep"] = get_tech_replicate(sample_info)
    info_n["biol_rep"] = get_biol_replicate(sample_info)

    assert not (bool(info_n["biol_rep"]) and bool(info_n["donor"])), \
        "as donor is a form of biological replicate they should not co-occur in a name: {}".format(sample_info)

    return info_n

def process_sample_ontology(obo, sample_info):
    """
     loops through all is_a relationships in tags and look if it contains any relevant annotation.
     Here, we extract the time and whether the sample is a biological or technical replicate.

     Args:
         obo: OBOOntology Object
         obo_term: OBOObject (term)

     Returns:
         time_o: term_id of a TIME ontology or None if not specified.
         tech_rep_o: True, if the sample is a technical replicate, else None
         biol_rep_o, True, if the sample is a biological replicate, else None

    >>> obo = OBOOntology()
    >>> obo.load(open("test/testdata/ff-phase2-140729.obo"))
    >>> sample_info = "tpm of occipital lobe, fetal, donor1.CNhs11784.10073-102A1"
    >>> pprint(process_sample_ontology(obo, sample_info))
    {'biol_rep': True,
     'lib_id': 'CNhs11784',
     'name': 'occipital lobe, fetal, donor1',
     'obo_id': 'FF:10073-102A1',
     'sample_type': ['tissue'],
     'tech_rep': None,
     'time': None}
    """
    info_o = {
        "tech_rep": None,
        "biol_rep": None,
        "time": None,
        "obo_id": get_obo_id(sample_info),
        "lib_id": get_lib_id(sample_info),
    }
    logging.info("{}: Searching Ontology".format(info_o["lib_id"]))
    obo_term = obo.term(info_o["obo_id"])
    info_o["name"] = obo_term.name
    tags = obo_term.tags()
    for tag, tag_value, _, _ in tags:
        if tag == "is_a":
            if is_time_term(obo, tag_value):
                assert info_o["time"] is None, 'multiple matches: {0}'.format(tags)
                info_o["time"] = ontology_to_time(obo, tag_value)
            if tag_value == TECH_REP:
                assert info_o["tech_rep"] is None, 'multiple matches: {0}'.format(tags)
                info_o["tech_rep"] = True
            if tag_value == BIOL_REP:
                assert info_o["biol_rep"] is None, 'multiple matches: {0}'.format(tags)
                info_o["biol_rep"] = True
    info_o["sample_type"] = get_sample_type_from_ontology(obo, obo_term)
    return info_o

def merge_sample_info(info_n, info_o, info_si, annot_notes=[]):
    """
    Put the information from name, ontology and supplementary information together.
    Check for consistency and take note, if information is lacking in the ontology.

    Args:
        info_n: dict, info from name, retrieve through process_sample_name
        info_o: dict, info from ontology, retrieve trough process_sample_ontology
        info_si: dict, info from supplement, get from data frame
        annot_notes: list passed by reference that will be filled with a note if an annotation is
            missing in the ontology

    Returns:
        annot, dict with the merged annotations

    >>> obo = OBOOntology()
    >>> obo.load(open("test/testdata/ff-phase2-140729.obo"))
    >>> sample_info = "tpm of 293SLAM rinderpest infection, 00hr, biol_rep1.CNhs14406.13541-145H4"
    >>> info_n = process_sample_name(sample_info)
    >>> info_o = process_sample_ontology(obo, sample_info)
    >>> info_si = {"obo_id": "FF:13541-145H4", "lib_id": 'CNhs14406', "sample_type": "tissue"}
    >>> annotation_notes = []
    >>> pprint(merge_sample_info(info_n, info_o, info_si, annotation_notes))
    {'biol_rep': True,
     'donor': None,
     'lib_id': 'CNhs14406',
     'name': '293SLAM rinderpest infection, 00hr, biol_rep1',
     'name_orig': 'tpm of 293SLAM rinderpest infection, 00hr, '
                  'biol_rep1.CNhs14406.13541-145H4',
     'obo_id': 'FF:13541-145H4',
     'sample_type': 'tissue',
     'tech_rep': False,
     'time': '00hr'}
    >>> pprint(annotation_notes)
    [{'field_name': 'biol_rep',
      'lib_id': 'CNhs14406',
      'new_value': 'biol_rep1',
      'obo_id': 'FF:13541-145H4'},
     {'field_name': 'sample_type',
      'lib_id': 'CNhs14406',
      'new_value': 'tissue',
      'obo_id': 'FF:13541-145H4'}]

    apparently, for CNhs14406, the ontology contains no information, that is is a biological replicate.

    """
    assert info_n["obo_id"] == info_o["obo_id"] == info_si["obo_id"] is not None, "obo_ids not matching"
    assert info_n["lib_id"] == info_o["lib_id"] == info_si["lib_id"] is not None, "lib_ids not matching"
    logging.info("{}: Merging Information".format(info_n["lib_id"]))

    def add_annotation_note(field_name, new_value):
        """add note, that an annotation is missing in the ontology"""
        logging.debug("{}: missing in ontology. ".format(field_name))
        annot_notes.append({
            "lib_id": info_n["lib_id"],
            "obo_id": info_n["obo_id"],
            "field_name": field_name,
            "new_value": new_value
        })

    def simply_merge(field_name):
        """Merge the three dicts, assert that the same if not None or key does not exist"""
        dicts = [info_n, info_o, info_si]
        vals = [d.get(field_name, None) for d in dicts]
        true_vals = [v for v in vals if v is not None]
        assert len(set(true_vals)) <= 1, "value mismatch"
        val = true_vals[0] if len(true_vals) > 0 else None
        if vals[1] is None and len(true_vals) > 1:
            add_annotation_note(field_name, val)
        return val

    def merge_biol_rep():
        if bool(info_n["biol_rep"]) or bool(info_n["donor"]):
            if not bool(info_o["biol_rep"]):
                add_annotation_note("biol_rep", info_n["biol_rep"] if info_n["biol_rep"] else info_n["donor"])
        return bool(info_o["biol_rep"]) or bool(info_n["biol_rep"]) or bool(info_n["donor"])

    def merge_tech_rep():
        if bool(info_n["tech_rep"]):
            if not bool(info_o["tech_rep"]):
                add_annotation_note("tech_rep", info_n["tech_rep"])
        return bool(info_o["tech_rep"]) or bool(info_n["tech_rep"])

    def merge_sample_type():
        # here, we tolerate inconsistencies and prefer the ontology if available
        if len(info_o["sample_type"]) > 0:
            if info_si["sample_type"] is not None and (info_si["sample_type"] not in info_o["sample_type"]):
                logging.warning("sample_type: inconsistent. Ontology: {}, SI: {}".format(
                    info_o["sample_type"], info_si["sample_type"]))
            if len(info_o["sample_type"]) == 1:  # there are cases with multiple sample types in the ontology
                return info_o["sample_type"][0]
            else:
                logging.warning("sample_type: multiple sample types in ontology: {}".format(info_o["sample_type"]))
                return "multiple"
        else:
            add_annotation_note("sample_type", info_si["sample_type"])
            return info_si["sample_type"]

    annot = {
        "lib_id": info_n["lib_id"], # no merge required due to assertion
        "obo_id": info_n["obo_id"], # no merge required due to assertion
        "biol_rep": merge_biol_rep(),
        "tech_rep": merge_tech_rep(),
        "donor": simply_merge("donor"),
        "time": simply_merge("time"),
        "name": simply_merge("name"),
        "name_orig": simply_merge("name_orig"),
        "sample_type": merge_sample_type()
    }

    return annot


#
# Copyright © 2019 Ronald C. Beavis
# Licensed under Apache License, Version 2.0, January 2004
# Project information at https://wiki.thegpm.org/wiki/Jsms
#

import sys
import json
import re
import hashlib
import gzip

ifile = None
isGz = 0
try:
	fpath = sys.argv[1]
	if fpath.find('.gz') == -1 or fpath.rfind('.gz') != len(fpath) - 3:
		ifile = open(fpath,'r', encoding='utf-8')
	else:
		ifile = gzip.open(fpath,'rt')

except:
	print('Error opening file specified on command line:\ne.g. ">jsms_validator.py FILE_PATH"')
	exit()

mhash = hashlib.sha256()
ln = 1
hash = ''
es = 0
ws = 0
types = {}
for line in ifile:
	line = line.strip()
	estr = ''
	try:
		estr = 'json loading'
		js = json.loads(line)
		if 'validation' not in js:
			mhash.update(line.encode())
		else:
			estr = 'no file hash value found'
			hash = js['value']
			if 'validation' in types:
				types['validation'] += 1
			else:
				types['validation'] = 1
		if 'lv' in js:
			if 'lv' in types:
				types['lv'] += 1
			else:
				types['lv'] = 1
			estr = 'no pm key found'
			mp = js['pm']
			estr = 'no np key found'
			np = js['np']
			estr = 'no ms key found'
			if np != len(js['ms']):
				ws += 1
				print('Warning: np [%i] does not match length of array ms [%i]' % (js['np'],len(js['ms'])))
				print('line %i = %s' % (ln,line))
			estr = 'no is key found'
			if np != len(js['is']):
				ws += 1
				print('Warning: np [%i] does not match length of array is [%i]' % (js['np'],len(js['is'])))
				print('line %i = %s' % (ln,line))
		if 'format' in js:
			if 'format' in types:
				types['format'] += 1
			else:
				types['format'] = 1
			if js['format'] != 'jsms 1.0':
				es += 1
				print('Error: line %i %s' % (ln,'unsupported format "%s"' % js['format']))
				print('line %i = %s' % (ln,line))

	except:
		es += 1
		print('Error: line %i %s' % (ln,estr))
		print('line %i = %s' % (ln,line))
	ln += 1
if hash != mhash.hexdigest():
	print('Error: file hash does not match calculated hash\n%s - file hash\n%s - calculated' % (hash,mhash.hexdigest()))
	es += 1
if len(types) != 3:
	if 'validation' not in types:
		print('Error: file does not contain a validation object')
		es += 1
	if 'lv' not in types:
		print('Error: file does not contain an lv object')
		es += 1
	if 'format' not in types:
		print('Error: file does not contain a format object')
		es += 1
for t in types:
	print('"%s" objects = %i' % (t,types[t]))
print('Errors = %i, warnings = %i' % (es,ws))


from typing_extensions import Annotated
import typer
import uvicorn
import os

app = typer.Typer(invoke_without_command=True)

@app.callback()
def main(
    host: str = "127.0.0.1",
    port: int = 8081,
    workers: int = 1,
    reload: Annotated[bool, typer.Option("--reload")] = False,
    docs: bool = False,
):
    """
    Launch the Autogen UI CLI .Pass in parameters host, port, workers, and reload to override the default values.
    """

    os.environ["AUTOGENUI_API_DOCS"] = str(docs)

    uvicorn.run(
        "autogenui.web.app:app",
        host=host,
        port=port,
        workers=workers,
        reload=reload,
    )

@app.command()
def models():
    print("A list of supported providers:")

def run():
    app()

if __name__ == "__main__":
    app()


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2018/7/30 16:53
# Project:
# @Author: ZQJ
# @Email : zihe@yscredit.com
from pyspider.libs.base_handler import *
from lxml import etree
import re

class Handler(BaseHandler):
    crawl_config = {
        'itag': 'v6.5',
        'time_out': 4000,
        # 'proxy': 'H14LXDJ6O07CAFDP:150D24D434AC09EE@proxy.abuyun.com:9010',
    }

    retry_delay = {
        0: 60,
        1: 60 * 5,
        2: 60 * 10,
        3: 60 * 15,
        4: 60 * 20,
        5: 60 * 25,
    }

    schema_def = [{"name": "id", "type": "string"},
                  {"name": "_id_", "type": "string"},
                  {"name": "ann_type", "type": "string"},
                  {"name": "announcer", "type": "string"},
                  {"name": "case_no", "type": "string"},
                  {"name": "ann_content", "type": "string"},
                  {"name": "ann_date", "type": "string"},
                  {"name": "content_url", "type": "string"},
                  {"name": "ann_html", "type": "string"},
                  {"name": "pdf_url", "type": "string"},
                  {"name": "source", "type": "string"},
                  {"name": "defendant", "type": "string"}
                  ]

    @every(minutes=24 * 60)
    @config(age=60 * 60)
    def on_start(self):
        url = 'http://zqzyssfw.gdzqfy.gov.cn/zqzy/front/ggxxList?ah=&fjm=JE0&gglx=sd&xszt=splc&pagenumber=2&dir=up&cs_token_flag=1&cs_token=21a2b8d642f845738e119bf21e63da63'
        self.crawl(url, callback=self.get_page)

    @config(age=60 * 60)
    def get_page(self, response):
        html = response.etree
        last_page = html.xpath('//div[@class="left"]//text()')
        all_page = int(re.findall(r'共(\d+)页', ''.join(last_page))[0]) + 1
        for i in range(1, 51):  # all_page
            url = 'http://zqzyssfw.gdzqfy.gov.cn/zqzy/front/ggxxList?ah=&fjm=JE0&gglx=sd&xszt=splc&pagenumber=' + str(
                i) + '&dir=up&cs_token_flag=1&cs_token=21a2b8d642f845738e119bf21e63da63'
            self.crawl(url, callback=self.index_page)

    @config(age=60 * 60)
    def index_page(self, response):
        try:
            html = response.etree
            table = html.xpath('//div[@class="doclist"]/table/tr')
            key = table[0].xpath('.//text()')
            key = list(map(lambda x: re.sub(r'\s', '', x), key))
            key = list(filter(lambda x: len(x) > 0, key))

            for each in table[1:]:
                case = each.xpath('.//text()')
                case = list(map(lambda x: re.sub(r'\s', '', x), case))
                case = list(filter(lambda x: len(x) > 0, case))
                print(case)

                ann_content = re.sub(r'[\{\}\']', '', str(dict(zip(key[1:], case[1:]))))
                case_on = case[1]
                ann_date = case[3] + 'T00:00:00+08:00'
                ann_html = etree.tounicode(each)
                defendant = case[5]

                yield {
                    'ann_type': '送达公告',
                    'announcer': '肇庆市中级人民法院',
                    'defendant': str(defendant),
                    'case_no': str(case_on),
                    'ann_content': str(ann_content),
                    'ann_date': str(ann_date),
                    'content_url': str(response.url),
                    'ann_html': str(ann_html),
                    'pdf_url': '',
                    'source': '肇庆市中级人民法院网上诉讼平台',
                    'id': '',
                    '_id_': ''
                }
        except Exception as e:
            print(e)


# -*- coding: utf-8 -*-
"""
Created on Fri Jan  5 10:42:16 2018

E-mail: xieydd@gmail.com

@author: xieydd

@description:使用SVM进行人脸识别
"""
import numpy,time
from sklearn import datasets
from sklearn import svm
from sklearn import decomposition
from sklearn import manifold
from sklearn.cross_validation import train_test_split as tts
from sklearn import svm,neural_network
from sklearn.metrics import classification_report,precision_score,recall_score,f1_score
from sklearn import pipeline
from sklearn.preprocessing import StandardScaler,MinMaxScaler
from sklearn.grid_search import GridSearchCV
from matplotlib import pyplot
import matplotlib.colors as colors

def get_data():
    face_data=datasets.fetch_olivetti_faces()
    #face_data=datasets.load_iris()
    data=face_data.data
    target=face_data.target
    return data,target

def pca(x,n):
    pca_learner=decomposition.PCA(n_components=n)
    x=pca_learner.fit_transform(x)
    return x

def pca_svm(pca_n=10,svm_C=1):
    t1=time.time()
    data,target=get_data()
    #scale_learner=StandardScaler()
    #data=scale_learner.fit_transform(data)
    x_train,x_test,y_train,y_test=tts(data,target,random_state=33)
    pca_learner=decomposition.PCA(n_components=pca_n)
    x_train=pca_learner.fit_transform(x_train)
    svm_learner=svm.SVC(C=svm_C)
    svm_learner.fit(x_train,y_train)
    x_test_pre=pca_learner.transform(x_test)
    y_test_pre=svm_learner.predict(x_test_pre)
    # report=classification_report(y_test,y_test_pre)
    # print 'The Main Explanied: ',numpy.sum(pca_learner.explained_variance_ratio_)
    # print report
    # print x_test_pre.shape,y_test_pre.shape,y_test.shape
    ac=svm_learner.score(x_test_pre,y_test)
    p=precision_score(y_test,y_test_pre,average='weighted')
    r=recall_score(y_test,y_test_pre,average='weighted')
    f1=2.0/(1.0/p+1.0/r)
    t=time.time()-t1
    return ac,p,r,f1,t

def pca_svm_time_score_compare():
    ac_score=[]
    p_score=[]
    r_score=[]
    f1_score=[]
    tt=[]
    stand=MinMaxScaler((20,30))
    steps=numpy.arange(10,410,10)
    for n in steps:
        ac,p,r,f1,t=pca_svm(pca_n=n)
        p_score.append(p)
        f1_score.append(f1)
        r_score.append(r)
        ac_score.append(ac)
        tt.append(t)
    p_score_stand=stand.fit_transform(numpy.array(p_score).reshape((-1,1)))
    r_score_stand=stand.fit_transform(numpy.array(r_score).reshape((-1,1)))
    f1_score_stand=stand.fit_transform(numpy.array(f1_score).reshape((-1,1)))
    ac_score_stand=stand.fit_transform(numpy.array(ac_score).reshape((-1,1)))
    figure=pyplot.figure()

    pyplot.subplot(2,1,1)
    pyplot.scatter(steps,f1_score,label='f1-score',color='red',s=p_score_stand,alpha=0.7)
    pyplot.scatter(steps,r_score,label='recall-score',color='blue',s=r_score_stand,alpha=0.7)
    pyplot.scatter(steps,p_score,label='precision-score',color='yellow',s=f1_score_stand,alpha=0.7)
    pyplot.scatter(steps,ac_score,label='accuracy-score',color='purple',s=ac_score_stand,alpha=0.7)
    pyplot.xlabel('n-components')
    pyplot.ylabel('score')
    pyplot.legend()
    pyplot.title('The Score Of SVM After PCA To N_components')
    pyplot.subplot(2,1,2)
    pyplot.plot(steps,tt,label='cost-time',color='black',marker='o')
    # for i in range(len(tt)):
        # pyplot.text(steps[i],ac_score[i],str(round(tt[i],1))+'s',fontdict=dict(size=10,weight='normal'))
        # pyplot.plot([steps[i],steps[i]],[0,ac_score[i]],'--b')
    pyplot.legend()
    pyplot.xlabel('n-components')
    pyplot.ylabel('time')
    pyplot.show()

# pca before svm fitting is better
def pca_svm_pipeline():
    #svm_C=numpy.linspace(0.5,10,10)
    svm_C=[1]
    pca_n_components=numpy.arange(5,200,10)
    data,target=get_data()
    x_train,x_test,y_train,y_test=tts(data,target,random_state=33)
    #scale_learner=StandardScaler()
    pca_learner=decomposition.PCA()
    svm_learner=svm.SVC()
    pipe=pipeline.Pipeline([('pca',pca_learner),('svm',svm_learner)])
    gscv=GridSearchCV(pipe,
                      {'pca__n_components':pca_n_components,'svm__C':svm_C},n_jobs=-1)
    gscv.fit(x_train,y_train)
    y_test_pre=gscv.predict(x_test)
    report=classification_report(y_test,y_test_pre)
    print(gscv.best_params_ )
    print(report)
    target_pre=gscv.predict(data)
    n1,n2=data.shape
    figure=pyplot.figure()
    L=numpy.zeros((40,))
    xx=numpy.linspace(0,1,64)+13
    yy=numpy.linspace(1,0,64)+13
    xx,yy=numpy.meshgrid(xx,yy)
    for i in range(n1):
        k=target_pre[i]
        g=L[k]
        L[k]+=1
        xx1=xx-k
        yy1=yy-g
        pyplot.contourf(xx1,yy1,data[i].reshape((64,64)),cmap='gray')
        if target[i]!=target_pre[i]:
            pyplot.scatter(numpy.mean(xx1),numpy.mean(yy1),marker='x',c='red',s=40)
    pyplot.axis('off')
    pyplot.grid('off')
    pyplot.title('PCA & SVM Recongnize Faces')
    pyplot.show()

if __name__=='__main__':
    pca_svm_pipeline()  #Grid Search and show the results
    pca_svm_time_score_compare()  #Direct Search


import json

from flask import render_template

from models.Business import Business
from models.Form1099NECCreateRequest import Form1099NECCreateRequest
from models.Form1099NecList import Form1099NecList
from models.NECFormData import NECFormData
from models.Recipient import Recipient
from models.ReturnData import ReturnData
from models.ReturnHeader import ReturnHeader
from models.ScheduleFiling import ScheduleFiling
from models.States import States
from models.SubmissionManifest import SubmissionManifest
from models.USAddress import USAddress

def save_form_nec(input_request_json):
    businessId = ''
    if 'business_list' in input_request_json:
        businessId = input_request_json['business_list'][0]

    rName = ''
    if 'rName' in input_request_json:
        rName = input_request_json['rName'][0]

    rTIN = ''
    if 'rTIN' in input_request_json:
        rTIN = input_request_json['rTIN'][0]

    amount = ''
    if 'amount' in input_request_json:
        amount = input_request_json['amount'][0]

    recipientId = None

    if 'recipientsDropDown' in input_request_json:
        recipientId = input_request_json['recipientsDropDown'][0]

    requestModel = Form1099NECCreateRequest()

    returnHeader = ReturnHeader()

    business = Business()
    business.set_BusinessId(businessId)
    returnHeader.set_Business(business.__dict__)
    requestModel.set_ReturnHeader(returnHeader.__dict__)

    submissionManifest = SubmissionManifest()
    submissionManifest.set_TaxYear(2020)
    submissionManifest.set_IsFederalFiling(2020)
    submissionManifest.set_IsStateFiling(True)
    submissionManifest.set_IsPostal(True)
    submissionManifest.set_IsOnlineAccess(True)
    submissionManifest.set_IsTinMatching(True)
    submissionManifest.set_IsScheduleFiling(True)
    scheduleFiling = ScheduleFiling()
    scheduleFiling.set_EfileDate("04/21/2021")
    submissionManifest.set_ScheduleFiling(scheduleFiling.__dict__)
    requestModel.set_SubmissionManifest(submissionManifest.__dict__)
    returnDataList = []
    returnData = ReturnData()
    # returnData.set_RecordId(null)
    returnData.set_SequenceId("1")
    # set Recipient data
    recipientModel = Recipient()
    if recipientId != '-1':
        recipientModel.set_RecipientId(recipientId)
    else:
        recipientModel.set_RecipientId('')

    recipientModel.set_TINType("EIN")
    recipientModel.set_TIN(rTIN)
    recipientModel.set_FirstPayeeNm(rName)
    recipientModel.set_SecondPayeeNm("")
    recipientModel.set_IsForeign(False)
    usAddress = USAddress()
    usAddress.set_Address1("1751 Kinsey Rd")
    usAddress.set_Address2("Main St")
    usAddress.set_City("Dothan")
    usAddress.set_State("AL")
    usAddress.set_ZipCd("36303")
    recipientModel.set_USAddress(usAddress.__dict__)
    # recipientModel.set_ForeignAddress(null)
    recipientModel.set_Email("sharmila.k@dotnetethics.com")
    recipientModel.set_Fax("1234567890")
    recipientModel.set_Phone("1234567890")
    returnData.set_Recipient(recipientModel.__dict__)
    # set NEC data
    necFormDataModel = NECFormData()
    necFormDataModel.set_B1NEC(amount)
    necFormDataModel.set_B4FedTaxWH(54.12)
    necFormDataModel.set_IsFATCA(True)
    necFormDataModel.set_Is2ndTINnot(True)
    necFormDataModel.set_AccountNum("20123130000009000001")
    statesList = []
    stateModel = States()
    stateModel.set_StateCd("PA")
    stateModel.set_StateWH(15)
    stateModel.set_StateIdNum("99999999")
    stateModel.set_StateIncome(16)
    statesList.append(stateModel.__dict__)  # State 1
    stateModel = States()
    stateModel.set_StateCd("AZ")
    stateModel.set_StateWH(17)
    stateModel.set_StateIdNum("99-999999")
    stateModel.set_StateIncome(18)
    statesList.append(stateModel.__dict__)  # State 2
    necFormDataModel.set_States(statesList)

    returnData.set_NECFormData(necFormDataModel.__dict__)
    returnData.set_MISCFormData(None)
    returnDataList.append(returnData.__dict__)

    requestModel.set_ReturnData(returnDataList)

    return requestModel

# Validate 1099 NEC Save response
def nec_save_response_validation(response):
    if response['StatusCode'] == 200:

        return render_template('success.html',
                               response='StatusMessage=' + response['StatusMessage'] + '<br>SubmissionId =' + response[
                                   'SubmissionId'], ErrorMessage=' Form 1099-NEC Created Successfully',
                               ButtonText="Form 1099-NEC", FormType="NEC")

    elif 'Form1099Records' in response and response['Form1099Records'] is not None and 'ErrorRecords' in response[
        'Form1099Records'] and response['Form1099Records']['ErrorRecords'][0] is not None and 'Errors' in \
            response['Form1099Records']['ErrorRecords'][0] and response['Form1099Records']['ErrorRecords'][0][
        'Errors'] is not None:

        errorRecords = []

        for errorList in response['Form1099Records']['ErrorRecords']:

            if 'Errors' in errorList and errorList['Errors'] is not None:

                for err in errorList['Errors']:
                    errorRecords.append(err)

        return render_template('error_list.html', errorList=errorRecords,
                               status=str(response['StatusCode']) + " - " + str(response['StatusName']) + " - " + str(
                                   response['StatusMessage']))
    else:

        return render_template('success.html', response='StatusMessage=' + str(response['StatusCode']),
                               ErrorMessage='Message=' + json.dumps(response))

# Validate 1099 NEC List response
def form_nec_list_response_validation(response):
    form1099NecList = []
    if response is not None:
        if 'Form1099Records' in response:
            if response['Form1099Records'] is not None:
                for records in response['Form1099Records']:
                    recipientData = Form1099NecList()
                    if 'RecipientNm' in records['Recipient']:
                        recipientData.set_RecipientNm(
                            records['Recipient']['RecipientNm'])
                    elif 'RecipientName' in records['Recipient']:
                        recipientData.set_RecipientNm(
                            records['Recipient']['RecipientName'])

                    recipientData.set_TIN(records['Recipient']['TIN'])
                    recipientData.set_RecipientId(
                        records['Recipient']['RecordId'])
                    recipientData.set_SubmissionId(records['SubmissionId'])
                    recipientData.set_BusinessNm(records['BusinessNm'])
                    recipientData.set_Status(records['Recipient']['Status'])
                    form1099NecList.append(recipientData.__dict__)

    return json.dumps(form1099NecList)

# Validate 1099 NEC Transmit response
def form_1099_nec_transmit_response_validation(response):
    if response is not None:

        if response['StatusCode'] == 200:

            return render_template('success.html',
                                   response='Status Timestamp=' + response['Form1099Records']['SuccessRecords'][0][
                                       'StatusTs'],
                                   ErrorMessage='Status= ' + response['Form1099Records']['SuccessRecords'][0]['Status'],
                                   ButtonText="Form 1099-NEC", FormType="NEC")

        elif 'Errors' in response and response['Errors'] is not None:

            return render_template('error_list.html', errorList=response['Errors'],
                                   status=str(response['StatusCode']) + " - " + str(
                                       response['StatusName']) + " - " + str(response['StatusMessage']))

        else:
            return render_template('success.html', response='StatusMessage=' + str(response['StatusCode']),
                                   ErrorMessage='Message=' + json.dumps(response))

# Validate 1099 NEC Get Pdf response
def form_1099_nec_get_pdf_response_validation(response):
    if 'Form1099NecRecords' in response and response['Form1099NecRecords'] is not None:
        if 'Message' in response['Form1099NecRecords'][0]:
            return render_template('pdf_response.html', errorList=response['Form1099NecRecords'],
                                   FormType="Form 1099-NEC")
        else:
            return "OK"
    elif 'Errors' in response and response['Errors'] is not None:
        return render_template('pdf_response.html', errorList=response['Errors'])

    return "OK"


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

"""
demo3_max.py  最值
"""
import numpy as np
import datetime as dt
import matplotlib.pyplot as mp
import matplotlib.dates as md

# 日月年转年月日的函数
def dmy2ymd(dmy):  # 传过来的是第一列的值
    dmy = str(dmy, encoding='utf-8')
    time = dt.datetime.strptime(dmy, '%d-%m-%Y')
    t = time.date().strftime('%Y-%m-%d')
    return t

# 读文件
dates, opening_prices, highest_prices, \
lowest_prices, closing_prices, volume = np.loadtxt(
    '../da_data/aapl.csv', delimiter=',',
    usecols=(1, 3, 4, 5, 6, 7), unpack=True,
    dtype='M8[D], f8, f8, f8, f8,f8',
    converters={1: dmy2ymd})

# 30天股票的波动区间，最低价～最高价
max_price = highest_prices.max()
min_price = np.min(lowest_prices)
print('max:', max_price, 'min:', min_price)

# 最高点和最低点都是哪一天
max_ind = np.argmax(highest_prices)
min_ind = np.argmin(lowest_prices)
print('max date:', dates[max_ind])
print('min date:', dates[min_ind])

# maximum与minimum
a = np.arange(1, 10).reshape(3, 3)
b = np.arange(1, 10)[::-1].reshape(3, 3)
print(a)
print(b)
print(np.maximum(a, b))
print(np.minimum(a, b))


from itertools import permutations
from itertools import product

number_list=[1,2,3,4]
def display_formula(operation_list,origin_list):
    display=""
    for i in range (0,3):
        display+=str(origin_list[i])+operation_list[i]
    display+=str(origin_list[3])
    return (display)
def calculate(operation_list,operation_index,number_list):
    a=number_list[operation_index]
    b=number_list[operation_index+1]
    operation=operation_list[operation_index]
    if operation=="+":
        ab=a+b
    elif operation=="-":
        ab=a-b
    elif operation=="*":
        ab=a*b
    elif operation=="/":
        ab=a/b
    del(number_list[operation_index:operation_index+2])
    del(operation_list[operation_index])
    number_list.insert(operation_index, ab)
    return (operation_list,operation_index-1,number_list)

for number_element in permutations(number_list):
    for operation_element in product('+-*/', repeat=3):
        number_list=list(number_element)
        operation_list=list(operation_element)
        M_D=[]
        i=0
        while i < len(operation_list):
            if operation_list[i] in ["*","/"]:
                operation_list,i,number_list = calculate(operation_list,i,number_list)
            i+=1
        i=0
        while i < len(operation_list):
            operation_list,i,number_list = calculate(operation_list,i,number_list)
            i+=1
        if number_list[0]==24:
            print (display_formula(list(operation_element),list(number_element)))

Scientific_Computing_in_Physics/OWO Python/24 card game/24_permutation.py


from itertools import islice, count
from day12 import day12

def run():
    print("Part 1 result:", part1())

def part1():
    inst = day12.parse_input("day25/input.txt")

    for i in count():
        expected = False
        for ix, (_, _, x) in enumerate(day12.execute_to_output(inst, [i, 0, 0, 0])):
            if x[-1] != expected: break
            expected = not expected

            if ix > 100:
                return i

    raise RuntimeError("No solution in expected bounds")


# -*- coding: utf-8 -*-

"""Tests for `saxo_openapi` package."""

import requests_mock
from .unittestsetup import test_generic, ReqMockTest
import saxo_openapi.endpoints.rootservices as rs
from parameterized import parameterized

class TestSaxo_RootServices_Features(ReqMockTest):
    """Tests for `rootservices-features` endpoints."""

    def setUp(self):
        super(TestSaxo_RootServices_Features, self).setUp()

    @parameterized.expand([
        (rs.features, "Availability", {}),
        (rs.features, "CreateAvailabilitySubscription", {}),
        (rs.features, "RemoveAvailabilitySubscription", {}),
      ])
    @requests_mock.Mocker(kw='mock')
    def test_all(self, _mod, clsNm, route, **kwargs):
        test_generic(self, _mod, clsNm, route, **kwargs)


from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class ProfilManager(models.Manager):
    def create_profil(self, user):
        profil = self.create(user=user)
        return profil

class Profail(models.Model):
    user=models.OneToOneField(User,null=True,on_delete=models.CASCADE)
    photo=models.ImageField(upload_to="static/user_photo/",blank=True,default="static\img\images.png")
    address=models.CharField(max_length=200,default='user')
    phone_number = models.CharField(max_length=20,default='user')
    objects=ProfilManager()
    def __str__(self):
        return str(self.user)


import urllib.request, urllib.parse, urllib.error
import http.client
# import simplejson as json
import json	 # replace w/ ujson or simplejson? (needs C binary)
import jsonfilter

############ Public API ##############
class pubapi():

 def connects(url, con_list):
  sconn = http.client.HTTPSConnection(url)
  jdata = []
  for path in range(len(con_list)):
   sconn.request("GET", con_list[path]['con'])
   response = sconn.getresponse().read().decode()
   jdata.append({'meth':con_list[path]['meth'],'pair':con_list[path]['pair'],'datas':json.loads(response)})
   print(url+con_list[path]['con'])
  jsonfilter.filters.jfstart(url, jdata)

  sconn.close()
  #end connects

 #trades
 def btce(url = "btc-e.com"):
  # limited to 15sec
  methods = ['ticker', 'depth']; # 'trades' (removing for now)
  pairs = ['btc_usd', 'eur_usd', 'btc_eur'];
  pairstring = "-".join(pairs)
  params = {"limit" : 2000, "ignore_invalid" : 1}
  url_values = urllib.parse.urlencode(params)
  con_list = []
  for meth in range(len(methods)):
   con_list.append({'meth':methods[meth], 'pair':pairs, 'con':"/api/3/%s" % methods[meth] + "/" + pairstring + "?" + url_values})
  pubapi.connects(url, con_list)

 def bitfinex(url = "api.bitfinex.com"):
  # ~1/sec
  methods = ['pubticker', 'book']; # 'trades' (removing for now)
  pairs = ['btcusd', 'drkusd', 'drkbtc'];
  params = {"limit_bids" : 2000, "limit_asks" : 2000}
  url_values = urllib.parse.urlencode(params)
  con_list = []
  for meth in range(len(methods)):
   for pair in range(len(pairs)):
    con_list.append({'meth':methods[meth], 'pair':pairs[pair], 'con':"/v1/%s" % methods[meth] + "/" + pairs[pair] + "?" + url_values})
  pubapi.connects(url, con_list)

 def btcchina(url = "data.btcchina.com"):
  # ~1/sec
  methods = ['orderbook', 'ticker']; # 'trades' (removing for now)
  pairs = ['btccny', 'ltccny', 'ltcbtc'];  # "ltccny", "btcltc"
  params = {"limit" : 5000}
  ob_values = urllib.parse.urlencode(params)
  tparams = {"limit" : 10000}
  trades_values = urllib.parse.urlencode(tparams)
  tlist = []
  for i in range(len(pairs)):tlist.append('ticker_'+pairs[i])
  con_list = []
  con_list.append({'meth':methods[1], 'pair':tlist, 'con':"/data/ticker?market=all"})
  for pair in range(len(pairs)):
   con_list.append({'meth':methods[0], 'pair':pairs[pair], 'con':"/data/%s" % methods[0] + "?market=" + pairs[pair] + "&" + ob_values})
  pubapi.connects(url, con_list)
 #end trades

###
## for testing
#pubapi.btce()
#pubapi.bitfinex()
#pubapi.btcchina()


#!/usr/bin/env python3

import unittest
import os
import numpy as np
from scipy.io import netcdf_file
from booz_xform import Booz_xform

TEST_DIR = os.path.join(os.path.dirname(__file__), 'test_files')

class RegressionTest(unittest.TestCase):
    def test_regression(self):

        configurations = ['circular_tokamak',
                          'up_down_asymmetric_tokamak',
                          'li383_1.4m',
                          'LandremanSenguptaPlunk_section5p3']

        for configuration in configurations:
            wout_filename = 'wout_' + configuration + '.nc'
            boozmn_filename = 'boozmn_' + configuration + '.nc'
            boozmn_new_filename = 'boozmn_new_' + configuration + '.nc'
            f = netcdf_file(os.path.join(TEST_DIR, boozmn_filename),
                                   'r', mmap=False)
            b = Booz_xform()
            b.read_wout(os.path.join(TEST_DIR, wout_filename))
            # Transfer parameters from the reference file to the new
            # calculation
            b.mboz = f.variables['mboz_b'][()]
            b.nboz = f.variables['nboz_b'][()]
            b.compute_surfs = f.variables['jlist'][()] - 2

            b.run()

            # Compare 2D arrays
            vars = ['bmnc_b', 'rmnc_b', 'zmns_b', 'numns_b', 'gmnc_b']
            asym = bool(f.variables['lasym__logical__'][()])
            if asym:
                vars += ['bmns_b', 'rmns_b', 'zmnc_b', 'numnc_b', 'gmns_b']

            rtol = 1e-12
            atol = 1e-12
            for var in vars:
                # gmnc_b is misspelled in the fortran version
                var_ref = var
                if var == 'gmnc_b':
                    var_ref = 'gmn_b'

                # Handle the issue that we now use the variable nu,
                # whereas the boozmn format uses the variable
                # p = -nu.
                sign = 1
                if var[:2] == 'nu':
                    sign = -1
                    var_ref = 'p' + var[2:]

                # Reference values:
                arr1 = f.variables[var_ref][()]
                # Newly computed values:
                arr2 = getattr(b, var).transpose()

                print('abs diff in ' + var + ':', np.max(np.abs(arr1 - sign * arr2)))
                np.testing.assert_allclose(arr1, sign * arr2,
                                           rtol=rtol, atol=atol)

            # Now compare some values written to the boozmn files.
            b.write_boozmn(boozmn_new_filename)
            f2 = netcdf_file(boozmn_new_filename)

            vars = f.variables.keys()
            # These variables will not match:
            exclude = ['rmax_b', 'rmin_b', 'betaxis_b', 'version', 'pres_b', 'beta_b', 'phip_b']
            for var in vars:
                if var in exclude:
                    continue
                # Reference values:
                arr1 = f.variables[var][()]
                # Newly computed values:
                arr2 = f2.variables[var][()]
                print('abs diff in ' + var + ':', np.max(np.abs(arr1 - arr2)))
                np.testing.assert_allclose(arr1, arr2,
                                           rtol=rtol, atol=atol)

            f.close()

if __name__ == '__main__':
    unittest.main()


from pathlib import Path
import numpy as np
import cv2
from isegm.data.base import ISDataset
from isegm.data.sample import DSample

class TubesDataset(ISDataset):
    def __init__(self, dataset_path, split, custom_augmentor=None, **kwargs):
        super(TubesDataset, self).__init__(**kwargs)
        self.dataset_path = Path(dataset_path)
        self._data_path = self.dataset_path / split
        self._images_path = self._data_path / 'images'
        self._insts_path = self._data_path / 'masks'
        self.dataset_samples = [x.name for x in sorted(self._images_path.glob('*.png'))]
        self._custom_augmentor = custom_augmentor

    def get_sample(self, index) -> DSample:
        image_name = self.dataset_samples[index]
        mask_name = image_name.replace("image", "mask")
        image_path = str(self._images_path / image_name)
        mask_path = str(self._insts_path / mask_name)

        image = cv2.imread(image_path, 0)
        image = np.repeat(image[:, :, np.newaxis], 3, axis=2)
        instances_mask = cv2.imread(mask_path, 0)
        instances_mask[instances_mask > 0] = 1

        if image is None or len(image.shape) != 3:
            print(f"Failed to read {image_path}")
        if instances_mask is None or len(instances_mask.shape) != 2:
            print(f"Failed to read {mask_path}")

        return DSample(image, instances_mask, object_ids=[1], sample_id=index,
                       custom_augmentor=self._custom_augmentor)


import os
from setuptools import find_packages, setup

with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme:
    README = readme.read()

setup(
    name='ridoculous',
    version='0.0.4',
    packages=find_packages(exclude=['examples']),
    include_package_data=True,
    description='Easily document any object to a markdown file',
    long_description=README,
    long_description_content_type='text/markdown',
    url='https://www.github.com/tannerburns/ridoculous',
    author='Tanner Burns',
    author_email='tjburns102@gmail.com',
    install_requires=[],
    classifiers=[
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3.7',
    ],
)


# 학습 데이터의 일부분을 빼서 Validation set 을 만드는 스크립트이다.

import os
from os import listdir
from os.path import join
from utils.utils import load_BGR, make_dirs
from random import *
import shutil

train_dirs =      '/home/lab/works/datasets/ssd2/ntire/train/splited_none_overlaped'

validation_dirs = '/home/lab/works/datasets/ssd2/ntire/validation/from_ntire_trainset'

image_count = 140

# 폴더 안의 이미지 수 확인을 위해 임의의 폴더의 이미지 개수 확인.
train_folder_dirs = [join(train_dirs, x) for x in sorted(listdir(train_dirs))]
temp = [join(train_folder_dirs[0], x) for x in sorted(listdir(train_folder_dirs[0]))]
total_img_count = len(temp)
print(total_img_count)

# 0 부터 이미지의 최대 개수 사이의 숫자 중 임의의 숫자 140장 뽑음.
picked_img_from_trainset = set()
while len(picked_img_from_trainset) < image_count:
    picked_img_from_trainset.add(randint(0, total_img_count-1))

for train_folder_dir in train_folder_dirs:
    print('-----------------------------------')
    folder_basename = os.path.basename(train_folder_dir)
    img_dirs = [join(train_folder_dir, x) for x in sorted(listdir(train_folder_dir))]

    img_folder_dir_new = make_dirs(validation_dirs + '/' + folder_basename)

    for i in picked_img_from_trainset:
        fullname = img_dirs[i]
        basename = os.path.basename(img_dirs[i])
        changedname = img_folder_dir_new + '/' + basename
        print(fullname)
        print(changedname)
        shutil.move(fullname, changedname)

#
#
# # 학습 데이터셋의 주소
# train_gt_dir =    '/home/lab/works/datasets/ssd2/ntire/train/splited_none_overlaped/GroundTruth'
# train_noisy_dir = '/home/lab/works/datasets/ssd2/ntire/train/splited_none_overlaped/Noisy'
#
# # 새로 만들 Validation 이미지들의 저장될 주소.
# validation_gt_dir = make_dirs('/home/lab/works/datasets/ssd2/NTIRE_challenge_DB/sRGB/validation/GroundTruth')
# validation_noisy_dir = make_dirs('/home/lab/works/datasets/ssd2/NTIRE_challenge_DB/sRGB/validation/Noisy')
#
# train_gt_img_dirs = [join(train_gt_dir, x) for x in sorted(listdir(train_gt_dir))]
# train_noisy_img_dirs = [join(train_noisy_dir, x) for x in sorted(listdir(train_noisy_dir))]
#
# # 생성할 validation 이미지들의 개수
#
# picked_img_from_trainset = set()
#
# while len(picked_img_from_trainset) < 50:
#     picked_img_from_trainset.add(randint(0, len(train_gt_img_dirs)-1))
#
# print(picked_img_from_trainset)
#
# for i in picked_img_from_trainset:
#     # gt 옮겨주기.
#     fullname = train_gt_img_dirs[i]
#     basename = os.path.basename(train_gt_img_dirs[i])
#     changedname = validation_gt_dir + '/' + basename
#     shutil.move(fullname, changedname)
#
#     # noisy 옮겨주기
#     fullname = train_noisy_img_dirs[i]
#     basename = os.path.basename(train_noisy_img_dirs[i])
#     changedname = validation_noisy_dir + '/' + basename
#     shutil.move(fullname, changedname)
#


#!/usr/bin/env python3
from optparse import OptionParser
import matplotlib.pyplot as plt
import numpy as np
from utils.plots import display, display_var
from data_treatement import adc_hist
from utils.geometry import generate_geometry_0
from utils.histogram import Histogram
from spectra_fit import fit_dark_spe

parser = OptionParser()
# Job configuration
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

parser.add_option("-c", "--create_histo", dest="create_histo", action="store_true",
                  help="load the SPE from dark run histograms from file", default=False)

parser.add_option("-p", "--perform_fit", dest="perform_fit", action="store_true",
                  help="perform fit of SPE from dark run", default=False)

parser.add_option("-f", "--file_list", dest="file_list",
                  help="input filenames separated by ','", default='117,118,119,120,121')

parser.add_option("--evt_max", dest="evt_max",
                  help="maximal number of events", default=50000, type=int)

parser.add_option("-n", "--n_evt_per_batch", dest="n_evt_per_batch",
                  help="number of events per batch", default=1000, type=int)

# File management
parser.add_option("--file_basename", dest="file_basename",
                  help="file base name ", default="CameraDigicam@localhost.localdomain_0_000.%s.fits.fz")
parser.add_option("-d", "--directory", dest="directory",
                  help="input directory", default="/data/datasets/CTA/DATA/20161214/")

parser.add_option("--histo_filename", dest="histo_filename",
                  help="Histogram SPE file name", default="spe_hv_on.npz")

parser.add_option("--output_directory", dest="output_directory",
                  help="directory of histo file", default='/data/datasets/CTA/DarkRun/20161214/')

parser.add_option("--fit_filename", dest="fit_filename",
                  help="name of fit file with SPE", default='spe_hv_on_fit.npz')

parser.add_option("--input_fit_filename", dest="input_fit_filename",
                  help="Input fit file name", default="adc_hv_off_fit.npz")

# Arrange the options
(options, args) = parser.parse_args()
options.file_list = options.file_list.split(',')

# Define the histograms
adcs = Histogram(bin_center_min=0., bin_center_max=4095., bin_width=1., data_shape=(1296,))

# Get the fit results from the HV OFF run
if options.verbose:
    print('--|> Recover data from %s' % (options.output_directory + options.input_fit_filename))
file = np.load(options.output_directory + options.input_fit_filename)
prev_fit_result = np.copy(file['adcs_fit_result'])

# Get the adcs
if options.create_histo:
    # Fill the adcs hist from data
    adc_hist.run(adcs, options, 'SPE',prev_fit_result=prev_fit_result)

if options.verbose:
    print('--|> Recover data from %s' % (options.output_directory + options.histo_filename))
file = np.load(options.output_directory + options.histo_filename)
adcs = Histogram(data=np.copy(file['adcs']), bin_centers=np.copy(file['adcs_bin_centers']))

# Recover fit from the HV off
if options.perform_fit:
    print('--|> Compute gain, cross talk sigma_i and sigma_e from ADC distributions with HV OFF')
    #TODO full fit including cross talk
    # Fit the baseline and sigma_e of all pixels
    adcs.fit(fit_dark_spe.fit_func, fit_dark_spe.p0_func, fit_dark_spe.slice_func,
             fit_dark_spe.bounds_func, config=prev_fit_result)
    if options.verbose:
        print('--|> Save the data in %s' % (options.output_directory + options.fit_filename))
    np.savez_compressed(options.output_directory + options.fit_filename,
                        adcs_fit_result=adcs.fit_result)

if options.verbose:
    print('--|> Recover data from %s' % (options.output_directory + options.fit_filename))
file = np.load(options.output_directory + options.fit_filename)
adcs.fit_result = np.copy(file['adcs_fit_result'])
adcs.fit_function = fit_dark_spe.fit_func

# Leave the hand
plt.ion()

# Define Geometry
geom = generate_geometry_0()

# Perform some plots
display_var(adcs, geom, title='$\sigma_e$ [ADC]', index_var=0, limit_min=0., limit_max=1., bin_width=0.05)
display_var(adcs, geom, title='$\sigma_i$ [ADC]', index_var=1, limit_min=0., limit_max=1., bin_width=0.05)
display_var(adcs, geom, title='Gain [ADC/p.e.]' , index_var=2, limit_min=4., limit_max=6., bin_width=0.05)
display_var(adcs, geom, title='Offset to baseline' , index_var=6, limit_min=-100., limit_max=100., bin_width=1)
display([adcs], geom, fit_dark_spe.slice_func, norm='log', config=prev_fit_result)