import numpy as np
import pylab as plt
import h5py
import mne

from scipy.signal import welch, hilbert
from pynfb.postprocessing.utils import get_info, add_data_simple, get_colors, fft_filter, dc_blocker, load_rejections
from collections import OrderedDict
from IPython.display import clear_output

# load raw
from json import loads

from pynfb.widgets.helpers import ch_names_to_2d_pos

settings_file = 'D:\\vnd_spbu\\pilot\\mu5days\\vnd_spbu_5days.json'
with open(settings_file, 'r', encoding="utf-8") as f:
    settings = loads(f.read())

dir_ = settings['dir']
subj = 0
day = 0

def preproc(x, fs, rej=None):
    x = dc_blocker(x)
    x = fft_filter(x, fs, band=(3, 45))
    if rej is not None:
        x = np.dot(x, rej)
    return x

from pynfb.protocols.ssd.topomap_selector_ica import ICADialog
from PyQt5 import QtGui, QtWidgets

a = QtWidgets.QApplication([])

fig1, axes1 = plt.subplots(ncols=3)

for subj in range(3,4):
    for day in range(2,5):
        experiments = settings['subjects'][subj]
        experiment = experiments[day]
        reject = True
        with h5py.File('{}\\{}\\{}'.format(settings['dir'], experiment, 'experiment_data.h5')) as f:
            fs, channels, p_names = get_info(f, settings['drop_channels'])
            rejection, alpha, ica = load_rejections(f, reject_alpha=False)
            raw_before = OrderedDict()
            raw_after = OrderedDict()
            for j, name in enumerate(p_names):
                if j < 3:
                    x = preproc(f['protocol{}/raw_data'.format(j + 1)][:], fs, rejection if reject else None)
                    raw_before = add_data_simple(raw_before, name, x)
                elif j > len(p_names) - 3:
                    x = preproc(f['protocol{}/raw_data'.format(j + 1)][:], fs, rejection if reject else None)
                    print(x.shape)
                    raw_after = add_data_simple(raw_after, name, x)

        xx = np.concatenate([raw_before['Opened'], raw_before['Baseline'], raw_after['Baseline'], raw_before['Left'], raw_before['Right'],  raw_after['Left'], raw_after['Right'], ])
        #xx[:, channels.index('C3')] = xx[:, channels.index('C3')]
        rejection, spatial, topography, unmixing_matrix, bandpass, _ = ICADialog.get_rejection(xx, channels, fs, mode='csp', states=None)
        from mne.viz import plot_topomap
        print(spatial)
        fig, axes = plt.subplots(ncols=rejection.topographies.shape[1])
        if not isinstance(axes, type(axes1)) :
            axes = [axes]
        for ax, top in zip(axes, rejection.topographies.T):
            plot_topomap(top, ch_names_to_2d_pos(channels), axes=ax, show=False)
        fig.savefig('csp_S{}_D{}.png'.format(subj, day+1))
        fig.show()


from rich.console import Console

from org.metadatacenter.model.PlanTask import PlanTask
from org.metadatacenter.model.PrePostType import PrePostType
from org.metadatacenter.model.PreReleaseBranchType import PreReleaseBranchType
from org.metadatacenter.model.TaskType import TaskType
from org.metadatacenter.operator.Operator import Operator
from org.metadatacenter.taskfactory.BuildShellTaskFactory import BuildShellTaskFactory
from org.metadatacenter.taskfactory.ReleaseCleanupShellTaskFactory import ReleaseCleanupShellTaskFactory

console = Console()

class ReleaseCleanupOperator(Operator):

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

    @staticmethod
    def expand(task: PlanTask):
        ReleaseCleanupOperator.expand_for_release(task)

    @classmethod
    def expand_for_release(cls, task):
        repo_list = [task.repo]

        for repo in repo_list:
            if repo.pre_post_type == PrePostType.SUB:
                not_handled = PlanTask("Skip sub repo", TaskType.NOOP, repo)
                not_handled.add_task_as_task(BuildShellTaskFactory.noop(repo))
                task.add_task_as_task(not_handled)
            elif repo.pre_post_type == PrePostType.NONE or repo.pre_post_type == PrePostType.PRE:
                shell_wrapper = PlanTask("Clean up repo", TaskType.SHELL_WRAPPER, repo)
                shell_wrapper.add_task_as_task(ReleaseCleanupShellTaskFactory.cleanup_generic(repo))
                task.add_task_as_task(shell_wrapper)


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

"""
序列化是将数据结构或对象转换为一系列位的过程，以便它可以存储在文件或内存缓冲区中，或通过网络连接链路传输，以便稍后在同一个或另一个计算机环境中重建。

设计一个算法来序列化和反序列化 二叉搜索树 。 对序列化/反序列化算法的工作方式没有限制。 您只需确保二叉搜索树可以序列化为字符串，并且可以将该字符串反序列化为最初的二叉搜索树。

编码的字符串应尽可能紧凑。

示例 1：

输入：root = [2,1,3]
输出：[2,1,3]

示例 2：

输入：root = []
输出：[]

提示：

树中节点数范围是 [0, 104]
0 <= Node.val <= 104
题目数据 保证 输入的树是一棵二叉搜索树。

注意：不要使用类成员/全局/静态变量来存储状态。 你的序列化和反序列化算法应该是无状态的。

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/serialize-and-deserialize-bst
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

"""

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

import json

class Codec:
    def serialize(self, root: TreeNode) -> str:
        """Encodes a tree to a single string.
        """
        self.preorder = []
        self.inorder = []
        self.pre_dfs(root)
        self.in_dfs(root)
        return json.dumps({"preorder": self.preorder, "inorder": self.inorder})

    def deserialize(self, data: str) -> TreeNode:
        """Decodes your encoded data to tree.
        """
        print(data)
        data_map = json.loads(data)
        preorder = data_map["preorder"]
        inorder = data_map["inorder"]
        inorder_map = {i: v for v, i in enumerate(inorder)}
        print(inorder_map)
        root = self.create(preorder, inorder, inorder_map, 0, len(inorder))
        return root

    def pre_dfs(self, root):
        if root is None:
            return
        self.preorder.append(root.val)
        self.pre_dfs(root.left)
        self.pre_dfs(root.right)

    def in_dfs(self, root):
        if root is None:
            return
        self.in_dfs(root.left)
        self.inorder.append(root.val)
        self.in_dfs(root.right)

    def create(self, preorder, inorder, inorder_map, in_left, in_right):
        if not preorder or in_left > in_right:
            return None

        val = preorder.pop(0)
        mid = inorder_map[val]
        root = TreeNode(val)
        # 这里需要注意的是，这里其实是引用级别的策略，不是这种其他的逻辑修改。
        root.left = self.create(preorder, inorder, inorder_map, in_left, mid - 1)
        root.right = self.create(preorder, inorder, inorder_map, mid + 1, in_right)
        return root

算法分析和归类/树/序列化和反序列化二叉搜索树.py

序列化和反序列化二叉搜索树.py


"""Worker impl module."""
from io import BytesIO
from typing import List

import cv2
import numpy as np
from imutils.video import FileVideoStream
from loguru import logger
from PIL import Image

from server.base.Message import BaseFailed
from server.base.Worker import BaseWorker
from server.engine.detection.Message import (
    Box,
    DetectionInputType,
    DetectionResult,
    DetectionTask,
    FrameData,
    PatchData,
)
from server.engine.detection.Model import DetectionModel
from server.util.types import EngineDevice
from server.util.util import create_timestamp

class DetectionWorker(BaseWorker):
    __model: DetectionModel

    __batch_size: int

    def __init__(
        self, device: EngineDevice = None, batch_size: int = 32, queue_size: int = 100
    ):
        self.__batch_size = batch_size
        if device is None:
            device = EngineDevice()

        super().__init__(queue_size=queue_size, device=device)

    def init(self):
        self.__model = DetectionModel(device=self.device)
        logger.info(f"[{self.__class__.__name__}] worker ready")

    def __transform(self, frame):
        if frame is None:
            return None
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        return frame

    def __process_video(self, video_path: str) -> List[FrameData]:

        frame_indices: List[int] = []
        frames: List[np.ndarray] = []

        stream = FileVideoStream(path=video_path, transform=self.__transform).start()

        index = 0
        while stream.running():
            frame = stream.read()  # HWC
            if frame is None:
                continue
            frames.append(frame)
            frame_indices.append(index)
            index += 1

        results: List[FrameData] = [
            FrameData(frame_index=i, patches=[]) for i in range(index)
        ]

        target_length = len(frame_indices)
        for start in range(0, target_length, self.__batch_size):
            end = min(start + self.__batch_size, target_length)
            batch = frames[start:end]

            ts_start = create_timestamp()
            indices, boxes, class_ids, scores, class_names = self.__model(frames=batch)
            ts_end = create_timestamp()

            logger.info(
                f"[{__class__.__name__}] Batch size {len(batch)}: {ts_end - ts_start} ms"
            )

            for index, box, class_id, score, class_name in zip(
                indices, boxes, class_ids, scores, class_names
            ):
                patch = PatchData(
                    box=Box(x1=box[0], y1=box[1], x2=box[2], y2=box[3]),
                    class_id=class_id,
                    score=score,
                    class_name=class_name,
                )
                results[start + index].patches.append(patch)

        return results

    def __process_image(self, bimages: List[bytes]) -> List[FrameData]:
        batch = []
        for bimage in bimages:
            image = np.asarray(Image.open(BytesIO(bimage)).convert("RGB"))  # HWC
            batch.append(image)

        num_images = len(batch)

        ts_start = create_timestamp()
        indices, boxes, class_ids, scores, class_names = self.__model(frames=batch)
        ts_end = create_timestamp()

        logger.info(
            f"[{__class__.__name__}] Batch size {len(batch)}: {ts_end - ts_start} ms"
        )

        results: List[FrameData] = [
            FrameData(frame_index=i, patches=[]) for i in range(num_images)
        ]

        for index, box, class_id, score, class_name in zip(
            indices, boxes, class_ids, scores, class_names
        ):
            patch = PatchData(
                box=Box(x1=box[0], y1=box[1], x2=box[2], y2=box[3]),
                class_id=class_id,
                score=score,
                class_name=class_name,
            )
            results[index].patches.append(patch)
        return results

    def process(self, task: DetectionTask):
        if task.input_type == DetectionInputType.IMAGE:
            ret = self.__process_image(task.image_data)
            self.sink.put(DetectionResult(data=ret, task_id=task.id))
        elif task.input_type == DetectionInputType.VIDEO:
            ret = self.__process_video(task.video_path)
            self.sink.put(DetectionResult(data=ret, task_id=task.id))
        else:
            raise RuntimeError(f"Invalid input type: {task.input_type}")

    def handle(self, msg: DetectionTask) -> DetectionResult:
        try:
            self.process(msg)
        except Exception as e:
            self.sink.put(BaseFailed(task_id=msg.id))
            raise RuntimeError(f"Failed to process task `{msg.id}`: {e}")


# This Python file uses the following encoding: ascii
# This module does all dirty work for the program.
import json
import re
from datetime import datetime

import requests
import telethon
import schedule
import telebot
from bs4 import BeautifulSoup
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetHistoryRequest

import src.config as c
from modules import functions as f
from classes.database import Database
# from bot_main import main as bot_func

tg_client = TelegramClient(c.username, c.api_id, c.api_hash)
# tg_client.start()

async def dump_comments(channel, post_id):
    # Error processing if channel comments turned off
    try:
        all_messages = []
        async for message in tg_client.iter_messages(channel, reply_to=post_id, reverse=True):
            if isinstance(message.sender, telethon.tl.types.User):
                mess_date = message.date.timestamp()
                comm_length = c.comments_length - 5
                message_text = message.text[:comm_length] + '...' if len(message.text) > comm_length else message.text
                new_rec = [mess_date, message.sender.username, message.sender.first_name, message_text]
            else:
                new_rec = None
            all_messages.append(new_rec)

        for i, mess in enumerate(all_messages):
            if all_messages[i] is None:
                del all_messages[i]

    except telethon.errors.rpcerrorlist.MsgIdInvalidError:
        all_messages = None

    return all_messages

