text:
import os
from options.test_options import TestOptions
from models import create_model
from util.util import tensor2labelim, tensor2confidencemap
from models.sne_model import SNE
import torchvision.transforms as transforms
import torch
import numpy as np
import cv2 as cv
import traceback
import shutil

class dataset():
	def __init__(self):
		self.num_labels = 2

def load_images(path_to_sequence):
	res = [os.path.join(path_to_sequence, img) for img in os.listdir(path_to_sequence)]
	res.sort()
	return res

def get_cam_param(ins):
	if ins < 3:
		fx = 718.856
		ox = 607.1928
		oy = 185.2157
		bf = 387.5744
	elif ins == 3:
		fx = 721.5377
		ox = 609.5593
		oy = 172.854
		bf = 387.5744
	else:
		fx = 707.0912
		ox = 601.8873
		oy = 183.1104
		bf = 379.8145
	camParam = torch.tensor([[fx, 0.000000e+00, ox],
							 [0.000000e+00, fx, oy],
							 [0.000000e+00, 0.000000e+00, 1.000000e+00]], dtype=torch.float32)  # camera parameters
	return camParam, bf

if __name__ == '__main__':
	try:
		opt = TestOptions().parse()
		opt.num_threads = 1
		opt.batch_size = 1
		opt.serial_batches = True  # no shuffle
		opt.isTrain = False

		example_dataset = dataset()
		model = create_model(opt, example_dataset)
		model.setup(opt)
		model.eval()

		file_path = os.path.join(opt.data_path, opt.sequence, 'image_2')
		left_filenames = load_images(file_path)

		depth_path = os.path.join(opt.depth_path, opt.sequence)
		depth_filenames = load_images(depth_path)

		save_path = os.path.join(opt.save_path, opt.sequence)
		if os.path.exists(save_path):
			shutil.rmtree(save_path)
		os.mkdir(save_path)

		camParam, bf = get_cam_param(int(opt.sequence))
		# if you want to use your own data, please modify rgb_image, depth_image, camParam and use_size correspondingly.
		for i in range(len(left_filenames)):
			rgb_image = cv.cvtColor(cv.imread(left_filenames[i]), cv.COLOR_BGR2RGB)
			depth_image = cv.imread(depth_filenames[i], cv.IMREAD_ANYDEPTH)
			depth_image = depth_image.astype(np.float32)
			row = depth_image.shape[0]
			col = depth_image.shape[1]
			depth_image = np.true_divide(np.ones((row, col)), depth_image)
			depth_image = depth_image * bf * 256.0
			oriHeight, oriWidth, _ = rgb_image.shape
			oriSize = (oriWidth, oriHeight)

			# resize image to enable sizes divide 32
			use_size = (1248, 384)
			rgb_image = cv.resize(rgb_image, use_size)
			rgb_image = rgb_image.astype(np.float32) / 255

			# compute normal using SNE
			sne_model = SNE()

			normal = sne_model(torch.tensor(depth_image.astype(np.float32) / 1000), camParam)
			normal_image = normal.cpu().numpy()
			normal_image = np.transpose(normal_image, [1, 2, 0])
			# cv.imwrite(os.path.join('examples', 'normal.png'), cv.cvtColor(255*(1+normal_image)/2, cv.COLOR_RGB2BGR))
			normal_image = cv.resize(normal_image, use_size)

			rgb_image = transforms.ToTensor()(rgb_image).unsqueeze(dim=0)
			normal_image = transforms.ToTensor()(normal_image).unsqueeze(dim=0)

			with torch.no_grad():
				pred = model.netRoadSeg(rgb_image, normal_image)

				palet_file = 'datasets/palette.txt'
				impalette = list(np.genfromtxt(palet_file, dtype=np.uint8).reshape(3 * 256))
				# pred_img = tensor2labelim(pred, impalette)
				# pred_img = cv.resize(pred_img, oriSize)
				prob_map = tensor2confidencemap(pred)
				prob_map = cv.resize(prob_map, oriSize)
				# cv.imwrite(os.path.join('examples', 'pred.png'), pred_img)
				cv.imwrite(os.path.join(save_path, '{0:06}.png'.format(i)), prob_map)
				print('{} frames'.format(i))
	except:
		traceback.print_exc()

max_stars_repo_name: linpeisensh/SNE-RoadSeg

max_issues_repo_name: linpeisensh/SNE-RoadSeg

max_forks_repo_name: linpeisensh/SNE-RoadSeg


text:
"""
    Файл, содержащий функции для развертки фазы сигнала
"""

import numpy as np
from numpy import pi
from time import time
import operator
from progress.bar import IncrementalBar
from insar.auxiliary import min_span_transitions_matrix_cell, pi_mult

def path_independence_test(source_matrix: np.ndarray) -> bool:
    """
    функция проверки на возможность применения зависимых от пути развёртывания алгоритмов
    :param source_matrix: исходная матрица
    :return: true - в случае если нет зависимости от пути развёртывания
            false - в случае если есть зависимость от пути развёртывания
    """
    der_x_y = np.diff(source_matrix, axis=1)
    der_x_y = np.diff(der_x_y, axis=0)
    der_y_x = np.diff(source_matrix, axis=0)
    der_y_x = np.diff(der_y_x, axis=1)
    return (der_x_y == der_y_x).all()

