text:
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""FCIDump parser."""

from typing import Any, Dict, Set, Tuple, Iterator
import itertools
import re
import numpy as np

from qiskit_nature import QiskitNatureError

def parse(fcidump: str) -> Dict[str, Any]:
    """Parses a FCIDump output.

    Args:
        fcidump: Path to the FCIDump file.
    Raises:
        QiskitNatureError: If the input file cannot be found, if a required field in the FCIDump
            file is missing, if wrong integral indices are encountered, or if the alpha/beta or
            beta/alpha 2-electron integrals are mixed.
    Returns:
        A dictionary storing the parsed data.
    """
    try:
        with open(fcidump, "r", encoding="utf8") as file:
            fcidump_str = file.read()
    except OSError as ex:
        raise QiskitNatureError(f"Input file '{fcidump}' cannot be read!") from ex

    output = {}  # type: Dict[str, Any]

    # FCIDump starts with a Fortran namelist of meta data
    namelist_end = re.search("(/|&END)", fcidump_str)
    metadata = fcidump_str[: namelist_end.start(0)]
    metadata = " ".join(metadata.split())  # replace duplicate whitespace and newlines
    # we know what elements to look for so we don't get too fancy with the parsing
    # pattern explanation:
    #  .*?      any text
    #  (*|*),   match either part of this group followed by a comma
    #  [-+]?    match up to a single - or +
    #  \d*.\d+  number format
    pattern = r".*?([-+]?\d*\.\d+|[-+]?\d+),"
    # we parse the values in the order in which they are listed in Knowles1989
    _norb = re.search("NORB" + pattern, metadata)
    if _norb is None:
        raise QiskitNatureError("The required NORB entry of the FCIDump format is missing!")
    norb = int(_norb.groups()[0])
    output["NORB"] = norb
    _nelec = re.search("NELEC" + pattern, metadata)
    if _nelec is None:
        raise QiskitNatureError("The required NELEC entry of the FCIDump format is missing!")
    output["NELEC"] = int(_nelec.groups()[0])
    # the rest of these values may occur and are set to their defaults otherwise
    _ms2 = re.search("MS2" + pattern, metadata)
    output["MS2"] = int(_ms2.groups()[0]) if _ms2 else 0
    _isym = re.search("ISYM" + pattern, metadata)
    output["ISYM"] = int(_isym.groups()[0]) if _isym else 1
    # ORBSYM holds a list, thus it requires a little different treatment
    _orbsym = re.search(r"ORBSYM.*?" + r"(\d+)," * norb, metadata)
    output["ORBSYM"] = [int(s) for s in _orbsym.groups()] if _orbsym else [1] * norb
    _iprtim = re.search("IPRTIM" + pattern, metadata)
    output["IPRTIM"] = int(_iprtim.groups()[0]) if _iprtim else -1
    _int = re.search("INT" + pattern, metadata)
    output["INT"] = int(_int.groups()[0]) if _int else 5
    _memory = re.search("MEMORY" + pattern, metadata)
    output["MEMORY"] = int(_memory.groups()[0]) if _memory else 10000
    _core = re.search("CORE" + pattern, metadata)
    output["CORE"] = float(_core.groups()[0]) if _core else 0.0
    _maxit = re.search("MAXIT" + pattern, metadata)
    output["MAXIT"] = int(_maxit.groups()[0]) if _maxit else 25
    _thr = re.search("THR" + pattern, metadata)
    output["THR"] = float(_thr.groups()[0]) if _thr else 1e-5
    _thrres = re.search("THRRES" + pattern, metadata)
    output["THRRES"] = float(_thrres.groups()[0]) if _thrres else 0.1
    _nroot = re.search("NROOT" + pattern, metadata)
    output["NROOT"] = int(_nroot.groups()[0]) if _nroot else 1

    # If the FCIDump file resulted from an unrestricted spin calculation the indices will label spin
    # rather than molecular orbitals. This means, that a line must exist which encodes the
    # coefficient for the spin orbital with index (norb*2, norb*2). By checking for such a line we
    # can distinguish between unrestricted and restricted FCIDump files.
    _uhf = bool(
        re.search(
            fr".*(\s+{norb * 2}\s+{norb * 2}\s+0\s+0)",
            fcidump_str[namelist_end.start(0) :],
        )
    )

    # the rest of the FCIDump will hold lines of the form x i a j b
    # a few cases have to be treated differently:
    # i, a, j and b are all zero: x is the core energy
    # TODO: a, j and b are all zero: x is the energy of the i-th MO  (often not supported)
    # j and b are both zero: x is the 1e-integral between i and a (x = <i|h|a>)
    # otherwise: x is the Coulomb integral ( x = (ia|jb) )
    hij = np.zeros((norb, norb))
    hij_elements = set(itertools.product(range(norb), repeat=2))
    hijkl = np.zeros((norb, norb, norb, norb))
    hijkl_elements = set(itertools.product(range(norb), repeat=4))
    hij_b = hijkl_ab = hijkl_ba = hijkl_bb = None
    hij_b_elements = hijkl_ab_elements = hijkl_ba_elements = hijkl_bb_elements = set()
    if _uhf:
        beta_range = [n + norb for n in range(norb)]
        hij_b = np.zeros((norb, norb))
        hij_b_elements = set(itertools.product(beta_range, repeat=2))
        hijkl_ab = np.zeros((norb, norb, norb, norb))
        hijkl_ba = np.zeros((norb, norb, norb, norb))
        hijkl_bb = np.zeros((norb, norb, norb, norb))
        hijkl_ab_elements = set(itertools.product(range(norb), range(norb), beta_range, beta_range))
        hijkl_ba_elements = set(itertools.product(beta_range, beta_range, range(norb), range(norb)))
        hijkl_bb_elements = set(itertools.product(beta_range, repeat=4))
    orbital_data = fcidump_str[namelist_end.end(0) :].split("\n")
    for orbital in orbital_data:
        if not orbital:
            continue
        x = float(orbital.split()[0])
        # Note: differing naming than ijkl due to E741 and this iajb is inline with this:
        # https://hande.readthedocs.io/en/latest/manual/integrals.html#fcidump-format
        i, a, j, b = [int(i) for i in orbital.split()[1:]]
        if i == a == j == b == 0:
            output["ecore"] = x
        elif a == j == b == 0:
            # TODO: x is the energy of the i-th MO
            continue
        elif j == b == 0:
            try:
                hij_elements.remove((i - 1, a - 1))
                hij[i - 1][a - 1] = x
            except KeyError as ex:
                if _uhf:
                    hij_b_elements.remove((i - 1, a - 1))
                    hij_b[i - 1 - norb][a - 1 - norb] = x
                else:
                    raise QiskitNatureError(
                        "Unkown 1-electron integral indices encountered in " f"'{(i, a)}'"
                    ) from ex
        else:
            try:
                hijkl_elements.remove((i - 1, a - 1, j - 1, b - 1))
                hijkl[i - 1][a - 1][j - 1][b - 1] = x
            except KeyError as ex:
                if _uhf:
                    try:
                        hijkl_ab_elements.remove((i - 1, a - 1, j - 1, b - 1))
                        hijkl_ab[i - 1][a - 1][j - 1 - norb][b - 1 - norb] = x
                    except KeyError:
                        try:
                            hijkl_ba_elements.remove((i - 1, a - 1, j - 1, b - 1))
                            hijkl_ba[i - 1 - norb][a - 1 - norb][j - 1][b - 1] = x
                        except KeyError:
                            hijkl_bb_elements.remove((i - 1, a - 1, j - 1, b - 1))
                            hijkl_bb[i - 1 - norb][a - 1 - norb][j - 1 - norb][b - 1 - norb] = x
                else:
                    raise QiskitNatureError(
                        "Unkown 2-electron integral indices encountered in " f"'{(i, a, j, b)}'"
                    ) from ex

    # iterate over still empty elements in 1-electron matrix and populate with symmetric ones
    # if any elements are not populated these will be zero
    _permute_1e_ints(hij, hij_elements, norb)

    if _uhf:
        # do the same for beta spin
        _permute_1e_ints(hij_b, hij_b_elements, norb, beta=True)

    # do the same of the 2-electron 4D matrix
    _permute_2e_ints(hijkl, hijkl_elements, norb)

    if _uhf:
        # do the same for beta spin
        _permute_2e_ints(hijkl_bb, hijkl_bb_elements, norb, beta=2)
        _permute_2e_ints(hijkl_ab, hijkl_ab_elements, norb, beta=1)  # type: ignore
        _permute_2e_ints(hijkl_ba, hijkl_ba_elements, norb, beta=1)  # type: ignore

        # assert that EITHER hijkl_ab OR hijkl_ba were given
        if np.allclose(hijkl_ab, 0.0) == np.allclose(hijkl_ba, 0.0):
            raise QiskitNatureError(
                "Encountered mixed sets of indices for the 2-electron \
                    integrals. Either alpha/beta or beta/alpha matrix should be specified."
            )

        if np.allclose(hijkl_ba, 0.0):
            hijkl_ba = hijkl_ab.transpose()

    output["hij"] = hij
    output["hij_b"] = hij_b
    output["hijkl"] = hijkl
    output["hijkl_ba"] = hijkl_ba
    output["hijkl_bb"] = hijkl_bb

    return output

def _permute_1e_ints(
    hij: np.ndarray, elements: Set[Tuple[int, ...]], norb: int, beta: bool = False
) -> None:
    for elem in elements.copy():
        shifted = tuple(e - (beta * norb) for e in elem)
        hij[shifted] = hij[shifted[::-1]]
        elements.remove(elem)

def _permute_2e_ints(
    hijkl: np.ndarray, elements: Set[Tuple[int, ...]], norb: int, beta: int = 0
) -> None:
    for elem in elements.copy():
        shifted = tuple(e - ((e >= norb) * norb) for e in elem)
        # initially look for "transposed" element if spins are equal
        if beta != 1 and elem[::-1] not in elements:
            hijkl[shifted] = hijkl[shifted[::-1]]
            elements.remove(elem)
            continue
        # then look at permutations of indices within the bra and ket respectively
        bra_perms = set(itertools.permutations(elem[:2]))
        ket_perms = set(itertools.permutations(elem[2:]))
        permutations: Iterator[Any]
        if beta == 1:
            # generally (ij|ab) != (ab|ij)
            # thus, the possible permutations are much less when the spins differ
            permutations = itertools.product(bra_perms, ket_perms)
        else:
            # ( ij | kl ) gives { ( ij | kl ), ( ij | lk ), ( ji | kl ), ( ji | lk ) }
            # AND { ( kl | ij ), ( kl | ji ), ( lk | ij ), ( lk | ji ) }
            # BUT NOT ( ik | jl ) etc.
            permutations = itertools.chain(
                itertools.product(bra_perms, ket_perms),
                itertools.product(ket_perms, bra_perms),
            )
        for perm in {e1 + e2 for e1, e2 in permutations}:
            if perm in elements:
                continue
            hijkl[shifted] = hijkl[tuple(e - (e >= norb) * norb for e in perm)]
            elements.remove(elem)
            break

max_stars_repo_name: iuliazidaru/qiskit-nature

max_issues_repo_name: iuliazidaru/qiskit-nature

max_forks_repo_name: iuliazidaru/qiskit-nature


text:
import pickle
import numpy as np
import json

sentence = "I feel a little bit tired today , but I am really happy !"
sentence_neg = "Although the rain stopped , I hate this thick cloud in the sky ."

x_text = [line.split('\t')[2] for line in open('../data/s17/tst', "r").readlines()][:20]
x_text.append(sentence)
x_text.append(sentence_neg)

a= [[{"forms": ["This", "is", "the", "first", "document", "."], "offsets": [[0, 4], [5, 7], [8, 11], [12, 17], [18, 26], [26, 27]]}, {"forms": ["Contents", "of", "the", "first", "document", "are", "here", "."], "offsets": [[28, 36], [37, 39], [40, 43], [44, 49], [50, 58], [59, 62], [63, 67], [67, 68]]}], [{"forms": ["This", "is", "the", "second", "document", "."], "offsets": [[0, 4], [5, 7], [8, 11], [12, 18], [19, 27], [27, 28]]}, {"forms": ["The", "delimiter", "is", "not", "required", "for", "the", "last", "document", "."], "offsets": [[29, 32], [33, 42], [43, 45], [46, 49], [50, 58], [59, 62], [63, 66], [67, 71], [72, 80], [80, 81]]}]]

tokens = sentence.strip().split(' ')
tokens_neg = sentence_neg.strip().split(' ')
with open('output.pkl', 'rb') as handle:
    y = pickle.load(handle)
    att = pickle.load(handle)
    sentences = pickle.load(handle)

all_dic = []
for idx, s in enumerate(sentences):
    s_dic = {}
    s_dic['forms'] = s.split(' ')
    s_dic['sentiment'] = y[idx].tolist()
    for i in range(5):
        s_dic['sentiment-attention-%d' % (i+1)] = att[idx][i].tolist()

    all_dic.append(s_dic)

all_json = json.dumps(all_dic)
with open('samples.json', 'w') as outfile:
    json.dump(all_json, outfile)

for sent in sentences:
    print(sent)

i=0
gram_index = 0
sample_index =1
sentence_neg_len = len(tokens_neg)
sentence_len = len(tokens)
att[gram_index][sample_index][0]

all_att = []
for sample_index in range(2):
    one_sample_att = []
    for gram_index in range(5):
        norm_one_sample = att[gram_index][sample_index][0]/max(att[gram_index][sample_index][0])
        one_sample_att.append(norm_one_sample[-sentence_len+gram_index:])

    all_att.append(one_sample_att)

print('d')

ext: py

reason: import numpy


text:
from __future__ import division, print_function, absolute_import

import numpy as np
import misc
import pandas as pd
import sys
import matplotlib.pyplot as plt

from keras_utils import *
from misc import *

import importlib
importlib.reload(misc)

import tensorflow.keras as keras
from tensorflow.keras.datasets import cifar10
from mnist import MNIST
# load dataset
#mnist = MNIST('/home/labs/ahissarlab/orra/datasets/mnist')#MNIST('/home/orram/Documents/datasets/MNIST')
mnist = MNIST('/home/orram/Documents/datasets/MNIST')
images, labels = mnist.load_training()

# fmnist = torchvision.datasets.FashionMNIST('/home/orram/Documents/datasets/fmnist', train = True, download = True)
# images, labels = fmnist.data, fmnist.targets

import importlib
importlib.reload(misc)
from misc import Logger
import os

hp = HP()
hp.save_path = 'saved_runs'

hp.description = "syclop mnist check runs "
hp.this_run_name = 'syclop_check'
def deploy_logs():
    if not os.path.exists(hp.save_path):
        os.makedirs(hp.save_path)

    dir_success = False
    for sfx in range(1):  # todo legacy
        candidate_path = hp.save_path + '/' + hp.this_run_name + '_' + str(os.getpid()) + '/'
        if not os.path.exists(candidate_path):
            hp.this_run_path = candidate_path
            os.makedirs(hp.this_run_path)
            dir_success = True
            break
    if not dir_success:
        error('run name already exists!')

    sys.stdout = Logger(hp.this_run_path+'log.log')
    print('results are in:', hp.this_run_path)
    print('description: ', hp.description)
    #print('hyper-parameters (partial):', hp.dict)

def bad_res101(img,res):
    sh=np.shape(img)
    dwnsmp=cv2.resize(img,res, interpolation = cv2.INTER_CUBIC)
    upsmp = cv2.resize(dwnsmp,sh[:2], interpolation = cv2.INTER_CUBIC)
    return upsmp

def bad_res102(img,res):
    sh=np.shape(img)
    dwnsmp=cv2.resize(img,res, interpolation = cv2.INTER_CUBIC)
    return dwnsmp

sample = 5#int(sys.argv[4])
concat = False
# rnn_net = 3#int(sys.argv[1]) #Choose CNN one img network

# if rnn_net == 1:
#     rnn_net = no_cnn(n_timesteps = sample, input_size = 3, input_dim = 1)
# elif rnn_net == 2:
#     rnn_net = no_cnn_dense(n_timesteps = sample, input_size = 3, input_dim = 1)
# elif rnn_net == 3:
#     rnn_net = rnn_model(n_timesteps = sample, input_size = 28, input_dim = 1)

res = 6# int(sys.argv[3])

rnn_net = convgru(n_timesteps = sample, input_size = 6, input_dim = 1,concat = False)
#cnn_net = convgru(n_timesteps = sample, input_size = 6, input_dim = 1)
cnn_accur = []
cnn_train = []

rnn_accur = []
rnn_train = []
n_timesteps = sample

#deploy_logs()
train_dataset, test_dataset = create_mnist_dataset(images, labels,res = res,
                                   sample = sample, return_datasets=True,
                                   mixed_state = False, add_seed = 0, show_fig = True,
                                   )#bad_res_func = bad_res101, up_sample = True)
train_dataset_x, train_dataset_y = mnist_split_dataset_xy(train_dataset)
test_dataset_x, test_dataset_y = mnist_split_dataset_xy(test_dataset)
print("##################### Fit {} and trajectories model on training data res = {} ##################".format(rnn_net.name,res))
rnn_history = rnn_net.fit(
    train_dataset_x,
    train_dataset_y,
    batch_size=64,
    epochs=20,
    # We pass some validation for
    # monitoring validation loss and metrics
    # at the end of each epoch8966000080108643, 0.8992000222206116, 0.8920000195503235, 0.8925999999046326, 0.8903999924659729, 0.895600
    validation_data=(test_dataset_x, test_dataset_y),
    verbose = 1)
print('################# {} Validation Accuracy = '.format(rnn_net.name),rnn_history.history['val_sparse_categorical_accuracy'])
rnn_accur.append(rnn_history.history['val_sparse_categorical_accuracy'][-1])
print('################# {} Training Accuracy = '.format(rnn_net.name),rnn_history.history['sparse_categorical_accuracy'])
rnn_train.append(rnn_history.history['sparse_categorical_accuracy'][-1])

plt.figure()
plt.plot(rnn_history.history['sparse_categorical_accuracy'], label = 'train')
plt.plot(rnn_history.history['val_sparse_categorical_accuracy'], label = 'val')
plt.legend()
#plt.savefig('mixed reolution 2.png')

max_stars_repo_name: sashkarivkind/imagewalker

max_issues_repo_name: sashkarivkind/imagewalker

max_forks_repo_name: sashkarivkind/imagewalker


text:
"""Functions for slicing, joining, padding, packing. Most of the code is copied from the upcoming PyTorch release and
should be removed once the new version becomes available.
"""
import numpy as np
import torch
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence

# PyTorch code. Should be removed once the new version becomes available

def pad_sequence(sequences, batch_first=False):
    r"""Pad a list of variable length Variables with zero

    ``pad_sequence`` stacks a list of Variables along a new dimension,
    and padds them to equal length. For example, if the input is list of
    sequences with size ``Lx*`` and if batch_first is False, and ``TxBx*``
    otherwise. The list of sequences should be sorted in the order of
    decreasing length.

    B is batch size. It's equal to the number of elements in ``sequences``.
    T is length longest sequence.
    L is length of the sequence.
    * is any number of trailing dimensions, including none.

    Example:
        >>> from torch.nn.utils.rnn import pad_sequence
        >>> a = Variable(torch.ones(25, 300))
        >>> b = Variable(torch.ones(22, 300))
        >>> c = Variable(torch.ones(15, 300))
        >>> pad_sequence([a, b, c]).size()
        torch.Size([25, 3, 300])

    Note:
        This function returns a Variable of size TxBx* or BxTx* where T is the
            length of longest sequence.
        Function assumes trailing dimensions and type of all the Variables
            in sequences are same.

    Arguments:
        sequences (list[Variable]): list of variable length sequences.
        batch_first (bool, optional): output will be in BxTx* if True, or in
            TxBx* otherwise

    Returns:
        Variable of size ``T x B x * `` if batch_first is False
        Variable of size ``B x T x * `` otherwise
    """

    # assuming trailing dimensions and type of all the Variables
    # in sequences are same and fetching those from sequences[0]
    max_size = sequences[0].size()
    max_len, trailing_dims = max_size[0], max_size[1:]
    prev_l = max_len
    if batch_first:
        out_dims = (len(sequences), max_len) + trailing_dims
    else:
        out_dims = (max_len, len(sequences)) + trailing_dims

    out_variable = Variable(sequences[0].data.new(*out_dims).zero_())
    for i, variable in enumerate(sequences):
        length = variable.size(0)
        # temporary sort check, can be removed when we handle sorting internally
        if prev_l < length:
                raise ValueError("lengths array has to be sorted in decreasing order")
        prev_l = length
        # use index notation to prevent duplicate references to the variable
        if batch_first:
            out_variable[i, :length, ...] = variable
        else:
            out_variable[:length, i, ...] = variable

    return out_variable

def pack_sequence(sequences):
    r"""Packs a list of variable length Variables

    ``sequences`` should be a list of Variables of size ``Lx*``, where L is
    the length of a sequence and * is any number of trailing dimensions,
    including zero. They should be sorted in the order of decreasing length.

    Example:
        >>> from torch.nn.utils.rnn import pack_sequence
        >>> a = Variable(torch.Tensor([1,2,3]))
        >>> b = Variable(torch.Tensor([4,5]))
        >>> c = Variable(torch.Tensor([6]))
        >>> pack_sequence([a, b, c]])
        PackedSequence(data=
         1
         4
         6
         2
         5
         3
        [torch.FloatTensor of size 6]
        , batch_sizes=[3, 2, 1])

    Arguments:
        sequences (list[Variable]): A list of sequences of decreasing length.

    Returns:
        a :class:`PackedSequence` object
    """
    return pack_padded_sequence(pad_sequence(sequences), [v.size(0) for v in sequences])

def split(tensor, split_size_or_sections, dim=0):
    """Splits the tensor into chunks.
    If ``split_size_or_sections`` is an integer type, then ``tensor`` will be
    split into equally sized chunks (if possible).
    Last chunk will be smaller if the tensor size along a given dimension
    is not divisible by ``split_size``.
    If ``split_size_or_sections`` is a list, then ``tensor`` will be split
    into ``len(split_size_or_sections)`` chunks with sizes in ``dim`` according
    to ``split_size_or_sections``.

    Arguments:
        tensor (Tensor): tensor to split.
        split_size_or_sections (int) or (list(int)): size of a single chunk or
        list of sizes for each chunk
        dim (int): dimension along which to split the tensor.
    """
    if dim < 0:
        dim += tensor.dim()
    dim_size = tensor.size(dim)

    if isinstance(split_size_or_sections, int):
        split_size = split_size_or_sections
        num_splits = (dim_size + split_size - 1) // split_size
        last_split_size = split_size - (split_size * num_splits - dim_size)

        def get_split_size(i):
            return split_size if i < num_splits - 1 else last_split_size
        return tuple(tensor.narrow(int(dim), int(i * split_size), int(get_split_size(i))) for i
                     in range(0, num_splits))

    else:
        if dim_size != sum(split_size_or_sections):
            raise ValueError("Sum of split sizes exceeds tensor dim")
        split_indices = [0] + split_size_or_sections
        split_indices = torch.cumsum(torch.Tensor(split_indices), dim=0)

        return tuple(
            tensor.narrow(int(dim), int(start), int(length))
            for start, length in zip(split_indices, split_size_or_sections))

# The following methods allow running RNN on sequences that are not ordered by their length.

def _array_to_tensor(arr, cuda):
    t = torch.LongTensor(arr)
    if cuda:
        t = t.cuda()
    return Variable(t)

def _forward_and_reverse_indices(sequence):
    forward_indices = list(np.argsort(sequence))[::-1]
    reverse_indices = sorted(enumerate(forward_indices), key=lambda x: x[1])
    reverse_indices = list(zip(*reverse_indices)[0])
    return forward_indices, reverse_indices

def run_padded_rnn(sequences, rnn, init_state, seq_lengths, cuda):
    """ Runs rnn on the padded but unordered sequences.

    :param sequences: A tensor of shape batch_size x max_seq_length x dim;
    :param rnn: instance of nn.GRU;
    :param init_state: tensor num_layers x batch_size x hidden_size;
    :param seq_lengths: a list with batch_size integers <= max_seq_length;
    :param cuda: bool;
    :return: Variable num_layers x batch_size x hidden_size;
    """

    # We use pack_sequence which requires sequences sorted by their length in desc order.
    forward_indices, reverse_indices = _forward_and_reverse_indices(seq_lengths)

    ordered_sequences = torch.index_select(sequences, dim=0, index=_array_to_tensor(forward_indices, cuda))
    packed_sequences = pack_padded_sequence(ordered_sequences, sorted(seq_lengths, key=lambda x: -x), batch_first=True)
    _, state = rnn(packed_sequences, init_state)
    return torch.index_select(state, dim=1, index=_array_to_tensor(reverse_indices, cuda))

def run_rnn(sequences, rnn, init_state, cuda):
    """ Runs rnn on a list of unordered sequences.

    :param sequences: A list of batch_size elements each with shape seq_lengths[i] x dim;
    :param rnn: instance of nn.GRU;
    :param init_state: tensor num_layers x batch_size x hidden_size;
    :param cuda: bool;
    :return: Variable num_layers x batch_size x hidden_size;
    """

    seq_lengths = [v.size(0) for v in sequences]
    # We use pack_sequence which requires sequences sorted by their length in desc order.
    forward_indices, reverse_indices = _forward_and_reverse_indices(seq_lengths)

    ordered_sequences = [sequences[i] for i in forward_indices]
    packed_sequences = pack_sequence(ordered_sequences)
    _, state = rnn(packed_sequences, init_state)
    return torch.index_select(state, dim=1, index=_array_to_tensor(reverse_indices, cuda))


text:
""" Module randomDeformations