async def dump_post_info(channel, post_id):
    all_messages = []

    class DateTimeEncoder(json.JSONEncoder):
        def default(self, o):
            if isinstance(o, datetime):
                return o.isoformat()
            if isinstance(o, bytes):
                return list(o)
            return json.JSONEncoder.default(self, o)

    history = await tg_client(GetHistoryRequest(
        peer=channel,
        offset_id=(post_id + 1),
        offset_date=None,
        add_offset=0,
        limit=1, max_id=0, min_id=0,
        hash=0))

    messages = history.messages

    for message in messages:
        all_messages.append(message.to_dict())

    # Check if the ID received match the ID asked
    if all_messages[0]['id'] != post_id:
        all_data = False
    else:
        publication_date = all_messages[0]['date']
        views = all_messages[0]['views']
        forwards = all_messages[0]['forwards']

        publication_date = publication_date.timestamp()

        all_data = {'publication_date': publication_date,
                    'views': views,
                    'forwards': forwards}

    return all_data

def dump_subscribers_count(channel_name: str):
    """Takes channel name and returns the number of its subscribers."""
    url = f'https://tgstat.ru/channel/@{channel_name}'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:45.0) Gecko/20100101 Firefox/45.0'
    }
    r = requests.get(url, headers=headers)
    r = r.text

    try:
        # Extract raw subscribers count
        soup = BeautifulSoup(r, 'html.parser').find('h2').get_text()

        # Convert subscribers count to int
        subscribers_count = re.findall(r'\d+', soup)
        subscribers_count = int(''.join(subscribers_count))
    except AttributeError:
        subscribers_count = 0

    return subscribers_count

async def collecting_data(post: list):
    """Takes post_id and flag if it is first time."""

    # Create channel link and channel object
    channel_link = f'https://t.me/{post[0][1]}'
    channel = await tg_client.get_entity(channel_link)
    channel_post_id = post[0][2]

    # Dump comments
    all_comments = await dump_comments(channel, channel_post_id)

    # Dump post info
    all_data = await dump_post_info(channel, channel_post_id)
    if not all_data:
        f.track_status(post[0], to_archive=True)

    if post[1]:
        all_data['subscribers_count'] = dump_subscribers_count(post[0][1])

    return all_data, all_comments

def put_data_to_posts(post_id, data):
    query = 'UPDATE posts SET publication_date = %s, subscribers_count = %s WHERE post_id = %s'
    params = (data['publication_date'], data['subscribers_count'], post_id)

    with Database() as db:
        db.update_query(query, params)

def put_data_to_stats(post_id, data):
    # Take last added data from base
    query = 'SELECT views, forwards FROM stats WHERE post_id = %s'
    params = (post_id,)

    with Database() as db:
        old_data = db.select_query(query, params)

    # Put data into stats
    if len(old_data) > 0:
        if old_data[-1][0] == data['views'] and old_data[-1][1] == data['forwards']:
            pass
    else:
        date_now = datetime.now().timestamp()
        query = 'INSERT INTO stats (post_id, date, views, forwards) VALUES (%s, %s, %s, %s)'
        params = (post_id, date_now, data['views'], data['forwards'])

        with Database() as db:
            db.update_query(query, params)

def put_data_to_comments(post_id, comments, is_first_collecting: bool):
    comments_to_load = []
    if not is_first_collecting:

        # Load all previous comments from the base
        query = 'SELECT comment_date FROM comments WHERE post_id = %s'
        params = (post_id,)

        with Database() as db:
            old_comments = db.select_query(query, params)

        # Compare old and new comment sets
        old_comments_1 = set()
        [old_comments_1.add(comm[0]) for comm in old_comments]

        new_comments = set()
        [new_comments.add(comm[0]) for comm in comments]
        diff_set = new_comments - old_comments_1

        # Update list of comments to load
        for comm in comments:
            if comm[0] in diff_set:
                comm.insert(0, post_id)
                comm.append('1')
                comm = tuple(comm)
                comments_to_load.append(comm)

    else:
        comments_to_load = []
        for comm in comments:
            comm.insert(0, post_id)
            comm.append('1')
            comm = tuple(comm)
            comments_to_load.append(comm)

    # Put comments into comments
    query = 'INSERT INTO comments (post_id, comment_date, author_username, author, comment_text, is_new) ' \
            'VALUES (%s, %s, %s, %s, %s, %s)'

    with Database() as db:
        db.update_many_query(query, comments_to_load)

def put_data_tobase(post_id, data, comments, is_first_collecting: bool):
    if is_first_collecting:
        put_data_to_posts(post_id, data)

    put_data_to_stats(post_id, data)

    if comments is not None:
        put_data_to_comments(post_id, comments, is_first_collecting)

def get_active_post_list():
    # Get post attributes from the base
    query = 'SELECT post_id, channel_name, channel_post_id, publication_date FROM posts WHERE is_archive = 0'
    with Database() as db:
        post_info = db.select_query(query)

    full_data = []
    for post in post_info:
        full_post = []
        is_first_collecting = False if post[3] is not None else True
        full_post.append(post)
        full_post.append(is_first_collecting)
        full_data.append(full_post)

    return full_data

def send_comments():
    # Create bot connection
    bot = telebot.TeleBot(c.bot_token)
    bot.delete_webhook()

    # Main activity
    result = f.prepare_comments()
    if result.flag:
        for user in result.list_of_users:
            bot.send_message(user, result.notification_message, reply_markup=result.list_of_buttons)

def main_main_main():

    with tg_client:
        tg_client.loop.run_until_complete(main())
    send_comments()
    print(f'Done at {datetime.now().strftime("%H:%M")}')

def with_tg_client(main):

    def another_main():
        def main_main_main():

            with tg_client:
                tg_client.loop.run_until_complete(main())
            send_comments()
            print(f'Done at {datetime.now().strftime("%H:%M")}')

        # schedule.every(15).minutes.do(main_main_main)
        schedule.every(15).minutes.do(main_main_main)
        while True:
            schedule.run_pending()

    return another_main

@with_tg_client
async def main():
    print('Start backend working (internal message)')

    # Get all active posts from the base
    full_data = get_active_post_list()

    for post in full_data:
        data, comments = await collecting_data(post)
        if not data:
            continue
        put_data_tobase(post[0][0], data, comments, post[1])

if __name__ == '__main__':
    print('Start backend working!')
    main()


#!/usr/bin/python3
import sys
import zenoh
from zenoh import Zenoh, Value
import time
from datetime import datetime
import rospy
from sensor_msgs.msg import JointState

# Data pushed by a robot
storage_path = '/mystorage'

def callback_joint_states(joint_states, args):
    global sample_id
    position = joint_states.position
    position = [round(pos, 4) for pos in position]
    x1, x2, y1, y2, z1, z2 = position
    # Zenoh data format
    value = { 'x1': str(x1), 'y1': str(y1), 'z1': str(z1),
              'x2': str(x2), 'y2': str(y2), 'z2': str(z2) }

    now = datetime.now()
    minute_id = str(now.minute)
    second_id = str(now.second)

    # Zenoh path
    sample_path = minute_id + '/' + second_id + '/' + str(sample_id)
    workspace.put(sample_path, value)
    sample_id = sample_id + 1

if __name__=="__main__":
    if len(sys.argv) < 3:
        print("Usage <address of zenohd router> <rostopic> <rostopic hz>")
    address = sys.argv[1]
    rostopic = sys.argv[2]
    sample_id = 0

    conf = {
      'mode': 'client',
      'peer': '{}'.format(address),
    }

    print("Opening Zenoh session...")
    zenoh = Zenoh(conf)

    # Create a new storage called my-storage
    path = '/@/router/local/plugin/storages/backend/memory/storage/my-storage'

    # Workspace creation
    print("New Zenoh workspace...")
    storage = zenoh.workspace()

    # Clearing the router
    print("Clearing the router first..")
    try:
        storage.delete(storage_path)
        print("Clear router")
    except Exception:
        print("Router is good")
        pass

    # Storage creation
    print("Creating storage...")
    value = {"path_expr": storage_path + '/**'}
    storage.put(path, value)
    workspace = zenoh.workspace(storage_path)
    now = datetime.now()
    print("Robot data acquisition started at:", now)

    # Setting up ROS publisher node
    rospy.init_node('zenoh_subscriber', disable_signals=True)
    sub = rospy.Subscriber(rostopic, JointState, callback_joint_states,
                           (zenoh, workspace, sample_id))

    rospy.spin()
    zenoh.close()
    now = datetime.now()
    print("Robot data acquisition stopped at:", now)
    print("Number of samples acquired:", sample_id - 1)


import torch
from torch_geometric.nn.conv import GENConv
from torch_geometric.nn import Linear, global_add_pool, DeepGCNLayer
import torch.nn.functional as F
from torch.nn import BatchNorm1d, Dropout, SELU, LayerNorm, ReLU
from torch_geometric.utils import add_self_loops, scatter
from utils.pooling import GlobalAddPooling
from utils.sigmoid import Sigmoid

class GCN2Regressor(torch.nn.Module):
    """
    Graph Neural Network for regression
    a customizable number of The GENeralized Graph Convolution (GENConv) layer from <https://arxiv.org/pdf/2006.07739.pdf>
    max graph pool layer followed by a two-layer fully-connected prediction networks
    """

    def __init__(
        self,
        node_feature_dim,
        edge_feature_dim,
        hidden_channels,
        pool_dim,
        fully_connected_channels,
        output_channels,
    ):
        """
        Args:
            node_feature_dim (int): Size of node featrues
            edge_feature_dim (int): Size of edge features
            hidden_channels (tuple or list): output size of each GEN layer
            pool_dim (int): size of the pooling layer
            fully_connected_channels (tuple or list): dimension for fully-connected prediction layers
        """
        super(GCN2Regressor, self).__init__()

        self.layers = torch.nn.ModuleList()
        self.lin = torch.nn.ModuleList()
        self.node_encoder = torch.nn.ModuleList()
        self.edge_encoder = torch.nn.ModuleList()

        for i in range(len(hidden_channels)):
            conv = GENConv(
                hidden_channels[i],
                pool_dim,
                edge_dim=hidden_channels[i],
                msg_norm=True,
                learn_msg_scale=True,
            )
            norm = LayerNorm(pool_dim)
            act = ReLU(inplace=True)
            layer = DeepGCNLayer(conv, norm, act, block="plain", dropout=0.47)
            self.layers.append(layer)

            self.lin.append(Linear(hidden_channels[i], pool_dim))

            self.node_encoder.append(Linear(node_feature_dim, hidden_channels[i]))
            self.edge_encoder.append(Linear(edge_feature_dim, hidden_channels[i]))

        self.regressor = torch.nn.Sequential(
            torch.nn.Linear(pool_dim, fully_connected_channels[0]),
            torch.nn.ReLU(),
            torch.nn.BatchNorm1d(fully_connected_channels[0]),
            torch.nn.Dropout(p=0.47),
            torch.nn.Linear(fully_connected_channels[0], fully_connected_channels[1]),
            torch.nn.ReLU(),
            torch.nn.BatchNorm1d(fully_connected_channels[1]),
            torch.nn.Dropout(p=0.47),
            torch.nn.Linear(fully_connected_channels[1], output_channels),
        )

        self.pool = GlobalAddPooling()

    def forward(self, atom_feat, edge_index, edge_attr, batch):
        """
        update nodes using atom feature, edge feature, and edge index information for every batch
        """
        # 1. Obtain node embeddings
        readout = 0

        for layer, lin, node_encoder, edge_encoder in zip(
            self.layers, self.lin, self.node_encoder, self.edge_encoder
        ):
            data = node_encoder(atom_feat)
            data_edge_attr = edge_encoder(edge_attr)
            layer_read = layer(data, edge_index, data_edge_attr)
            readout += F.softmax(layer_read, dim=-1)

        # 2. Readout layer
        x = self.pool(readout, batch)  # [batch_size, hidden_channels]

        # 3. Fully Connected Neural Net
        x = self.regressor(x)

        return x


# Built-in modules
import argparse
import os
import gzip
import random
import shutil
from pathlib import Path
from datetime import datetime, timedelta

# Basics of Python data handling and visualization
import numpy as np
import pandas as pd
import geopandas as gpd
import tqdm
#from shapely import wkt

from lib.utils import _get_point
from configs.utils import load_config
from lib.train_utils import get_data, load_pickle, save_pickle
from algorithms.ensemble import Ensemble