def itohs_unwrapping(source_array: np.ndarray) -> np.ndarray:
    """
    функция одномерной развёртки фазы
    :param source_array: исходный массив
    """
    unwrapped = source_array.copy()
    if len(source_array.shape) > 1:
        rows, columns = source_array.shape
        unwrapped = np.ravel(unwrapped)
    phase_difference = unwrapped[1:] - unwrapped[:-1]
    phase_difference = np.arctan2(np.sin(phase_difference), np.cos(phase_difference))
    phase_difference[0] += unwrapped[0]
    unwrapped[1:] = phase_difference.cumsum()
    if len(source_array.shape) > 1:
        unwrapped = np.reshape(unwrapped, (rows, columns))
    return unwrapped

def td_itohs_unwrapping(wrapped_phase: np.ndarray) -> np.ndarray:
    """
    Функция двумерной развёртки фазы.
    Алгоритм является зависящим от пути (path-dependence)
    Применяется, если справедливы предположения:
        - в абсолютной фазе между значениями в соседних ячейках нет разности по модулю большей пи
        - смешанные производные исходной матрицы равны
    :param source_array: двумерная матрица с исходной фазой
    :return: двумерная матрица, содержащая абсолютую фазу
    """
    unwrapped = wrapped_phase.copy()
    unwrapped[:, 0] = itohs_unwrapping(wrapped_phase[:, 0])
    for row in range(1, unwrapped.shape[0]):
        unwrapped[row, :] = itohs_unwrapping(unwrapped[row, :])
    return unwrapped

def relnp(wrapped_phase: np.ndarray, relative_matrix: np.ndarray) -> np.ndarray:
    """
        Fast two-dimensional phase-unwrapping algorithm
    based on sorting by reliability following a noncontinuous path
    DOI: 10.134/AO.41.007437
    :param wrapped_phase: матрица свёрнутой фазы
    :param relative_matrix: матрица качества для матрицы фазы
    :return: абсолютная фаза
    """

    def finished(wrapped: np.ndarray, sorted_edges: np.ndarray) -> np.ndarray:
        """
        Фунцкия непосредственно развёртки фазы по подготовленным данным
        :param wrapped: wrapped phase matrix
        :param sorted_edges: sorted array of vertex identifying edges
        :return: unwrapped phase matrix
        """
        bar = IncrementalBar('Edges completed', max=sorted_edges.shape[0], suffix="%(percent)f%%")
        unwrapped_phase = wrapped.copy().flatten()

        # ключ - номер компоненты связности,
        # значение - добавочный множитель для развёртки, количество элементов в компоненте связности
        adjacent_components = dict()
        number_edges = unwrapped_phase.size

        nodes = np.arange(number_edges + 1)

        for edge in sorted_edges:
            first_node = edge[0]  # индекс первого элемента
            first_value = unwrapped_phase[first_node]  # значение фазы в ячейке первого элемента
            first_num = nodes[first_node]  # номер компоненты связности, которой принадлежит первый элемент
            second_node = edge[1]  # индекс второго элемента
            second_value = unwrapped_phase[second_node]  # занчение фазы в ячейке второго элемента
            second_num = nodes[second_node]  # номер компоненты связности, которой принадлежит второй элемент

            components_flag = 0  # переменная хранящая код типов компонент связности анализируемых вершин:
            # 00 - обе компоненты одноэлементны
            # 10 - в первой компоненте более одной вершины, во второй компоненте - одна
            # 01 - в первой компоненте - одна вершина, во второй компоненте более одной вершины
            # 11 - во обеих компонентах более одной вершины

            # получение информации о числе вершин в первой компоненте связности
            if first_num in adjacent_components.keys():
                components_flag += 10
            # получение информации о числе вершин во второй компоненте связности
            if second_num in adjacent_components.keys():
                components_flag += 1

            # случай, когда обе компоненты связности содержат более одной вершины
            if components_flag == 11:
                first_component = adjacent_components[first_num]  # множество индесков ячеек, принадлежащих компоненте связности, в которой лежит первый элемент
                second_component = adjacent_components[second_num]  # множество индесков ячеек, принадлежащих компоненте связности, в которой лежит второй элемент
                if len(first_component) > len(second_component): # в первой компоненте связности вершин больше, чем во второй
                    del adjacent_components[second_num]  # удаляем малую компоненту связности из словаря
                    adjacent_components[first_num] = adjacent_components[first_num].union(second_component)  # объединяем компоненты связности
                    diff = first_value - second_value  # находим разность значений фазы в вершинах рассматриваемого ребра
                    if np.abs(diff) > pi:
                        unwrapped_phase[list(second_component)] += 2 * pi * pi_mult(diff) # компенсация разрыва фазы
                    nodes[list(second_component)] = first_num  # установка принадлежности элементов второй компоненты свяности к первой
                else:  # во второй компоненте связности вершин не меньше, чем в первой
                    del adjacent_components[first_num]   # удаляем малую компоненту связности из словаря
                    adjacent_components[second_num] = adjacent_components[second_num].union(first_component)  # объединяем компоненты связности
                    #diff = first_value - second_value  # находим разность значений фазы в вершинах рассматриваемого ребра
                    diff = second_value - first_value  # находим разность значений фазы в вершинах рассматриваемого ребра
                    if np.abs(diff) > pi:
                        #unwrapped_phase[list(first_component)] += second_value + np.arctan2(np.tan(diff)) - first_value  # компенсация разрыва фазы
                        unwrapped_phase[list(first_component)] += 2 * pi * pi_mult(diff)  # компенсация разрыва фазы
                    nodes[list(first_component)] = second_num  # установка принадлежности элементов первой компоненты свяности ко второй

            elif components_flag == 10:  # в первой компоненте более одной вершины, во второй компоненте - одна
                diff = first_value - second_value  # находим разность значений фазы в вершинах рассматриваемого ребра
                if np.abs(diff) > pi:
                    unwrapped_phase[second_node] += 2 * pi * pi_mult(diff)   # компенсация разрыва фазы
                adjacent_components[first_num].add(second_node)  # добавление второй ячейки к первой компоненте связности
                nodes[second_node] = first_num  # установка принадлежности второй ячейки свяности к первой компоненте связности

            else:
                diff = second_value - first_value  # находим разность значений фазы в вершинах рассматриваемого ребра
                if np.abs(diff) > pi:
                    unwrapped_phase[first_node] += 2 * pi * pi_mult(diff)    # компенсация разрыва фазы
                if components_flag == 1:  # в первой компоненте - одна вершина, во второй компоненте более одной вершины
                    adjacent_components[second_num].add(first_node)  # добавление первой ячейки ко второй компоненте связности
                else:  # обе компоненты одноэлементны
                    adjacent_components[second_node] = {first_node, second_node}  # добавление компоненты свзности в словарь
                nodes[first_node] = second_num  # установка принадлежности первой ячейки свяности ко второй компоненте связности
            bar.next()
        bar.finish()
        del sorted_edges
        del adjacent_components
        print(np.max(unwrapped_phase))
        print(np.min(unwrapped_phase))
        return unwrapped_phase.reshape(wrapped.shape)

    # так как в дальнейшем используется функция построения минимального остова, а развёртывать фазу необходимо
    # по ячейкам с наибольшими значениями матрицы качества, то необходимо выполнить операцию обращающую порядок
    # элементов в матрице
    tmp_relative_matrix = relative_matrix.copy()
    rel_min = np.min(tmp_relative_matrix)
    if rel_min == 0:
        tmp_relative_matrix += 1
    else:
        tmp_relative_matrix -= rel_min - 1  # добавляем единицу для того, чтобы можно было обращать
    tmp_relative_matrix = 1 / tmp_relative_matrix

    start = time()
    adj_matrix_mst = min_span_transitions_matrix_cell(tmp_relative_matrix)
    end = time()
    del tmp_relative_matrix

    print("Algorithm before sorting need {} seconds".format(end - start))
    # получим сортированный по возрастанию список рёбер, заданный номерами узлов(индексами ячеек матрицы),
    # в данном порядке будем производить развёртку фазы
    start = time()
    sorted_edges = np.array(sorted(adj_matrix_mst.items(), reverse=True, key=operator.itemgetter(1)), dtype=object)[:, 0]
    end = time()
    print('Sorting need {} seconds'.format(end - start))

    del adj_matrix_mst

    start = time()
    f = finished(wrapped_phase, sorted_edges)
    end = time()
    print('Finish unwrapping need {}'.format(end - start))
    return f