Provides a mean to produce random deformations.

Defines a function create_random_deformation() and a class to
keep a set of random deformations: RandomDeformations.

By setting the seed of the random number generator, the (pseudo) random
deformations can be repeated.

"""

import numpy as np
import pirt

# Due to the from_field_multiscale thingy on the end to make the deform
# injective this function becomes really slow.
def create_random_deformation_gaussian(im, amplitude=1, min_sigma=10, nblobs=50, seed=None):
    """ create_random_deformation(im, amplitude=1, min_sigma=10, nblobs=50, seed=None)

    Create a random deformation using Gaussian blobs or different scales.
    Returns a DeformationField instance.

    See also the class RandomDeformations.

    Parameters
    ----------
    im : numpy array
        The image to create a deformation field for.
    amplitude : scalar
        The relative amplitude of the deformations.
    min_sigma : scalar
        The smallest sigma to create Gaussian blobs for. The largest sigma
        is a quarter of the maximum shape element of the image.
    nblobs : integer
        The amount of Gaussian blobs to compose the deformation with.
    seed : int or None
        Seed for the random numbers to draw. If you want to repeat the
        same deformations, apply the same seed multiple times.

    """

    # Seed generator
    np.random.seed(seed)

    fields = []
    for dimension in range(len(im.shape)):

        # Make field that preserves sampling if it is an Aarray, and is
        # expressed using float32 dtype.
        field = pirt.Aarray(im.shape, fill=0.0, dtype='float32')
        if hasattr(im, 'sampling'):
            field.sampling = im.sampling

        for iter in range(nblobs):

            # Get randomly sampled variables
            if True:
                # Get sigma
                max_sigma = max(field.shape)/4
                t = 2**np.random.uniform(0,1) # between 1 and 2
                sigma = (t-1) * (max_sigma-min_sigma) + min_sigma

                # Get amplitude
                amp = np.random.uniform(-1,1) * sigma**0.5 * amplitude

                # Get position
                pos = []
                for d in range(field.ndim):
                    tmp = np.random.uniform(0,field.shape[d])
                    pos.append(int(tmp))

            # Create patch
            patch = pirt.gaussfun.gaussiankernel2(sigma,0,0)
            patch = amp * patch / patch.max()

            # Get tail
            tail = int(np.ceil( patch.shape[0]/2 ))

            # Put the patch in
            if True:

                # Get upper right and lower left
                pos1, pos2 = [], []
                for d in range(field.ndim):
                    pos1.append( pos[d] - tail )
                    pos2.append( pos[d] + tail )

                # Get patch indices
                pos3, pos4 = [], []
                for d in range(field.ndim):
                    pos3.append( 0 )
                    pos4.append( tail*2 )

                # Correct indices
                for d in range(field.ndim):
                    if pos1[d] < 0:
                        pos3[d] = -pos1[d]
                        pos1[d] = 0
                    if pos2[d] >= field.shape[d]:
                        pos4[d] = field.shape[d] - pos2[d] - 2
                        pos2[d] = field.shape[d] - 1

                # Build slice objects
                slices_field = []
                slices_patch = []
                for d in range(field.ndim):
                    slices_field.append( slice(pos1[d],pos2[d]+1) )
                    slices_patch.append( slice(pos3[d],pos4[d]+1) )

                # Put patch in
                field[tuple(slices_field)] += patch[tuple(slices_patch)]

        # Store field
        fields.append(field)

    # Make sure the deform is injectrive, and has frozenedges if required
    gridsampling = min_sigma / 2.0
    deform = pirt.DeformationFieldBackward.from_field_multiscale(fields, gridsampling,
                                            injective=True, frozenedge=True)
    # Apply a bit of Gaussian diffusion
    fields = []
    for field in deform:
        fields.append( pirt.diffuse(field, 1.0) )

    # Done
    return pirt.DeformationFieldBackward(*fields)

def create_random_deformation(im, amplitude=20, scale=50, n=50,
                                frozenedge=True, mapping='backward', seed=None):
    """ create_random_deformation(im, amplitude=20, scale=50, n=50,
                                frozenedge=True, mapping='backward', seed=None)

    Create a random deformation by creating two random sets of
    deformation vectors which are then converted to an injective
    deformation field using Lee and Choi's method.

    See also the class RandomDeformations.

    Parameters
    ----------
    im : numpy array or pirt.FieldDescription
        The image to create a deformation field for, or anything tha can be
        converted to a FieldDesctription instance.
    amplitude : scalar
        The relative amplitude of the deformations. The deformation vectors
        are randomly chosen with a maximum norm of this amplitude.
    scale : scalar
        The smallest resolution of the B-spline grid to regularize the
        deformation. Default 50.
    n : integer
        The amount of vectors to generate. Default 50.
    frozenedge : bool
        Whether the edges remain fixed or not (default True).
    mapping : {'forward', 'backward'}
        Whether the generated deformation uses forward or backward mapping.
        Default backward.
    seed : int or None
        Seed for the random numbers to draw. If you want to repeat the
        same deformations, apply the same seed multiple times.

    """

    # Seed generator
    np.random.seed(seed)

    # Get field description
    fd = pirt.FD(im)

    # Init pointsets
    pp1 = pirt.PointSet(fd.ndim)
    pp2 = pirt.PointSet(fd.ndim)

    # Generate random points
    for iter in range(n):

        # Generate random deformation vector
        p1, p2 = [],[]
        for i in range(fd.ndim):
            d = fd.ndim - i - 1

            # Get range and amplitude: in world coords!
            ran = fd.shape[d] * fd.sampling[d]
            amp = float(amplitude)

            # Generate numbers
            loc1 = np.random.uniform(0, ran)
            loc2 = np.random.uniform( max(0, loc1-amp), min(ran, loc1+amp))
            p1.append(loc1)
            p2.append(loc2)

        # Store
        pp1.append(*p1)
        pp2.append(*p2)

    # Get deformationfield class
    if mapping.lower() == 'forward':
        DeformationField = pirt.DeformationFieldForward
    elif mapping.lower() == 'backward':
        DeformationField = pirt.DeformationFieldBackward
    else:
        raise ValueError('Invalid mapping.')

    # Create field
    # mmm, it might not really matter whether we regularize the deform
    # using a forward or backward deform, does it?
    gridsampling = scale / 1.0
    deform = DeformationField.from_points_multiscale(fd, gridsampling,
                            pp1, pp2, injective=True, frozenedge=frozenedge)

    # Apply a bit of Gaussian diffusion
    fields = []
    for field in deform:
        fields.append( pirt.diffuse(field, 1.0) )

    # Done
    return DeformationField(*fields)

class RandomDeformations:
    """ RandomDeformations(im, amplitude=20, scale=50, n=50,
                                frozenedge=True, mapping='backward', seed=None)

    Represents a collection of random deformations. This can be used
    in experiments to test multiple methods on the same set of random
    deformations.

    It creates (and stores) random deformations on the fly when requested.

    The seed given to create_random_deformation is (seed + 100*index).
    It can be used to produce a random, yet repeatable set of deformations.
    For the sake of science, let's use a seed when doing experiments that
    are going to be published in a scientific medium, so they
    can be reproduced by others. For the sake of simplicity, let's agree to
    use the arbitrary seeds listed here: 1234 for training sets, 5678 for
    test sets.

    See also the function create_random_deformation()

    Example
    -------
    rd = RandomDeformations(im)
    deform1 = rd[0]
    deform2 = rd[1]
    # etc.

    """

    def __init__(self, im, amplitude=20, scale=50, n=50,
                                frozenedge=True, mapping='backward', seed=None):
        # Store image description
        self._fd = pirt.FD(im)
        # Store other parameters
        self._amplitude = float(amplitude)
        self._scale = float(scale)
        self._n = int(n)
        #
        self._frozenedge = bool(frozenedge)
        self._mapping = mapping
        self._seed = seed

        # Store dict with deforms
        self._deforms = {}

    def __getitem__(self, index):
        """ Get the deformation at the specified index. If it does not yet
        exist, will create a new deformation using arguments specified
        during initialization.
        """

        # Check
        if not isinstance(index, int) or index<0:
            raise ValueError('Can only index using (non-negative) integers.')

        # Create item if not already there
        if index not in self._deforms.keys():
            seed = None
            if self._seed is not None:
                seed = self._seed + index*100
            deform = create_random_deformation( self._fd, self._amplitude,
                                                self._scale, self._n,
                                                self._frozenedge, self._mapping,
                                                seed )
            self._deforms[index] = deform

        # Return
        return self._deforms[index]

    def get(self, index, *args, **kwargs):
        """ get(index, *args, **kwargs)

        Get the deformation at the specified index. If it does not yet
        exist, will create a new deformation using the specified arguments.

        Note that this will not take the seed into account.

        """

        # Create item if not already there
        if index not in self._deforms.keys():
            deform = create_random_deformation(self._fd, *args, **kwargs)
            self._deforms[index] = deform

        # Return
        return self._deforms[index]

max_stars_repo_name: almarklein/pirt

max_issues_repo_name: almarklein/pirt

max_forks_repo_name: almarklein/pirt


text:
r"""Puiseux Series Ring :mod:`abelfunctions.puiseux_series_ring`
============================================================