def preprocess(config, update=False):
    if not os.path.isfile(config["cache_path"]) or update:
        data, gts, target_size = get_data(
            indir, outdir, label, aoi, config["feature_keys"], update=update)
        save_pickle((data, gts, target_size), config["cache_path"])
    else:
        (data, gts, target_size) = load_pickle(config["cache_path"])

    # Dataset
    dataset = {"X": [],
               "Y": [],
               "gt": [],
               "native_Y": [],
               "target_Y": [],
               "lat": [],
               "lon": [],
               "coords": [],
               "date": []}
    grid = np.zeros(target_size)
    bbox = data['data'][0]["s5p" if label == "NO2" else "cams"].bbox
    for date, lat, lon, gt in tqdm.tqdm(gts[["Date", "SITE_LATIT", "SITE_LONGI", "AirQuality"]].values):
        y_ind, x_ind = _get_point(lat, lon, grid, bbox)
        try:
            day_ind = data["date"].index(int(date))

            ind = data['date'].index(date)
            native = data['feat_dicts'][ind]['no2_native' if label ==
                                             'NO2' else 'pm25_native']
            target = data['feat_dicts'][ind]['no2_target' if label ==
                                             'NO2' else 'pm25_target']

            if np.isnan(native[y_ind, x_ind]):
                continue

            dataset["X"].append(data['feats'][day_ind][y_ind, x_ind])
            dataset["native_Y"].append(native[y_ind, x_ind])
            dataset["target_Y"].append(target[y_ind, x_ind])
            dataset["gt"].append(gt)
            dataset["lat"].append(lat)
            dataset["lon"].append(lon)
            dataset["coords"].append((lat, lon))
            dataset["date"].append(date)

        except:
            pass
            #print("Error on date:", date)

    dataset = {k: np.array(v) for k, v in dataset.items()}
    # -------------------

    gt, native_Y = dataset["gt"], dataset["native_Y"]
    # Filter any very off GT

    if label == "NO2":
        dataset["Y"] = gt / (6.02214 * 1e4 * 1.9125) - native_Y
        v1 = gt
        v2 = native_Y * 6.02214 * 1e4 * 1.9125
        m = np.min([v2, v1], axis=0)
        r = (v1 / v2)
        if "Africa" in aoi:
            mask = (r < 3.5) & (r > 0.7)
        elif "California" == aoi:
            mask = (r < 4.5) & (r > 0.7)
        elif "Italy" == aoi:
            mask = (r < 2.5) & (r > 0.85)

    else:
        dataset["Y"] = gt - native_Y
        v1 = gt
        v2 = native_Y
        if "Africa" in aoi:
            m = np.min([v2, v1], axis=0)
            mask = ~((abs(v2 - v1) > (m * 3)) & ((v2 > 1) | (v1 > 1)))
        elif aoi == "Italy":
            mask = ~((abs(v2 - v1) > v2 * 0.9) & ((v2 > 5) | (v1 > 5)))
        elif aoi == "California":
            mask = ~((abs(v2 - v1) > v2 * 0.535) & ((v2 > 5) | (v1 > 5)))

    print('Rate of kept samples from filter:', mask.mean())
    print('Correlation between native measure and ground truth',
          np.corrcoef(gt[mask], native_Y[mask]))

    X = dataset["X"][mask]
    Y = dataset["Y"][mask]
    coords = dataset["coords"][mask]
    return X, Y, coords, data