ext: py

reason: import numpy,from numpy


text:
"""
Module to solve the xmas-elves problem.

The xmas-elves problem means, that there are three or more people which
want to randomly draw for each person another person among themselves
to make a gift to. Every persons gets one of the other persons asigned
to make a gift to this person. In the end every person makes one gift
and every person receives one gift.

In the role of making a gift a person is called elf. In the role of
receiving a gift a person is called giftee. Allthough every person in the
problem has both roles the persons are represented as Elf objects.

The whole assignment is done using a network graph where the persons
represent the nodes (in form of Elf objects) and the edges between
the persons indicate which person could make a gift to whom of the
other persons. By default no edges are create between nodes which are
partners.

The algorithm chooses randomly among those nodes with the lowest degree
of ingoing edges to get the next node (giftee) to which an elf is
randomly assigned to. Once the elf is chosen and assigned, all of the other
ingoing nodes of the giftee are removed from the graph. The same procedure
applies to the outgoing edges of the node with elf role. At the end there
is a directed graph with one or more subgraphs where every node has excatly
one outgoing and one incomming edge.
"""

import networkx as nx
import openpyxl as xl
import csv
import random
import logging
import time
import datetime
import smtplib

from textwrap import dedent
from pprint import pprint
from argparse import ArgumentParser
from configparser import ConfigParser
from pathlib import Path
from collections import Counter, defaultdict
from email.message import EmailMessage

from .user_dialog_prompts import yes_or_no

logger = logging.getLogger(__name__)
logger_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
stream_formatter = logging.Formatter(logger_format)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(stream_formatter)
logger.addHandler(stream_handler)
logger.setLevel(logging.INFO)

class Elf:
    """
    Object to represent one person taking part in the "game".

    Initialised with a name, email-address and partner. The partner
    is an empty string if there is none. The partner can be used to
    determine if the partner could be a giftee or not. The create_edges
    method makes use of this variable.
    """

    def __init__(self, name, email, partner, giftee=None):
        self.name = name
        self.email = email
        self.partner = partner
        self.giftee = giftee

    def __str__(self):
        return "Elf(name={}, email={}, partner={})".format(
            self.name,
            self.email,
            self.partner
        )

    def __repr__(self):
        return str(self)

    def __iter__(self):
        yield from (self.name, self.email, self.partner, self.giftee)

def nodes_from_string(string):
    """
    Helper method to create elf nodes from a comma separate string
    with line breaks (see example under if __name__ == "__main__").
    """
    data_lines = [
        line
        for line in string.splitlines()
        if line != ''
    ]
    reader = csv.reader(data_lines)
    return create_nodes(reader)