The ring of Puiseux series. Defines how to construct an element of the ring and
how other rings can coerce into this ring.

Classes
-------

.. autosummary::

    PuiseuxSeriesRing_generic
    PuiseuxSeriesRing_domain
    PuiseuxSeriesRing_field

Functions
---------

.. autosummary::

    PuiseuxSeriesRing
    is_PuiseuxSeriesRing

Examples
--------

See :mod:`abelfunctions.puiseux_series_ring_element` for examples.

Contents
--------
"""
import weakref

from sage.all import parent
from sage.rings.all import IntegralDomain, CommutativeRing, Field

from abelfunctions.puiseux_series_ring_element import (
    is_PuiseuxSeries,
    PuiseuxSeries,
)
from sage.categories.complete_discrete_valuation import CompleteDiscreteValuationFields
from sage.categories.fields import Fields
from sage.rings.laurent_series_ring import (
    is_LaurentSeriesRing,
    LaurentSeriesRing,
)
from sage.rings.laurent_series_ring_element import is_LaurentSeries
from sage.rings.power_series_ring import (
    is_PowerSeriesRing,
    PowerSeriesRing,
)
from sage.rings.power_series_ring_element import is_PowerSeries

puiseux_series = {}
def PuiseuxSeriesRing(base_ring, name=None, names=None, default_prec=None, sparse=False):
    if not names is None: name = names
    if name is None:
        raise TypeError('You must specify the name of the indeterminate of the Puiseux series ring.')

    if default_prec is None:
        from sage.misc.defaults import series_precision
        default_prec = series_precision()

    global puiseux_series
    key = (base_ring, name, default_prec, sparse)
    if key in puiseux_series:
        x = puiseux_series[key]()
        if x is not None: return x

    if isinstance(base_ring, Field):
        R = PuiseuxSeriesRing_field(base_ring, name, default_prec, sparse)
    elif isinstance(base_ring, IntegralDomain):
        R = PuiseuxSeriesRing_domain(base_ring, name, default_prec, sparse)
    elif isinstance(base_ring, CommutativeRing):
        R = PuiseuxSeriesRing_generic(base_ring, name, default_prec, sparse)
    else:
        raise TypeError("base_ring must be a commutative ring")
    puiseux_series[key] = weakref.ref(R)
    return R

def is_PuiseuxSeriesRing(x):
    return isinstance(x, PuiseuxSeriesRing_generic)

class PuiseuxSeriesRing_generic(CommutativeRing):
    def __init__(self, base_ring, name=None, default_prec=None, sparse=False,
                 category=None):
        CommutativeRing.__init__(
            self, base_ring, names=name,
            category=getattr(self, '_default_category', Fields()))

        # If self is R(( x^(1/e) )) then the corresponding Laurent series
        # ring will be R(( x ))
        self._laurent_series_ring = LaurentSeriesRing(
            base_ring, name=name, default_prec=default_prec, sparse=sparse)

    def base_extend(self, R):
        if R.has_coerce_map_from(self.base_ring()):
            return self.change_ring(R)
        else:
            raise TypeError("no valid base extension defined")

    def change_ring(self, R):
        return PuiseuxSeriesRing(R, self.variable_name(),
                                 default_prec=self.default_prec(),
                                 sparse=self.is_sparse())

    def is_sparse(self):
        return self.laurent_series_ring().is_sparse()

    def is_field(self, proof = True):
        return self.base_ring().is_field()

    def is_dense(self):
        return self.laurent_series_ring().is_dense()

    def __reduce__(self):
        return self.__class__, (self.base_ring(), self.variable_name(),
                                self.default_prec(), self.is_sparse())

    def _repr_(self):
        s = "Puiseux Series Ring in %s over %s"%(self.variable_name(), self.base_ring())
        if self.is_sparse():
            s = 'Sparse ' + s
        return s

    Element = PuiseuxSeries
    def _element_constructor_(self, x, e=1):
        r"""Construct a Puiseux series from `x`.

        INPUT:

        - ``x`` -- an object that can be converted into a Puiseux series in (x-a)

        - ``a`` -- (default: 0) the series is in powers of (var - a)

        - ``e`` -- (default: 1) the ramification index of the series
        """
        P = parent(x)

        # 1. x is a Puiseux series belonging to this ring
        if isinstance(x, self.element_class) and P is self:
            return x
        # 2. x is a Puiseux series but not an element of this ring. the laurent
        #    part should be coercible to the laurent series ring of self
        elif isinstance(x, self.element_class):
            l = self.laurent_series_ring()(x.laurent_part)
            e = x.ramification_index
        # 3. x is a member of the base ring then convert x to a laurent series
        #    and set the ramificaiton index of the Puiseux series to 1.
        elif P is self.base_ring():
            l = self.laurent_series_ring()(x)
            e = 1
        # 4. x is a Laurent or power series with the same base ring
        elif ((is_LaurentSeries(x) or is_PowerSeries(x))
              and P is self.base_ring()):
            l = self.laurent_series_ring()(x)
        # 5. everything else: try to coerce to laurent series ring
        else:
            l = self.laurent_series_ring()(x)
            e = 1

        return self.element_class(self, l, e=e)

    def _coerce_map_from_(self, P):
        r"""Return a coercion map from `P` to `self`, or `True`, or `None`.

        The following rings admit a coercion map to the Puiseux series ring
        `A((x-a)^(1/e))`:

        - any ring that admits a coercion map to `A`

        - any Laurent series ring, power series ring, or polynomial ring in the
          variable `(x-a)` over a ring admitting a coercion map to `A`

        - any Puiseux series ring with the same center `a` and ramification
          index equal to a multiple of `self`'s ramification index. For
          example, Puiseux series in (x-a)^(1/2) can be interpreted as Puiseux
          series in (x-a)^(1/4).

        """
        # any ring that has a coercion map to A
        A = self.base_ring()
        if A is P:
            return True
        f = A.coerce_map_from(P)
        if f is not None:
            return self.coerce_map_from(A) * f

        # Laurent series rings, power series rings, and polynomial rings with
        # the same variable name and the base rings are coercible
        if ((is_PuiseuxSeriesRing(P) or is_LaurentSeriesRing(P) or
             is_PowerSeriesRing(P))
            and P.variable_name() == self.variable_name()
            and A.has_coerce_map_from(P.base_ring())):
            return True

        # # other Puiseux series rings with the same variable name and
        # # center. Puiseux series rings with difference ramification indices are
        # # coercible to each other.
        # if (is_PuiseuxSeriesRing(P) and
        #     P.variable_name() == self.variable_name()):
        #     return True

    def gen(self, n=0):
        if n != 0:
            raise IndexError("Generator n not defined.")
        try:
            return self.__generator
        except AttributeError:
            #l = self.laurent_series_ring()([0,1])
            self.__generator = PuiseuxSeries(self, [0,1], e=1)
            return self.__generator

    def ngens(self):
        return 1

    def laurent_series_ring(self):
        return self._laurent_series_ring

    def default_prec(self):
        return self.laurent_series_ring().default_prec()

class PuiseuxSeriesRing_domain(PuiseuxSeriesRing_generic, IntegralDomain):
    def __init__(self, base_ring, name=None, default_prec=None, sparse=False):
        PuiseuxSeriesRing_generic.__init__(self, base_ring, name, default_prec, sparse)

class PuiseuxSeriesRing_field(PuiseuxSeriesRing_generic, Field):
    _default_category = CompleteDiscreteValuationFields()

    def __init__(self, base_ring, name=None, default_prec=None, sparse=False):
        PuiseuxSeriesRing_generic.__init__(self, base_ring, name, default_prec, sparse)

max_stars_repo_name: RParini/abelfunctions

max_issues_repo_name: RParini/abelfunctions

max_forks_repo_name: RParini/abelfunctions

reason: from sage


text:
# Copyright 2018-2020 Ivan Alles. See also the LICENSE file.

import enum
import json
import os

import numpy as np
import cv2
import tensorflow as tf
from tensorflow import keras

from localizer import utils

class Object:
    """ A predicted object. Duck-type compatible with dataset.Object. """
    def __init__(self, x, y, angle, category, confidence):
        self.origin = np.array([x, y], dtype=np.float32)
        self.angle = angle  # Angle in radiance
        self.category = int(category)
        self.confidence = confidence

    def __str__(self):
        return f'{self.origin[0]:.2f} {self.origin[1]:.2f} {np.rad2deg(self.angle):.2f} ' \
               f'{self.category} {self.confidence:.2f}'

class TrainingModelChannels(enum.IntEnum):
    """ Indices of output channels. """
    Y = 0  # y-coordinate.
    X = 1  # x-coordinate.
    SA = 2  # sin(angle).
    CA = 3  # cos(angle).
    COUNT = 4  # Number of channels.

class PredictionModelOutputs(enum.IntEnum):
    """ Indices of prediction model outputs. """
    OBJECTS = 0  # Objects, a 2d array num_objects x (x, y, angle, category, confidence)
    # The rest is used for diagnostics
    MODEL = 1  # Training model output.
    CONFIDENCE = 2  # Confidence level.
    AVERAGE_POS = 3  # Average x, y positions.
    AVERAGE_ANGLE = 4  # Average sin(angle), cos(angle).
    OUTPUT_WINDOW_POS = 5  # Window function output.

class Localizer:
    """
    Predicts object positions, orientations, and categories on an image.
    """

    def __init__(self, config_file_name):
        with open(os.path.join(config_file_name), encoding='utf-8') as f:
            self._cfg = json.load(f)

        self._diag = False
        self._diag_dir = 'localizer_diag'

        self._model_dir = os.path.dirname(config_file_name)
        self._model_path = os.path.join(self._model_dir, 'model.tf')

        self._sigma = self._cfg['sigma']

        self._confidence_ksize = self._cfg['confidence_ksize']
        self._pos_ksize = self._cfg['pos_ksize']
        self._angle_ksize = self._cfg['angle_ksize']

        self._model_scale = float(self._cfg['model_scale'])
        self._input_scale = self._cfg['input_scale']

        self._image_shape = None

    @property
    def cfg(self):
        return self._cfg

    @property
    def diag(self):
        return self._diag

    @diag.setter
    def diag(self, value):
        self._diag = value
        if self._diag:
            os.makedirs(self._diag_dir, exist_ok=True)

    @property
    def diag_dir(self):
        return self._diag_dir

    @diag_dir.setter
    def diag_dir(self, value):
        self._diag_dir = value
        if self._diag:
            os.makedirs(self._diag_dir, exist_ok=True)

    def _update_input_image_parameters(self, image_shape):
        if self._image_shape == image_shape:
            return

        self._image_shape = image_shape

        input_size = tuple(np.ceil(np.array(image_shape[:2]) * self._input_scale).astype(int))

        color_channels = image_shape[2] if len(image_shape) > 2 else 1
        self._model_input_shape = tuple(input_size) + (color_channels,)

        self._create_model()

        # Transform to convert from image to input.
        self._input_t_image = utils.make_transform2(self._input_scale)
        input_t_output = utils.make_transform2(1 / self._model_scale)
        self._image_t_output = np.dot(np.linalg.inv(self._input_t_image), input_t_output)

    @staticmethod
    def _compute_pos_kernels(size, sigma):
        """
        Compute kernels for positions.

        pos = (y, x)
        gaussian = exp(-0.5 * (pos/sigma)**2).
        :param size - size of the kernel. Must be an odd integer.
        :return:  pos tensor (size, size, 2), gaussian tensor (size, size).
        """

        if size % 2 != 1:
            raise ValueError('Kernel size must be odd')

        hs = size // 2
        row = -np.array(range(-hs, hs + 1), dtype=np.float32)
        pos = np.zeros((size, size, 2), dtype=np.float32)
        pos[:, :, 1] = np.broadcast_to(row, (size, size))
        pos[:, :, 0] = pos[:, :, 1].T

        n2 = ((pos / sigma) ** 2).sum(axis=2)
        gaussian = np.exp(-0.5 * n2)
        return pos, gaussian

    def _create_model(self):
        model = keras.models.load_model(self._model_path)

        if self._diag:
            keras.utils.plot_model(model, to_file=os.path.join(self._diag_dir, 'model.svg'), dpi=50, show_shapes=True)

        # Compute model with dummy values to let it compute the output shape.
        dummy_input = np.zeros((1,) + self._model_input_shape)
        self._model_output_shape = model.predict(dummy_input).shape[1:]

        self._category_count = model.output.shape[1]

        output_pos = model.output[:, :, :, :, TrainingModelChannels.Y:TrainingModelChannels.X + 1]
        output_angle = model.output[:, :, :, :, TrainingModelChannels.SA:TrainingModelChannels.CA + 1]

        scale = np.full(2, 1. / self._sigma)

        def make_window(v):
            sr2 = tf.reduce_sum(tf.square(v * scale), axis=-1, keepdims=True)
            gaussian = tf.math.exp(-0.5 * sr2)
            window = v * scale * gaussian
            return window

        output_window_pos = make_window(output_pos)

        def show_kernel(k, name, factor):
            for i in range(k.shape[2]):
                im = cv2.resize(k[:, :, i] * factor, (0, 0), fx=10, fy=10, interpolation=cv2.INTER_AREA)
                cv2.imshow(name + f'.i{i}', utils.red_green(im))
            cv2.waitKey(0)

        p, g = self._compute_pos_kernels(self._confidence_ksize, self._sigma)
        k = p / self._sigma * np.expand_dims(g, 2)
        # Normalize so that the convolution to itself gives 1.
        s = (k * k).sum(axis=(0, 1), keepdims=True) * 2
        if (s == 0).any():
            raise ValueError('Confidence kernel is zero, increase kernel size')
        confidence_kernel = k / s

        if False:  # Test code
            show_kernel(confidence_kernel, 'confidence_kernel', 20)

        confidence_map = tf.nn.conv2d(output_window_pos, np.expand_dims(confidence_kernel, 3),
                                 strides=1,
                                 padding='SAME', name='confidence_map')

        confidence_loc_max = (tf.nn.max_pool(confidence_map, 3, 1, 'SAME', 'NCDHW') - confidence_map) == 0
        confidence_thr = tf.math.logical_and(confidence_loc_max, confidence_map >= self._cfg['confidence_thr'])
        confidence = confidence_map * tf.cast(confidence_thr, tf.float32)

        def make_average_function(output, ksize, name):
            _, k = self._compute_pos_kernels(ksize, self._sigma)
            k /= k.sum()
            if False:  # Test code
                show_kernel(np.expand_dims(k, 2), name + '_kernel', 10)

            kernel = np.zeros((ksize, ksize, 2, 2))
            for i in range(2):
                kernel[:, :, i, i] = k

            average_func = tf.nn.conv2d(output, kernel,
                                        strides=1,
                                        padding='SAME',
                                        name=name)
            return average_func

        average_pos = make_average_function(output_pos, self._pos_ksize, name='average_pos')
        average_angle = make_average_function(output_angle, self._angle_ksize, name='average_angle')

        confidence = tf.squeeze(confidence, -1)
        local_max_idx = tf.where(confidence)
        category = tf.cast(local_max_idx[:, 1], tf.float32)
        yx = tf.gather_nd(average_pos, local_max_idx) + tf.cast(local_max_idx[:, 2:], np.float32)
        sc = tf.gather_nd(average_angle, local_max_idx)
        angle = tf.math.atan2(sc[:, 0], sc[:, 1])
        conf = tf.gather_nd(confidence, local_max_idx)
        objects = tf.stack([yx[:, 1], yx[:, 0], angle, category, conf], 1)

        # Add outputs in the order defined by PredictionModelOutputs.
        all_outputs = [objects]
        if self._diag:
            all_outputs += [model.output, confidence, average_pos, average_angle, output_window_pos]
        self._model = keras.Model(inputs=model.input, outputs=all_outputs)

        # This stopped working with TF 2.4.
        # if self._diag:
        #     tf.keras.utils.plot_model(self._model, to_file=os.path.join(self._diag_dir, 'prediction_model.svg'),
        #                               dpi=50, show_shapes=True)

        if self._cfg.get('save_prediction_model', False):
            self._model.save(os.path.join(self._model_dir, 'prediction_model.tf'))

    def predict(self, image):
        """
        param: image. Must have the correct number of input channels (RGB or grayscale).
        return: a list of predicted objects.
        """

        image = image.astype(np.float32)

        def pad_size(size, pad_to):
            return pad_to - size % pad_to if size % pad_to > 0 else 0

        pad_to = self._cfg.get('pad_to', 0)
        if pad_to:
            image = np.pad(image, (
                (0, pad_size(image.shape[0], pad_to)),
                (0, pad_size(image.shape[1], pad_to)),
                (0, 0)))

        self._update_input_image_parameters(image.shape)

        batch_size = 1

        input = cv2.warpAffine(image, self._input_t_image[:2, :],
                               (self._model_input_shape[1::-1]),
                               flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)

        class Batch:
            pass

        batch = Batch()
        batch.inputs = np.expand_dims(input, 0)

        result = self._model.predict(batch.inputs)

        if self._diag:
            batch.output_on_image = np.zeros((batch_size, self._category_count, 2) + image.shape)

            for bi in range(batch_size):
                confidence = result[PredictionModelOutputs.CONFIDENCE][bi]
                average_pos = result[PredictionModelOutputs.AVERAGE_POS][bi]
                average_angle = result[PredictionModelOutputs.AVERAGE_ANGLE][bi]

                if self._diag:
                    batch.outputs = np.expand_dims(result[PredictionModelOutputs.MODEL][bi], 0)
                    batch.output_window_pos = np.expand_dims(result[PredictionModelOutputs.OUTPUT_WINDOW_POS][bi], 0)
                    batch.confidence = np.expand_dims(confidence, 0)
                    batch.average_pos = np.expand_dims(average_pos, 0)
                    batch.average_angle = np.expand_dims(average_angle, 0)

                    def blend(img, data, factor):
                        alpha = 0.5
                        data = cv2.warpAffine(data, self._image_t_output[:2, :],
                                              (image.shape[1], image.shape[0]),
                                              flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
                        data = utils.red_green(data * factor)
                        return alpha * data + (1 - alpha) * img

                    for cat in range(self._category_count):
                        batch.output_on_image[bi][cat][0] = blend(image, batch.outputs[0, cat, :, :, 0], 1)
                        batch.output_on_image[bi][cat][1] = blend(image, batch.outputs[0, cat, :, :, 1], 1)

                    utils.save_batch_as_images(batch, self._diag_dir, prefix='{:02}'.format(bi), fmt='{0}{2}.png')

        if type(result) == list:
            objects = result[PredictionModelOutputs.OBJECTS]
        else:
            objects = result

        pos = np.hstack([objects[:, :2], np.ones((objects.shape[0], 1))])
        pos = np.dot(pos, self._image_t_output.T)[:, :2]

        predictions = []
        for i in range(len(objects)):
            predictions.append(Object(pos[i][0], pos[i][1], objects[i][2], objects[i][3], objects[i][4]))

        return predictions


text:
from __future__ import division, unicode_literals, print_function, absolute_import

from labscript_utils import check_version
check_version('labscript', '2.0.1', '4')
check_version('zprocess', '2.4.8', '4')
# # from labscript_utils import PY2
# if PY2:
#     str = unicode

from labscript_devices import BLACS_tab
from labscript import Device, LabscriptError, set_passed_properties
import numpy as np
from enum import Enum

class Picomotors(Device):
    description = 'Interface for setting picomotor values'
    @set_passed_properties(
        property_names = {
            "connection_table_properties": ["BIAS_port"],
        }
    )
    def __init__(self, name,  BIAS_port, **kwargs):
        Device.__init__(self, name, **kwargs)
        self.BLACS_connection = BIAS_port

    def do_checks(self):
        pass

    def generate_code(self, hdf5_file):
        group = self.init_device_group(hdf5_file)
        pass

import os

from qtutils.qt.QtCore import *
from qtutils.qt.QtGui import *

from blacs.tab_base_classes import Worker, define_state
from blacs.tab_base_classes import MODE_MANUAL, MODE_TRANSITION_TO_BUFFERED, MODE_TRANSITION_TO_MANUAL, MODE_BUFFERED

from blacs.device_base_class import DeviceTab

from qtutils import UiLoader
import qtutils.icons

@BLACS_tab
class PicomotorsTab(DeviceTab):
    def initialise_GUI(self):
        layout = self.get_tab_layout()
        ui_filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'camera.ui')
        self.ui = UiLoader().load(ui_filepath)
        layout.addWidget(self.ui)

        port = int(self.settings['connection_table'].find_by_name(self.settings["device_name"]).BLACS_connection)
        self.ui.port_label.setText(str(port))

        self.ui.check_connectivity_pushButton.setIcon(QIcon(':/qtutils/fugue/arrow-circle'))

        self.ui.host_lineEdit.returnPressed.connect(self.update_settings_and_check_connectivity)
        self.ui.use_zmq_checkBox.toggled.connect(self.update_settings_and_check_connectivity)
        self.ui.check_connectivity_pushButton.clicked.connect(self.update_settings_and_check_connectivity)

    def get_save_data(self):
        return {'host': str(self.ui.host_lineEdit.text()), 'use_zmq': self.ui.use_zmq_checkBox.isChecked()}

    def restore_save_data(self, save_data):
        if save_data:
            host = save_data['host']
            self.ui.host_lineEdit.setText(host)
            if 'use_zmq' in save_data:
                use_zmq = save_data['use_zmq']
                self.ui.use_zmq_checkBox.setChecked(use_zmq)
        else:
            self.logger.warning('No previous front panel state to restore')

        # call update_settings if primary_worker is set
        # this will be true if you load a front panel from the file menu after the tab has started
        if self.primary_worker:
            self.update_settings_and_check_connectivity()

    def initialise_workers(self):
        worker_initialisation_kwargs = {'port': self.ui.port_label.text()}
        self.create_worker("main_worker", PicomotorsWorker, worker_initialisation_kwargs)
        self.primary_worker = "main_worker"
        self.update_settings_and_check_connectivity()

    @define_state(MODE_MANUAL, queue_state_indefinitely=True, delete_stale_states=True)
    def update_settings_and_check_connectivity(self, *args):
        icon = QIcon(':/qtutils/fugue/hourglass')
        pixmap = icon.pixmap(QSize(16, 16))
        status_text = 'Checking...'
        self.ui.status_icon.setPixmap(pixmap)
        self.ui.server_status.setText(status_text)
        kwargs = self.get_save_data()
        responding = yield(self.queue_work(self.primary_worker, 'update_settings_and_check_connectivity', **kwargs))
        self.update_responding_indicator(responding)

    def update_responding_indicator(self, responding):
        if responding:
            icon = QIcon(':/qtutils/fugue/tick')
            pixmap = icon.pixmap(QSize(16, 16))
            status_text = 'Server is responding'
        else:
            icon = QIcon(':/qtutils/fugue/exclamation')
            pixmap = icon.pixmap(QSize(16, 16))
            status_text = 'Server not responding'
        self.ui.status_icon.setPixmap(pixmap)
        self.ui.server_status.setText(status_text)

class PicomotorsWorker(Worker):
    def init(self):
        global socket; import socket
        global zmq; import zmq
        global zprocess; import zprocess
        global shared_drive; import labscript_utils.shared_drive as shared_drive

        self.host = ''
        self.use_zmq = False

    def update_settings_and_check_connectivity(self, host, use_zmq):
        self.host = host
        self.use_zmq = use_zmq
        if not self.host:
            return False
        if not self.use_zmq:
            return self.initialise_sockets(self.host, self.port)
        else:
            response = zprocess.zmq_get_string(self.port, self.host, data='hello')
            if response == 'hello':
                return True
            else:
                raise Exception('invalid response from server: ' + str(response))

    def initialise_sockets(self, host, port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        assert port, 'No port number supplied.'
        assert host, 'No hostname supplied.'
        assert str(int(port)) == port, 'Port must be an integer.'
        s.settimeout(10)
        s.connect((host, int(port)))
        s.send(b'hello\r\n')
        response = s.recv(1024).decode('utf8')
        s.close()
        if 'hello' in response:
            return True
        else:
            raise Exception('invalid response from server: ' + response)

    def transition_to_buffered(self, device_name, h5file, initial_values, fresh):
        h5file = shared_drive.path_to_local(h5file)
        if not self.use_zmq:
            return self.transition_to_buffered_sockets(h5file,self.host, self.port)
        response = zprocess.zmq_get_string(self.port, self.host, data=h5file)
        if response != 'ok':
            raise Exception('invalid response from server: ' + str(response))
        response = zprocess.zmq_get_string(self.port, self.host, timeout = 10)
        if response != 'done':
            raise Exception('invalid response from server: ' + str(response))
        return {} # indicates final values of buffered run, we have none

    def transition_to_buffered_sockets(self, h5file, host, port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(120)
        s.connect((host, int(port)))
        s.send(b'%s\r\n' % h5file.encode('utf8'))
        response = s.recv(1024).decode('utf8')
        if not 'ok' in response:
            s.close()
            raise Exception(response)
        response = s.recv(1024).decode('utf8')
        if not 'done' in response:
            s.close()
            raise Exception(response)
        return {} # indicates final values of buffered run, we have none

    def transition_to_manual(self):
        if not self.use_zmq:
            return self.transition_to_manual_sockets(self.host, self.port)
        response = zprocess.zmq_get_string(self.port, self.host, 'done')
        if response != 'ok':
            raise Exception('invalid response from server: ' + str(response))
        response = zprocess.zmq_get_string(self.port, self.host, timeout = 10)
        if response != 'done':
            raise Exception('invalid response from server: ' + str(response))
        return True # indicates success

    def transition_to_manual_sockets(self, host, port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(120)
        s.connect((host, int(port)))
        s.send(b'done\r\n')
        response = s.recv(1024).decode('utf8')
        if response != 'ok\r\n':
            s.close()
            raise Exception(response)
        response = s.recv(1024).decode('utf8')
        if not 'done' in response:
            s.close()
            raise Exception(response)
        return True # indicates success

    def abort_buffered(self):
        return self.abort()

    def abort_transition_to_buffered(self):
        return self.abort()

    def abort(self):
        if not self.use_zmq:
            return self.abort_sockets(self.host, self.port)
        response = zprocess.zmq_get_string(self.port, self.host, 'abort')
        if response != 'done':
            raise Exception('invalid response from server: ' + str(response))
        return True # indicates success

    def abort_sockets(self, host, port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(120)
        s.connect((host, int(port)))
        s.send(b'abort\r\n')
        response = s.recv(1024).decode('utf8')
        if not 'done' in response:
            s.close()
            raise Exception(response)
        return True # indicates success

    def program_manual(self, values):
        return {}

    def shutdown(self):
        return

max_stars_repo_name: aviplane/labscript-devices

max_issues_repo_name: aviplane/labscript-devices

max_forks_repo_name: aviplane/labscript-devices


text:
"""
This file contains class definition for elements in a set of Tangram.
Typically there are Triangles, Squares and Parallelograms.
Maybe add more elements in the future for 13, 14, 15... Tangram.