def main(config, update=False):
    X, Y, coords, data = preprocess(config, update=update)

    ensemble = Ensemble(config, indir, outdir)
    print("models:", ensemble.models)

    mu = np.nanmean(data["feats"], axis=(0, 1, 2))
    sigma = np.nanstd(data["feats"], axis=(0, 1, 2))

    ensemble.train(X, Y, coords=coords, mu=mu, sigma=sigma)
    ensemble.save()
    print("Done.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', required=True)
    parser.add_argument('--update', action="store_true")
    args = parser.parse_args()

    random.seed(0)

    config = load_config(args.config)
    aoi = config["aoi"]
    label = config["label"]
    indir = Path("./data/train") / config["aoi"]
    outdir = Path("./wdir") / config["aoi"]

    main(config, args.update)


"""
    -*- coding: utf-8 -*-
    @Author: yaofanghao
    @Date: 2023/7/24 10:42
    @Filename: test.py
    @Software: PyCharm
"""

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

import os
import cv2
import numpy as np

def readimg(image_path, output_path):
    image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
    # image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    output_image_name = image_path.rsplit("\\", 1)[-1]
    output_path_all = output_path+output_image_name

    # 处理模块，在这里进行修改
    _, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

    cv2.imencode('.jpg', binary_image)[1].tofile(output_path_all)

    print("111")
    # cv2.imshow('Binary Image', binary_image)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    return 1

# image_path="C:\\Users\\Rainy\\Desktop\\test_camera\\img\\210.jpg"
# output_path="C:\\Users\\Rainy\\Desktop\\test_camera\\img_out\\"
# readimg(image_path=image_path, output_path=output_path)


import sqlite3

sq_baglanti = sqlite3.connect("Kütüphane.db")

imlec = sq_baglanti.cursor()

def tablo_olustur():
    imlec.execute("CREATE TABLE IF NOT EXISTS kitaplik (isim TEXT, yazar TEXT, Yayınevi TEXT, Sayfa_sayısı INT)")
    sq_baglanti.commit()
tablo_olustur()

def veri_ekle():
    imlec.execute("Insert into kitaplik Values('İstanbul Hatırası','Ahmet Ümit','Everest',561)")
    sq_baglanti.commit()
# veri_ekle()

def veri_ekle2(isim,yazar,yayinevi,sayfa):
    imlec.execute(f"Insert into kitaplik Values('{isim}','{yazar}','{yayinevi}',{sayfa})")
    sq_baglanti.commit()

isim = input("Kitap İsmini Giriniz: ")
yazar = input("Yazar İsmini Giriniz: ")
yayinevi = input("Yayınevi İsmini Giriniz: ")
sayfa = int(input("Sayfa Sayısını Giriniz: "))

if input("Girdiğiniz bilgileri eklemek istiyor musunuz ?\n(y \ n) ") == "y":
    veri_ekle2(isim, yazar, yayinevi, sayfa)
    print("Bilgiler Eklendi")
else:
    print("Ekleme Yapılmadı")

sq_baglanti.close()


from abc import ABC, abstractmethod
from typing import Protocol

from src.schedule.demand_schedule_strategy import DemandScheduleStrategy
from src.schedule.random_schedule_strategy import RandomScheduleStrategy
from src.schedule.regular_schedule_strategy import RegularScheduleStrategy
from src.schedule.schedule_configuration import ScheduleConfiguration
from src.schedule.schedule_strategy import ScheduleStrategy
from src.wrapper.train_builder import TrainBuilder

class SpawnerProtocol(Protocol):
    """Protocol for the Spawner"""

    train_spawner: TrainBuilder

class Schedule(ABC):
    """An abstract schedule for spawning SUMO vehicles."""

    id: str
    _blocked: bool
    _seconds_to_be_spawned: list[int]
    strategy: ScheduleStrategy

    STRATEGY_CLASSES: dict[str, type] = {
        "RegularScheduleStrategy": RegularScheduleStrategy,
        "RandomScheduleStrategy": RandomScheduleStrategy,
        "DemandScheduleStrategy": DemandScheduleStrategy,
    }

    @classmethod
    def strategy_from_schedule_configuration(
        cls, schedule_configuration: ScheduleConfiguration
    ) -> ScheduleStrategy:
        """Dynamically cosntructs a ScheduleStrategy from a ScheduleConfiguration

        :param schedule_configuration: The Schedule configuration
        :return: A ScheduleStrategy
        """
        strategy_type = schedule_configuration.strategy_type
        strategy_class = cls.STRATEGY_CLASSES[strategy_type]
        assert issubclass(strategy_class, ScheduleStrategy)
        return strategy_class.from_schedule_configuration(schedule_configuration)

    def __init__(self, strategy: ScheduleStrategy, id_: str):
        """Constructs a Schedule."""
        self._blocked = False
        self._seconds_to_be_spawned = []
        self.strategy = strategy
        self.id = id_  # pylint: disable=invalid-name

    @abstractmethod
    def _spawn(self, spawner: SpawnerProtocol, seconds: int) -> bool:
        """Spawns a vehicle.

        :param spawner: The calling Spawner.
        :param seconds: The elapsed seconds
        """
        raise NotImplementedError()

    def maybe_spawn(self, seconds: int, spawner: SpawnerProtocol):
        """Spawns a vehicle if the schedule strategy allows it.

        :param seconds: The elapsed seconds
        :param spawner: The calling spawner.
        """
        if not self._blocked and self.strategy.should_spawn(seconds):
            self._seconds_to_be_spawned.append(seconds)

        if len(self._seconds_to_be_spawned) > 0:
            if self._spawn(spawner, self._seconds_to_be_spawned[-1]):
                self._seconds_to_be_spawned.pop()

    def block(self):
        """Blocks the schedule.

        :raises RuntimeError: when the schedule is already blocked
        """
        if self._blocked:
            raise RuntimeError(f"Schedule {self.id} is already blocked")
        self._blocked = True

    def unblock(self):
        """Unblocks the schedule.

        :raises RuntimeError: when the schedule is already unblocked
        """
        if not self._blocked:
            raise RuntimeError(f"Schedule {self.id} is already unblocked")
        self._blocked = False


#p-1
#Python libraries for math and graphics
import numpy as np
import mpmath as mp
import math as ma
import matplotlib.pyplot as plt
from numpy import linalg as LA

def line_dir_pt(m,A,k1,k2):
  len = 10
  dim = A.shape[0]
  x_AB = np.zeros((dim,len))
  lam_1 = np.linspace(k1,k2,len)
  for i in range(len):
    temp1 = A + lam_1[i]*m
    x_AB[:,i]= temp1.T
  return x_AB

#Input parameters
A=  np.array(([0,(6/5)]))
k=3
m=np.array(([k-3,(-4+k**2)]))
z=np.array(([0,1],[-1,0]))
n=z@m
f=m@A

print('equation of line parallel to x-axis with k=',k,'is',np.array2string(m),'x=',int(f))

##Generating the line
k1=-1
k2=1
x_AB = line_dir_pt(n,A,k1,k2)

#Plotting all lines
plt.plot(x_AB[0,:],x_AB[1,:],label='({})X={}'.format(" ".join([str(i) for i in m]),int(f)))

    # Add labels and show the plot
plt.xlabel('x')
plt.ylabel('y')
plt.title('Equation of line parallel to x-axis for k=3')
plt.legend(loc='best')
plt.grid()
plt.axis('equal')
#plt.show()
plt.savefig('../figs/fig1.pdf')


'''
Descripttion:
version:
Author: Catop
Date: 2021-02-10 07:47:09
LastEditTime: 2021-02-18 07:40:13
'''
#coding:utf-8

from flask import Flask,request,jsonify
import json
import requests
import dbconn
import goapi
import time
import os
import compress
import urllib.parse
import random
import ocrplus
import inputFilter
import json

app = Flask(__name__)
@app.route('/', methods=['POST'])
def getEvent():
    data = request.json
    post_type = data.get('post_type')
    if(post_type == 'message'):
        message_type = data.get('message_type')
        #print(data)
        message = data.get('message')
        user_id = str(data.get('user_id'))
        if(message_type=='private' and (dbconn.check_register(user_id)==1 or '注册' in message)):
            #仅接收注册用户的消息和注册消息
            print(f"--------------------\n接收消息@{user_id}:{message[:20]}")
            readMsg(user_id,message)
    elif(post_type == 'request'):
        request_type = data.get('request_type')
        if(request_type=='friend'):
            user_id = str(data.get('user_id'))
            comment = str(data.get('comment'))
            flag = str(data.get('flag'))
            print(f"--------------------\n接收加好友请求@{user_id}:{comment[:20]}")
            time.sleep(random.randint(5,10))
            goapi.add_request(flag)
            time.sleep(random.randint(5,10))
            goapi.sendMsg(user_id,"欢迎！\n请先注册，例如'注册@小明@智科20-2'\n 班级请严格按格式输入，否则可能统计不上哦,另外特别注意'-'是减号不是破折号，也不要加空格")
    else:
        #暂不处理其他类型上报，为防止go-cq报错而设置
        pass

    return data

def readMsg(user_id,message):
    #处理消息核心
    user_id = str(user_id)
    #管理员列表
    admin_list = show_admin()

    if('image' in message):
        if(dbconn.check_register(user_id)):
            #用户已注册
            get_img(user_id,message)
        else:
            #用户未注册
            goapi.sendMsg(user_id,'您还没注册呢，请输入例如"注册@小明@智科20-2"\n 班级请严格按格式输入，否则可能统计不上哦,另外特别注意'-'是减号不是破折号，也不要加空格')
        return
    if('注册' in message):
        try:
            user_name = message.split('@')[1]
            user_class = message.split('@')[2]
            #简单过滤用户输入
            if not(inputFilter.is_Chinese(user_name) and inputFilter.check_length(user_name)):
                goapi.sendMsg(user_id,'输入好像有点问题呢')
                return
            if not(inputFilter.is_valid(user_class) and inputFilter.check_length(user_class)):
                goapi.sendMsg(user_id,'输入好像有点问题呢')
                return

        except:
            goapi.sendMsg(user_id,'输入好像有点问题呢')
        else:
            if(dbconn.check_register(user_id)):
                goapi.sendMsg(user_id,'您已注册过啦~')
                re_register_user(user_id,user_name,user_class)
            else:
                    register_user(user_id,user_name,user_class)
        return
    if('/admin' in message):
        user_class = dbconn.get_user(user_id)['user_class']
        upload_date = time.strftime("%Y-%m-%d", time.localtime())
        print(user_id)
        if(user_id in admin_list.keys()):
            group_id = admin_list[user_id]
            if('群提醒' in message):
                send_alert(group_id,dbconn.get_user(user_id)['user_class'],'group')
            elif('提醒'in message):
                send_alert(user_id,dbconn.get_user(user_id)['user_class'],'private')
                upload_date = time.strftime("%Y-%m-%d", time.localtime())
                time.sleep(1)
                #ocr_err_upload(user_id,user_class,upload_date)
                ocr_err_upload(user_id,user_class,upload_date)
                time.sleep(2)
                send_images_info(user_id,user_class)
            elif('打包'in message):
                cmp_ret = compress.zip_file(upload_date,dbconn.get_user(user_id)['user_class'])
                goapi.sendMsg(user_id,f"---打包完毕---\n共处理:{cmp_ret['file_num']}张照片")
                goapi.sendMsg(user_id,'下载地址:'+urllib.parse.quote(cmp_ret['file_name']))
            elif('成员'in message):
                list_class_menbers(user_id,user_class)
            elif('核对'in message):
                goapi.sendMsg(user_id,'get')
                err_imgid = message.split('@')[1]
                ocr_times = message.split('@')[2]
                ocr_scores = message.split('@')[3]
                if(dbconn.err_check):
                    err_code = dbconn.err_check(err_imgid)
                    if(err_code):
                        dbconn.manual_update(ocr_times,ocr_scores,err_imgid)
                        goapi.sendMsg(user_id,f"人工校对完成:{dbconn.get_user_by_migid(err_imgid)}\n参赛次数:{ocr_times}\n积分:{ocr_scores}")
                elif(err_code == 0):
                    goapi.sendMsg(user_id,'该图片无需校对，请重新检查输入的id')
                elif(err_code == -1):goapi.sendMsg(user_id,f"已经人工校对过了，覆盖校对:\n{dbconn.get_user_by_migid(imgid)}\n参赛次数:{ocr_times}\n积分:{ocr_scores}")

            else:
                goapi.sendMsg(user_id,"目前支持以下管理指令呢：\n群提醒\n提醒\n打包\n成员\n")
        else:

            goapi.sendMsg(user_id,"无管理权限")

    if('/sudo' in message and (user_id=='601179193' or user_id=="29242764" or user_id=="1476821890" or user_id=="3439911708")):
        #try:

        if('增加管理员' in message):
            admin_id = message.split('@')[1]
            group_id = message.split('@')[2]
            goapi.sendMsg(user_id,str(add_admin(admin_id,group_id)))
        elif('删除管理员' in message):
            admin_id = message.split('@')[1]
            goapi.sendMsg(user_id,str(del_admin(admin_id)))
        elif('展示管理员' in message):
            goapi.sendMsg(user_id,str(show_admin()))
        else:
            goapi.sendMsg(user_id,'增加管理员\n删除管理员\n展示管理员')
        #except:
            #goapi.sendMsg(user_id,'增加管理员\n删除管理员\n展示管理员')
        #else:
            #pass

    return

def get_img(user_id,message):
    #从message中解析到图片下载地址，并保存数据库，下载文件
    try:
        img_url = message.split('url=')[1][0:-1]
        user_name = dbconn.get_user(user_id)['user_name']
        user_class = dbconn.get_user(user_id)['user_class']
        upload_date = time.strftime("%Y-%m-%d", time.localtime())
        upload_time = time.strftime("%H:%M:%S", time.localtime())

        #修改文件名格式(注意只保存文件名和数据库中显示的file_name改变，目录等名称不变)
        file_date = time.strftime("%Y%m%d", time.localtime())

        #安全过滤
        if not(img_url[0:24] == 'http://c2cpicdw.qpic.cn/'):
            goapi.sendMsg(user_id,'图片url解析错误')
            return

        #判断文件目录是否存在
        if not(os.path.exists(f"images/{upload_date}")):
            os.mkdir(f"images/{upload_date}")
        if not(os.path.exists(f"images/{upload_date}/{user_class}")):
            os.mkdir(f"images/{upload_date}/{user_class}")

        file_name = f"/{upload_date}/{user_class}/{user_class}班-{user_name}-{file_date}.jpg"
        if(dbconn.check_today_upload(user_id,upload_date)):
            goapi.sendMsg(user_id,'您今天已经上传过照片啦，已覆盖之前的图片~')

        download_img(img_url,file_name)
        #print(img_url)
    except Exception as err:
        goapi.sendMsg(user_id,'图片下载出错了！')
        print(err)
    else:
        print("成功处理图片:"+file_name)
        goapi.sendMsg(user_id,"成功处理图片，正在识别...\n"+f"{user_class}班-{user_name}-{file_date}.jpg")

        """图片识别部分"""
        try:
            print(file_name)
            ocr_ret = ocrplus.ocr_img("images"+file_name)
            ocr_err_code = ocr_ret['err_code']
            if(ocr_err_code == 0):
                goapi.sendMsg(user_id,f"参赛次数:{ocr_ret['个人参赛次数']}\n个人积分:{ocr_ret['个人积分']}")
                ocr_times = ocr_ret['个人参赛次数']
                ocr_scores = ocr_ret['个人积分']
            else:
                print("图片无法识别:"+ocr_ret)
                #图片识别接口返回无法识别
                goapi.sendMsg(user_id,f"OCR无法识别，团支书将人工复核~")
                dbconn.insert_img(user_id,file_name,upload_date,upload_time,'1','0','0')
                return
        except:
            #图片识别接口出错
            print("OCR接口出错:")
            goapi.sendMsg(user_id,f"qwq图片识别出错了！可能是字体原因，请尝试更换字体后重试")
            dbconn.insert_img(user_id,file_name,upload_date,upload_time,'1','0','0')
        else:
            dbconn.insert_img(user_id,file_name,upload_date,upload_time,ocr_err_code,ocr_times,ocr_scores)

    return

def download_img(img_url,file_name):
    res = requests.get(img_url, stream=True)
    if res.status_code == 200:
        open('images/'+file_name, 'wb').write(res.content)
        #print("image"+file_name+"saved successfully.")

def register_user(user_id,user_name,user_class):
    if(dbconn.register_user(user_id,user_name,user_class) == 1):
        goapi.sendMsg(user_id,'注册成功，现在可以开始上传图片了~')
    else:
        goapi.sendMsg(user_id,'注册失败'+f"user_id={user_id},user_name={user_name},user_class={user_class}")

def re_register_user(user_id,user_name,user_class):
    if(dbconn.re_register_user(user_id,user_name,user_class) == 1):
        goapi.sendMsg(user_id,'已更新字段')
    else:
        goapi.sendMsg(user_id,'更新字段失败:'+f"user_id={user_id},user_name={user_name},user_class={user_class}")

def send_alert(group_id,user_class,type='private'):
    group_menbers = dbconn.get_class_members(user_class)
    current_date = time.strftime("%Y-%m-%d", time.localtime())
    alert_users = {}
    #user_id:last_date
    for user_id in group_menbers:
        #print(user_id)
        try:
            last_date = dbconn.check_status(user_id)
        except TypeError:
            #还没发过照片
            alert_users[user_id] = '无记录'
        else:
            if(str(last_date)!=str(current_date)):
                alert_users[user_id] = str(last_date)[5:]

    #print(alert_users)
    msg = f"今天还有{len(alert_users)}位小可爱未完成哦\n"

    if(type=='private'):
        for user_id in alert_users.keys():
            last_date = alert_users[user_id]
            msg += f"{dbconn.get_user(user_id)['user_name']}({alert_users[user_id]})\n"
        msg+=f"{user_class} {current_date}\n完成情况:{len(group_menbers)-len(alert_users)}/{len(group_menbers)}"
        goapi.sendMsg(group_id,msg)
    elif(type=='group'):
        for user_id in alert_users.keys():
            last_date = alert_users[user_id]
            msg += f"[CQ:at,qq={user_id}]({alert_users[user_id]})\n"
        msg += f"{user_class} {current_date}\n完成情况:{len(group_menbers)-len(alert_users)}/{len(group_menbers)}"
        goapi.sendGroupMsg(group_id,msg)

def list_class_menbers(user_id,user_class):
    msg = f"{user_class}成员情况：\n"
    ret = dbconn.get_class_members(user_class)
    for i in range(0,len(ret)):
        msg += f"{ret[i]} {dbconn.get_user(str(ret[i]))['user_name']}\n"
    msg += f"共计{str(len(ret))}人"

    goapi.sendMsg(user_id,msg)

def ocr_err_upload(user_id,user_class,upload_date):
    """为管理员上报ocr错误的图片"""
    msg = "OCR无法识别以下图片:\n"
    err_list = []
    err_imgid = []
    class_menbers = dbconn.get_class_members(user_class)
    for i in range(0,len(class_menbers)):
        img_date = dbconn.check_status(class_menbers[i])
        if(str(img_date) == str(upload_date)):
            img_info = dbconn.get_latest_img_info(class_menbers[i],upload_date)[0]
            #print(img_info)
            if(img_info['ocr_err_code'] == 1):
                print(img_info)
                err_list.append(img_info['file_name'])
                err_imgid.append(img_info['imgid'])

    for i in range(0,len(err_list)):
        cqCode = f"[CQ:image,file=file:{os.getcwd()}/images{err_list[i]}]"
        msg += f"id:{err_imgid[i]}\n"
        msg += f"{cqCode}\n"
    msg += f"数量:{len(err_list)}/{len(class_menbers)}\n"
    msg += "可人工核对，操作格式: /admin 核对@id@次数@分数，例如：\n核对id为233，次数66，分数99则输入：/admin 核对@233@66@99"
    goapi.sendMsg(user_id,msg)
    return msg

######################################################################
# def manual_identify(user_id,user_class,upload_date):
#     """管理员手动识别ocr错误的图片"""
#     msg="OCR无法识别以下图片"
#     goapi.sendMsg(user_id,msg)
#     err_list = []
#     user_info = []
#     class_menbers = dbconn.get_class_members(user_class)
#     for i in range(0,len(class_menbers)):
#         img_date = dbconn.check_status(class_menbers[i])
#         if(str(img_date) == str(upload_date)):
#             img_info = dbconn.get_latest_img_info(class_menbers[i],upload_date)[0]
#             #print(img_info)
#             if(img_info['ocr_err_code'] == 1):
#                 print(img_info)
#                 err_list.append(img_info['file_name'])
#     i = 0
#     while i < len(err_list):
#         cqCode = f"[CQ:image,file=file:{os.getcwd()}/images{err_list[i]}]"
#         msg = f"{cqCode}\n"
#         msg += f"请人工核对，格式：核对@答题次数@答题分数，例如:核对@43@505"
#         goapi.sendMsg(user_id,msg)
#         if ("核对" in message )
#             ocr_times = message.split('@')[1]
#             ocr_scores = message.split('@')[2]

######################################################################

def send_images_info(user_id,user_class):
    """上报班级图片情况（次数和成绩）"""
    class_menbers = dbconn.get_class_members(user_class)
    today_upload_count = 0
    upload_date = time.strftime("%Y-%m-%d", time.localtime())
    msg = f"{user_class} {upload_date}情况:\n"
    for i in range(0,len(class_menbers)):
        if(str(dbconn.check_status(class_menbers[i])) == str(upload_date)):
            today_upload_count += 1
            img_info = dbconn.get_latest_img_info(class_menbers[i],upload_date)[0]
            if(img_info['ocr_err_code'] == 0):
                msg += f"·{dbconn.get_user(class_menbers[i])['user_name']} 次数{img_info['ocr_times']} 分数{img_info['ocr_scores']}\n"
            else:
                msg += f"·{dbconn.get_user(class_menbers[i])['user_name']} 未识别到\n"

    msg += f"共计{today_upload_count}张照片"

    goapi.sendMsg(user_id,msg)
    return msg

def add_admin(user_id,class_id):
    admin_list = show_admin()
    admin_list[str(user_id)] = str(class_id)
    with open("admin_list.txt","w") as f:
        json.dump(admin_list,f)
    return admin_list

def del_admin(user_id):
    admin_list = show_admin()
    del admin_list[str(user_id)]
    with open("admin_list.txt","w") as f:
        json.dump(admin_list,f)
    return admin_list

    return admin_list

def show_admin():
    with open("admin_list.txt","r") as f:
        admin_list = json.load(f)
    return admin_list

if __name__ == '__main__':
    app.run(host='127.0.0.1',port='5000',debug=False)
    #send_alert('1038368144','信安20-2','group')
    #goapi.sendMsg('29242764','[CQ:image,file=file:/Users/catop/Desktop/ChangZhengBot/go-cq/res1.png]')
    #print(ocr_err_upload('29242764','信安20-2','2021-02-13'))
    #goapi.sendMsg('29242764','[CQ:image,file=file:/Users/catop/Desktop/ChangZhengBot/images/2021-02-13/信安20-2/信安20-2班-张宏远-20210213.jpg]')
    #ocr_err_upload('601179193','信安20-2','2021-02-13')
    #print(send_images_info('29242764','信安20-2'))
    #print(show_admin())
    #print(del_admin('1234567890'))


import ixmp
import numpy as np
import numpy.testing as npt
import pandas as pd
import pandas.testing as pdt
import pytest

from message_ix import Scenario
from message_ix.testing import SCENARIO, make_dantzig

@pytest.fixture
def dantzig_message_scenario(message_test_mp):
    yield Scenario(message_test_mp, **SCENARIO["dantzig"])

def test_year_int(test_mp):
    scen = make_dantzig(test_mp, solve=True, multi_year=True)

    # Dimensions indexed by 'year' are returned as integers for all item types
    assert scen.set("cat_year").dtypes["year"] == "int"
    assert scen.par("demand").dtypes["year"] == "int"
    assert scen.par("bound_activity_up").dtypes["year_act"] == "int"
    assert scen.var("ACT").dtypes["year_vtg"] == "int"
    assert scen.equ("COMMODITY_BALANCE_GT").dtypes["year"] == "int"

def test_add_spatial_single(test_mp):
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")
    data = {"country": "Austria"}
    scen.add_spatial_sets(data)

    exp = ["World", "Austria"]
    obs = scen.set("node")
    npt.assert_array_equal(obs, exp)

    exp = ["World", "global", "country"]
    obs = scen.set("lvl_spatial")
    npt.assert_array_equal(obs, exp)

    exp = [["country", "Austria", "World"]]
    obs = scen.set("map_spatial_hierarchy")
    npt.assert_array_equal(obs, exp)

def test_add_spatial_multiple(test_mp):
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")
    data = {"country": ["Austria", "Germany"]}
    scen.add_spatial_sets(data)

    exp = ["World", "Austria", "Germany"]
    obs = scen.set("node")
    npt.assert_array_equal(obs, exp)

    exp = ["World", "global", "country"]
    obs = scen.set("lvl_spatial")
    npt.assert_array_equal(obs, exp)

    exp = [["country", "Austria", "World"], ["country", "Germany", "World"]]
    obs = scen.set("map_spatial_hierarchy")
    npt.assert_array_equal(obs, exp)

def test_add_spatial_hierarchy(test_mp):
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")
    data = {"country": {"Austria": {"state": ["Vienna", "Lower Austria"]}}}
    scen.add_spatial_sets(data)

    exp = ["World", "Vienna", "Lower Austria", "Austria"]
    obs = scen.set("node")
    npt.assert_array_equal(obs, exp)

    exp = ["World", "global", "state", "country"]
    obs = scen.set("lvl_spatial")
    npt.assert_array_equal(obs, exp)

    exp = [
        ["state", "Vienna", "Austria"],
        ["state", "Lower Austria", "Austria"],
        ["country", "Austria", "World"],
    ]
    obs = scen.set("map_spatial_hierarchy")
    npt.assert_array_equal(obs, exp)

@pytest.mark.parametrize(
    "args, kwargs, exp",
    [
        # Two periods of duration 20, 1 each of duration 10 and 15
        (
            ([2020, 2030, 2050, 2070, 2085],),
            dict(),
            {
                "year": [2020, 2030, 2050, 2070, 2085],
                "fmy": [2020],
                # 20 is chosen for the first period
                "dp": [20, 10, 20, 20, 15],
            },
        ),
        # Mix of positional and keyword arguments; firstmodelyear given
        (
            ([2020, 2030, 2040, 2060],),
            dict(firstmodelyear=2030),
            {
                "year": [2020, 2030, 2040, 2060],
                # firstmodelyear as selected
                "fmy": [2030],
                # 10 is chosen for the first period
                "dp": [10, 10, 10, 20],
            },
        ),
        # Years out of order
        (
            ([2030, 2010, 2020],),
            dict(),
            {
                "year": [2010, 2020, 2030],
                "fmy": [2010],
                "dp": [10, 10, 10],
            },
        ),
        # Deprecated usage with a dict as the first positional argument
        (
            (dict(year=[2010, 2020]),),
            dict(),
            {"year": [2010, 2020], "fmy": [2010], "dp": [10, 10]},
        ),
        (
            (dict(year=[2010, 2020], firstmodelyear=2020),),
            dict(),
            {"year": [2010, 2020], "fmy": [2020], "dp": [10, 10]},
        ),
        # Deprecated usage with user errors
        pytest.param(
            (dict(firstmodelyear=2010),),
            dict(),
            None,
            marks=pytest.mark.xfail(raises=ValueError),
        ),
        pytest.param(
            (dict(year=[2010, 2020], firstmodelyear=2010),),
            dict(firstmodelyear=2020),
            None,
            marks=pytest.mark.xfail(raises=ValueError),
        ),
        pytest.param(
            (dict(year=[2010, 2020], foo="bar"),),
            dict(),
            None,
            marks=pytest.mark.xfail(raises=ValueError),
        ),
        pytest.param(
            (dict(year=[2010, 2020]),),
            dict(data=dict(year=[2010, 2020])),
            None,
            marks=pytest.mark.xfail(raises=ValueError),
        ),
    ],
)
def test_add_horizon(test_mp, args, kwargs, exp):
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")

    # Call completes successfully
    if isinstance(args[0], dict):
        with pytest.warns(
            DeprecationWarning,
            match=(
                r"dict\(\) argument to add_horizon\(\); use year= and "
                "firstmodelyear="
            ),
        ):
            scen.add_horizon(*args, **kwargs)
    else:
        scen.add_horizon(*args, **kwargs)

    # Sets and parameters have the expected contents
    npt.assert_array_equal(exp["year"], scen.set("year"))
    npt.assert_array_equal(exp["fmy"], scen.cat("year", "firstmodelyear"))
    npt.assert_array_equal(exp["dp"], scen.par("duration_period")["value"])

def test_add_horizon_repeat(test_mp, caplog):
    """add_horizon() does not handle scenarios with existing years."""
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")

    # Create a base scenario
    scen.add_horizon([2010, 2020, 2030])
    npt.assert_array_equal([10, 10, 10], scen.par("duration_period")["value"])

    with pytest.raises(
        ValueError,
        match=r"Scenario has year=\[2010, 2020, 2030\] and related values",
    ):
        scen.add_horizon([2015, 2020, 2025], firstmodelyear=2010)

def test_cat_all(dantzig_message_scenario):
    scen = dantzig_message_scenario
    df = scen.cat("technology", "all")
    npt.assert_array_equal(
        df, ["canning_plant", "transport_from_seattle", "transport_from_san-diego"]
    )

def test_cat_list(test_mp):
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")

    # cat_list() returns default 'year' categories in a new message_ix.Scenario
    exp = ["firstmodelyear", "lastmodelyear", "initializeyear_macro"]
    assert exp == scen.cat_list("year")

def test_add_cat(dantzig_message_scenario):
    scen = dantzig_message_scenario
    scen2 = scen.clone(keep_solution=False)
    scen2.check_out()
    scen2.add_cat(
        "technology", "trade", ["transport_from_san-diego", "transport_from_seattle"]
    )
    df = scen2.cat("technology", "trade")
    npt.assert_array_equal(df, ["transport_from_san-diego", "transport_from_seattle"])
    scen2.discard_changes()

def test_add_cat_unique(message_test_mp):
    scen = Scenario(message_test_mp, **SCENARIO["dantzig multi-year"])
    scen2 = scen.clone(keep_solution=False)
    scen2.check_out()
    scen2.add_cat("year", "firstmodelyear", 1963, True)
    assert [1963] == scen2.cat("year", "firstmodelyear")

def test_years_active(test_mp):
    test_mp.add_unit("year")
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")
    scen.add_set("node", "foo")
    scen.add_set("technology", "bar")

    # Periods of uneven length
    years = [1990, 1995, 2000, 2005, 2010, 2020, 2030]

    # First period length is immaterial
    duration = [1900, 5, 5, 5, 5, 10, 10]
    scen.add_horizon(year=years, firstmodelyear=years[-1])
    scen.add_par(
        "duration_period", pd.DataFrame(zip(years, duration), columns=["year", "value"])
    )

    # 'bar' built in period '1995' with 25-year lifetime:
    # - is constructed in 1991-01-01.
    # - by 1995-12-31, has operated 5 years.
    # - operates until 2015-12-31. This is within the period '2020'.
    scen.add_par(
        "technical_lifetime",
        pd.DataFrame(
            dict(
                node_loc="foo",
                technology="bar",
                unit="year",
                value=25,
                year_vtg=years[1],
            ),
            index=[0],
        ),
    )

    result = scen.years_active("foo", "bar", years[1])

    # Correct return type
    assert isinstance(result, list)
    assert isinstance(result[0], int)

    # Years 1995 through 2020
    npt.assert_array_equal(result, years[1:-1])

def test_years_active_extend(message_test_mp):
    scen = Scenario(message_test_mp, **SCENARIO["dantzig multi-year"])

    # Existing time horizon
    years = [1963, 1964, 1965]
    result = scen.years_active("seattle", "canning_plant", years[1])
    npt.assert_array_equal(result, years[1:])

    # Add years to the scenario
    years.extend([1993, 1995])
    scen.check_out()
    scen.add_set("year", years[-2:])
    scen.add_par("duration_period", "1993", 28, "y")
    scen.add_par("duration_period", "1995", 2, "y")

    # technical_lifetime of seattle/canning_plant/1964 is 30 years.
    # - constructed in 1964-01-01.
    # - by 1964-12-31, has operated 1 year.
    # - by 1965-12-31, has operated 2 years.
    # - operates until 1993-12-31.
    # - is NOT active within the period '1995' (1994-01-01 to 1995-12-31)
    result = scen.years_active("seattle", "canning_plant", 1964)
    npt.assert_array_equal(result, years[1:-1])

def test_years_active_extended2(test_mp):
    test_mp.add_unit("year")
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")
    scen.add_set("node", "foo")
    scen.add_set("technology", "bar")

    # Periods of uneven length
    years = [1990, 1995, 2000, 2005, 2010, 2020, 2030]

    # First period length is immaterial
    duration = [1900, 5, 5, 5, 5, 10, 10]
    scen.add_horizon(year=years, firstmodelyear=years[-1])
    scen.add_par(
        "duration_period", pd.DataFrame(zip(years, duration), columns=["year", "value"])
    )

    # 'bar' built in period '2020' with 10-year lifetime:
    # - is constructed in 2011-01-01.
    # - by 2020-12-31, has operated 10 years.
    # - operates until 2020-12-31. This is within the period '2020'.
    # The test ensures that the correct lifetime value is retrieved,
    # i.e. the lifetime for the vintage 2020.
    scen.add_par(
        "technical_lifetime",
        pd.DataFrame(
            dict(
                node_loc="foo",
                technology="bar",
                unit="year",
                value=[20, 20, 20, 20, 20, 10, 10],
                year_vtg=years,
            ),
        ),
    )

    result = scen.years_active("foo", "bar", years[-2])

    # Correct return type
    assert isinstance(result, list)
    assert isinstance(result[0], int)

    # Years 2020
    npt.assert_array_equal(result, years[-2])

def test_years_active_extend3(test_mp):
    test_mp.add_unit("year")
    scen = Scenario(test_mp, **SCENARIO["dantzig"], version="new")
    scen.add_set("node", "foo")
    scen.add_set("technology", "bar")

    # Periods of uneven length
    years = [1990, 1995, 2000, 2005, 2010, 2020, 2030]

    scen.add_horizon(year=years, firstmodelyear=2010)

    scen.add_set("year", [1992])
    scen.add_par("duration_period", "1992", 2, "y")
    scen.add_par("duration_period", "1995", 3, "y")

    scen.add_par(
        "technical_lifetime",
        pd.DataFrame(
            dict(
                node_loc="foo",
                technology="bar",
                unit="year",
                value=[20],
                year_vtg=1990,
            ),
        ),
    )

    obs = scen.years_active("foo", "bar", 1990)

    assert obs == [1990, 1992, 1995, 2000, 2005]

def test_new_timeseries_long_name64(message_test_mp):
    scen = Scenario(message_test_mp, **SCENARIO["dantzig multi-year"])
    scen = scen.clone(keep_solution=False)
    scen.check_out(timeseries_only=True)
    df = pd.DataFrame(
        {
            "region": [
                "India",
            ],
            "variable": [
                ("Emissions|CO2|Energy|Demand|Transportation|Aviation|" "Domestic|Fre"),
            ],
            "unit": [
                "Mt CO2/yr",
            ],
            "2012": [
                0.257009,
            ],
        }
    )
    scen.add_timeseries(df)
    scen.commit("importing a testing timeseries")

def test_new_timeseries_long_name64plus(message_test_mp):
    scen = Scenario(message_test_mp, **SCENARIO["dantzig multi-year"])
    scen = scen.clone(keep_solution=False)
    scen.check_out(timeseries_only=True)
    df = pd.DataFrame(
        {
            "region": [
                "India",
            ],
            "variable": [
                (
                    "Emissions|CO2|Energy|Demand|Transportation|Aviation|"
                    "Domestic|Freight|Oil"
                ),
            ],
            "unit": [
                "Mt CO2/yr",
            ],
            "2012": [
                0.257009,
            ],
        }
    )
    scen.add_timeseries(df)
    scen.commit("importing a testing timeseries")

def test_rename_technology(dantzig_message_scenario):
    scen = dantzig_message_scenario
    assert scen.par("output")["technology"].isin(["canning_plant"]).any()

    clone = scen.clone("foo", "bar")
    clone.rename("technology", {"canning_plant": "foo_bar"})
    assert not clone.par("output")["technology"].isin(["canning_plant"]).any()
    assert clone.par("output")["technology"].isin(["foo_bar"]).any()
    clone.solve(quiet=True)
    assert np.isclose(clone.var("OBJ")["lvl"], 153.675)

def test_rename_technology_no_rm(dantzig_message_scenario):
    scen = dantzig_message_scenario
    assert scen.par("output")["technology"].isin(["canning_plant"]).any()

    clone = scen.clone("foo", "bar")
    # also test if already checked out
    clone.check_out()

    clone.rename("technology", {"canning_plant": "foo_bar"}, keep=True)
    assert clone.par("output")["technology"].isin(["canning_plant"]).any()
    assert clone.par("output")["technology"].isin(["foo_bar"]).any()

def test_excel_read_write(message_test_mp, tmp_path):
    # Path to temporary file
    tmp_path /= "excel_read_write.xlsx"
    # Convert to string to ensure this can be handled
    fname = str(tmp_path)

    scen1 = Scenario(message_test_mp, **SCENARIO["dantzig"])
    scen1 = scen1.clone(keep_solution=False)
    scen1.check_out()
    scen1.init_set("new_set")
    scen1.add_set("new_set", "member")
    scen1.init_par("new_par", idx_sets=["new_set"])
    scen1.add_par("new_par", "member", 2, "-")
    scen1.commit("new set and parameter added.")

    # Writing to Excel without solving
    scen1.to_excel(fname)

    # Writing to Excel when scenario has a solution
    scen1.solve(quiet=True)
    scen1.to_excel(fname)

    scen2 = Scenario(message_test_mp, model="foo", scenario="bar", version="new")

    # Fails without init_items=True
    with pytest.raises(ValueError, match="no set 'new_set'"):
        scen2.read_excel(fname)

    # Succeeds with init_items=True
    scen2.read_excel(fname, init_items=True, commit_steps=True)

    exp = scen1.par("input")
    obs = scen2.par("input")
    pdt.assert_frame_equal(exp, obs)

    assert scen2.has_par("new_par")
    assert 2 == scen2.par("new_par").at[0, "value"]

    scen2.solve(quiet=True)
    assert np.isclose(scen2.var("OBJ")["lvl"], scen1.var("OBJ")["lvl"])

def test_clone(tmpdir):
    # Two local platforms
    mp1 = ixmp.Platform(driver="hsqldb", path=tmpdir / "mp1")
    mp2 = ixmp.Platform(driver="hsqldb", path=tmpdir / "mp2")

    # A minimal scenario
    scen1 = Scenario(mp1, model="model", scenario="scenario", version="new")
    scen1.add_spatial_sets({"country": "Austria"})
    scen1.add_set("technology", "bar")
    scen1.add_horizon(year=[2010, 2020])
    scen1.commit("add minimal sets for testing")

    assert len(mp1.scenario_list(default=False)) == 1

    # Clone
    scen2 = scen1.clone(platform=mp2)

    # Return type of ixmp.Scenario.clone is message_ix.Scenario
    assert isinstance(scen2, Scenario)

    # Close and re-open both databases
    mp1.close_db()  # TODO this should be done automatically on del
    mp2.close_db()  # TODO this should be done automatically on del
    del mp1, mp2
    mp1 = ixmp.Platform(driver="hsqldb", path=tmpdir / "mp1")
    mp2 = ixmp.Platform(driver="hsqldb", path=tmpdir / "mp2")

    # Same scenarios present in each database
    assert all(mp1.scenario_list(default=False) == mp2.scenario_list(default=False))

    # Load both scenarios
    scen1 = Scenario(mp1, "model", "scenario")
    scen2 = Scenario(mp2, "model", "scenario")

    # Contents are identical
    assert all(scen1.set("node") == scen2.set("node"))
    assert all(scen1.set("year") == scen2.set("year"))


from slackclient import SlackClient
from kubernetes import client, config,watch
import argparse
import json
from datetime import date, datetime, timezone

slack_token = "YOUR TOKEN"
sc = SlackClient(slack_token)

gbl_channel=""

def build_arg_parser():
    """
    Builds a standard argument parser with arguments for talking to vCenter
    -s service_host_name_or_ip
    -o optional_port_number
    -u required_user
    -p optional_password
    """
    parser = argparse.ArgumentParser(
        description='Standard Arguments for talking to k8s')

    parser.add_argument('-k', '--ctxt',
                        required=True,
                        action='store',
                        help='Set context kubernetes')

    parser.add_argument('-e', '--only_error',
                        required=False,
                        action='store_true',
                        help='Enable error check')

    parser.add_argument('-c', '--channel',
                        required=True,
                        action='store',
                        help='Channel name to use when sending to slack')

    return parser

def get_args():
    """
    Supports the command-line arguments needed to form a connection to vSphere.
    """
    parser = build_arg_parser()

    args = parser.parse_args()

    return (args)

def sc_call():
    sc.api_call(
      "chat.postMessage",
      channel="#kubedev_chan",
      text="Hello from Python! :tada:"
    )

def send_slack_msg(chan,ico,text):

    sc.api_call("chat.postMessage",
    channel=chan,
    icon_emoji =ico,
    as_user=False,
    attachments= text)

def create_field(my_list, title, value, short=True):
    if value != None:
        if (  isinstance(value, dict)):
            dictstr = '\n'.join('{} : {}'.format(key, value) for key, value in value.items())
            my_list.append( { "title" :title, "value" :dictstr, "short" : short})
        elif ( isinstance(value, datetime) ):
           my_list.append( { "title" :title, "value" :value.isoformat(), "short" : short})
        else:
            my_list.append( { "title" :title, "value" :value, "short" : short})

def extract_event_info(ctxt, event, mode='good'):
    global gbl_channel
    my_list=[]

    create_field(my_list, "namespaces", event.metadata.namespace)
    create_field(my_list, "name", event.metadata.name)
    create_field(my_list, "labels", event.metadata.labels)
    create_field(my_list, "status.message", str(event.status.container_statuses[0].state))
    create_field(my_list, "status.start_time", event.status.start_time)
    create_field(my_list, "status.phase", event.status.phase)
    c = datetime.now(timezone.utc) - event.status.start_time
    create_field(my_list, "Runing since", str(c))
    text=[
    {

      "color" : mode,
      "pretext" : "Just from information from k8s" + ctxt ,
      "fields" : my_list
    }
    ]

    send_slack_msg(gbl_channel, ":k8s:", text)

def main():
    global gbl_channel
    args = get_args()
    gbl_channel=args.channel
    contexts, active_context = config.list_kube_config_contexts()
    if not contexts:
        print("Cannot find any context in kube-config file.")
        return

    client1 = client.CoreV1Api(
        api_client=config.new_client_from_config(context=args.ctxt))

    only_error = args.only_error

    for event in client1.list_pod_for_all_namespaces(watch=False).items:
        if only_error == True :
            for i in event.status.container_statuses :
                if(i.ready == False and i.restart_count != 0):
                    extract_event_info(args.ctxt, event, "danger")
            #if (event.status.phase != "Running" and event.status.phase != "Succeeded"):
            #    extract_event_info(args.ctxt, event, "danger")
        else:
            extract_event_info(args.ctxt, event )

if __name__ == '__main__':
    main()


import os
from datetime import date
from maya import cmds

today = date.today()
ws = cmds.workspace( q=True, rootDirectory=True )
current_date = today.strftime("%Y%m%d")
file_path_abs = cmds.file(q=True, sn=True)
file_path_rlt = file_path_abs.replace(ws, "")
asset_name = file_path_rlt.split("/")[-1].split("_")[1]
print(asset_name)
full_path = "/".join([ws, "images", "dailyScreenShot", asset_name, current_date])
print(full_path)
if not os.path.exists(full_path):
    os.makedirs(full_path)

class ImgName():
    i = 0
    def __call__(self, name):
        self.i += 1
        return '{:02}_{}'.format(self.i, name)
In = ImgName()

def take_screenShot(name, width=1920, height=1080):
    img_path = "/".join([full_path, name + ".jpg"])
    cmds.playblast(fr=0, v=False, fmt="image", c="jpg", orn=False, cf=img_path, wh=[width,height], p=100)

def switch_light(on=True):
    currentPanel = cmds.getPanel(withFocus=True)

    if cmds.getPanel(to=currentPanel) == "modelPanel":
        if on:
            cmds.modelEditor(currentPanel, e=True, dl="default")
        else:
            cmds.modelEditor(currentPanel, e=True, dl="none")

def zoom(v):
    currentPanel = cmds.getPanel(withFocus=True)

    if cmds.getPanel(to=currentPanel) == "modelPanel":
        curCamera=cmds.modelEditor(currentPanel,q=1,av=1,cam=1)
        cmds.xform(curCamera, rt=[0, 0, v], r=True, os=True)

def fit():
    if cmds.ls("GEO"):
        cmds.select(cmds.ls("GEO"), r=True)
    else:
        cmds.select(cmds.listRelatives(cmds.ls(geometry=True), p=True, path=True), r=True)
    cmds.viewFit()
    cmds.select(cl=True)

def isolateSelected(state):
    viewport = cmds.getPanel( withFocus = True)
    if cmds.ls("GEO"):
        cmds.select(cmds.ls("GEO"), r=True)
    else:
        cmds.select(cmds.listRelatives(cmds.ls(geometry=True), p=True, path=True), r=True)
    if 'modelPanel' in viewport:
        if state:
            cmds.isolateSelect( viewport, addSelected=True )
            cmds.isolateSelect( viewport, s=True )
            cmds.isolateSelect( viewport, addSelected=True )
            cmds.isolateSelect( viewport, s=True )
        else:
            cmds.isolateSelect( viewport, s=False )
    cmds.select(cl=True)

def wireframe_switcher(state=None):
    viewport = cmds.getPanel( withFocus = True)
    if 'modelPanel' in viewport:

        currentState = cmds.modelEditor( viewport, q = True, wireframeOnShaded = True) if state == None else not state
        if currentState:
            cmds.modelEditor( viewport, edit = True, wireframeOnShaded = False)
        else:
            cmds.modelEditor( viewport, edit = True, wireframeOnShaded = True)

old_sl = cmds.ls(sl=True)
current_view = cmds.lookThru(q=True)
cmds.lookThru('persp')
isolateSelected(True)

# fit()
# zoom(-20)
wireframe_switcher(False)
switch_light(True)

take_screenShot(In("default"))

switch_light(False)
take_screenShot(In("shadow"))
switch_light(True)

wireframe_switcher(True)
take_screenShot(In("wireframe"))
wireframe_switcher(False)

# zoom(20)

cmds.lookThru('front')
fit()
# cmds.dolly( 'front', os=0.6666 )
take_screenShot(In("front"))
# cmds.dolly( 'front', os=1.5 )
cmds.hotkey( 'z', query=True, alt=True )

cmds.lookThru('side')
fit()

# cmds.dolly( 'side', os=0.6666 )
take_screenShot(In("side"))
cmds.hotkey( 'z', query=True, alt=True )

# cmds.dolly( 'side', os=1.5 )
cmds.lookThru(current_view)
cmds.select(old_sl)
isolateSelected(False)


import locale
from tkinter import font as tk_font

import PySimpleGUI as sg
from tracking.tracker import TrackerState

from . import popup
from .events import Events, Keys
from .localization import TXT
from .theme import TH
from .tracker_widget import TrackerWidget

# locale parser info for printing date
locale.setlocale(locale.LC_TIME, TXT.locale_setting)  # date in correct language

class TrackerWidgetsHandler:
    def __init__(self, window, trackers, splash):
        self.widgets = []
        self.trackers = trackers

        self.widgets_frame = window[Keys.all_tracker_widgets]
        self.old_trackers = window[Keys.old_tracker_widgets]
        self.new_trackers = window[Keys.new_tracker_widgets]
        self.widget_menu = window[Keys.menu]
        self.archives_button = window[Events.archives]
        self.refresh_button = window[Events.refresh]
        self.deleted_button = window[Events.trash]
        self.its_empty = window[Keys.its_empty]

        self.archives_updated()
        self.deleted_updated()

        n_trackers = len(trackers.trackers)
        for i, tracker in enumerate(trackers.trackers):
            splash.update(f"{TXT.tracker_creation} {i + 1}/{n_trackers}")
            self._create_widget(window, tracker, new=False)

        self.update_window_size(window)
        self.recenter(window, True)

    def _create_widget(self, window, tracker, new=False):
        widget = TrackerWidget(tracker)
        self.widgets.append(widget)

        where = self.new_trackers if new else self.old_trackers
        window.extend_layout(where, widget.create_layout())

        # finalize only shown trackers to speed up startup
        if widget.tracker.state == TrackerState.shown:
            widget.finalize(window)
            widget.update(window)

    def new(self, window):
        popup_edit = popup.Edit(
            TXT.new, "", TXT.new, [], self.trackers.couriers_handler, window
        )
        ok, *tracker_params = popup_edit.loop()
        if ok:
            tracker = self.trackers.new(*tracker_params)
            self._create_widget(window, tracker, new=True)

    def _get_widgets_with_state(self, state):
        return [widget for widget in self.widgets if widget.tracker.state == state]

    def _get_sorted(self, widgets):
        return self.trackers.sort(widgets, get_tracker=lambda widget: widget.tracker)

    def _choose(self, window, title, state, ok_name, added_button=None):
        widgets = self._get_sorted(self._get_widgets_with_state(state))
        w_desc = (
            max(len(widget.get_description()) for widget in widgets) if widgets else 0
        )
        w_date = (
            max(len(widget.get_creation_date()) for widget in widgets) if widgets else 0
        )

        choices = []
        for widget in widgets:
            color = TH.ok_color if widget.get_delivered() else TH.warn_color
            date = f"{widget.get_creation_date()},".ljust(w_date + 1)
            txt = f"{date} {widget.get_description().ljust(w_desc)} - {widget.get_idship()}"
            choices.append((txt, color))

        popup_choices = popup.Choices(choices, title, window, ok_name, added_button)
        exit_result, chosen = popup_choices.loop()
        return exit_result, [widgets[i] for i in chosen]

    def show_archives(self, window):
        exit_result, chosen = self._choose(
            window, TXT.unarchive, TrackerState.archived, ok_name=TXT.unarchive
        )
        if exit_result:
            for widget in chosen:
                widget.unarchive(window)

    def show_deleted(self, window):
        def_delete_button_key = "-definitly delete-"
        def_delete_button = dict(
            txt=TXT.delete_definitly,
            mouse_over_color=TH.warn_color,
            key=def_delete_button_key,
        )
        exit_result, chosen = self._choose(
            window,
            TXT.restore,
            TrackerState.deleted,
            ok_name=TXT.restore,
            added_button=def_delete_button,
        )
        if exit_result == def_delete_button_key and chosen:
            w_desc = max(len(widget.get_description()) for widget in chosen)
            txt = "\n".join(
                f"{widget.get_description().ljust(w_desc)} - {widget.get_idship()}"
                for widget in chosen
            )
            popup_warn = popup.AskConfirmation(
                TXT.delete_definitly,
                txt,
                window,
            )
            definitly_delete = popup_warn.loop()
            if definitly_delete:
                for widget in chosen:
                    widget.definitly_delete(window)

        else:
            for widget in chosen:
                widget.undelete(window)

    def archives_updated(self):
        n_archives = self.trackers.count_state(TrackerState.archived)
        color = TH.archives_color if n_archives else TH.archives_color_empty
        self.archives_button.update(
            f"{TXT.archives}({n_archives})", button_color=(color, None)
        )

    def deleted_updated(self):
        n_deleted = self.trackers.count_state(TrackerState.deleted)
        color = TH.trash_color if n_deleted else TH.trash_color_empty
        self.deleted_button.update(
            f"{TXT.trash}({n_deleted})", button_color=(color, None)
        )

    def update(self, window):
        for widget in self._get_widgets_with_state(TrackerState.shown):
            widget.update(window)

    def _count_has_something_to_update(self):
        shown = self._get_widgets_with_state(TrackerState.shown)
        return [widget.has_something_to_update for widget in shown].count(True)

    def updating_changed(self):
        n_has_something_to_update = self._count_has_something_to_update()
        self.refresh_button.update(disabled=n_has_something_to_update == 0)

    def _set_min_width(self, min_width):
        for widget in self._get_widgets_with_state(TrackerState.shown):
            widget.set_min_width(min_width)

    def update_window_size(self, window):
        shown = self._get_widgets_with_state(TrackerState.shown)

        # needed to get the actual sizes
        window.refresh()  # or visibility_changed() that produces different glitches

        self.widgets_frame.contents_changed()
        self.its_empty.update(visible=not shown)

        menu_w = self.widget_menu.Widget.winfo_reqwidth()
        menu_h = self.widget_menu.Widget.winfo_reqheight()
        self._set_min_width(menu_w)

        # wanted size
        scrollbar = self.widgets_frame.vsb
        if shown:
            w = max(widget.get_pixel_width() for widget in shown)
            h = sum(widget.get_pixel_height() for widget in self.widgets) + menu_h + 5

            # need a scrollbar ?
            screen_w, screen_h = window.get_screen_size()
            max_h = screen_h - TH.window_height_screen_margin

            if h > max_h:
                scrollbar.pack(side=sg.tk.RIGHT, fill="y")
                w += scrollbar.winfo_width()

            else:
                scrollbar.pack_forget()

            window.size = min(w, screen_w), min(h, max_h)
            self.recenter(window)

        else:
            scrollbar.pack_forget()

            # needed to set height because the scrollbar missing prevents the right height computation in pySimpleGUI
            window.size = (
                menu_w,
                menu_h
                + self.its_empty.Widget.winfo_reqheight()
                + self.its_empty.Pad[1] * 2,
            )

            # add spaces in its_empty to fit w
            wfont = tk_font.Font(self.its_empty.ParentForm.TKroot, self.its_empty.Font)
            n_spaces = round(menu_w / wfont.measure(" "))
            self.its_empty.update(TXT.empty.center(n_spaces))

    @staticmethod
    def recenter(window, force=False):
        W, H = window.get_screen_size()
        w, h = window.size
        x, y = window.current_location()
        if force:
            x = max(0, int((W - w) * 0.5))
            y = max(0, int((H - h) * 0.5))
        else:
            y = max(0, int((H - h) * 0.5)) if y + h > H else y
        window.move(x, y)


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

# In[1]:

import pandas as pd
import numpy as np
import csv
import json
import urllib.parse
from statistics import mean
import networkx as nx

# In[2]:

i=1
reader=open("paths_finished.tsv")
paths = csv.reader(reader, delimiter="\t")
paths_finished=[]
for row in paths:
    if(i>16):
        l=row[3].split(';')
        s=urllib.parse.unquote(l[0])
        d=urllib.parse.unquote(l[-1])
        paths_finished.append([s,d])
    i+=1
#paths_finished

# In[3]:

with open("articleAllCategory.json", "r") as f:
    artAllCat = json.load(f)
#artAllCat

# In[4]:

with open("human_path_len.json", "r") as f:
    total_len = json.load(f)
#total_len

# In[5]:

with open("short_path.json", "r") as f:
    short_path = json.load(f)
#short_path

# In[6]:

with open("back_clicks.json", "r") as f:
    back_clicks = json.load(f)
#back_clicks

# In[7]:

finish_cat_pair={}
x=1
for ele in paths_finished:
    s_cat=artAllCat[ele[0]]
    d_cat=artAllCat[ele[1]]
    human_path=total_len[str(x)]-2*back_clicks[str(x)]
    ratio=human_path/short_path[str(x)]
    x+=1
    all_permutation=[[i,j] for i in s_cat for j in d_cat]
    for per in all_permutation:
        finish_cat_pair[tuple(per)]=finish_cat_pair.get(tuple(per),[])
        finish_cat_pair[tuple(per)].append(ratio)
#finish_cat_pair

# In[8]:

finish_list=[]
for (key,val) in finish_cat_pair.items():
    avg=mean(val)
    finish_list.append([key[0],key[1],avg])
#finish_list

# In[9]:

finish_list.sort()

# In[10]:

df=pd.DataFrame(finish_list,columns=['From_Category','To_Category','Ratio_of_human_to_shortest'])

# In[11]:

df.to_csv("category-ratios.csv",index=False)

# In[ ]:


#!/usr/bin/env python3
"""
Handy utility to reformat some data for use in a d3js chart.
  read input xls data file
  find final averages sheet
  allow for different years
  find the rows we want
  write intermediate per-year csv files??
  get maxaverages, do scaling
  write the yearly jsons
"""

__author__ = "Richard Leir"
__copyright__ = "Copyright 2018, Richard Leir"
__credits__ = ["Kathy Conlan", "Mike Bostock"]
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Richard Leir"
__email__ = "rleir at leirtech ddot com"
__status__ = "Production"

import csv
import json
from xlrd import open_workbook # type: ignore
from importdata import chart_by_mapsite
from typing import Dict, List

# The input spreadsheet might contain more species than we want to display.
# Here is the way to indicate which species we want to display:
featured_species = {} # type: Dict[ str, str]

all_years = [ ] # type: List[ str]

# Site name keys below match the column headers in the input xlsx
site_keys = [] # type: List[ str]

species_keys = []

all_data   = {} # type: Dict
max_average   = {} # type: Dict

def initData() -> None:
    global all_years
    with open("data/allyears.json") as json_file:
        all_years= json.load( json_file)

    global featured_species
    global species_keys
    with open("data/featured_species.json") as json_file:
        featured_species = json.load( json_file)
    species_keys = list(featured_species.keys())

    positions = [ ] # type: List[ Dict[ str,str]]
    with open("data/positions.json") as json_file:
        positions= json.load( json_file)
    for position in positions:
        site_keys.append(position['placeName'])

    for year in all_years:
        all_data[year] = {}

        for site_key in site_keys:
            all_data[year][site_key] = []
            all_data[year][site_key].append({})
            all_data[year][site_key][0]["data"   ]={}
            all_data[year][site_key][0]["average"]={}
            for species in species_keys:
                abbr = featured_species[species]
                all_data[year][site_key][0]["data"][abbr]=0
                all_data[year][site_key][0]["average"][abbr]=-10

def readFiles() -> None:
        # read xls, find avg sheet
        path = "data/SpeciesCounts.xlsx"
        wb = open_workbook(path)
        final_av_s_found = 0
        for sheet in wb.sheets():
            shname = sheet.name
            if shname.endswith("ave"):
                final_av_s_found = 1
                coltoyear=[] # type: List[ str]
                maxcol = 0
                for row in range(sheet.nrows):
                    if row == 0:
                        for col in range(sheet.ncols):
                            if "MAX" == sheet.cell(row,col).value:
                                maxcol = col
                    elif row == 1:
                        coltoyear = readheader(sheet,row)
                    else:
                        for col in range(sheet.ncols):
                            if col == 0:
                                species = sheet.cell(row,col).value
                                if species not in featured_species:
                                    continue
                                else:
                                    abbr = featured_species[species]
                                    for datacol in range(sheet.ncols):
                                        colval = coltoyear[datacol]
                                        if maxcol == datacol:
                                            max_average[abbr]= sheet.cell(row,datacol).value

                                        if colval == "MAXAVG":
                                            max_average[abbr]= sheet.cell(row,datacol).value

                                        elif datacol > 0 and colval != "not ave col":
                                            year = coltoyear[datacol][-4:]
                                            site = coltoyear[datacol][:-4]
                                            all_data[year][site][0]["average"][abbr] = sheet.cell(row,datacol).value
        if final_av_s_found == 0 :
            print("final averages sheet not found in " + path)

def scaleData() -> None:
    running_count = {}
    check_max_average   = {}

    for species_key in species_keys:
        abbr = featured_species[species_key]
        check_max_average  [abbr] = 0
        running_count[abbr] = 0

    # find the max average for each species
    for year in all_years:
        for site_key in site_keys:
            for species_key in species_keys:
                abbr = featured_species[species_key]
                replicates_average = all_data[year][site_key][0]["average"][abbr]
                if  check_max_average[ abbr] < replicates_average :
                    check_max_average[  abbr] = replicates_average
                    running_count[abbr] += 1

    for species_key in species_keys:
        abbr = featured_species[species_key]
        if check_max_average[abbr] <= 0 :
            print("error, running count 0 " + abbr)
        if running_count[abbr] <= 0 :
            print("error, running count 0 " + abbr)

    # scale all the data in the range 0 to 100 where 100 represents the max average
    for year in all_years:
        for site_key in site_keys:
            for species_key in species_keys:
                abbr = featured_species[species_key]
                replicates_average = all_data[year][site_key][0]["average"][abbr]

                if max_average[  abbr] != check_max_average[  abbr]:
                    print("max_avg="+ str(max_average[  abbr]) + " check_max=" + str(check_max_average[  abbr]) + " species=" + abbr)
                max_avg = check_max_average[  abbr]

                display = -10.0 # should be 0, but -10 is a flag saying 'no data'
                #display = 1.0 # should be 0, but 1 avoids a bug in the tweening
                if replicates_average > -10 :
                    #display = replicates_average*100 / (max_avg+0.00001)
                    display = replicates_average*100 / max_avg
                all_data[year][site_key][0]["data"][abbr] = display

def writeFiles() -> None:
    for year in all_years:
        # write output
        output_file = "data/all_sites_" + str(year) + ".csv"

        with open(output_file, 'w', newline='') as csvfile:
            fieldnames = ["organism","abbr"] + site_keys
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()
            for species_key in species_keys:
                abbr = featured_species[ species_key]
                row_data = {'organism': species_key, 'abbr': abbr}
                for site_key in site_keys:
                    row_data[site_key] =        all_data[year][site_key][0]["data"][abbr]
                writer.writerow(row_data)

def readheader(sheet,row) -> List[str]:
    coltoyear=[]
    for col in range(sheet.ncols):
        colheader = sheet.cell(row,col).value
        if colheader == "MAX":
            coltoyear.append("MAXAVG")
        elif not colheader.endswith("-ave"):
            coltoyear.append("not ave col")

        else:
                siten =  colheader[:-6]
                year = colheader[-6:-4]
                if int(year) > 80:
                    year = "19"+year
                else:
                    year = "20"+year
                coltoyear.append(siten+year)
    return coltoyear

if __name__ == "__main__":
    # execute only if run as a script

    initData()
    readFiles()
    scaleData()
    writeFiles()

    for year in all_years:
        chart_by_mapsite(year)


import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

args = getResolvedOptions(sys.argv, ["JOB_NAME"])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args["JOB_NAME"], args)