def nodes_from_csv(filename):
    """
    Helper method to create elf nodes from a csv file.
    """
    with open(filename, 'r') as f:
        reader = csv.reader(f)
        return create_nodes(reader)

def nodes_from_excel(filename):
    wb = xl.load_workbook(filename)
    ws = wb.active
    data = [
        [value for value in row]
        for row in ws.values
    ]
    return create_nodes(data)

def create_nodes(data_list):
    """
    Factory method to create Elf object nodes
    from the given data lines.
    """
    return [
        Elf(name, email, partner, None)
        for name, email, partner, *_ in data_list
    ]

def create_edges(nodes, allow_partner=False):
    """
    Given a list of Elf nodes, return a list of edges.

    allow_partner:  if True, partners are also taken into account
        for possible gift making relationships. On default, partners
        are not allowed to be drawn as either elf or giftee of one
        another.
    """
    edges = [
        (node, neighbour)
        for node in nodes
        for neighbour in nodes
        if node != neighbour
    ]
    if not allow_partner:
        for node in nodes:
            for neighbour in nodes:
                if node.partner == neighbour.name:
                    edges.remove((node, neighbour))
    return edges

def random_min_indegree(graph, remaining_giftees):
    """
    Return the node with lowest degree of ingoing edges.

    If there is more than one node with the minimum degree, one of
    these nodes is drawn randomly from the list of minimum degree nodes.

    graph: the graph to determine minimum degree of ingoing edges on.
    remaining_giftees: list of Elf objects without elf assigned to them.
    """
    min_degree = len(graph.nodes)
    min_set = []
    for node in remaining_giftees:
        min_degree = min([min_degree, graph.in_degree(node)])
    for node in remaining_giftees:
        if graph.in_degree(node) <= min_degree:
            min_set.append(node)
    chosen = random.choice(min_set)
    return chosen

def xmas_elves(original_graph):
    """
    Return a new graph object for which the xmas-elves problem is solved.

    A new instance of a directed graph object is created an the actual
    algorithem is run on this new graph object.
    """
    graph = nx.DiGraph(original_graph)
    try:
        queue = [node for node in graph.nodes]
        while len(queue) > 0:
            current_node = random_min_indegree(graph, queue)
            queue.remove(current_node)
            chosen_wichtel = random.choice(
                list(graph.predecessors(current_node))
            )
            chosen_wichtel.giftee = current_node
            obsolete_ingoing = [
                node
                for node in graph.predecessors(current_node)
                if node != chosen_wichtel
            ]
            for node in obsolete_ingoing:
                graph.remove_edge(node, current_node)
            obsolete_outgoing = [
                node
                for node in graph.successors(chosen_wichtel)
                if node != current_node
            ]
            for node in obsolete_outgoing:
                graph.remove_edge(chosen_wichtel, node)
        if None in [n.giftee for n in graph.nodes]:
            return xmas_elves(original_graph)
    except IndexError as e:
        logger.warning(e)
        return xmas_elves(original_graph)
    return graph

def parse_args():
    parser = ArgumentParser()
    parser.add_argument(
        'personsource',
        type=str,
        help=dedent("""\
            Comma and newline separated string or path to file\
            containing info about the persons to be included\
            in the xmas-elves problem.
            Structure: name,email,partner,
            """)
    )
    parser.add_argument(
        '-m', '--mailconfig',
        type=str,
        help=dedent("""\
            Path to file containing configuration for sending\
            E-Mails. The config file is in .ini format and\
            contains a section named "email" with the following\
            key, value pairs:
            - smtp_mode, indicates wheter ssl or tls should be used
            - smtp_server, address of the smtp server to be used
            - smtp_port, port used to authenticate to
            - sender_address, e-mail address of the sender
            - sender_password, password to authenticate the sender\
            with the smtp server
            """)
    )
    parser.add_argument(
        '-n', '--dry-run',
        action='store_true',
        help=dedent("""\
            With dry-run there are no e-mails sent and the solution
            to the problem is printed to standard output.
            """)
    )
    parser.add_argument(
        '--allow-partners',
        action='store_true',
        help=dedent("""\
            Switch to control if partners should be allowed in elf
            giftee relationships.
            """)
    )
    parser.add_argument(
        '-s', '--stats',
        action='store_true',
        help=dedent("""\
            Solve the problem 20'000 times and print the counts for
            each elf giftee combination.
            """)
    )

    return parser.parse_args()

def create_nodes_from_type(string):
    if Path(string).exists():
        logger.debug(
            "Path.exists() in create_nodes_from_type: {}".format(
                Path(string).exists()
            )
        )
        ext = Path(string).suffix
        if ext in ['.xls', '.xlsx']:
            logger.debug("going to return nodes_from_excel")
            try:
                return nodes_from_excel(string)
            except Exception as e:
                raise e
                # ("Couldn't parse excel file successfully.")
        else:
            try:
                return nodes_from_csv(string)
            except Exception as e:
                raise e
                # ("Couldn't parse csv file successfully.")
    else:
        try:
            return nodes_from_string(string)
        except Exception as e:
            raise e
            # ("Couldn't parse the string successfully.")

def parse_mail_config(configfile):
    parser = ConfigParser()
    parser.read(configfile)
    return dict(parser['Email'])