Also, this file defines some hyperparameters for calculating.
"""

import copy
from math import sin, cos
import numpy as np

import normal_tangram.utils as utils
from normal_tangram.config import PI, SQRT_2, ANGLE_NUM, STANDARD_SIZE, \
    ELEMENT_DICT, TRIANGLE_STATE, SQUARE_STATE, PARALLELOGRAM_STATE

class Point:

    def __init__(self, x=0., y=0.):
        self.x = float(x)
        self.y = float(y)

    def __eq__(self, other):
        """Define the equal operator between 2 points.
        Return True when the coordinates of 2 points are the same.
        """
        if self.x == other.x and self.y == other.y:
            return True
        return False

    def __add__(self, other):
        """Define the add operator between 2 points."""
        return Point(x=self.x + other.x, y=self.y + other.y)

    def __sub__(self, other):
        """Define the sub operator between 2 points.
        Can be used to calculate the vector.
        """
        return Point(x=self.x - other.x, y=self.y - other.y)

    def constrain(self, max_x, max_y):
        """Use max_x, max_y to keep point within mask."""
        self.x = max(min(self.x, max_x), 0)
        self.y = max(min(self.y, max_y), 0)

    def get_coordinate(self):
        return [self.x, self.y]

    def point_is_coincide_point(self, another_point, threshold):
        """If the distance between 2 points is within an error threshold,
        then we say they coincide with each other.
        """
        if utils.get_distance_point_to_point(self, another_point) < threshold:
            return True
        return False

    def display(self):
        """For debug."""
        print('coordinate:', self.get_coordinate())

class Segment:

    def __init__(self, p1, p2):
        self.p1 = copy.deepcopy(p1)
        self.p2 = copy.deepcopy(p2)
        self.midpoint = Point(x=(p1.x + p2.x) / 2., y=(p1.y + p2.y) / 2.)
        self.vec = self.p2 - self.p1

    def constrain(self, max_x, max_y):
        """Use max_x, max_y to keep point within mask."""
        self.p1.constrain(max_x, max_y)
        self.p2.constrain(max_x, max_y)
        self.midpoint = Point(x=(self.p1.x + self.p2.x) / 2., y=(self.p1.y + self.p2.y) / 2.)
        self.vec = self.p2 - self.p1

    def point_is_on_segment(self, another_point, threshold):
        """Judge whether a point is ON this segment.
        Endpoint of the segment is considered on here.
        threshold is a term that allows for tiny error (caused by non-integer of coordinates)
        """
        dist1 = utils.get_distance_point_to_point(self.p1, another_point)
        dist2 = utils.get_distance_point_to_point(self.p2, another_point)
        if dist1 < threshold or dist2 < threshold:
            # the point is one of the endpoints of this segment
            return True

        v1 = self.p1 - another_point
        v2 = self.p2 - another_point
        if abs(utils.inner_product(v1, v2) + dist1 * dist2) < threshold ** 2:
            return True
        return False

    def __segment_is_intersect_segment(self, another_segment, threshold):
        """Judge intersect using cross product.
        Won't be used outside the class so set it as private method.
        """
        # 2 segments are not parallel
        v1 = another_segment.p1 - self.p1
        v2 = another_segment.p2 - self.p1
        if utils.cross_product(v1, self.vec) * utils.cross_product(v2, self.vec) < -(threshold ** 4):
            return True
        return False

    def segment_is_cross_segment(self, another_segment, threshold):
        """Judge whether this segment is cross over another segment.
        Use mutual cross product to judge.
        Any parallel or even coinciding segments are not considered intersect here.
        __|__ is not considered intersect too. Only --|-- is considered intersect!
        threshold is a term that allows for tiny error (caused by non-integer of coordinates)
        """
        if self.point_is_on_segment(another_segment.p1, threshold) or \
                self.point_is_on_segment(another_segment.p2, threshold) or \
                another_segment.point_is_on_segment(self.p1, threshold) or \
                another_segment.point_is_on_segment(self.p2, threshold):
            # one point of one segment is on another segment
            return False

        if abs(self.vec.x * another_segment.vec.y - self.vec.y * another_segment.vec.x) < threshold ** 2:
            # 2 segments are parallel
            return False

        if self.__segment_is_intersect_segment(another_segment, threshold) and \
                another_segment.__segment_is_intersect_segment(self, threshold):
            return True
        return False

    def display(self):
        """For debug."""
        print('point1:', self.p1.get_coordinate())
        print('point2:', self.p2.get_coordinate())

class Element:
    """Abstract class for all elements."""

    def __init__(self, element_id, unit_length):
        """Actually we should init the class using init function."""
        self.points = None
        self.point_num = 0
        self.midpoint = None
        self.segments = []
        self.element_id = element_id
        self.unit_length = unit_length
        self.position = None
        self.state = None
        self.area = STANDARD_SIZE[ELEMENT_DICT[self.element_id]][0] * (self.unit_length ** 2)
        self.length = STANDARD_SIZE[ELEMENT_DICT[self.element_id]][-1] * self.unit_length
        self.size = 0

    def init(self, points):
        """Init element by setting points etc."""
        self.points = copy.deepcopy(points)
        self.point_num = len(points)
        self.midpoint = self.get_midpoint()
        self.segments = [
            Segment(self.points[i], self.points[(i + 1) % self.point_num]) for i in range(self.point_num)
        ]
        # add segments between every midpoints
        # in order to judge intersection more accurately
        self.segments += [
            Segment(p1=self.segments[i].midpoint,
                    p2=self.segments[(i + 1) % self.point_num].midpoint)
            for i in range(self.point_num)
        ]

    def set_points(self, p, position):
        """Calculate the coordinates for vertices of this element.
        Input:
            p: a point with coordinate
            position: can be used to determine angle
        """
        pass

    def get_midpoint(self):
        """Get the midpoint of the whole element by averaging all x and y coordinates."""
        x = 0.
        y = 0.
        for point in self.points:
            x += (point.x / self.point_num)
            y += (point.y / self.point_num)

        return Point(x=x, y=y)

    def constrain(self, max_x, max_y):
        """Use max_x, max_y to keep point within mask."""
        for point in self.points:
            point.constrain(max_x, max_y)
        for segment in self.segments:
            segment.constrain(max_x, max_y)

    def element_is_coincide_element(self, another_element, threshold):
        """Judge whether two elements coincide."""
        if type(self) != type(another_element):
            return False

        if type(self) == Triangle and type(another_element) == Triangle:
            if self.size != another_element.size:
                return False

        num = self.point_num
        flag = np.array([False for _ in range(num)])
        for i in range(num):
            for j in range(num):
                if self.points[i].point_is_coincide_point(another_element.points[j], threshold):
                    flag[i] = True
                    break
        if flag.all():
            return True
        return False

    def element_is_intersect_element(self, another_element, threshold):
        """Judge whether an element intersects with this element."""
        if self.element_is_coincide_element(another_element, threshold):
            return True
        for segment1 in self.segments:
            for segment2 in another_element.segments:
                if segment1.segment_is_cross_segment(segment2, threshold):
                    return True
        return False

    def point_is_inside_element(self, another_point, threshold):
        """Judge whether a point is within this element.
        A point on the edges of an element is not considered INSIDE here.
        """
        for segment in self.segments[:self.point_num]:
            if segment.point_is_on_segment(another_point, threshold):
                return False
        area = 0.
        for i in range(self.point_num):
            area += \
                utils.triangle_area([self.points[i], self.points[(i + 1) % self.point_num], another_point])
        if abs(area - self.area) < threshold ** 2:
            return True
        return False

    def display(self):
        """For debug."""
        print('element id:', self.element_id)
        print('state:', self.state)
        print('position:', self.position)
        print('length:', self.length)
        for i in range(self.point_num):
            print('point', i, 'coordinate:', self.points[i].get_coordinate())

class Triangle(Element):
    """Class for triangle. Note that point0 is right angle vertex."""

    def __init__(self, element_id, size=0, unit_length=0.):
        """
        Input:
            size: 0 means small, 1 means medium, 2 means large
        """
        super(Triangle, self).__init__(element_id, unit_length)

        assert size in range(3)
        self.size = size

    def init(self, points):
        """For triangle, I think we should add more segments."""
        super(Triangle, self).init(points)
        # add more segments for large triangle
        if self.element_id in ['1', '2']:
            self.segments += [
                Segment(p1=self.points[i], p2=self.segments[(i + 1) % 3].midpoint)
                for i in range(3)
            ]

    def set_points(self, p, position):
        """Calculate the coordinates for vertices of this triangle.
        Input:
            p: a point with its coordinate.
                Maybe p0 or p1 or p2, judging by position // ANGLE_NUM
            position: can be used to determine angle and type of p
        """
        # totally 3 states for triangles
        self.state = position // ANGLE_NUM
        assert self.state in range(TRIANGLE_STATE)
        position %= ANGLE_NUM
        self.position = position
        angle = 2. * PI * position / ANGLE_NUM

        if self.state == 0:
            self.__set_points_via_p0(p, angle)
        elif self.state == 1:
            self.__set_points_via_p1(p, angle)
        else:
            self.__set_points_via_p2(p, angle)

    def __set_points_via_p0(self, p0, angle):
        """Calculating the coordinates of this triangle's vertices using p0."""
        x0, y0 = p0.get_coordinate()
        p1 = Point(x=x0 + self.length * sin(angle),
                   y=y0 + self.length * cos(angle))
        p2 = Point(x=x0 + self.length * cos(angle),
                   y=y0 - self.length * sin(angle))
        self.init([p0, p1, p2])

    def __set_points_via_p1(self, p1, angle):
        """Calculating the coordinates of this triangle's vertices using p1."""
        x1, y1 = p1.get_coordinate()
        p0 = Point(x=x1 + self.length * sin(0.25 * PI + angle),
                   y=y1 + self.length * cos(0.25 * PI + angle))
        p2 = Point(x=x1 + SQRT_2 * self.length * sin(angle),
                   y=y1 + SQRT_2 * self.length * cos(angle))
        self.init([p0, p1, p2])

    def __set_points_via_p2(self, p2, angle):
        """Calculating the coordinates of this triangle's vertices using p2."""
        x2, y2 = p2.get_coordinate()
        p0 = Point(x=x2 + self.length * sin(angle),
                   y=y2 + self.length * cos(angle))
        p1 = Point(x=x2 + SQRT_2 * self.length * cos(0.25 * PI - angle),
                   y=y2 + SQRT_2 * self.length * sin(0.25 * PI - angle))
        self.init([p0, p1, p2])