# Script generated for node Amazon S3
AmazonS3_node1703098546880 = glueContext.create_dynamic_frame.from_options(
    format_options={
        "quoteChar": '"',
        "withHeader": True,
        "separator": ",",
        "multiline": False,
        "optimizePerformance": False,
    },
    connection_type="s3",
    format="csv",
    connection_options={"paths": ["s3://sbhujbal123/raw/"], "recurse": True},
    transformation_ctx="AmazonS3_node1703098546880",
)

# Script generated for node Change Schema
ChangeSchema_node1703099461870 = ApplyMapping.apply(
    frame=AmazonS3_node1703098546880,
    mappings=[
        ("Beverage_category", "string", "Beverage_category", "string"),
        ("Beverage", "string", "Beverage", "string"),
        ("Beverage_prep", "string", "Beverage_prep", "string"),
        ("Calories", "string", "Calories", "bigint"),
        ("`Total Fat (g)`", "string", "Total_Fat_g", "decimal"),
        ("`Trans Fat (g)`", "string", "Trans_Fat_g", "decimal"),
        ("`Saturated Fat (g)`", "string", "Saturated_Fat_g", "decimal"),
        ("`Sodium (mg)`", "string", "Sodium_mg", "decimal"),
        ("`Total Carbohydrates (g)`", "string", "Total_Carbohydrates_g", "bigint"),
        ("`Cholesterol (mg)`", "string", "Cholesterol_mg", "bigint"),
        ("`Dietary Fibre (g)`", "string", "Dietary_Fibre_g", "decimal"),
        ("`Sugars (g)`", "string", "Sugars_g", "bigint"),
        ("`Protein (g)`", "string", "Protein_g", "string"),
        ("`Vitamin A (% DV)`", "string", "Vitamin_A_perdv", "decimal"),
        ("`Vitamin C (% DV)`", "string", "Vitamin_C_perdv ", "decimal"),
        ("`Calcium (% DV)`", "string", "Calcium__perdv", "string"),
        ("`Iron (% DV)`", "string", "Iron_perdv", "decimal"),
        ("`Caffeine (mg)`", "string", "Caffeine_mg", "decimal"),
    ],
    transformation_ctx="ChangeSchema_node1703099461870",
)