def mail_html_text(text):
    text = text.replace('\n\n', '\n')
    return "".join([
        f'
{line}
' if line != '' else '
'
        for line in text.split('\n')
    ])

def mail_text(reciever, giftee):
    year = datetime.date.today().year
    return dedent("""\
        Hallo {},

        Weihnachten {} steht vor der Tür und ich wurde gebeten, die
        Wichtelauslosung zu starten.

        Du bist das Wichteli von {}. Vielen Dank, dass du mitmachst!

        Ich wünsche dir eine schöne Adventszeit.

        Liebe Grüsse
        Dein automatischer Wichtler
        """).format(reciever, year, giftee)

def mail_texts(graph):
    texts = [
        mail_text(elf.name, elf.giftee.name)
        for elf in graph.nodes
    ]
    return texts

def mail_messages(graph, sender):
    for node in graph.nodes:
        name, email, _, giftee = node
        msg = EmailMessage()
        msg['Subject'] = "Wichtelauslosung {}".format(
            datetime.date.today().year
        )
        msg['From'] = sender
        msg['To'] = email
        content = mail_text(name, giftee.name)
        msg.set_content(content)
        msg.add_alternative(mail_html_text(content), subtype='html')
        yield msg

def send_xmas_mails(config, graph):
    SMTP_SERVER = config['smtp_server']
    SMTP_PORT = int(config['smtp_port'])
    SENDER_ADDRESS = config['sender_address']
    SENDER_PASSWORD = config['sender_password']
    for msg in mail_messages(graph, SENDER_ADDRESS):
        with smtplib.SMTP("{}:{}".format(SMTP_SERVER, SMTP_PORT)) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.login(SENDER_ADDRESS, SENDER_PASSWORD)
            smtp.send_message(msg)
        time.sleep(2)

def draw_graph(graph):
    import matplotlib.pyplot as plt
    labels = {node: node.name for node in graph.nodes}
    plt.subplot(111)
    nx.draw(graph, labels=labels)
    plt.show()

def resend_mail_for_node(filename, nodename, mailconfig):
    """
    Resend the mail for one Node from a solved graph stored as gpickle.

    To be used, by importing the module in an interpreter and providing
    the paramters necessairy.

    filename: path to stored gpickle graph
    nodename: name of the elf to send the mail again
    mailconfig: path to the mail configuration file
    """
    solved_graph = nx.read_gpickle(filename)
    for node in solved_graph:
        if node.name == nodename:
            wanted_node = node
    new_graph = nx.DiGraph()
    new_graph.add_node(wanted_node)
    mail_config = parse_mail_config(mailconfig)
    send_xmas_mails(mail_config, new_graph)

def main():
    args = parse_args()
    nodes = create_nodes_from_type(args.personsource)
    # prepare edges
    edges = create_edges(nodes, args.allow_partners)
    # Create a directed graph
    graph = nx.from_edgelist(edges, nx.DiGraph)
    solved_graph = xmas_elves(graph)
    message_texts = mail_texts(solved_graph)

    if args.dry_run:
        for line in message_texts:
            print(line.replace("\n", " "))
    else:
        date = datetime.date.today()
        persons_path = Path(args.personsource)
        persons_name = persons_path.name
        filename = "{}/{}-{}.gpickle".format(
            persons_path.parent,
            date,
            persons_name
        )
        nx.write_gpickle(solved_graph, filename)
        if yes_or_no("Really send e-mails?"):
            mail_config = parse_mail_config(args.mailconfig)
            send_xmas_mails(mail_config, solved_graph)
        else:
            print("Not sending e-mails for the solved graph")
            draw_graph(solved_graph)

    if args.stats:  # and graph not loaded from file
        # Do some statistics with the graph:
        all_dict = defaultdict(list)
        for i in range(20000):
            g = xmas_elves(graph)
            for node in g.nodes:
                all_dict[node.name].append(node.giftee.name)

        res = {
            key: Counter(value)
            for key, value in all_dict.items()
        }
        """#Could be used to create a tabled view of up to 8 runs of xmas_elves
        for key, value in all_dict.items():
            values = ["{: <9s}".format(v) for v in value]
            print("{: <9s}\t{}".format(key, values))
        """
        pprint(res)

if __name__ == "__main__":

    main()
    csv_text = dedent("""\
        Angelina,angelina@email.com,Walter,
        Walter,walter@email.com,Angelina,
        Irena,irena@email.com,Isaac,
        Isaac,isaac@email.com,Irena,
        Gavin,gavin@email.com, ,
        Vince,vince@email.com,Vincent,
        Vincent,vincent@email.com,Vince,
        Renetta,renetta@email.com,Demetrius,
        Demetrius,demetrius@email.com,Renetta,
        Belinda,belinda@email.com, ,
        """)

    # prepare nodes
    nodes = nodes_from_string(csv_text)
    # nodes = nodes_from_csv('xmaselves_persons.txt')
    # prepare edges
    edges = create_edges(nodes)
    # Create a directed graph
    graph = nx.from_edgelist(edges, nx.DiGraph)

reason: import networkx


text:
#!/usr/bin/python3
"""adjust atom radius based on occupancy.
"""
import os
import random
import argparse
import numpy as np
from pathlib import Path
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
    "-i",
    "--input",
    required=True,
    nargs="+",
    help="input pdb file, output chimera session as input.view.py")
parser.add_argument('--ribbon',
                    action="store_true",
                    help="open file one more time and show as ribbon")
args = parser.parse_args()