class Square(Element):
    """Class for triangle."""

    def __init__(self, element_id, unit_length=0.):
        super(Square, self).__init__(element_id, unit_length)

    def set_points(self, p, position):
        """Calculate the coordinates for vertices of this square.
        Input:
            p: a point with coordinate
            position: can be used to determine angle
        """
        # totally 1 state for square
        self.state = position // ANGLE_NUM
        assert self.state in range(SQUARE_STATE)
        position %= ANGLE_NUM
        self.position = position
        angle = 2. * PI * position / ANGLE_NUM

        self.__set_points_via_p0(p, angle)

    def __set_points_via_p0(self, p0, angle):
        """Calculating the coordinates of this triangle's vertices using p0."""
        x0, y0 = p0.get_coordinate()
        p1 = Point(x=x0 + self.length * sin(angle),
                   y=y0 + self.length * cos(angle))
        p2 = Point(x=x0 + self.length * sin(angle) + self.length * cos(angle),
                   y=y0 + self.length * cos(angle) - self.length * sin(angle))
        p3 = Point(x=x0 + self.length * cos(angle),
                   y=y0 - self.length * sin(angle))
        self.init([p0, p1, p2, p3])

class Parallelogram(Element):
    """Class for Parallelogram. Note that point0 is the acute angle vertex."""

    def __init__(self, element_id, unit_length=0.):
        super(Parallelogram, self).__init__(element_id, unit_length)

    def set_points(self, p, position):
        """Calculate the coordinates for vertices of this parallelogram.
        Input:
            p: a point with its coordinate.
                Maybe p0 or p1, judging by position // ANGLE_NUM % 2
            position: can be used to determine angle and type of p
                Also, position // ANGLE_NUM // 2 is INVERSE STATE
        """
        # totally 4 states for parallelogram
        self.state = position // ANGLE_NUM
        assert self.state in range(PARALLELOGRAM_STATE)
        position %= ANGLE_NUM
        self.position = position
        angle = 2. * PI * position / ANGLE_NUM

        if self.state == 0:
            self.__set_points_via_p0(p, angle)
        elif self.state == 1:
            self.__set_points_via_p1(p, angle)
        elif self.state == 2:
            self.__set_points_via_inverse_p0(p, angle)
        else:
            self.__set_points_via_inverse_p1(p, angle)

    def __set_points_via_p0(self, p0, angle):
        """Calculating the coordinates of this parallelogram's vertices using p0."""
        x0, y0 = p0.get_coordinate()
        p1 = Point(x=x0 + SQRT_2 * self.length * sin(angle),
                   y=y0 + SQRT_2 * self.length * cos(angle))
        p2 = Point(x=x0 + SQRT_2 * self.length * sin(angle) + self.length * cos(0.25 * PI - angle),
                   y=y0 + SQRT_2 * self.length * cos(angle) + self.length * sin(0.25 * PI - angle))
        p3 = Point(x=x0 + self.length * cos(0.25 * PI - angle),
                   y=y0 + self.length * sin(0.25 * PI - angle))
        self.init([p0, p1, p2, p3])

    def __set_points_via_p1(self, p1, angle):
        """Calculating the coordinates of this parallelogram's vertices using p1."""
        x1, y1 = p1.get_coordinate()
        p0 = Point(x=x1 + SQRT_2 * self.length * sin(0.25 * PI - angle),
                   y=y1 - SQRT_2 * self.length * cos(0.25 * PI - angle))
        p2 = Point(x=x1 + self.length * sin(angle),
                   y=y1 + self.length * cos(angle))
        p3 = Point(x=x1 + SQRT_2 * self.length * sin(0.25 * PI - angle) + self.length * sin(angle),
                   y=y1 - SQRT_2 * self.length * cos(0.25 * PI - angle) + self.length * cos(angle))
        self.init([p0, p1, p2, p3])

    def __set_points_via_inverse_p0(self, p0, angle):
        """Calculating the coordinates of this parallelogram's vertices using inverse_p0."""
        x0, y0 = p0.get_coordinate()
        p1 = Point(x=x0 + self.length * sin(angle),
                   y=y0 + self.length * cos(angle))
        p2 = Point(x=x0 + self.length * sin(angle) + SQRT_2 * self.length * cos(0.25 * PI - angle),
                   y=y0 + self.length * cos(angle) + SQRT_2 * self.length * sin(0.25 * PI - angle))
        p3 = Point(x=x0 + SQRT_2 * self.length * cos(0.25 * PI - angle),
                   y=y0 + SQRT_2 * self.length * sin(0.25 * PI - angle))
        self.init([p0, p1, p2, p3])

    def __set_points_via_inverse_p1(self, p1, angle):
        """Calculating the coordinates of this parallelogram's vertices using inverse_p1."""
        x1, y1 = p1.get_coordinate()
        p0 = Point(x=x1 + self.length * sin(0.25 * PI - angle),
                   y=y1 - self.length * cos(0.25 * PI - angle))
        p2 = Point(x=x1 + SQRT_2 * self.length * sin(angle),
                   y=y1 + SQRT_2 * self.length * cos(angle))
        p3 = Point(x=x1 + self.length * sin(0.25 * PI - angle) + SQRT_2 * self.length * sin(angle),
                   y=y1 - self.length * cos(0.25 * PI - angle) + SQRT_2 * self.length * cos(angle))
        self.init([p0, p1, p2, p3])