# Script generated for node Amazon S3
AmazonS3_node1703098733075 = glueContext.getSink(
    path="s3://sbhujbal123/curated/",
    connection_type="s3",
    updateBehavior="LOG",
    partitionKeys=[],
    compression="snappy",
    enableUpdateCatalog=True,
    transformation_ctx="AmazonS3_node1703098733075",
)
AmazonS3_node1703098733075.setCatalogInfo(
    catalogDatabase="sundb", catalogTableName="sb_nutrition"
)
AmazonS3_node1703098733075.setFormat("glueparquet")
AmazonS3_node1703098733075.writeFrame(ChangeSchema_node1703099461870)
job.commit()


import json
import boto3

#遍历所有EC2 Instance，返回instance_id
def ec2():
    ec2instance = boto3.resource('ec2').instances.all()
    ec2_list = []
    for i in ec2instance:
        ec2_list.append(i.instance_id)
    print ("There are " + str(len(ec2_list)) + " EC2 instances")
    return ec2_list

#遍历所有RDS Instance，返回DBInstanceIdentifier
def rds():
    rdsinstance = boto3.client("rds").describe_db_instances()
    print ("There are " + str(len(list(rdsinstance))-1) + " databases")
    for i in range (0, len(list(rdsinstance))):
        print ("DB instance " + str(i) , end=" ")
        if (list(rdsinstance)[i] == "DBInstances"):
            print ("is " + rdsinstance["DBInstances"][i]["Engine"] + ", " + rdsinstance["DBInstances"][i]["DBInstanceIdentifier"] + ", " + rdsinstance["DBInstances"][i]["DBInstanceArn"])
    return True