radnom_str = ''.join(random.choices("abcdefghijklmnopqrstuvwxyz", k=10))
command_file = f'/tmp/.chimera.{radnom_str}.com'
with open(command_file, 'w') as f:
    for i, pdb_file in enumerate(args.input):
        pdb_file = Path(pdb_file).resolve()
        f.write(f"open {pdb_file}\n")
        if args.ribbon:
            model_id = i * 2
        else:
            model_id = i
        f.write(f"~rib #{model_id}\n"
                f"disp #{model_id}\n"
                f"repr bs #{model_id}\n"
                # f'labelopt info %(occupancy).2f\n'
                f'labelopt info %(name)s %(occupancy).2f\n')
        # set red sphere background to atoms with occupancy < -0.01
        bg_id = model_id + 100
        f.write(f"open #{bg_id} {pdb_file}\n"
                f"~rib #{bg_id}\n"
                f"~disp #{bg_id}\n"
                f"~bond #{bg_id}\n"
                f"select #{bg_id}@/occupancy<-0.01\n"
                f"disp sel\n"
                # f"repr sphere sel\n"
                f"repr bs sel\n"  #  ball-and-stick
                # f"setattr a radius 0.7 sel\n"
                f"color black ,a sel\n"
                f"transparency 80 sel\n")
        indices = []
        occupancies = []
        for line in open(pdb_file):
            if len(line) >= 6 and line[:6] in ("ATOM  ", "HETATM"):
                index = int(line[6:11])
                occ = float(line[54:60])
                indices.append(index)
                occupancies.append(occ)
                f.write(f'select #{model_id}@/serialNumber={index}\n')
                if occ >= 0.3:
                    f.write(f"label sel\n")
                    f.write(f"color black ,la sel\n")
                if occ <= -0.1:
                    f.write(f"label sel\n")
                    f.write(f"color red ,la sel\n")
                if occ >= 0:
                    r = occ * 6 + 0.8
                else:
                    r = -occ * 6 + 0.8
                f.write(f"setattr a radius {r:.3} sel\n")
                if occ < -0.01:
                    f.write(
                        f"setattr a radius {r*1.5} #{bg_id}@/serialNumber={index}\n"
                    )
        # label highest 1 and lowest 1
        rank = np.argsort(occupancies)
        for rank_idx in (rank[0], rank[-1]):
            occ = occupancies[rank_idx]
            index = indices[rank_idx]
            f.write(f'select #{model_id}@/serialNumber={index}\n')
            if occ >= 0:
                f.write(f"label sel\n")
                f.write(f"color black ,la sel\n")
            else:
                f.write(f"label sel\n")
                f.write(f"color red ,la sel\n")
        if args.ribbon:
            # f.write("color byhet\n")
            f.write(f"open {pdb_file}\n")
            f.write(f"~disp #{model_id+1}\n")

    f.write("setattr g display false\n"
            "~select\n"
            f"save {pdb_file}.view.py\n"
            "stop\n")

    # without hetatom color with --nogui
    # os.system("chimera --nogui /tmp/.chimera.com")
os.system(f"chimera {command_file}")
os.remove(command_file)
print(f"output chimera seesion file on {pdb_file}.view.py")

max_stars_repo_name: hnlab/can-ai-do

max_issues_repo_name: hnlab/can-ai-do

max_forks_repo_name: hnlab/can-ai-do

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 28 05:42:48 2017