text:
import torch
from torchvision import datasets, transforms
from img_datasets import CovidNetDataset
import numpy as np
from torch.autograd import Variable
import os
from utils import LoggerUtils

class Scheduler():
    """docstring for Scheduler"""

    def __init__(self, config, objects):
        super(Scheduler, self).__init__()
        self.config = config
        self.objects = objects
        self.epoch = 0
        self.initialize()

    def initialize(self):
        self.setup_data_pipeline()
        self.setup_training_params()
        log_config = {"log_path": self.config["log_path"]}
        self.logger = LoggerUtils(log_config)

    def get_split(self, dataset):

        dataset_size = len(dataset)
        indices = list(range(dataset_size))
        split = int(np.floor(self.config["train_split"] * dataset_size))
        np.random.shuffle(indices)

        train_indices, test_indices = indices[:split], indices[split:]
        train_dataset = torch.utils.data.Subset(dataset, train_indices)
        test_dataset = torch.utils.data.Subset(dataset, test_indices)
        return train_dataset, test_dataset

    def setup_data_pipeline(self):
        self.IM_SIZE = self.config["img_size"]
        trainTransform = transforms.Compose([
            transforms.Resize((self.IM_SIZE, self.IM_SIZE)),
            transforms.ToTensor()])

        train_config = {"transforms": trainTransform,
                        "root_path": self.config["dataset_path"] + "train/",
                        "file_path": self.config["dataset_path"] + "train_split_v3.txt"}

        test_config = {"transforms": trainTransform,
                        "root_path": self.config["dataset_path"] + "test/",
                        "file_path": self.config["dataset_path"] + "test_split_v3.txt"}

        if self.config["dataset"] == "covidnet_full":
            train_dataset = CovidNetDataset(train_config)
            test_dataset = CovidNetDataset(test_config)

        if self.config["split"] is True:
            train_dataset, test_dataset = self.get_split(dataset)

        self.trainloader = torch.utils.data.DataLoader(
            train_dataset, batch_size=self.config["train_batch_size"],
            shuffle=True, num_workers=5)

        self.testloader = torch.utils.data.DataLoader(
            test_dataset, batch_size=self.config["test_batch_size"],
            shuffle=False, num_workers=5)

    def setup_training_params(self):
        self.epoch = 0
        self.model = self.objects["model"]

        self.loss_fn = self.objects["loss_fn"]
        self.optim = self.objects["optim"]
        self.device = self.objects["device"]

        self.model_path = self.config["model_path"] + "/model_pred.pt"

    def test(self):
        self.model.eval()
        test_loss, pred_correct = 0, 0
        total = 0
        all_predictions, all_labels = np.array([]),np.array([])
        os.mkdir("{}/{}".format(self.config["log_path"], self.epoch))

        with torch.no_grad():
            for batch_idx, (data, labels) in enumerate(self.testloader):
                data = Variable(data).to(self.device)
                labels = Variable(labels).to(self.device)

                prediction = self.model(data)
                all_predictions = np.append(all_predictions, [prediction.detach().cpu().numpy()[:, 1]])
                all_labels = np.append(all_labels, [labels.detach().cpu().numpy()])

                test_loss += self.loss_fn(prediction, labels)
                pred_correct += (prediction.argmax(dim=1) ==
                                labels).sum().item()
                total += int(data.shape[0])

        test_loss /= total
        pred_acc = pred_correct / total

        self.logger.log_metrics("test/", all_predictions, all_labels, self.epoch)

        self.logger.log_scalar("test/loss", test_loss.item(), self.epoch)
        self.logger.log_scalar("test/pred_accuracy", pred_acc, self.epoch)

        self.logger.log_console("epoch {}, average test loss {:.4f}, pred_accuracy {:.3f}".format(self.epoch,
                                                              test_loss,
                                                              pred_acc))

    def train(self):
        train_loss = 0
        pred_correct_total = 0
        total = 0
        for batch_idx, (data, labels) in enumerate(self.trainloader):
            data = Variable(data).to(self.device)
            labels = Variable(labels).to(self.device)
            self.optim.zero_grad()

            prediction = self.model(data)

            # self.logger.log_metrics("train/", prediction, labels)

            loss = self.loss_fn(prediction, labels)
            pred_correct = (prediction.argmax(dim=1) == labels).sum().item()

            loss.backward()
            self.optim.step()

            train_loss += loss.item()
            pred_correct_total += pred_correct
            total += int(data.shape[0])

            step_num = len(self.trainloader) * self.epoch + batch_idx
            self.logger.log_scalar("train/loss", loss.item(), step_num)
            self.logger.log_scalar("train/accuracy", pred_correct_total / total,
                                   step_num)
            if batch_idx % 100 == 0:
                self.logger.log_console("train epoch {}, iter {}, loss {:.4f}, accuracy {:.3f}"
                                        .format(self.epoch, batch_idx,
                                                loss / len(data),
                                                pred_correct / len(data)))
                self.logger.log_model_stats(self.model)
                self.logger.save_model(self.model, self.model_path)

        self.logger.log_console("epoch {}, train loss {:.4f}, accuracy {:.3f}"
                                .format(self.epoch, train_loss / total,
                                        pred_correct_total / total))
        self.epoch += 1