"""Style transfer reimplementation

- Get a pre-trained encoder
- Get a style image
- Get a content image
- Get a gaussian noise image
- Define style loss and content loss
- Optimize the noise image to lower that loss
"""
from collections import namedtuple

import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import models

class VGG16(nn.Module):
    def __init__(self):
        super(VGG16, self).__init__()
        self.pretrained = models.vgg16(pretrained=True).features
        self.pool_indices = [4, 9, 16, 23, 30]
        self.output = namedtuple(
            "Outputs", ["slice{}".format(idx) for idx in range(len(self.pool_indices))]
        )

    def forward(self, input_x):
        outputs = []
        h = input_x
        last_idx = 0
        for pool_idx in self.pool_indices:
            h = self.pretrained[last_idx:pool_idx](h)
            outputs.append(h)
            last_idx = pool_idx

        return self.output(*outputs)

def prepare_image(np_image):
    image = torch.FloatTensor(np_image) / 255
    image = image.permute(2, 0, 1).unsqueeze(0)
    mean = image.new_tensor([0.485, 0.456, 0.406]).view(-1, 1, 1)
    std = image.new_tensor([0.229, 0.224, 0.225]).view(-1, 1, 1)
    return (image - mean) / std

def get_npimage(image):
    mean = image.new_tensor([0.485, 0.456, 0.406]).view(-1, 1, 1)
    std = image.new_tensor([0.229, 0.224, 0.225]).view(-1, 1, 1)
    np_image = (image * std + mean) * 255.0
    np_image = np_image.clamp(0.0, 255.0)
    np_image = np_image.squeeze().permute(1, 2, 0).cpu().data.numpy()
    return np_image.astype(np.uint8)