@author: james.lee
"""

"""
collect data and create a log of arb opportunities
"""

from xapi import XApi
from xapi import Exchange
from xapi import BaseQuotePair
from binance.client import Client
from binance.exceptions import BinanceAPIException, BinanceRequestException, BinanceWithdrawException
from bittrex.bittrex import Bittrex
import pytest
import json
import time
import datetime
import pickle
import pandas as pd
import numpy as np

def save_object(obj, filename):
    with open(filename, 'wb') as output:
        pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
# sample usage

xapi_client = XApi()
prices = xapi_client.exchange_info()

try:
    with open('arb_log.pkl', 'rb') as input:
        df= pickle.load(input)
except FileNotFoundError:
     df = pd.DataFrame(columns=['security', 'base_security', 'quote_security','buy_exchange', 'sell_exchange','price_buy_exchange','price_sell_exchange','spread','time','descr'])

while True:
    prices = xapi_client.exchange_info()
    for security in prices:
        bncbid = prices[security][Exchange.BNC]['bid_price']
        bncask = prices[security][Exchange.BNC]['ask_price']
        bitbid = prices[security][Exchange.BTX]['bid_price']
        bitask = prices[security][Exchange.BTX]['ask_price']
        buyBncArb = (bitbid- bncask)/bncask
        buyBitArb = (bncbid - bitask)/bitask
        if(buyBncArb > .015):
            insert = [security, security.base, security.quote, Exchange.BNC.name, Exchange.BTX.name, bncask, bitbid, round(buyBncArb,4), datetime.datetime.now(), 'buy ' + security.base + ' at BNC'+' and sell it at BIT' ]
            df.loc[len(df)+1] = insert
        if(buyBitArb > .015):
            insert = [security, security.base, security.quote, Exchange.BTX.name, Exchange.BNC.name, bitask, bncbid, round(buyBitArb,4), datetime.datetime.now(), 'buy ' + security.base + ' at BIT'+' and sell it at BIN' ]
            df.loc[len(df)+1] = insert
    if(len(df))>10000:
        df = df.tail(10000)
    save_object(df, 'arb_log.pkl')
    time.sleep(5)

#datetime.datetime.now().strftime('%Y/%m/%d /%H:%M:%S')


text:
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import os
import cv2

# 如果使用mnist数据集，把MNIST设置为True
MNIST = False

if MNIST == True:
    from tensorflow.examples.tutorials.mnist import input_data

    data = input_data.read_data_sets('/tmp/')
    image_height = 28
    image_width = 28
    image_channel = 1

    batch_size = 128
    n_batches = data.train.num_examples // batch_size
else:
    picture_dir = 'little_girls'
    picture_list = []
    # 建议不要把图片一次加载到内存，为了节省内存，最好边加载边使用
    for (dirpath, dirnames, filenames) in os.walk(picture_dir):
        for filename in filenames:
            if filename.endswith('.jpg'):
                picture_list.append(os.sep.join([dirpath, filename]))

    print("图像总数: ", len(picture_list))

    # 图像大小和Channel
    image_height = 64
    image_width = 64
    image_channel = 3

    # 每次使用多少样本训练
    batch_size = 64
    n_batches = len(picture_list) // batch_size

    # 图片格式对应输入X
    img_data = []
    cnt = 0
    for img_file in picture_list:
        cnt += 1
        # if cnt >= 1000:
        #     break
        img_data.append(cv2.imread(img_file))
    img_data = np.array(img_data)
    img_data = img_data / 255.0
print(img_data.shape)   # (44112, 64, 64, 3)

X = tf.placeholder(tf.float32, shape=[None, image_height, image_width, image_channel])

def gated_cnn(W_shape_, fan_in, gated=True, payload=None, mask=None, activation=True):
    W_shape = [W_shape_[0], W_shape_[1], fan_in.get_shape()[-1], W_shape_[2]]
    b_shape = W_shape_[2]

    def get_weights(shape, name, mask=None):
        weights_initializer = tf.contrib.layers.xavier_initializer()
        W = tf.get_variable(name, shape, tf.float32, weights_initializer)

        if mask:
            filter_mid_x = shape[0] // 2
            filter_mid_y = shape[1] // 2
            mask_filter = np.ones(shape, dtype=np.float32)
            mask_filter[filter_mid_x, filter_mid_y + 1:, :, :] = 0.
            mask_filter[filter_mid_x + 1:, :, :, :] = 0.

            if mask == 'a':
                mask_filter[filter_mid_x, filter_mid_y, :, :] = 0.

            W *= mask_filter
        return W

    if gated:
        W_f = get_weights(W_shape, "v_W", mask=mask)
        W_g = get_weights(W_shape, "h_W", mask=mask)

        b_f = tf.get_variable("v_b", b_shape, tf.float32, tf.zeros_initializer)
        b_g = tf.get_variable("h_b", b_shape, tf.float32, tf.zeros_initializer)

        conv_f = tf.nn.conv2d(fan_in, W_f, strides=[1, 1, 1, 1], padding='SAME')
        conv_g = tf.nn.conv2d(fan_in, W_g, strides=[1, 1, 1, 1], padding='SAME')
        if payload is not None:
            conv_f += payload
            conv_g += payload

        fan_out = tf.multiply(tf.tanh(conv_f + b_f), tf.sigmoid(conv_g + b_g))
    else:
        W = get_weights(W_shape, "W", mask=mask)
        b = tf.get_variable("b", b_shape, tf.float32, tf.zeros_initializer)
        conv = tf.nn.conv2d(fan_in, W, strides=[1, 1, 1, 1], padding='SAME')
        if activation:
            fan_out = tf.nn.relu(tf.add(conv, b))
        else:
            fan_out = tf.add(conv, b)

    return fan_out

def pixel_cnn(layers=12, f_map=32):
    v_stack_in, h_stack_in = X, X

    for i in range(layers):
        filter_size = 3 if i > 0 else 7
        mask = 'b' if i > 0 else 'a'
        residual = True if i > 0 else False
        i = str(i)

        with tf.variable_scope("v_stack" + i):
            v_stack = gated_cnn([filter_size, filter_size, f_map], v_stack_in, mask=mask)
            v_stack_in = v_stack

        with tf.variable_scope("v_stack_1" + i):
            v_stack_1 = gated_cnn([1, 1, f_map], v_stack_in, gated=False, mask=mask)

        with tf.variable_scope("h_stack" + i):
            h_stack = gated_cnn([1, filter_size, f_map], h_stack_in, payload=v_stack_1, mask=mask)

        with tf.variable_scope("h_stack_1" + i):
            h_stack_1 = gated_cnn([1, 1, f_map], h_stack, gated=False, mask=mask)
            if residual:
                h_stack_1 += h_stack_in
            h_stack_in = h_stack_1

    with tf.variable_scope("fc_1"):
        fc1 = gated_cnn([1, 1, f_map], h_stack_in, gated=False, mask='b')

    color = 256
    with tf.variable_scope("fc_2"):
        fc2 = gated_cnn([1, 1, image_channel * color], fc1, gated=False, mask='b', activation=False)
        fc2 = tf.reshape(fc2, (-1, color))

        return fc2

def train_pixel_cnn():
    output = pixel_cnn()

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(logits=output, labels=tf.cast(tf.reshape(X, [-1]), dtype=tf.int32)))
    trainer = tf.train.RMSPropOptimizer(1e-3)
    gradients = trainer.compute_gradients(loss)
    clipped_gradients = [(tf.clip_by_value(_[0], -1, 1), _[1]) for _ in gradients]
    optimizer = trainer.apply_gradients(clipped_gradients)

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        saver = tf.train.Saver(tf.trainable_variables())

        for epoch in range(50):
            for batch in range(n_batches):

                if MNIST == True:
                    batch_X, _ = data.train.next_batch(batch_size)
                    batch_X = batch_X.reshape([batch_size, image_height, image_width, image_channel])
                else:
                    batch_X = img_data[batch_size * batch: batch_size * (batch + 1)]

                _, cost = sess.run([optimizer, loss], feed_dict={X: batch_X})
                print("epoch:", epoch, '  batch:', batch, '  cost:', cost)
            if epoch % 7 == 0:
                saver.save(sess, "./girl.ckpt", global_step=epoch)

# 训练
# train_pixel_cnn()

def generate_girl():
    output = pixel_cnn()

    predict = tf.reshape(tf.multinomial(tf.nn.softmax(output), num_samples=1, seed=100), tf.shape(X))
    # predict_argmax = tf.reshape(tf.argmax(tf.nn.softmax(output), dimension=tf.rank(output) - 1), tf.shape(X))

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        saver = tf.train.Saver(tf.trainable_variables())
        saver.restore(sess, './girl.ckpt-42')

        pics = np.zeros((1 * 1, image_height, image_width, image_channel), dtype=np.float32)

        for i in range(image_height):
            for j in range(image_width):
                for k in range(image_channel):
                    next_pic = sess.run(predict, feed_dict={X: pics})
                    pics[:, i, j, k] = next_pic[:, i, j, k]

        cv2.imwrite('./girl.jpg', pics[0])
        print('生成妹子图: girl.jpg')

# 生成图像
generate_girl()


text:
import cv2, os
import numpy as np
import matplotlib.pyplot as plt

# Gets current working directory
current_path = os.getcwd()

# Creates output folder
output_path = "output"
os.makedirs(output_path, exist_ok=True)

# Set input image name
image_name = "ArtGallery.jpg"

# Sets input image path
image_path = os.path.join(current_path, image_name)

# Read input image with cv2
image = cv2.imread(image_path)

# Shows image
plt.figure(figsize=(10, 8))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()

# Creates a copy of original input image
spare_image = image.copy()

# Defines source points of first region
src_points = [[38, 102], [341, 124], [342, 426], [42, 447]]

# Defines destination points of second region
dst_points = [[683, 119], [956, 68], [955, 515], [678, 452]]

# Calculates the homography matrix based on SVD
A = []
for i in range(len(src_points)):
    # Get each x, y of source points
    x, y = src_points[i][0], src_points[i][1]
    # Get each x' (u), y' (v) of destinatin points
    u, v = dst_points[i][0], dst_points[i][1]
    A.append([0,   0,   0,   -x,   -y,   -1,   v*x,   v*y,   v])
    A.append([x,   y,   1,    0,    0,    0,  -u*x,  -u*y,  -u])

# Transforms to array to do matrix decomposition
A = np.asarray(A)

# Does the SVD decomposition
U, S, V = np.linalg.svd(A)

# Normalize the output and reshape to matrix with shape 3x3
homo_matrix = (V[-1, :] / V[-1, -1]).reshape(3, 3)

################ PART ONE - MOVE FIRST REGION TO ANOTHER REGION ################

# Applies a perspective transformation to an image
persp_transform = cv2.warpPerspective(spare_image, homo_matrix,
                        (spare_image.shape[1], spare_image.shape[0]))

# Draws a filled convex polygon with black color
spare_image = cv2.fillConvexPoly(spare_image, np.asarray(dst_points).astype(int), 0, 16)

# Fills the black region with region which we want to swap
# Creates a dense multi-dimensional meshgrid
H, W = np.mgrid[:spare_image.shape[0], :spare_image.shape[1]]

# Filters out coordinates which color value equals to zeros, that means black region
w = W[spare_image[..., 2] == 0]
h = H[spare_image[..., 2] == 0]

# Fills black region with color from perspective transform result
for i in range(len(w)):
    spare_image[h[i], w[i]] = persp_transform[h[i], w[i]]

del persp_transform
# del dst_image

################ PART TWO - MOVE SECOND REGION TO ANOTHER REGION ################

# Applies a perspective transformation to an image
persp_transform = cv2.warpPerspective(image, np.linalg.inv(homo_matrix),
                        (image.shape[1], image.shape[0]))

# Draws a filled convex polygon with black color
dst_image = cv2.fillConvexPoly(spare_image, np.asarray(src_points).astype(int), 0, 16)

# Fills the black region with region which we want to swap
# Creates a dense multi-dimensional meshgrid
H, W = np.mgrid[:spare_image.shape[0], :spare_image.shape[1]]

# Filters out coordinates which color value equals to zeros, that means black region
w = W[dst_image[..., 2] == 0]
h = H[dst_image[..., 2] == 0]

# Fills black region with color from perspective transform result
for i in range(len(w)):
    dst_image[h[i], w[i]] = persp_transform[h[i], w[i]]

cv2.imwrite(f"{output_path}\\M10907803.jpg", dst_image)

demo = np.concatenate([image, dst_image], axis=1)
cv2.imwrite(f"{output_path}\\demo.jpg", demo)

plt.figure(figsize=(10, 8))
plt.title("Output")
plt.imshow(cv2.cvtColor(dst_image, cv2.COLOR_BGR2RGB))
plt.show()