max_stars_repo_name: tremblerz/covidscanner

max_issues_repo_name: tremblerz/covidscanner

max_forks_repo_name: tremblerz/covidscanner


text:
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

import numpy as np

import tensorflow as tf

from slim.data import dataset
from slim.data import dataset_data_provider
from slim.data import tfexample_decoder
from slim.datasets import datasets

OPENIMAGES_COUNTS = {'train': 1247361,
                     'val': 95181}

SST_COUNTS = {'train': 32649,
              'val': 3737}

LABEL_MAP = {i: s for i, s in enumerate(
    ['hold', 'rid', 'carry', 'eat', 'watch',
     'look', 'fly', 'swing', 'pull', 'hit', 'touch', 'throw', 'cast',
     'cut', 'read', 'catch', 'talk', 'drink', 'look',
     'swim', 'push', 'feed', 'graze', 'reflect', 'kick',
     'float', 'perch', 'brush', 'reach', 'pet', 'talk', 'serve',
     'sew', 'sniff', 'chase', 'lick', 'swing', 'hug',
     'lift', 'splash', 'spray']
    )}

def get_corners(rbox):
  ul_point = (tf.one_hot(tf.to_int32(rbox[0] * 55), 56)[:, None] *
              tf.one_hot(tf.to_int32(rbox[1] * 55), 56)[None, :])
  lr_point = (tf.one_hot(tf.to_int32(rbox[2] * 55), 56)[:, None] *
              tf.one_hot(tf.to_int32(rbox[3] * 55), 56)[None, :])
  box = tf.to_float(tf.concat([ul_point[:, :, None], lr_point[:, :, None]], 2))

  return box