def gram_matrix(features):
    """Construct the gram matrix for a feature map"""
    batch, channels, height, width = features.shape
    features = features.view(batch, channels, height * width)
    inner_prod = torch.bmm(features, features.transpose(1, 2))
    return inner_prod / (channels * height * width)

if __name__ == "__main__":

    content_weight = 1e3
    style_weight = 1e8

    # get encoder
    encoder = VGG16()
    encoder.eval()
    for each_param in encoder.parameters():
        each_param.requires_grad = False

    # get content & style images
    content_image = cv2.cvtColor(cv2.imread('datasets/content.jpg'), cv2.COLOR_BGR2RGB)
    content_image = prepare_image(content_image)
    style_image = cv2.cvtColor(cv2.imread('datasets/style_picasso.jpg'), cv2.COLOR_BGR2RGB)
    style_image = prepare_image(style_image)

    # random image to optimize
    image = torch.randn(*content_image.shape)
    criterion = nn.MSELoss()

    # use gpu
    encoder = encoder.cuda()
    content_image, style_image = content_image.cuda(), style_image.cuda()
    image = image.cuda()

    # optimization
    image = image.requires_grad_()
    optimizer = optim.Adam([image], lr=1e-3)

    style_output = encoder(style_image)
    style_grams = [gram_matrix(each) for each in style_output[:-2]]
    content_output = encoder(content_image)

    for idx in range(30001):
        output = encoder(image)

        grams = [gram_matrix(each) for each in output[:-2]]
        style_loss = 0
        for style1, style2 in zip(grams, style_grams):
            style_loss += criterion(style1, style2)

        content_loss = criterion(output[2], content_output[2])
        loss = content_weight * content_loss + style_weight * style_loss

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if idx % 1000 == 0:
            print(idx, loss.item(), style_loss.item(), content_loss.item())
            im = get_npimage(image)
            cv2.imwrite(f'logs/{idx}.png', cv2.cvtColor(im, cv2.COLOR_RGB2BGR))