def get_split_genome(name, root_dir, reader_class=tf.TFRecordReader):
  """Produces data split.

  Args:
    name: split name
    root_dir: dataset directory
    reader_class: reader class

  Returns:
    Dataset
  """

  file_pattern = os.path.join(root_dir, 'VG-%s_*' % name)

  num_samples = SST_COUNTS[name]

  keys_to_features = {
      'image/encoded': tf.FixedLenFeature(
          (), tf.string, default_value=''),
      'image/format': tf.FixedLenFeature(
          (), tf.string, default_value='jpeg'),
      'image/shape': tf.VarLenFeature(dtype=tf.int64),
      'image/relations/num': tf.VarLenFeature(dtype=tf.int64),
      'image/relations/predicates_raw': tf.FixedLenFeature((), tf.string),
      'image/relations/predicates': tf.VarLenFeature(dtype=tf.int64),
      'image/relations/predicates_label': tf.VarLenFeature(dtype=tf.int64),
      'image/relations/bboxes': tf.VarLenFeature(dtype=tf.int64),
      'image/relations/objects': tf.VarLenFeature(dtype=tf.int64),
      'image/relations/subjects': tf.VarLenFeature(dtype=tf.int64),
  }

  items_to_handlers = {
      'image': tfexample_decoder.Image('image/encoded'),
      'shape': tfexample_decoder.Tensor('image/shape'),
      'num_relations': tfexample_decoder.Tensor('image/relations/num'),
      'predicates_raw': tfexample_decoder.Tensor('image/relations/'
                                                 'predicates_raw'),
      'predicates': tfexample_decoder.Tensor('image/relations/predicates'),
      'predicates_label': tfexample_decoder.Tensor('image/relations/predicates_label'),
      'bboxes': tfexample_decoder.Tensor('image/relations/bboxes'),
      'objects': tfexample_decoder.Tensor('image/relations/objects'),
      'subjects': tfexample_decoder.Tensor('image/relations/subjects')
  }

  decoder = tfexample_decoder.TFExampleDecoder(keys_to_features,
                                               items_to_handlers)

  return dataset.Dataset(
      data_sources=file_pattern,
      reader=reader_class,
      decoder=decoder,
      num_samples=num_samples,
      items_to_descriptions={})

def setup_data_stream_genome(name,
                             batch_size,
                             image_res,
                             shuffle=True,
                             root_dir=):
  """Setup data stream for visual genome.

  Args:
    name: split name
    root_dir: dataset home
    batch_size: batch size
    num_classes: number of classes
    shuffle: shuffle data

  Returns:
    image, raw segmetation, downsampled segmentation,
    raw caption and caption tensroflow streams
  """

  data = get_split_genome(name, root_dir)
  data_provider = dataset_data_provider.DatasetDataProvider(data,
                                                            num_readers=4,
                                                            shuffle=shuffle)
  (image, shape, num_relations,
   predicates, bboxes, obj, subj) = data_provider.get(['image',
                                                       'shape',
                                                       'num_relations',
                                                       'predicates_label',
                                                       'bboxes',
                                                       'objects',
                                                       'subjects'])
  # Fix random relation
  num_relations = num_relations[0]
  i_rel = tf.random_uniform([], 0, num_relations, dtype=tf.int64)

  # Resize image
  image = tf.image.resize_bilinear(image[None], [image_res, image_res])[0]
  image = (image - 127.5) / 127.5

  predicates = tf.one_hot(predicates, 41)

  # Produce target segmentation
  shape = tf.to_float(shape)
  b = tf.to_float(bboxes)
  b = tf.reshape(tf.reshape(b, [-1, 8])[i_rel], [2, 4])
  b = tf.concat([b[:, 0:1] / shape[0], b[:, 1:2] / shape[1],
                 (b[:, 0:1] + b[:, 2:3]) / shape[0],
                 (b[:, 1:2] + b[:, 3:4]) / shape[1]], axis=1)

  b_rev = tf.concat([b[:, 0:1], 1 - b[:, 3:4],
                     b[:, 2:3], 1 - b[:, 1:2]], axis=1)

  boxes = tf.concat([get_corners(b[1]), get_corners(b[0])], 2)
  boxes_rev = tf.concat([get_corners(b_rev[1]), get_corners(b_rev[0])], 2)

  is_flip = tf.equal(tf.random_uniform([], 0, 2, dtype=tf.int32), 1)
  image = tf.cond(is_flip, lambda: tf.reverse(image, [1]), lambda: image)
  boxes = tf.cond(is_flip, lambda: boxes_rev, lambda: boxes)

  # Batch data
  (image, preds, boxes) = tf.train.batch([image, predicates[i_rel], boxes],
                                         batch_size, num_threads=4)

  return image, preds, boxes

### MISC #####################################

def visualize(imgs, box_cls, boxes, lmap):

  s = float(boxes.shape[2])

  imgs_with_box = []
  for img, c, box in zip(imgs, box_cls, boxes):

    box = [np.array([np.where(b[..., 0])[0][0] / s,
                     np.where(b[..., 0])[1][0] / s,
                     np.where(b[..., 1])[0][0] / s,
                     np.where(b[..., 1])[1][0] / s])
           for b in box]
    box = np.array(box)

    img = (img * 127.5 + 127.5).astype('uint8')
    vis_util.visualize_boxes_and_labels_on_image_array(
        img,
        box,
        np.array([np.where(c)[0][0]] * box.shape[0]),
        np.array([1] * box.shape[0]),
        {k: {'name': v} for k, v in lmap.items()},
        use_normalized_coordinates=True)
    imgs_with_box.append(img)

  return np.array(imgs_with_box)

def tile_image(x_gen, tiles=()):
  """Tiled image representations.

  Args:
    x_gen: 4D array of images (n x w x h x 3)
    tiles (int pair, optional): number of rows and columns

  Returns:
    Array of tiled images (1 x W x H x 3)
  """
  n_images = x_gen.shape[0]
  if not tiles:
    for i in range(int(np.sqrt(n_images)), 0, -1):
      if n_images % i == 0:
        break
    n_rows = i
    n_cols = n_images // i
  else:
    n_rows, n_cols = tiles
  full = [np.hstack(x_gen[c * n_rows:(c + 1) * n_rows]) for c in range(n_cols)]
  return np.expand_dims(np.vstack(full), 0)

def main(argv=()):
  del argv  # Unused.

if __name__ == '__main__':
  pass

max_stars_repo_name: google/VRD

max_issues_repo_name: google/VRD

max_forks_repo_name: google/VRD