text:
import unittest
import numpy

from cqcpy import integrals
from cqcpy import ci_utils

class CIUtilsTest(unittest.TestCase):
    def setUp(self):
        pass

    def test_mat_on_vec_cisd(self):
        nmo = 9
        na = 2
        nb = 2
        thresh = 1e-14

        ha = numpy.random.random((nmo, nmo))
        ha = ha + ha.transpose((1, 0))
        hb = ha
        Ia = numpy.random.random((nmo, nmo, nmo, nmo))
        Ia = Ia + Ia.transpose((1, 0, 3, 2))
        Ib = Ia
        Iabab = Ia

        sa = ci_utils.s_strings(nmo, na)
        sb = ci_utils.s_strings(nmo, nb)
        da = ci_utils.d_strings(nmo, na)
        db = ci_utils.d_strings(nmo, nb)
        occ = [1 if i < na else 0 for i in range(nmo)]
        ref = ci_utils.Dstring(nmo, occ)
        basis = []

        # scf ground state
        basis.append((ref, ref))

        # singly excited states
        for a in sa:
            basis.append((a, ref))
        for b in sb:
            basis.append((ref, b))

        # doubly excited states
        for a in da:
            basis.append((a, ref))
        for b in db:
            basis.append((ref, b))
        for a in sa:
            for b in sb:
                basis.append((a, b))

        nd = len(basis)
        vec = numpy.random.random(nd)
        H = numpy.zeros((nd, nd))
        for i, b in enumerate(basis):
            for j, k in enumerate(basis):
                H[i, j] = ci_utils.ci_matrixel(
                    b[0], b[1], k[0], k[1], ha, hb, Ia, Ib, Iabab, 0.0)
        ref = numpy.einsum('ij,j->i', H, vec)
        out = ci_utils.H_on_vec(basis, vec, ha, hb, Ia, Ib, Iabab)
        diff = numpy.linalg.norm(out - ref)/numpy.linalg.norm(ref)
        self.assertTrue(diff < thresh, "Difference: {}".format(diff))

    def test_cis(self):
        from pyscf import gto
        from pyscf.tdscf.rhf import scf

        # reference
        mol = gto.Mole()
        mol.build(
            atom='H 0 0 0; H 0 0 0.77',  # in Angstrom
            basis='631g', verbose=0)

        mf = scf.RHF(mol)
        mf.conv_tol_grad = 1e-9
        Escf = mf.kernel()
        mf.verbose = 0
        tda = mf.TDA()
        tda.nstates = 6
        erefs = tda.kernel()[0]
        tda.singlet = False
        ereft = tda.kernel()[0]

        # run CIS calculation
        mos = mf.mo_coeff
        nmo = mos.shape[1]
        hcore = mf.get_hcore()
        ha = numpy.einsum('mp,mn,nq->pq', mos, hcore, mos)
        hb = ha.copy()
        I = integrals.get_phys(mol, mos, mos, mos, mos)

        N = mol.nelectron
        na = N//2
        nb = na
        basis = ci_utils.ucis_basis(nmo, na, nb, gs=False)
        nd = len(basis)

        H = numpy.zeros((nd, nd))
        const = -Escf + mol.energy_nuc()
        F = mf.get_fock()
        F = numpy.einsum('mp,mn,nq->pq', mos, F, mos)
        for i, b in enumerate(basis):
            for j, k in enumerate(basis):
                H[i, j] = ci_utils.ci_matrixel(
                    b[0], b[1], k[0], k[1], ha, hb, I, I, I, const)

        eout, v = numpy.linalg.eigh(H)
        eref = numpy.sort(numpy.concatenate((erefs, ereft)))
        diff = numpy.linalg.norm(eref - eout)
        self.assertTrue(diff < 1e-10)

    def test_cisd(self):
        from pyscf import gto, scf, ci

        mol = gto.M(
            atom='Be 0 0 0',
            basis='631g', verbose=0)
        mf = scf.RHF(mol)
        mf.conv_tol = 1e-12
        Escf = mf.kernel()
        myci = ci.CISD(mf).run()
        ref = myci.e_corr

        # run naive CISD calculation
        mos = mf.mo_coeff
        nmo = mos.shape[1]
        hcore = mf.get_hcore()
        ha = numpy.einsum('mp,mn,nq->pq', mos, hcore, mos)
        hb = ha.copy()
        I = integrals.get_phys(mol, mos, mos, mos, mos)

        N = mol.nelectron
        na = N//2
        nb = na

        basis = ci_utils.ucisd_basis(nmo, na, nb)
        nd = len(basis)
        H = numpy.zeros((nd, nd))
        const = -Escf + mol.energy_nuc()
        for i, b in enumerate(basis):
            for j, k in enumerate(basis):
                H[i, j] = ci_utils.ci_matrixel(
                    b[0], b[1], k[0], k[1], ha, hb, I, I, I, const)

        eout, v = numpy.linalg.eigh(H)
        diff = abs(ref - eout[0])
        self.assertTrue(diff < 1e-12)

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


text:
from random import random
from traceback import print_tb
import torch
import torch.nn as nn
import numpy as np
import genotypes
from operations import *
from genotypes import PRIMITIVES, PRIMITIVES_DARTS, Genotype_nested, Genotype_opt
from architecture_processing import deserialize_architecture_to_alphas

class MixedOp(nn.Module):
  def __init__(self, C, stride, prims):
    super(MixedOp, self).__init__()
    self._ops = nn.ModuleList()
    self.stride = stride
    for primitive in prims:
      op = OPS[primitive](C, stride, False)
      if 'pool' in primitive:
        op = nn.Sequential(op, nn.BatchNorm2d(C, affine=False))
      self._ops.append(op)

  def forward(self, x, weights, cell_active):
    if cell_active:
      return sum(w * op(x) for w, op in zip(weights, self._ops))
    else:
      return sum(torch.tensor(random()).cuda() * op(x) for op in self._ops)

class Cell(nn.Module):

  def __init__(self, steps, multiplier, C_prev_prev, C_prev, C, reduction, reduction_prev, lr, criterion, weight_decay, epochs, weights, dartopti):
    super(Cell, self).__init__()
    self.reduction = reduction
    self.activated = True

    if reduction_prev:
      self.preprocess0 = FactorizedReduce(C_prev_prev, C, affine=False)
    else:
      self.preprocess0 = ReLUConvBN(C_prev_prev, C, 1, 1, 0, affine=False)
    self.preprocess1 = ReLUConvBN(C_prev, C, 1, 1, 0, affine=False)
    self._steps = steps
    self._multiplier = multiplier

    self._ops = nn.ModuleList()

    prims = PRIMITIVES if dartopti else PRIMITIVES_DARTS

    if weights is not None:
      self.alphas = torch.tensor(weights, requires_grad=True, dtype=torch.float32, device="cuda")
    elif criterion is not None:
      k = sum(1 for i in range(self._steps) for n in range(2+i))
      self.alphas = torch.zeros(k, len(prims), requires_grad=True, device="cuda")

    self.criterion = criterion
    if criterion:
      self.optimizer = torch.optim.Adam([self.alphas],
        lr=lr, betas=(0.9, 0.999), weight_decay=weight_decay)
      self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(self.optimizer, float(epochs), eta_min=0.001)

    for i in range(self._steps):
      for j in range(2+i):
        stride = 2 if reduction and j < 2 else 1
        op = MixedOp(C, stride, prims)
        self._ops.append(op)

  def loss(self, target1, input2, logits, cell, marginal_contributions):
    return self.criterion(logits, target1, input2, cell, marginal_contributions)

  def forward(self, s0, s1, weights=None):
    s0 = self.preprocess0(s0)
    s1 = self.preprocess1(s1)

    states = [s0, s1]
    offset = 0
    if weights is None:
      weights = self.alphas
    weights = torch.sigmoid(weights)

    for i in range(self._steps):
      s = sum(self._ops[offset+j](h, weights[offset+j], self.activated) for j, h in enumerate(states))
      offset += len(states)
      states.append(s)

    return torch.cat(states[-self._multiplier:], dim=1)

class NetworkCIFAR(nn.Module):

  def __init__(self, C, num_classes, layers, criterion, criterion_cell, cell_optim_lr, cell_optim_weight_decay, steps=4, multiplier=4, stem_multiplier=3,
                arch_baseline=None, op_threshold=None, epochs=50):
    super(NetworkCIFAR, self).__init__()
    self._C = C
    self._num_classes = num_classes
    self._layers = layers
    self._criterion = criterion
    self._steps = steps
    self._multiplier = multiplier
    self.op_threshold = op_threshold
    self.arch_baseline = arch_baseline

    self.reduction_indexes = [self._layers//3, 2*self._layers//3]

    C_curr = stem_multiplier*C
    self.stem = nn.Sequential(
      nn.Conv2d(3, C_curr, 3, padding=1, bias=False),
      nn.BatchNorm2d(C_curr)
    )
    self.cells = nn.ModuleList()

    baseline_alphas = None
    if arch_baseline:
      self.arch_baseline = eval(f"genotypes.{arch_baseline}")
      self._steps = self.arch_baseline.concat[-1] - 1
      self._layers = len(self.arch_baseline.seq)
      self.reduction_indexes = self.arch_baseline.reductions
      baseline_alphas = deserialize_architecture_to_alphas(self.arch_baseline)
      C_prev = self.init_cells_baseline(C_curr, baseline_alphas, epochs, cell_optim_lr, criterion_cell, cell_optim_weight_decay)
    else:
      C_prev = self.init_cells(C_curr, epochs, cell_optim_lr, criterion_cell, cell_optim_weight_decay)

    self.global_pooling = nn.AdaptiveAvgPool2d(1)
    self.classifier = nn.Linear(C_prev, num_classes)

  def init_cells(self, C_curr, epochs, cell_optim_lr, criterion_cell, cell_optim_weight_decay):
    C_prev_prev, C_prev, C_curr = C_curr, C_curr, self._C
    reduction_prev = False
    for i in range(self._layers):
      if i in self.reduction_indexes:
        C_curr *= 2
        reduction = True
      else:
        reduction = False
      cell = Cell(self._steps, self._multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev, cell_optim_lr, criterion_cell, cell_optim_weight_decay, epochs, None, dartopti=False)
      reduction_prev = reduction
      self.cells += [cell]

      C_prev_prev, C_prev = C_prev, self._multiplier*C_curr
    return C_prev

  def init_cells_baseline(self, C_curr, baseline_alphas, epochs, cell_optim_lr, criterion_cell, cell_optim_weight_decay):
    C_prev_prev, C_prev, C_curr = C_curr, C_curr, self._C
    reduction_prev = False
    seen = []
    self.cell_dict = {c: None for c in range(max(self.arch_baseline.seq))}
    for i in range(self._layers):
      if i in self.reduction_indexes:
        C_curr *= 2
        reduction = True
      else:
        reduction = False
      index = self.arch_baseline.seq[i]
      if index not in seen:
        crit = criterion_cell
        weights = baseline_alphas[index]
        self.cell_dict[index] = i
        seen.append(index)
      else:
        crit = None
        weights = None
      cell = Cell(self._steps, self._multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev, cell_optim_lr, crit, cell_optim_weight_decay, epochs, weights, dartopti=True)
      reduction_prev = reduction
      self.cells += [cell]

      C_prev_prev, C_prev = C_prev, self._multiplier*C_curr
    return C_prev

  def forward(self, input):
    s0 = s1 = self.stem(input)
    for i, cell in enumerate(self.cells):
      weights = self.cells[self.cell_dict[self.arch_baseline.seq[i]]].alphas if cell.criterion is None else None
      s0, s1 = s1, cell(s0, s1, weights)
    out = self.global_pooling(s1)
    logits = self.classifier(out.view(out.size(0),-1))
    return logits

  def arch_parameters(self, to_parse=False):
    k = sum(1 for i in range(self._steps) for n in range(2+i))
    prims = PRIMITIVES if self.arch_baseline else PRIMITIVES_DARTS
    num_ops = len(prims)
    length = len(self.cells) if to_parse or not(self.arch_baseline) else len(self.arch_baseline.genes)
    alphas = torch.zeros(length, k, num_ops, device='cuda')
    index = 0
    for i,c in enumerate(self.cells):
      if c.criterion:
        alphas[index] = c.alphas
        index += 1
      elif to_parse: # Output alphas so that we can parse the genotype like a standard one (Genotype_nested)
        alphas[index] = self.cells[self.cell_dict[self.arch_baseline.seq[i]]].alphas
        index += 1
    return alphas

  def arch_state_dicts(self):
    state_dicts = []
    for i,c in enumerate(self.cells):
      state_dicts.append(c.state_dict())
    return state_dicts

  def arch_criterions(self):
    criterions = []
    for i,c in enumerate(self.cells):
      criterions.append(c.criterion)
    return criterions

  def states(self):
    return {
      'alphas': self.arch_parameters(),
      'arch_state_dicts': self.arch_state_dicts(),
      'arch_criterions': self.arch_criterions(),
      'criterion': self._criterion,
      'network_state_dict': self.state_dict()
    }

  def restore(self, states):
    self.load_state_dict(states['network_state_dict'])
    self._criterion = states['criterion']
    for i,c in enumerate(self.cells):
      c.load_state_dict(states['arch_state_dicts'][i])
      c.alphas = states['alphas'][i]
      c.alphas.requires_grad_()
      c.criterion = states['arch_criterions'][i]

class NetworkImageNet(nn.Module):

  def __init__(self, C, num_classes, layers, criterion, criterion_cell, cell_optim_lr, cell_optim_weight_decay, steps=4, multiplier=4, stem_multiplier=3,
                arch_baseline=None, op_threshold=None, epochs=50):
    super(NetworkImageNet, self).__init__()
    self._C = C
    self._num_classes = num_classes
    self._layers = layers
    self._criterion = criterion
    self._steps = steps
    self._multiplier = multiplier
    self.op_threshold = op_threshold
    self.arch_baseline = arch_baseline

    self.reduction_indexes = [self._layers//3, 2*self._layers//3]

    C_curr = stem_multiplier*C
    self.stem = nn.Sequential(
      nn.Conv2d(3, C_curr, 3, padding=1, bias=False),
      nn.BatchNorm2d(C_curr)
    )
    self.cells = nn.ModuleList()

    baseline_alphas = None
    if arch_baseline:
      self.arch_baseline = eval(f"genotypes.{arch_baseline}")
      self._steps = self.arch_baseline.concat[-1] - 1
      self._layers = len(self.arch_baseline.seq)
      self.reduction_indexes = self.arch_baseline.reductions
      baseline_alphas = deserialize_architecture_to_alphas(self.arch_baseline)
      C_prev = self.init_cells_baseline(C_curr, baseline_alphas, epochs, cell_optim_lr, criterion_cell, cell_optim_weight_decay)
    else:
      C_prev = self.init_cells(C_curr, epochs, cell_optim_lr, cell_optim_weight_decay)

    self.global_pooling = nn.AdaptiveAvgPool2d(1)
    self.classifier = nn.Linear(C_prev, num_classes)

  def init_cells(self, C_curr, epochs, cell_optim_lr, criterion_cell, cell_optim_weight_decay):
    C_prev_prev, C_prev, C_curr = C_curr, C_curr, self._C
    reduction_prev = True
    for i in range(self._layers):
      if i in self.reduction_indexes:
        C_curr *= 2
        reduction = True
      else:
        reduction = False
      cell = Cell(self._steps, self._multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev, cell_optim_lr, criterion_cell, cell_optim_weight_decay, epochs, None)
      reduction_prev = reduction
      self.cells += [cell]

      C_prev_prev, C_prev = C_prev, self._multiplier*C_curr
    return C_prev

  def init_cells_baseline(self, C_curr, baseline_alphas, epochs, cell_optim_lr, criterion_cell, cell_optim_weight_decay):
    C_prev_prev, C_prev, C_curr = C_curr, C_curr, self._C
    reduction_prev = True
    seen = []
    self.cell_dict = {c: None for c in range(max(self.arch_baseline.seq))}
    for i in range(self._layers):
      if i in self.reduction_indexes:
        C_curr *= 2
        reduction = True
      else:
        reduction = False
      index = self.arch_baseline.seq[i]
      if index not in seen:
        crit = criterion_cell
        weights = baseline_alphas[index]
        self.cell_dict[index] = i
        seen.append(index)
      else:
        crit = None
        weights = None
      cell = Cell(self._steps, self._multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev, cell_optim_lr, crit, cell_optim_weight_decay, epochs, weights)
      reduction_prev = reduction
      self.cells += [cell]

      C_prev_prev, C_prev = C_prev, self._multiplier*C_curr
    return C_prev

  def forward(self, input):
    s0 = s1 = self.stem(input)
    for i, cell in enumerate(self.cells):
      weights = self.cells[self.cell_dict[self.arch_baseline.seq[i]]].alphas if cell.criterion is None else None
      s0, s1 = s1, cell(s0, s1, weights)
    out = self.global_pooling(s1)
    logits = self.classifier(out.view(out.size(0),-1))
    return logits

  def arch_parameters(self, to_parse=False):
    k = sum(1 for i in range(self._steps) for n in range(2+i))
    prims = PRIMITIVES if self.arch_baseline else PRIMITIVES_DARTS
    num_ops = len(prims)
    length = len(self.cells) if to_parse else len(self.arch_baseline.genes)
    alphas = torch.zeros(length, k, num_ops, device='cuda')
    index = 0
    for i,c in enumerate(self.cells):
      if c.criterion:
        alphas[index] = c.alphas
        index += 1
      elif to_parse: # Output alphas so that we can parse the genotype like a standard one (Genotype_nested)
        alphas[index] = self.cells[self.cell_dict[self.arch_baseline.seq[i]]].alphas
        index += 1
    return alphas

  def arch_state_dicts(self):
    state_dicts = []
    for i,c in enumerate(self.cells):
      state_dicts.append(c.state_dict())
    return state_dicts

  def arch_criterions(self):
    criterions = []
    for i,c in enumerate(self.cells):
      criterions.append(c.criterion)
    return criterions

  def states(self):
    return {
      'alphas': self.arch_parameters(),
      'arch_state_dicts': self.arch_state_dicts(),
      'arch_criterions': self.arch_criterions(),
      'criterion': self._criterion,
      'network_state_dict': self.state_dict()
    }

  def restore(self, states):
    self.load_state_dict(states['network_state_dict'])
    self._criterion = states['criterion']
    for i,c in enumerate(self.cells):
      c.load_state_dict(states['arch_state_dicts'][i])
      c.alphas = states['alphas'][i]
      c.alphas.requires_grad_()
      c.criterion = states['arch_criterions'][i]

ext: py

max_stars_repo_name: aheuillet/D-DARTS

max_issues_repo_name: aheuillet/D-DARTS

max_forks_repo_name: aheuillet/D-DARTS

reason: import numpy


text:
import os
import numpy as np
import json
import argparse
import pickle
import tensorflow as tf
from pixel_cnn_pp import nn
from pixel_cnn_pp.model import model_spec
#import ipdb

# -----------------------------------------------------------------------------
parser = argparse.ArgumentParser()
# data I/O
parser.add_argument('-i', '--data_dir', type=str, default='/local_home/tim/pxpp/data', help='Location for the dataset')
parser.add_argument('-o', '--save_dir', type=str, default='/local_home/tim/pxpp/save', help='Location for parameter checkpoints and samples')
parser.add_argument('-d', '--data_set', type=str, default='cifar', help='Can be either cifar|imagenet|lfw')
parser.add_argument('-t', '--save_interval', type=int, default=20, help='Every how many epochs to write checkpoint/samples?')
parser.add_argument('-r', '--load_params', dest='load_params', action='store_true', help='Restore training from previous model checkpoint?')
parser.add_argument('-uo', '--origin', dest='origin', action='store_true', help='using origin img?')
parser.add_argument('-ch', '--cond_h', dest='cond_h', action='store_true', help='using conditional h?')
# model
parser.add_argument('-q', '--nr_resnet', type=int, default=5, help='Number of residual blocks per stage of the model')
parser.add_argument('-n', '--nr_filters', type=int, default=160, help='Number of filters to use across the model. Higher = larger model.')
parser.add_argument('-m', '--nr_logistic_mix', type=int, default=10, help='Number of logistic components in the mixture. Higher = more flexible model')
parser.add_argument('-z', '--resnet_nonlinearity', type=str, default='concat_elu', help='Which nonlinearity to use in the ResNet layers. One of "concat_elu", "elu", "relu" ')
parser.add_argument('-c', '--class_conditional', dest='class_conditional', action='store_true', help='Condition generative model on labels?')
parser.add_argument('-ed', '--energy_distance', dest='energy_distance', action='store_true', help='use energy distance in place of likelihood')
# optimization
parser.add_argument('-l', '--learning_rate', type=float, default=0.001, help='Base learning rate')
parser.add_argument('-e', '--lr_decay', type=float, default=0.999995, help='Learning rate decay, applied every step of the optimization')
parser.add_argument('-b', '--batch_size', type=int, default=16, help='Batch size during training per GPU')
parser.add_argument('-u', '--init_batch_size', type=int, default=16, help='How much data to use for data-dependent initialization.')
parser.add_argument('-p', '--dropout_p', type=float, default=0.5, help='Dropout strength (i.e. 1 - keep_prob). 0 = No dropout, higher = more dropout.')
parser.add_argument('-x', '--max_epochs', type=int, default=5000, help='How many epochs to run in total?')
parser.add_argument('-g', '--nr_gpu', type=int, default=8, help='How many GPUs to distribute the training across?')
# evaluation
parser.add_argument('--polyak_decay', type=float, default=0.9995, help='Exponential decay rate of the sum of previous model iterates during Polyak averaging')
parser.add_argument('-ns', '--num_samples', type=int, default=1, help='How many batches of samples to output.')
# reproducibility
parser.add_argument('-s', '--seed', type=int, default=1, help='Random seed to use')
args = parser.parse_args()
print('input args:\n', json.dumps(vars(args), indent=4, separators=(',',':'))) # pretty print args

if args.data_set != 'lfw' or args.data_set != 'celeba':
    raise("unsupported dataset")
if not args.class_conditional:
    raise("unsupported no-class_conditional")

#不管用哪个预训练模型，均使用lfw的sample进行测试
import data.lfw_data as lfw_data
DataLoader = lfw_data.DataLoader

sample_data = DataLoader(args.data_dir, 'sample', args.batch_size * args.nr_gpu,
                         shuffle=False, return_labels=args.class_conditional)
obs_shape = sample_data.get_observation_size() # e.g. a tuple (32,32,3)

x_init = tf.placeholder(tf.float32, shape=(args.init_batch_size,) + obs_shape)
h_init = tf.placeholder(tf.float32, shape=(args.init_batch_size,) + sample_data.h.shape[1:])
xs = [tf.placeholder(tf.float32, shape=(args.batch_size, ) + obs_shape) for i in range(args.nr_gpu)]
hs = [tf.placeholder(tf.float32, shape=(args.init_batch_size,) + sample_data.h.shape[1:]) for i in range(args.nr_gpu)]

model_opt = { 'nr_resnet': args.nr_resnet, 'nr_filters': args.nr_filters, 'nr_logistic_mix': args.nr_logistic_mix, 'resnet_nonlinearity': args.resnet_nonlinearity, 'energy_distance': args.energy_distance }
model = tf.make_template('model', model_spec)

# run once for data dependent initialization of parameters
init_pass = model(x_init, h_init, init=True, dropout_p=args.dropout_p, **model_opt)

# keep track of moving average
all_params = tf.trainable_variables()
ema = tf.train.ExponentialMovingAverage(decay=args.polyak_decay)
maintain_averages_op = tf.group(ema.apply(all_params))
ema_params = [ema.average(p) for p in all_params]

# sample
new_x_gen = []
for i in range(args.nr_gpu):
   with tf.device('/gpu:%d' % i):
       out = model(xs[i], hs[i], ema=ema, dropout_p=0, **model_opt)
       if args.energy_distance:
           new_x_gen.append(out[0])
       else:
           new_x_gen.append(nn.sample_from_discretized_mix_logistic(out, args.nr_logistic_mix))

linear_interpolation = lambda h, cond_h, r: r*cond_h + (1-r)*h
def sample_from_model(sess, data, using_original_img=False, condition_h=None):
    x_origin, y_origin, h_origin = data
    x = x_origin.copy()

    if(not using_original_img):
        x = np.array([np.zeros((args.batch_size,) + obs_shape, dtype=np.float32) for i in range(args.nr_gpu)])
    else:
        x = np.cast[np.float32]((x - 127.5) / 127.5) # input to pixelCNN is scaled from uint8 [0,255] to float in range [-1,1]
        x = np.split(x, args.nr_gpu)

    feed_dict = {xs[i]: x[i] for i in range(args.nr_gpu)}
    if h_origin is not None:
        h = np.split(h_origin, args.nr_gpu)
        if condition_h is not None:
            for i in range(args.nr_gpu):
                h[i] = np.array([linear_interpolation(h[i][j], condition_h, j/(h[i].shape[0]-1)) for j in range(h[i].shape[0])])
        feed_dict.update({hs[i]: h[i] for i in range(args.nr_gpu)})

    for yi in range(obs_shape[0]):
        for xi in range(obs_shape[1]):
            new_x_gen_np = sess.run(new_x_gen, feed_dict)
            for i in range(args.nr_gpu):
                x[i][:, yi, xi, :] = new_x_gen_np[i][:, yi, xi, :]
    return np.concatenate(x, axis=0), x_origin, y_origin

saver = tf.train.Saver()
if args.origin:
    addition_path = 'origin'
else:
    addition_path = 'no_origin'
if args.cond_h:
    addition_path += '/other_cond'
else:
    addition_path += '/on_other_cond'
output_path = os.path.join(args.save_dir, 'experiment', args.data_set, addition_path)
if not os.path.exists(output_path):
    os.makedirs(output_path)
with tf.Session() as sess:
    if not args.load_params:
        raise("unsupported no pretrained model")
    ckpt_file = args.save_dir + '/params_' + args.data_set + '.ckpt'
    print('restoring parameters from', ckpt_file)
    saver.restore(sess, ckpt_file)

    if not args.cond_h:
        sample_list = []
        img_list = []
        label_list = []
        for data in sample_data:
            sample_x, origin_imgs, origin_labels = sample_from_model(sess, data, using_original_img=args.origin, condition_h=False)
            sample_list.append(sample_x)
            img_list.append(origin_imgs)
            label_list.append(origin_labels)
        pickle.dump({'sample': sample_list, 'img': img_list, 'label': label_list},
                    open(os.path.join(output_path, '{}_{}.p'.format(
                            args.data_set, addition_path.replace('_','').replace('/', '_'))),
                             "wb"))
    else:
        cond_img, cond_label, cond_h = sample_data.data_uniq, sample_data.labels_uniq, sample_data.h_uniq
        for i in range(cond_img.shape[0]):
            sample_list = []
            img_list = []
            label_list = []
            idx = 0
            for data in sample_data:
                if i == idx:
                    idx += 1
                    continue
                sample_x, origin_imgs, origin_labels = sample_from_model(sess, data, using_original_img=args.origin, condition_h=cond_h[i])
                sample_list.append(sample_x)
                img_list.append(origin_imgs)
                label_list.append(origin_labels)
                idx += 1

            pickle.dump({'sample': sample_list, 'img': img_list, 'label': label_list,
                         'cond_img': cond_img[i], 'cond_label': cond_label[i]},
                        open(os.path.join(output_path, '{}_{}_{}_{}.p'.format(
                            args.data_set, addition_path.replace('_','').replace('/', '_'), str(i), cond_label[i])),
                             "wb"))

max_stars_repo_name: GaryGaryry/pixel-cnn

max_issues_repo_name: GaryGaryry/pixel-cnn

max_forks_repo_name: GaryGaryry/pixel-cnn


text:
#!/usr/bin/env python3

"""Easy statistics

The main class `Statistics` just extends dict{str:function},
function here will act on the object of statistics.
The values of dict have not to be a function.
If it is a string, then the attribute of object will be called.

Please see the following example and the function `_call`, the underlying implementation.

Pseudo codes:
    ```
    if s: str
        f = getattr(obj, s)
        if f: function
            r = f()
        else
            r = f
    elif s: function
        r = s(obj)
    elif s is number:
        r = s
    ```

Example:

    >>> import numpy as np

    >>> T = np.random.random((100,100))
    >>> s = Statistics({'mean': lambda x: np.mean(np.mean(x, axis=1)), 'max': lambda x: np.max(np.mean(x, axis=1)), 'shape':'shape'})
    >>> print(s(T))
    >>> {'mean': 0.5009150557686407, 'max': 0.5748552862392957, 'shape': (100, 100)}

    >>> print(s(T, split=True)) # split the tuple if it needs
    >>> {mean': 0.5009150557686407, 'max': 0.5748552862392957, 'shape-0': 100, 'shape-1': 100}

`MappingStatistics` subclasses Statistics. It only copes with iterable object,
and maps it to an array by a "key" function.

Example:

    >>> s = MappingStatistics('mean', {'mean':np.mean, 'max':np.max})
    >>> print(s(T))
    >>> {'mean': 0.5009150557686407, 'max': 0.5748552862392957}

In the exmaple, 'mean', an attribute of T, maps T to a 1D array.

Advanced Usage:
`Statistics` acts on a list/tuple of objects iteratively, gets a series of results,
forming an object of pandas.DataFrame.

    history = pd.DataFrame(columns=s.keys())
    for obj in objs:
        history = history.append(s(obj), ignore_index=True)

"""

def _call(s, obj):
    """Core function of ezstat

    s {function | string} -- Statistics
    obj -- object of statistics

    An extension for s(x) or x.s
    If s is an string, then it only returns x.s() if callable, otherwise x.s.
    """
    if isinstance(s, str):
        if not hasattr(obj, s):
            raise ValueError(f"the object has no attribute '{s}'")
        f = getattr(obj, s)
        if callable(f):
            r = f()
        else:
            r = f
    elif callable(s):
        r = s(obj)
    elif isinstance(s, (int, float, tuple)):
        # deprecated
        r = s
    else:
        raise TypeError(f"The type of the value corresponding to key `{k}` is not permissible!")

    return r

class Statistics(dict):
    def __call__(self, *args, **kwargs):
        return self.do(*args, **kwargs)

    def do(self, obj, split=False):
        """
        execute a staistics

        Arguments:
            obj {object} -- an object (population) of statistics
            split {bool} -- if True, it will split the tuple-type statistics result to numbers

        Returns:
            dict

        Example:
        >>> import numpy as np

        >>> T = np.random.random((100,100))
        >>> s = Statistics({'mean': lambda x: np.mean(np.mean(x, axis=1)), 'max': lambda x: np.max(np.mean(x, axis=1)), 'shape':'shape'})
        >>> print(s(T))
        >>> {'mean': 0.5009150557686407, 'max': 0.5748552862392957, 'shape': (100, 100)}

        >>> print(s(T, split=True)) # split the tuple if it needs
        >>> {mean': 0.5009150557686407, 'max': 0.5748552862392957, 'shape-0': 100, 'shape-1': 100}
        """
        res = {}
        for k, s in self.items():
            # if s is True and isinstance(k, str):
            #     r = _call(k, obj)
            r = _call(s, obj)

            if split and isinstance(r, tuple):
                for i, ri in enumerate(r):
                    res[f'{k}-{i}'] = ri
            else:
                res[k] = r
        return res

    def dox(self, objs):
        data = map(s, objs)
        return pd.DataFrame(data=data, columns=s.keys())

class MappingStatistics(Statistics):
    """Just a wrapper of `Statistics`

    Only recommanded to cope with iterable object of statistics.
    It will transfrom the object to array by `key` (functional attribute) before doing statistics.

    Extends:
        Statistics

    Example:
    >>> import numpy as np

    >>> T = np.random.random((100,100))
    >>> s = MappingStatistics('mean', {'mean':np.mean, 'min':np.min})
    >>> print(s(T))
    >>> {'mean': 0.4995186088546244, 'min': 0.39975807140966796}

    In the exmaple, 'mean', an attribute of np.ndarray, maps each row of T to a number.
    As a result, the object of statistics is a 1D array.
    """

    def __init__(self, key=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._key = key

    @property
    def key(self):
        return self._key

    @key.setter
    def key(self, v):
        self._key = v

    def do(self, obj, split=False):
        """
        The object of statistics should be iterable
        """
        if self.key:
            obj = np.array([_call(self.key, k) for k in obj]) # should be an array of numbers
        return super().do(obj, split=split)

max_stars_repo_name: Freakwill/ezstat

max_issues_repo_name: Freakwill/ezstat

max_forks_repo_name: Freakwill/ezstat


text:
import numpy as np

from jass.base.const import color_masks, card_values, PUSH, trump_strings_german_long
from jass.base.player_round import PlayerRound
from jass.player.player import Player

# Trump Index and Corrections
# ------------------------------A--K--Q--J-10--9--8--7--6
# ------------------------------0--1--2--3--4--5--6--7--8
BEST_TRUMP_INDEXES  = np.array([3, 5, 0, 1, 2, 4, 6, 7, 8]) #TODO: Hyperparameter
BEST_OBE_INDEXES    = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8]) #TODO: Hyperparameter
BEST_UNDE_INDEXES   = np.array([8, 7, 6, 5, 4, 3, 2, 1, 0]) #TODO: Hyperparameter

START_SCORE = 36 #TODO: Hyperparameter
SCORE_STEP = (START_SCORE / 9) * -1 #linear curve

#PERFECT_WINS_FACTOR = 9 #TODO: Hyperparameter

def calculate_score(rnd: PlayerRound, trumpToCheck=None) -> np.array:
    """trump can be given to check specific trump on a round otherwise the rnd trump is taken
        return an array with four elemnts which each containing the score of one color """
    trump = None
    if trumpToCheck is not None:
        trump = trumpToCheck
    else:
        trump = rnd.trump

    score_per_color = np.array([0, 0, 0, 0])

    if trump == 0:
        #it's a D: Schellen trump
        score_per_color[0] = get_score_per_color_and_trump(0, rnd.get_valid_cards(), trump, 0)
        score_per_color[1] = get_score_per_color_and_trump(1, rnd.get_valid_cards(), trump, 1)
        score_per_color[2] = get_score_per_color_and_trump(2, rnd.get_valid_cards(), trump, 1)
        score_per_color[3] = get_score_per_color_and_trump(3, rnd.get_valid_cards(), trump, 1)

    elif trump == 1:
        #it's a H: Rosen trump
        score_per_color[0] = get_score_per_color_and_trump(0, rnd.get_valid_cards(), trump, 1)
        score_per_color[1] = get_score_per_color_and_trump(1, rnd.get_valid_cards(), trump, 0)
        score_per_color[2] = get_score_per_color_and_trump(2, rnd.get_valid_cards(), trump, 1)
        score_per_color[3] = get_score_per_color_and_trump(3, rnd.get_valid_cards(), trump, 1)

    elif trump == 2:
        #it's a S: Schilten trump
        score_per_color[0] = get_score_per_color_and_trump(0, rnd.get_valid_cards(), trump, 1)
        score_per_color[1] = get_score_per_color_and_trump(1, rnd.get_valid_cards(), trump, 1)
        score_per_color[2] = get_score_per_color_and_trump(2, rnd.get_valid_cards(), trump, 0)
        score_per_color[3] = get_score_per_color_and_trump(3, rnd.get_valid_cards(), trump, 1)

    elif trump == 3:
        #it's a C: Eichel trump
        score_per_color[0] = get_score_per_color_and_trump(0, rnd.get_valid_cards(), trump, 1)
        score_per_color[1] = get_score_per_color_and_trump(1, rnd.get_valid_cards(), trump, 1)
        score_per_color[2] = get_score_per_color_and_trump(2, rnd.get_valid_cards(), trump, 1)
        score_per_color[3] = get_score_per_color_and_trump(3, rnd.get_valid_cards(), trump, 0)

    elif trump == 4:
        #it's O: Obe
        #score_per_color[0] = get_score_per_color_and_trump(0, rnd.get_valid_cards(), trump, 1)
        #score_per_color[1] = get_score_per_color_and_trump(1, rnd.get_valid_cards(), trump, 1)
        #score_per_color[2] = get_score_per_color_and_trump(2, rnd.get_valid_cards(), trump, 1)
        #score_per_color[3] = get_score_per_color_and_trump(3, rnd.get_valid_cards(), trump, 1)
        pass

    elif trump == 5:
        #it's U: Une-Ufe
        #score_per_color[0] = get_score_per_color_and_trump(0, rnd.get_valid_cards(), trump, 2)
        #score_per_color[1] = get_score_per_color_and_trump(1, rnd.get_valid_cards(), trump, 2)
        #score_per_color[2] = get_score_per_color_and_trump(2, rnd.get_valid_cards(), trump, 2)
        #score_per_color[3] = get_score_per_color_and_trump(3, rnd.get_valid_cards(), trump, 2)
        pass

    else:
        raise ValueError("Wrong trump number! only works for 0 to 5 (D, H, S, C, Obe, Unde)")

    print("Calculate Score - Trump: {}, ScorePerColor: {}".format(trump_strings_german_long[trump], score_per_color))
    return score_per_color

def calculate_score_of_card(current_score: int, score_to_add: int, count_perfect_wins: bool, trump_card=True) -> int:
    if trump_card:
        current_score = current_score + score_to_add

    if count_perfect_wins:
        current_score = (current_score - score_to_add) + START_SCORE

    return current_score

#Trump Selection
def get_score_per_color_and_trump(color: int, hand: np.array, trump: int, trumpToCheck: int) -> int:
    """
    hand: one-dimensional array with 36 entries
    color: color index (0-3)
    trumpType: trump = 0, obe = 1, unde = 2
    """
    cards_of_color = hand[color*9:(color*9)+9]

    score = 0
    score_to_add = START_SCORE

    count_perfect_wins = True

    if trumpToCheck == 0:
        #check trump
        for i in range(0, BEST_TRUMP_INDEXES.size):
            if cards_of_color[BEST_TRUMP_INDEXES[i]] == 1:
                score = calculate_score_of_card(score, score_to_add, count_perfect_wins, trump<4)
            else:
                count_perfect_wins = False

            score_to_add = score_to_add + SCORE_STEP

        return score

    elif trumpToCheck == 1:
        #check obe
        for i in range(0, BEST_OBE_INDEXES.size):
            if cards_of_color[BEST_OBE_INDEXES[i]] == 1:
                score = calculate_score_of_card(score, score_to_add, count_perfect_wins, False)
            else:
                count_perfect_wins = False

            score_to_add = score_to_add + SCORE_STEP

        return score

    elif trumpToCheck == 2:
        #check onde
        for i in range(0, BEST_UNDE_INDEXES.size):
            if cards_of_color[BEST_UNDE_INDEXES[i]] == 1:
                score = calculate_score_of_card(score, score_to_add, count_perfect_wins, False)
            else:
                count_perfect_wins = False

            score_to_add = score_to_add + SCORE_STEP

        return score

    raise ValueError("Invalid trumpType: {}".format(trumpToCheck))

#Card calculations
def calculate_highest_card_per_color(rnd: PlayerRound) -> np.array:
    highest_card_per_color = np.array([-1, -1, -1, -1])

    for c in range(0, 4):
        cards = rnd.get_valid_cards()[c*9:(c*9)+9]

        if c == rnd.trump:
            #check highest trump
            highest_card_per_color[c] = get_highest_trump(c, cards)
        elif rnd.trump == 5:
            #check highest unde
            highest_card_per_color[c] = get_highest_unde(c, cards)
        else:
            #check highest obe
            highest_card_per_color[c] = get_highest_obe(c, cards)

    return highest_card_per_color

def calculate_lowest_card_per_color(rnd: PlayerRound) -> np.array:
    lowest_card_per_color = np.array([-1, -1, -1, -1])

    for c in range(0, 4):
        cards = rnd.get_valid_cards()[c*9:(c*9)+9]

        if c == rnd.trump:
            #check lowest trump
            lowest_card_per_color[c] = get_lowest_trump(c, cards)
        elif rnd.trump == 5:
            #check lowest unde
            lowest_card_per_color[c] = get_lowest_unde(c, cards)
        else:
            #check lowest obe
            lowest_card_per_color[c] = get_lowest_obe(c, cards)

    return lowest_card_per_color

def get_highest_trump(color_index: int, cards_of_color: np.array) -> int:
    return __get_card_index_by_color(color_index, cards_of_color, BEST_TRUMP_INDEXES)

def get_lowest_trump(color_index: int, cards_of_color: np.array) -> int:
    return __get_card_index_by_color(color_index, cards_of_color, BEST_TRUMP_INDEXES[::1])

def get_highest_obe(color_index: int, cards_of_color: np.array) -> int:
    return __get_card_index_by_color(color_index, cards_of_color, BEST_OBE_INDEXES)

def get_lowest_obe(color_index: int, cards_of_color: np.array) -> int:
    return __get_card_index_by_color(color_index, cards_of_color, BEST_OBE_INDEXES[::1])

def get_highest_unde(color_index: int, cards_of_color: np.array) -> int:
    return get_lowest_obe(color_index, cards_of_color)

def get_lowest_unde(color_index: int, cards_of_color: np.array) -> int:
    return get_highest_obe(color_index, cards_of_color)

def __get_card_index_by_color(color_index: int, cards_of_color: np.array, index_order: np.array) -> int:
    """
    gets the index of a color by its index order (to find best or worst color)
    """
    if color_index < 0 or color_index > 3:
        ValueError("color_index has to be within 0 and 3")

    for index in index_order:
        if cards_of_color[index] == 1:
            return index + (color_index * 9)

    return -1 #no cards of this color anymore

#Next best card
def calculate_next_best_card(rnd: PlayerRound) -> np.array:
    cards_missing = get_missing_cards(rnd.tricks)
    print("Missing Cards: {}".format(cards_missing))

    next_best_card_per_color = np.array([-1, -1, -1, -1])

    for c in range(0, 4):
        cards = cards_missing[c*9:(c*9)+9]
        if c == rnd.trump:
            #check highest trump
            next_best_card_per_color[c] = get_highest_trump(c, cards)
        elif rnd.trump == 5:
            #check highest unde
            next_best_card_per_color[c] = get_highest_unde(c, cards)
        else:
            #check highest obe
            next_best_card_per_color[c] = get_highest_obe(c, cards)

    return next_best_card_per_color

def get_missing_cards(tricks: np.array):
    missing_cards = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

    for trick in tricks:
        for card in trick:
            if card != -1:
                missing_cards[card] = 0

    return missing_cards


text:
# -*- coding: utf-8 -*-
# @Author:              QH Lam
# @Date:                2021-02-27
# @Module name:         utils.py
# @Module description:  Advanced Lane lines main function

import os
import cv2
import glob
import numpy as np
import matplotlib.pyplot as plt
import settings

from moviepy.editor import VideoFileClip
from AdvLaneline_utils import *
from AdvLanelinefit import *
from AdvLaneline_process_image import *

dump_image_folderpath = "output_images"

if __name__ == '__main__':
    currentfolderpath = os.path.abspath(os.getcwd())
    filetoread = os.path.join(currentfolderpath, dump_image_folderpath, "camcalibration.ini")
    settings.read_calibration_file(filetoread)
    video_input1 = VideoFileClip('challenge_video.mp4')#.subclip(22,26)
    video_output1 = 'challenge_video_output.mp4'
    processed_video = video_input1.fl_image(process_image)
    processed_video.write_videofile(video_output1, audio=False)

max_stars_repo_name: ebots-hung/CarND-Advanced-Lane-Lines

max_issues_repo_name: ebots-hung/CarND-Advanced-Lane-Lines

max_forks_repo_name: ebots-hung/CarND-Advanced-Lane-Lines


text:
from keras.preprocessing import sequence
from keras.models import load_model,save_model
from keras.models import Sequential
from keras.layers import Dense, Input
from keras.layers import LSTM
from keras.layers import GRU
from keras.layers.core import Activation
from keras.regularizers import l2
from keras.layers.wrappers import TimeDistributed
from keras.layers.wrappers import Bidirectional
from keras.layers.normalization import BatchNormalization
from keras.layers import Embedding
from keras.layers.core import Dropout
import numpy as np
import pandas as pd
import sys
import keras.backend as K
from sklearn.metrics import confusion_matrix, classification_report

# For reproducibility
np.random.seed(42)

class EntityExtractor:

    def __init__ (self, reader, embedding_pickle_file=None):

        self.reader = reader
        self.model = None

        if not (embedding_pickle_file is None):
            self.wordvecs = self.reader.load_embedding_lookup_table(embedding_pickle_file)

    def load (self, filepath):
        self.model = load_model(filepath)

    def save (self, filepath):
        self.model.save(filepath)

    def print_summary (self):
        print(self.model.summary())

    ##################################################
    # train
    ##################################################
    def train (self, train_file, output_resources_pickle_file, \
        network_type = 'unidirectional', \
        num_epochs = 1, batch_size = 50, \
        dropout = 0.2, reg_alpha = 0.0, \
        num_hidden_units = 150, num_layers = 1):

        train_X, train_Y = self.reader.read_and_parse_training_data(train_file, output_resources_pickle_file)

        print("Data Shape: ")
        print(train_X.shape)
        print(train_Y.shape)

        print("Hyper parameters:")
        print("output_resources_pickle_file = {}".format(output_resources_pickle_file))
        print( "network_type = {}".format(network_type))
        print( "num_epochs= {}".format(num_epochs ))
        print("batch_size = {}".format(batch_size ))
        print("dropout = ".format(dropout ))
        print("reg_alpha = {}".format(reg_alpha ))
        print("num_hidden_units = {}".format(num_hidden_units))
        print("num_layers = {}".format(num_layers ))

        self.model = Sequential()
        self.model.add(Embedding(self.wordvecs.shape[0], self.wordvecs.shape[1], \
                                 input_length = train_X.shape[1], \
                                 weights = [self.wordvecs], trainable = False))

        for i in range(0, num_layers):
            if network_type == 'unidirectional':
                # uni-directional LSTM
                self.model.add(LSTM(num_hidden_units, return_sequences = True))
            else:
                # bi-directional LSTM
                self.model.add(Bidirectional(LSTM(num_hidden_units, return_sequences = True)))

            self.model.add(Dropout(dropout))

        self.model.add(TimeDistributed(Dense(train_Y.shape[2], activation='softmax')))

        self.model.compile(loss='categorical_crossentropy', optimizer='adam')
        print(self.model.summary())

        self.model.fit(train_X, train_Y, epochs = num_epochs, batch_size = batch_size)

    #########################################
    # predict
    #########################################
    def predict(self, data_frame):
        import json
        from collections import OrderedDict as odict

        feat_vector_list, word_seq_list, num_tokens_list = self.reader.preprocess_unlabeled_data(data_frame)
        print("Data Shape: ")
        print(feat_vector_list.shape)
        # the output is a list of JSON strings
        predicted_tags= []
        ind = 0
        for feat_vector, word_seq, num_tokens in zip(feat_vector_list, word_seq_list, num_tokens_list):
            prob_dist = self.model.predict(np.array([feat_vector]), batch_size=1)[0]
            pred_tags = self.reader.decode_prediction_sequence(prob_dist)
            pred_dict = odict(zip(word_seq[-num_tokens:], pred_tags[-num_tokens:]))
            pred_str = json.dumps(pred_dict)
            predicted_tags.append(pred_str)
            ind += 1
            ### To see Progress ###
            if ind % 100 == 0:
                print("Tagging {} sentences".format(ind))

        #predicted_tags = np.array(predicted_tags)
        return predicted_tags

    #########################################
    # predict_1
    # read the data from the memory
    #########################################
    def predict_1(self, text_list):
        import json
        from collections import OrderedDict as odict

        feat_vector_list, word_seq_list, num_tokens_list = self.reader.get_feature_vectors_1(text_list)
        print("Data Shape: ")
        print(feat_vector_list.shape)
        # the output is a list of JSON strings
        predicted_tags= []
        ind = 0
        for feat_vector, word_seq, num_tokens in zip(feat_vector_list, word_seq_list, num_tokens_list):
            prob_dist = self.model.predict(np.array([feat_vector]), batch_size=1)[0]
            pred_tags = self.reader.decode_prediction_sequence(prob_dist)
            pred_dict = odict(zip(word_seq[-num_tokens:], pred_tags[-num_tokens:]))
            pred_str = json.dumps(pred_dict)
            predicted_tags.append(pred_str)
            ind += 1
            ### To see Progress ###
            if ind % 500 == 0:
               print("Tagging {} sentences".format(ind))

        #predicted_tags = np.array(predicted_tags)
        return predicted_tags

    ############################################
    # predict_2
    # read the data from a file
    ###########################################
    def predict_2(self, data_file):
        import json
        from collections import OrderedDict as odict

        feat_vector_list, word_seq_list, num_tokens_list = self.reader.get_feature_vectors_2(data_file)
        print("Data Shape: ")
        print(feat_vector_list.shape)
        # the output is a list of JSON strings
        predicted_tags= []
        ind = 0
        for feat_vector, word_seq, num_tokens in zip(feat_vector_list, word_seq_list, num_tokens_list):
            prob_dist = self.model.predict(np.array([feat_vector]), batch_size=1)[0]
            pred_tags = self.reader.decode_prediction_sequence(prob_dist)
            pred_dict = odict(zip(word_seq, pred_tags[-num_tokens:]))
            pred_str = json.dumps(pred_dict)
            predicted_tags.append(pred_str)
            ind += 1
            ### To see Progress ###
            if ind % 500 == 0:
               print("Tagging {} sentences".format(ind))

        #predicted_tags = np.array(predicted_tags)
        return predicted_tags

    ###########################################
    # evaluate_model
    ###########################################
    def evaluate_model(self, test_file, output_prediction_file):
        print("evaluate_model - Begin")
        test_X, test_Y, data_set, num_tokens_list = self.reader.read_and_parse_test_data(test_file)

        print("Data Shape: ")
        print(test_X.shape)
        print(test_Y.shape)

        f = open(output_prediction_file, 'w')
        predicted_tags= []
        target_tags = []
        batch_size = 500
        #for each line
        for ind in range(0,len(test_X), batch_size):
            batch_test_X = test_X[ind:ind+ batch_size]
            batch_test_Y = test_Y[ind:ind+ batch_size]
            batch_data_set = data_set[ind:ind+ batch_size]
            batch_num_tokens_list = num_tokens_list[ind:ind+ batch_size]

            #ind += 1
            ### To see Progress ###
            if ind % 500 == 0:
                print("processing sentences = " + str(ind))

            batch_class_prob_dist = self.model.predict(np.array(batch_test_X), batch_size=batch_size)
            for class_prob_dist,y,data_point, num_tokens in zip(batch_class_prob_dist, batch_test_Y, batch_data_set, batch_num_tokens_list):
                sent_predicted_tags = self.reader.decode_prediction_sequence(class_prob_dist)
                sent_target_tags = self.reader.decode_prediction_sequence(y)

                #remove padding
                sent_predicted_tags = sent_predicted_tags[-num_tokens:]
                sent_target_tags = sent_target_tags[-num_tokens:]

                if len(sent_target_tags) != num_tokens or \
                    len(sent_predicted_tags) != num_tokens:
                    print("stop here ............")

                for index in range(0, num_tokens):
                    target_tag = sent_target_tags[index]
                    pred_tag = sent_predicted_tags[index]

                    if target_tag != "NONE":
                        if pred_tag == "B-Chemical":
                            predicted_tags.append("B-Drug")
                        elif pred_tag == "I-Chemical":
                            predicted_tags.append("I-Drug")
                        elif pred_tag == 'None':
                            predicted_tags.append('O')
                        else:
                            predicted_tags.append(pred_tag)

                        if target_tag == "B-Chemical":
                            target_tags.append("B-Drug")
                        elif target_tag == "I-Chemical":
                            target_tags.append("I-Drug")
                        else:
                            target_tags.append(target_tag)

                ##for each token
                for target_tag, predicted_tag, word in zip(sent_target_tags, sent_predicted_tags, list(data_point[0])):
                    #f.write(word + '\t' + str(target_tag) + '\t' + str(predicted_tag) + '\n')
                    f.write(str(predicted_tag) + '\n')
                f.write("\n")

        f.close()

        predicted_tags = np.array(predicted_tags)
        target_tags = np.array(target_tags)
        evaluation_report = classification_report(target_tags, predicted_tags)

        all_tags = sorted(list(self.reader.tag_to_vector_map.keys()))
        simple_conf_matrix = confusion_matrix(target_tags, predicted_tags, labels= all_tags)

        conf_matrix_df = pd.DataFrame(data=simple_conf_matrix, columns = all_tags, index = all_tags)

        print("evaluate_model - End")
        return evaluation_report, conf_matrix_df


text:
import numpy as np

def get_repeat_length_in_history(history, tol=10 ** -5):
    """
    Find any repeat of last moves in history. Including repeats of just one element
    (so this can be used to check simple convergence as well as "cyclic convergence").

    Parameters
    ==========

    history: any iterator of numpy arrays.
    """
    size = len(history)
    for cycle_size in range(1, int(size / 2) + 1):
        if np.allclose(
            history[-cycle_size:],
            history[-2 * cycle_size : -cycle_size],
            atol=tol,
        ):
            return len(history[-cycle_size:])
    return float("inf")

def get_evolutionary_best_response(
    opponents,
    best_response_function,
    tol=10 ** -5,
    initial=np.array([1, 1, 1, 1]),
):

    history = [initial]
    best_response = best_response_function(opponents + history)
    history.append(best_response)

    repeat_length = get_repeat_length_in_history(history, tol=tol)
    while repeat_length >= float("inf"):

        best_response = best_response_function(opponents + [history[-1]])
        print("Next generation.")
        history.append(best_response)
        repeat_length = get_repeat_length_in_history(history, tol=tol)

    return best_response, history, repeat_length


text:
from spartan import expr, util, blob_ctx
import lanczos
import numpy as np
from spartan.array import distarray
from spartan.expr.shuffle import target_mapper

def svds(A, k=6):
  """Compute the largest k singular values/vectors for a sparse matrix.

  Parameters
  ----------
  A : sparse matrix
      Array to compute the SVD on, of shape (M, N)
  k : int, optional
      Number of singular values and vectors to compute.

 Returns
  -------
  u : ndarray, shape=(M, k)
      Unitary matrix having left singular vectors as columns.
  s : ndarray, shape=(k,)
      The singular values.
  vt : ndarray, shape=(k, N)
      Unitary matrix having right singular vectors as rows.
  """
  AT = expr.transpose(A)
  d, u = lanczos.solve(AT, A, k)
  d, v =  lanczos.solve(A, AT, k)
  return u, np.sqrt(d), v.T

max_stars_repo_name: MaggieQi/spartan

max_issues_repo_name: MaggieQi/spartan

max_forks_repo_name: MaggieQi/spartan


text:
import cv2
import networkx as nx

def image_file(file_name, gray=False):
    if gray:
        return cv2.imread(file_name, cv2.IMREAD_GRAYSCALE)
    else:
        return cv2.imread(file_name, cv2.IMREAD_COLOR)

def gpickle(file_name):
    return nx.read_gpickle(file_name)

# TODO: Live webcam feed

max_stars_repo_name: cdglabs/tableVision

max_issues_repo_name: cdglabs/tableVision

max_forks_repo_name: cdglabs/tableVision

reason: import networkx


text:
import os
import time
from collections import OrderedDict

import h5py
import lasagne
import matplotlib.pyplot as plt
import numpy as np
import theano
import theano.tensor as T

from visual_dynamics.utils import iter_util
try:
    from visual_dynamics.utils import visualization_theano
except ImportError:
    visualization_theano = None
from visual_dynamics.utils.config import ConfigObject, from_yaml
from visual_dynamics.utils.container import MultiDataContainer
from visual_dynamics.utils.transformer import Transformer
from . import predictor

class TheanoNetPredictor(predictor.NetPredictor, ConfigObject):
    def __init__(self, build_net, input_names, input_shapes, transformers=None, name=None, pretrained_fname=None,
                 solvers=None, environment_config=None, policy_config=None, **kwargs):
        """
        Args:
            build_net: Function that builds the net and returns a dict the network layers, which should contain at least
                all the root layers of the network. Different keys of this dict can map to the same layer.
            input_names: Iterable of names of the input variables (e.g. the image and velocity)
            input_shapes: Iterable of shapes for the image and velocity inputs.
            transformers: Iterable of transformers for the image and velocity inputs.
            name: Name of this net predictor. Defaults to class name.
            pretrained_fname: File name of h5 file with parameters to initialize the parameters of this net. The
                file name could also be an iteration number, in which case the file with the corresponding number
                in the default snapshot directory is used.
            kwargs: Optional arguments that are passed to build_net.
        """
        self.build_net = build_net
        self._kwargs = kwargs
        self.__dict__.update(kwargs)
        predictor.NetPredictor.__init__(self, input_names, input_shapes, transformers=transformers, name=name, backend='theano')
        self.pred_layers = self.build_net(self.preprocessed_input_shapes, **kwargs)
        for pred_layer in list(self.pred_layers.values()):
            self.pred_layers.update((layer.name, layer) for layer in lasagne.layers.get_all_layers(pred_layer) if layer.name is not None)
        self.input_vars = [self.pred_layers[input_name].input_var for input_name in self.input_names if input_name in self.pred_layers]
        # layer_name_aliases = [('x0', 'x'), ('x', 'x0'), ('x0_next', 'x_next'), ('x_next', 'x0_next'), ('x0', 'image_curr'), ('x0_next', 'image_next'), ('x0_next_pred', 'image_next_pred')]
        # for name, name_alias  in layer_name_aliases:
        #     if name in self.pred_layers and name_alias not in self.pred_layers:
        #         self.pred_layers[name_alias] = self.pred_layers[name]
        # self.pred_vars = OrderedDict(zip(self.pred_layers.keys(),
        #                              lasagne.layers.get_output(self.pred_layers.values(), deterministic=True)))
        print("Network %s has %d parameters" % (self.name, lasagne.layers.count_params(self.pred_layers.values())))
        self.transformers = transformers or [Transformer() for _ in self.preprocessed_input_shapes]
        self.pred_fns = {}
        self.jac_fns = {}
        if pretrained_fname is not None:
            try:
                iter_ = int(pretrained_fname)
                pretrained_fname = '%s_iter_%d_model.h5' % (self.get_snapshot_prefix(), iter_)
            except ValueError:
                pretrained_fname = pretrained_fname.replace('.yaml', '.h5')
            self.copy_from(pretrained_fname)
        # draw net and save to file
        net_graph_fname = os.path.join(self.get_model_dir(), 'net_graph.png')
        if visualization_theano is not None:
            visualization_theano.draw_to_file(self.get_all_layers(), net_graph_fname, output_shape=True, verbose=True)
        self._draw_fig_num = None
        # self.draw()
        self.solvers = solvers or []
        self.environment_config = environment_config
        self.policy_config = policy_config

    def train(self, solver_or_fname):
        if isinstance(solver_or_fname, str):
            with open(solver_or_fname) as solver_file:
                solver = from_yaml(solver_file)
        else:
            solver = solver_or_fname
        self.solvers.append(solver)
        data_fnames = solver.train_data_fnames + solver.val_data_fnames
        with MultiDataContainer(data_fnames) as data_container:
            environment_config = data_container.get_info('environment_config')
            policy_config = data_container.get_info('policy_config')
        if self.environment_config:
            if self.environment_config != environment_config:
                raise ValueError('environment config mismatch across trainings:\n%r\n%r'
                                 % (self.environment_config, environment_config))
        else:
            self.environment_config = environment_config
        if self.policy_config:
            if self.policy_config != policy_config:
                raise ValueError('policies config mismatch across trainings:\n%r\n%r'
                                 % (self.policy_config, policy_config))
        else:
            self.policy_config = policy_config
        solver.solve(self)

    def _compile_pred_fn(self, names):
        output_layers = [self.pred_layers[name] for name in names]
        pred_vars = lasagne.layers.get_output(output_layers, deterministic=True)
        input_vars = [input_var for input_var in self.input_vars if input_var in
                      theano.gof.graph.inputs(pred_vars)]
        start_time = time.time()
        print("Compiling prediction function...")
        pred_fn = theano.function(input_vars, pred_vars)
        print("... finished in %.2f s" % (time.time() - start_time))
        return pred_fn

    def predict(self, name_or_names, inputs, preprocessed=False):
        names = tuple(iter_util.flatten_tree(name_or_names))
        batch_size = self.batch_size(inputs, preprocessed=preprocessed)
        if not preprocessed:
            inputs = self.preprocess(inputs)
        inputs = [input_.astype(theano.config.floatX, copy=False) for input_ in inputs]
        if batch_size == 0:
            inputs = [input_[None, ...] for input_ in inputs]
        pred_fn = self.pred_fns.get(names) or self.pred_fns.setdefault(names, self._compile_pred_fn(names))
        preds = pred_fn(*inputs)
        if batch_size == 0:
            preds = [np.squeeze(pred, 0) for pred in preds]
        return iter_util.unflatten_tree(name_or_names, preds)

    def _get_jacobian_var(self, names, wrt_name, mode=None):
        """
        Returns the jacobian expressions and the respective outputs for a single
        data point. Assumes that the inputs being passed have a single leading
        dimension (i.e. batch_size=1).
        """
        output_wrt_vars = lasagne.layers.get_output([self.pred_layers[name] for name in (names + (wrt_name,))], deterministic=True)
        output_vars, wrt_var = output_wrt_vars[:-1], output_wrt_vars[-1]
        output_wrt_shapes = lasagne.layers.get_output_shape([self.pred_layers[name] for name in (names + (wrt_name,))])
        output_shapes, wrt_shape = output_wrt_shapes[:-1], output_wrt_shapes[-1]
        output_dim = sum([np.prod(output_shape[1:]) for output_shape in output_shapes])
        if len(wrt_shape) != 2 or wrt_shape[0] not in (1, None):
            raise ValueError("the shape of the wrt variable is %r but the"
                             "variable should be two-dimensional with the"
                             "leading axis being a singleton or None")
        _, wrt_dim = wrt_shape
        if mode is None:
            if wrt_dim < output_dim:
                mode = 'forward'
            else:
                mode = 'reverse'
        if mode in ('fwd', 'forward'):
            # compute jacobian as multiple Rop jacobian-vector product gradients for each index of wrt variable
            output_var = T.concatenate([output_var.flatten() for output_var in output_vars])
            jac_var, _ = theano.scan(lambda eval_points, output_var, wrt_var: theano.gradient.Rop(output_var, wrt_var, eval_points),
                                     sequences=np.eye(wrt_dim)[:, None, :],
                                     non_sequences=[output_var, wrt_var])
            jac_var = jac_var.T
        elif mode == 'batched':
            # same as forward mode but using batch computations as opposed to scan
            # see https://github.com/Theano/Theano/issues/4087
            output_var = T.concatenate([T.flatten(output_var, outdim=2) for output_var in output_vars], axis=1)
            jac_var = theano.gradient.Rop(output_var, wrt_var, np.eye(wrt_dim))
            input_vars = [input_var for input_var in self.input_vars if input_var in theano.gof.graph.inputs([jac_var])]
            rep_dict = {input_var: T.repeat(input_var, wrt_dim, axis=0) for input_var in input_vars}
            jac_var = theano.clone(jac_var, replace=rep_dict)
            jac_var = jac_var.T
        elif mode in ('rev', 'reverse'):
            # compute jacobian as multiple Lop vector-jacobian product gradients for each index of the output variable
            output_var = T.concatenate([output_var.flatten() for output_var in output_vars])
            jac_var = theano.gradient.jacobian(output_var, wrt_var)
            jac_var = jac_var[:, 0, :]
        elif mode == 'linear':
            jac_vars = []
            for output_var in output_vars:
                input_vars = [input_var for input_var in self.input_vars if
                              input_var in theano.gof.graph.inputs([output_var])]
                # using tensordot to multiply ones with the input_var seems to be faster than using repeat
                # rep_dict = {input_var: T.repeat(input_var, wrt_dim + 1, axis=0)
                #             for input_var in input_vars if input_var != wrt_var}
                rep_dict = {input_var: T.tensordot(T.ones((wrt_dim + 1, 1)), input_var, axes=1)
                            for input_var in input_vars if input_var != wrt_var}
                rep_dict[wrt_var] = np.r_[np.zeros((1, wrt_dim)), np.eye(wrt_dim)].astype(theano.config.floatX)
                rep_output_var = theano.clone(output_var, replace=rep_dict)
                jac_var = rep_output_var[1:] - rep_output_var[0]
                jac_var = jac_var.reshape((wrt_dim, -1, 1)).T
                jac_vars.append(jac_var)
        else:
            raise ValueError('mode can only be fwd, forward, rev, reverse, batched or linear, but %r was given' % mode)
        if mode != 'linear':
            split_inds = np.r_[0, np.cumsum([np.prod(output_shape[1:]) for output_shape in output_shapes])]
            jac_vars = [jac_var[start_ind:end_ind].reshape((1, -1, wrt_dim)) for (start_ind, end_ind) in zip(split_inds[:-1], split_inds[1:])]
        return jac_vars, output_vars

    def _compile_jacobian_fn(self, names, wrt_name, ret_outputs=False, mode=None):
        jac_vars, output_vars = self._get_jacobian_var(names, wrt_name, mode=mode)
        if ret_outputs:
            all_vars = jac_vars + output_vars
        else:
            all_vars = jac_vars
        input_vars = [input_var for input_var in self.input_vars if input_var in theano.gof.graph.inputs(all_vars)]
        start_time = time.time()
        print("Compiling jacobian function...")
        jac_fn = theano.function(input_vars, all_vars, on_unused_input='warn')
        print("... finished in %.2f s" % (time.time() - start_time))
        return jac_fn

    def jacobian(self, name_or_names, wrt_name, inputs, preprocessed=False, ret_outputs=False, mode=None):
        names = tuple(iter_util.flatten_tree(name_or_names))
        batch_size = self.batch_size(inputs, preprocessed=preprocessed)
        if not preprocessed:
            inputs = self.preprocess(inputs)
        inputs = [input_.astype(theano.config.floatX, copy=False) for input_ in inputs]
        if batch_size in (0, 1):
            if batch_size == 0:
                inputs = [input_[None, :] for input_ in inputs]
            jac_fn_args = (names, wrt_name, ret_outputs, mode)
            jac_fn = self.jac_fns.get(jac_fn_args) or \
                self.jac_fns.setdefault(jac_fn_args, self._compile_jacobian_fn(*jac_fn_args))
            preds = jac_fn(*inputs)
            if batch_size == 0:
                preds = [np.squeeze(pred, 0) for pred in preds]
        else:
            batched_preds = [self.jacobian(names, wrt_name, single_inputs,
                                           preprocessed=True, ret_outputs=ret_outputs,
                                           mode=mode) for single_inputs in zip(*inputs)]
            if ret_outputs:
                batched_preds = [sum(preds, []) for preds in batched_preds]
            preds = [np.array(pred) for pred in zip(*batched_preds)]
        if ret_outputs:
            return iter_util.unflatten_tree([name_or_names, name_or_names], preds)
        else:
            return iter_util.unflatten_tree(name_or_names, preds)

    def get_all_layers(self):
        layers = []
        for pred_layer in list(self.pred_layers.values()):
            layers.extend(lasagne.layers.get_all_layers(pred_layer))
        return lasagne.utils.unique(layers)

    def get_all_params(self, **tags):
        params = lasagne.layers.get_all_params(self.pred_layers.values(), **tags)
        params_dict = OrderedDict([(param.name, param) for param in params])
        if len(params_dict) != len(params):
            raise ValueError('parameters do not have unique names')
        return params_dict

    def get_all_param_values(self, **tags):
        params_dict = self.get_all_params(**tags)
        param_values_dict = OrderedDict([(name, param.get_value()) for (name, param) in params_dict.items()])
        return param_values_dict

    def set_all_param_values(self, param_values_dict, **tags):
        params_dict = self.get_all_params(**tags)
        set_param_names = []
        skipped_param_names = []

        for name, value in param_values_dict.items():
            try:
                param = params_dict[name]
            except KeyError:
                skipped_param_names.append(name)
                continue
            if param.get_value().shape != value.shape:
                raise ValueError('mismatch: parameter has shape %r but value to set has shape %r' %
                                 (param.get_value().shape, value.shape))
            param.set_value(value)
            set_param_names.append(name)
        if skipped_param_names:
            print('skipped parameters with names: %r' % skipped_param_names)
            print('set parameters with names: %r' % set_param_names)

    def save_model(self, model_fname):
        model_fname = model_fname.replace('.yaml', '.h5')
        all_param_values = self.get_all_param_values()
        print("Saving model parameters to file", model_fname)
        with h5py.File(model_fname, 'w') as h5_file:
            for name, value in all_param_values.items():
                h5_file.create_dataset(name, data=value)
        return model_fname

    def copy_from(self, model_fname):
        print("Copying model parameters from file", model_fname)
        with h5py.File(model_fname, 'r') as h5_file:
            param_values = dict()
            for name in h5_file.keys():
                param_values[name] = h5_file[name][:]
        param_values = OrderedDict([(name, value.astype(theano.config.floatX, copy=False)) for (name, value) in param_values.items()])
        self.set_all_param_values(param_values)

    def draw(self):
        net_graph_fname = os.path.join(self.get_model_dir(), 'net_graph.png')
        with open(net_graph_fname, 'rb') as net_graph_file:
            image = plt.imread(net_graph_file)
        plt.ion()
        fig = plt.figure(num=self._draw_fig_num, figsize=(10.*image.shape[1]/image.shape[0], 10.), tight_layout=True)
        self._draw_fig_num = fig.number
        plt.axis('off')
        fig.canvas.set_window_title('Net graph for %s' % self.name)
        plt.imshow(image)
        plt.draw()

    def _get_config(self):
        config = ConfigObject._get_config(self)
        config.update({'build_net': self.build_net,
                       'input_names': self.input_names,
                       'input_shapes': self.input_shapes,
                       'transformers': self.transformers,
                       'name': self.name,
                       'solvers': self.solvers,
                       'environment_config': self.environment_config,
                       'policy_config': self.policy_config})
        config.update(self._kwargs)
        return config

class TheanoNetFeaturePredictor(TheanoNetPredictor, predictor.FeaturePredictor):
    def __init__(self, build_net, input_names, input_shapes, feature_name,
                 next_feature_name, control_name, feature_jacobian_name=None,
                 transformers=None, name=None, pretrained_fname=None, **kwargs):
        TheanoNetPredictor.__init__(
            self, build_net, input_names, input_shapes,
            transformers=transformers, name=name,
            pretrained_fname=pretrained_fname, **kwargs)
        predictor.FeaturePredictor.__init__(
            self, input_names, input_shapes, feature_name, next_feature_name,
            control_name, feature_jacobian_name=feature_jacobian_name,
            transformers=transformers, name=name)

    def feature_jacobian(self, inputs, preprocessed=False, mode=None):
        assert len(inputs) == 2
        if self.feature_jacobian_name:
            jac, next_feature = \
                self.predict([self.feature_jacobian_name, self.next_feature_name],
                             inputs, preprocessed=preprocessed)
        else:
            jac, next_feature = self.jacobian(self.next_feature_name, self.control_name,
                                              inputs, preprocessed=preprocessed,
                                              ret_outputs=True, mode=mode)
        return jac, next_feature

    def _get_config(self):
        config = dict(TheanoNetPredictor._get_config(self))
        config.update(predictor.FeaturePredictor._get_config(self))
        return config

reason: import numpy,import theano


text:
# https://www.codewars.com/kata/55bf01e5a717a0d57e0000ec
# Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
# For example:
#  persistence(39) # returns 3, because 3*9=27, 2*7=14, 1*4=4
#                  # and 4 has only one digit

def persistence(n):
    c = 0
    if n < 10:
        return c

    while n >= 10:
        p = 1
        while n > 0:
            p *= n % 10
            n = n // 10
        c += 1
        n = p
    return c

# same as above with recursion

def persistence(n, c=0):
    if n < 10:
        return c

    p = 1
    while n > 0:
        p *= n % 10
        n = n // 10
    c += 1

    return persistence(p, c)

# using 3.8 math.prod
import math

def persistence(n):
    c, p = 0, 1
    while n >= 10:
        digits = [int(x) for x in str(n)]
        p = math.prod(digits)
        n = p
        c += 1

    return c

# using numpy
import numpy

def persistence(n):
    c, p = 0, 1
    while n >= 10:
        digits = [int(x) for x in str(n)]
        p = numpy.prod(digits)
        n = p
        c += 1

    return c

print(persistence(39), 3)
print(persistence(4), 0)
print(persistence(25), 2)
print(persistence(999), 4)

max_stars_repo_name: viszi/codes

max_issues_repo_name: viszi/codes

max_forks_repo_name: viszi/codes


text:
import argparse
import numpy as np
import os
import sys
sys.path.append(os.path.dirname(__file__))

import util
import blender_interface

p = argparse.ArgumentParser(description='Renders given obj file by rotation a camera around it.')
p.add_argument('--mesh_fpath', type=str, required=True, help='The path the of the 3D object.')
p.add_argument('--output_dir', type=str, required=True, help='The path the output will be dumped to.')
p.add_argument('--num_observations', type=int, required=True, help='Number of images to be rendered.')
p.add_argument('--sphere_radius', type=float, required=True, help='The radius of the sphere along which the camera is placed.')
p.add_argument('--mode', type=str, required=True, help='Options: train and test')

argv = sys.argv
argv = sys.argv[sys.argv.index("--") + 1:]

opt = p.parse_args(argv)

instance_name = opt.mesh_fpath.split('/')[-1]
instance_dir = os.path.join(opt.output_dir, instance_name)

renderer = blender_interface.BlenderInterface(resolution=128, background_color=(0,0,0))

if opt.mode == 'train':
    cam_locations = util.sample_spherical(opt.num_observations, opt.sphere_radius)
elif opt.mode == 'test':
    cam_locations = util.get_archimedean_spiral(opt.sphere_radius, opt.num_observations)

obj_location = np.zeros((1,3))

cv_poses = util.look_at(cam_locations, obj_location)
blender_poses = [util.cv_cam2world_to_bcam2world(m) for m in cv_poses]

shapenet_rotation_mat = np.array([[1.0000000e+00,  0.0000000e+00,  0.0000000e+00],
                                  [0.0000000e+00, -1.0000000e+00, -1.2246468e-16],
                                  [0.0000000e+00,  1.2246468e-16, -1.0000000e+00]])
# rot_mat = np.eye(3)
# hom_coords = np.array([[0., 0., 0., 1.]]).reshape(1, 4)
# obj_pose = np.concatenate((rot_mat, obj_location.reshape(3,1)), axis=-1)
# obj_pose = np.concatenate((obj_pose, hom_coords), axis=0)
obj_pose = np.eye(4)

renderer.import_mesh(opt.mesh_fpath, scale=1., object_world_matrix=obj_pose)
renderer.render(instance_dir, blender_poses, write_cam_params=True)


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb  6 12:30:54 2022

@author: docs9
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import lightgbm as lgb
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.linear_model import LinearRegression

from DataServer import GemforexData, BitflyData
from DataBuffer import DataBuffer
from StubAPI import StubAPI

from Math import Math
from const import BasicConst, IndicatorConst, ParameterConst
from TechnicalAnalysis import Indicator
from CandlePlot import CandlePlot, BandPlot, makeFig, gridFig, array2graphShape
from Strategy import AtrAlternate

c = BasicConst()
ind = IndicatorConst()
p = ParameterConst()

features1 = {
                        'ATR': {ind.TYPE:ind.ATR, p.WINDOW: 14},
                        'BBRatio': {ind.TYPE:ind.BB_RATIO, p.WINDOW: 20, p.SIGMA: 2.0},
                        'ADX':{ind.TYPE:ind.ADX, p.WINDOW: 14},
                        'PPO':{ind.TYPE:ind.PPO, p.FAST: 12, p.SLOW: 26},
                        'AroonOSC': {ind.TYPE:ind.AROON_OSC, p.WINDOW: 25},
                        'CCI': {ind.TYPE:ind.CCI, p.WINDOW: 20},
                        'MACDHist': {ind.TYPE:ind.MACD_HIST, p.FAST: 12, p.SLOW: 26, p.SIGNAL: 9},
                        'MFI': {ind.TYPE:ind.MFI, p.WINDOW:14},
                        'MOM': {ind.TYPE:ind.MOMENTUM, p.WINDOW: 26},
                        'RSI': {ind.TYPE:ind.RSI, p.WINDOW: 14},
                        'HTDCPeriod': {ind.TYPE: ind.HT_DC_PERIOD},
                        'HTDCPhase': {ind.TYPE: ind.HT_DC_PHASE},
                        'HTPhasorInphase': {ind.TYPE: ind.HT_PHASOR_INPHASE},
                        'HTPhasorQuadrature': {ind.TYPE: ind.HT_PHASOR_QUADRATURE},
                        'HTTrendmode': {ind.TYPE: ind.HT_TRENDMODE},
                        'HTTrendline': {ind.TYPE: ind.HT_TRENDLINE},
                        'LINEArreg': {ind.TYPE: ind.LINEARREG, p.WINDOW: 14},
                        'LINEArregAngle': {ind.TYPE: ind.LINEARREG_ANGLE, p.WINDOW: 14},
                        'LINEArregIntercept': {ind.TYPE: ind.LINEARREG_INTERCEPT, p.WINDOW: 14},
                        'LINEArregSlope': {ind.TYPE: ind.LINEARREG_SLOPE, p.WINDOW: 14},
                        'Beta': {ind.TYPE: ind.BETA, p.WINDOW: 5},
                        'Kama': {ind.TYPE: ind.KAMA, p.WINDOW: 30},
                        'CandleBody': {ind.TYPE: ind.CANDLE_BODY},
                        'Spike': {ind.TYPE: ind.SPIKE},
                        'Weekday': {ind.TYPE: ind.WEEKDAY},
                        'TimeBand': {ind.TYPE: ind.TIMEBAND}
                       }

features2 =          {
                        'ADX':{ind.TYPE:ind.ADX, p.WINDOW: 14},
                        'ADXR':{ind.TYPE:ind.ADXR, p.WINDOW: 14},
                        'APO': {ind.TYPE:ind.APO, p.FAST: 12, p.SLOW: 26},
                        'AROON_DOWN': {ind.TYPE: ind.AROON_DOWN, p.WINDOW: 14},
                        'AROON_UP': {ind.TYPE: ind.AROON_UP, p.WINDOW: 14},
                        'AROON_OSC': {ind.TYPE: ind.AROON_OSC, p.WINDOW: 14},
                        'CCI': {ind.TYPE:ind.CCI, p.WINDOW: 14},
                        'DX': {ind.TYPE:ind.DX, p.WINDOW: 14},
                        'MACD': {ind.TYPE:ind.MACD, p.FAST: 12, p.SLOW: 26, p.SIGNAL: 9 },
                        'MACD_signal': {ind.TYPE:ind.MACD_SIGNAL, p.FAST: 12, p.SLOW: 26, p.SIGNAL: 9 },
                        'MACD_hist': {ind.TYPE:ind.MACD_HIST, p.FAST: 12, p.SLOW: 26, p.SIGNAL: 9 },
                        'MFI': {ind.TYPE:ind.MFI, p.WINDOW:14},
                        'MOM': {ind.TYPE:ind.MOMENTUM, p.WINDOW: 10},
                        'RSI': {ind.TYPE:ind.RSI, p.WINDOW: 14},
                        'STOCHASTIC_slowk': {ind.TYPE: ind.STOCHASTIC_SLOWK, p.FASTK: 5, p.SLOWK: 3, p.SLOWD: 3},
                        'STOCHASTIC_slowd': {ind.TYPE: ind.STOCHASTIC_SLOWD, p.FASTK: 5, p.SLOWK: 3, p.SLOWD: 3},
                        'STOCHASTIC_fastk': {ind.TYPE: ind.STOCHASTIC_FASTK, p.FASTK: 5, p.FASTD: 3},
                        'ULTOSC': {ind.TYPE: ind.ULTOSC, p.FAST: 7, p.MID: 14, p.SLOW: 28},
                        'WILLR': {ind.TYPE: ind.WILLR, p.WINDOW: 14},
                        'HT_DC_period': {ind.TYPE: ind.HT_DC_PERIOD},
                        'HT_DC_phase': {ind.TYPE: ind.HT_DC_PHASE},
                        'HT_PHASOR_inphase': {ind.TYPE: ind.HT_PHASOR_INPHASE},
                        'HT_PHASOR_quadrature': {ind.TYPE: ind.HT_PHASOR_QUADRATURE},
                        'HT_TRENDLINE': {ind.TYPE: ind.HT_TRENDLINE},
                        'HT_TRENDMODE': {ind.TYPE: ind.HT_TRENDMODE},
                        'Beta': {ind.TYPE: ind.BETA, p.WINDOW: 5},
                        'LINEARREG': {ind.TYPE: ind.LINEARREG, p.WINDOW: 14},
                        'LINEARREG_ANGLE': {ind.TYPE: ind.LINEARREG_ANGLE, p.WINDOW: 14},
                        'LINEARREG_INTERCEPT': {ind.TYPE: ind.LINEARREG_INTERCEPT, p.WINDOW: 14},
                        'LINEARREG_SLOPE': {ind.TYPE: ind.LINEARREG_SLOPE, p.WINDOW: 14},
                        'STDDEV': {ind.TYPE: ind.STDDEV, p.WINDOW: 5, p.SIGMA: 1},
                        'BB_UP': {ind.TYPE: ind.BB_UP, p.WINDOW: 5, p.SIGMA: 2},
                        'BB_DOWN': {ind.TYPE: ind.BB_DOWN, p.WINDOW: 5, p.SIGMA: 2},
                        'BB_MID': {ind.TYPE: ind.BB_MID, p.WINDOW: 5, p.SIGMA: 2},
                        'DEMA': {ind.TYPE: ind.DEMA, p.WINDOW: 30},
                        'EMA': {ind.TYPE: ind.EMA, p.WINDOW: 30},
                        'KAMA': {ind.TYPE: ind.KAMA, p.WINDOW: 30},
                        'MA': {ind.TYPE: ind.MA, p.WINDOW: 30},
                        'MIDPOINT': {ind.TYPE: ind.MIDPOINT, p.WINDOW: 14},
                        'T3': {ind.TYPE: ind.T3, p.WINDOW: 5},
                        'TEMA': {ind.TYPE: ind.TEMA, p.WINDOW: 30},
                        'TRIMA': {ind.TYPE: ind.TRIMA, p.WINDOW: 30},
                        'WMA': {ind.TYPE: ind.WMA, p.WINDOW: 30}
                       }

def trade(server, atr_coef, atr_window):
    indicator_param = {'ATR': {ind.TYPE:ind.ATR, p.WINDOW:atr_window}}
    indicators = Indicator.makeIndicators(indicator_param)

    api = StubAPI(server)
    buffer = DataBuffer(indicators)
    buffer.loadData(api.allData())
    #print('len: ', buffer.length(), buffer.technical.keys())
    #print('from:', buffer.fromTime(), 'to: ', buffer.lastTime())
    #tohlcv, technical_data = buffer.dataByDate(2020, 7, 5)
    tohlcv = buffer.tohlcv
    technical_data = buffer.technical

    atr = technical_data['ATR']
    atralt = AtrAlternate(atr_coef, 1, indicators)

    summary = atralt.simulateEveryBar(tohlcv)
    #print(tohlcv.keys())
    #save('./trade_gold.csv', tohlcv, [c.TIME, c.OPEN, c.HIGH, c.LOW, c.CLOSE, 'ATR', 'long_price_open', 'long_price_close', 'short_price_open', 'short_price_close'])

    time = array2graphShape(tohlcv, [c.TIME])
    long_profit_acc = tohlcv['long_profit_acc']
    short_profit_acc = tohlcv['short_profit_acc']

    (fig, axes) = gridFig([1], (12, 4))
    fig.subplots_adjust(hspace=0.6, wspace=0.4)
    #graph1 = CandlePlot(fig, axes[0], server.title())
    #keys = [c.OPEN, c.HIGH, c.LOW, c.CLOSE]
    #graph1.drawCandle(time, tohlcv, keys)
   #graph1.drawLine(time, long_price)
   #graph1.drawLine(time, short_price, color='blue')

    #graph2 = CandlePlot(fig, axes[0], 'ATR')
    #graph2.drawLine(time, atr, color='red')
    graph3 = CandlePlot(fig, axes[0], 'Profit of ' + server.title() + '  coef: ' + str(atr_coef) + ' window: ' + str(atr_window))
    graph3.drawLine(time, long_profit_acc, color='red')
    graph3.drawLine(time, short_profit_acc, color='blue')

    return tohlcv

def isnanInVector(vector):
    for v in vector:
        if np.isnan(v):
            return True
        elif v is None:
            return True
    return False

def vectorize(data: dict, keys):
    d = data[keys[0]]
    n = len(d)
    m = len(keys) - 1
    data_list = []
    for key in keys:
        data_list.append(data[key])
    x = []
    y = []
    indices = []
    for i in range(n):
        vector = []
        for j in range(m + 1):
            vector.append(data_list[j][i])
        if  isnanInVector(vector) == False:
            y.append(vector[0])
            x.append(vector[1:])
            indices.append(i)
    return (np.array(x), np.array(y), indices)

def splitData(size, n_splits):
    l = int(size / n_splits)
    out = []
    for i in range(n_splits):
        train = []
        test = []
        begin = i * l
        stop = (i + 1) * l
        if i == n_splits - 1:
            stop = size
        for j in range(begin, stop):
            test.append(j)
        for j in range(0, begin):
            train.append(j)
        for j in range(stop, size):
            train.append(j)
        out.append([train, test])
    return out

def crossValidation(model, x, y, features):
    length = y.shape[0]
    indices = splitData(length, 5)
    pred = np.full(y.shape, np.nan)
    for [train_index, test_index] in indices:
        model.fit(x[train_index], y[train_index], feature_name=features)
        pred[test_index] = model.predict(x[test_index])
        lgb.plot_importance(model, figsize=(12, 6))

    return pred

def forceZero(predict, y):
    length = y.shape[0]
    for i in range(length):
        if y[i] == 0.0:
            predict[i] = 0.0
    return

def filtered(ror, predict, is_long):
    out = [0.0]
    for i in range(1, len(ror)):
        if is_long:
            if predict[i - 1] > 0:
                out.append(ror[i])
            else:
                out.append(0.0)
        else:
            if predict[i - 1] < 0:
                out.append(ror[i])
            else:
                out.append(0.0)
    return out

def index(array, indices):
    out = []
    for index in indices:
        out.append(array[index])
    return out

def fitting(features:dict, data:dict):
    indicators = Indicator.makeIndicators(features)
    for indicator in indicators:
        indicator.calc(data)

    ind2 = Indicator(ind.ROR, 'ROR')
    ind2.calc(data)

    keys = ['ROR'] + list(features.keys())
    x, y, indices = vectorize(data, keys)

    model = lgb.LGBMRegressor(n_jobs=-1, random_state=1)
    predict = crossValidation(model, x, y, list(features.keys()))
    forceZero(predict, y)
    evaluate(y, predict)

    short_ror = data['short_ror']
    new_short_ror = filtered(short_ror[indices], predict, False)
    new_short_ror_acc = Math.accumulate(new_short_ror)

    long_ror = data['long_ror']
    new_long_ror = filtered(long_ror[indices], predict, True)
    new_long_ror_acc = Math.accumulate(new_long_ror)

    time = index(data['time'], indices)
    (fig, axes) = gridFig([1], (12, 4))
    fig.subplots_adjust(hspace=0.6, wspace=0.4)
    graph1= CandlePlot(fig, axes[0], 'ror acc fitted')
    graph1.drawLine(time, new_long_ror_acc, color='red')
    graph1.drawLine(time, new_short_ror_acc, color='blue')

def evaluate(y_test, y_pred):
    mae = mean_absolute_error(y_test, y_pred)
    mse = mean_squared_error(y_test, y_pred)
    rmse = mean_squared_error(y_test, y_pred, squared=False)
    model = LinearRegression(
                         fit_intercept=True,
                         normalize=False,
                         copy_X=True,
                         n_jobs=1,
                         positive=True
                        )

    cor = np.corrcoef(y_test, y_pred)
    er = error_rate(y_test, y_pred)

    print('MAE: ', mae)
    print('MSE: ', mse)
    print('RMSE: ', rmse)
    print('Error Rate: ', er)
    print('correlation coef: ', cor[0][1])

    fig, ax = makeFig(1, 1, (8, 8))
    ax.scatter(y_test, y_pred, alpha=0.5, s=5)
    ax.set_xlim(-0.1, 0.2)
    ax.set_ylim(-0.1, 0.2)

def error_rate(test, pred):
    count = 0
    for t, p in zip(test, pred):
        if t * p < 0:
            count += 1
    return count / len(test)

def testGemforex1():
    title = 'Gold'
    interval = 'M5'
    csv_path = '../data/gemforex/XAUUSD_M5_202010262200_202203281055.csv'
    server = GemforexData(title, interval)
    server.loadFromCsv(csv_path)
    for window in [7, 10, 14, 21]:
        for coef in [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]:
            trade(server, coef, window)

def testGemforex2():
    title = 'Gold'
    interval = 'M5'
    csv_path = '../data/gemforex/XAUUSD_M5_202010262200_202203281055.csv'
    server = GemforexData(title, interval)
    server.loadFromCsv(csv_path)
    coef = 0.5
    window = 14
    data = trade(server, coef, window)
    fitting(features1, data)

def testBitfly():
    server = BitflyData('btcjpy', 'M15')
    server.loadFromCsv('../data/bitflyer/btcjpy_m15.csv')
    data = trade(server, 0.5, 14)
    fitting(features2, data)
    #print(data.keys())

if __name__ == '__main__':
    testBitfly()

max_stars_repo_name: IKU-Trader/TradeBots

max_issues_repo_name: IKU-Trader/TradeBots

max_forks_repo_name: IKU-Trader/TradeBots


text:
import numpy as np
from ..metas import ReplayMemory

class MemorySimple(ReplayMemory):

    def __init__(self, _capacity):
        self.data = []

    def store(self, *args):
        self.data.append(args)

    def __getitem__(self, item):
        return self.data[item]

    def sample(self, batch_size=1):
        return np.random.choice(self.data, size=batch_size)

    def __len__(self):
        return len(self.data)

    def __iter__(self):
        return iter(self.data)

    def clear(self):
        self.data = []


text:
"""
Functions to convert timestamps
==============================

"""

from __future__ import print_function
import time
import datetime
import numpy as np
import pandas as pd

def calcTime_Mat2DOY(matlab_time):

        #### EXAMPLE OF USE:
        #### pytime = calcTime_Mat2DOY(matlab_time)

    print ('Converting MATLAB timesteps to DOY:')

    timestamps = pd.to_datetime(matlab_time-719529, unit='D')
    python_time = timestamps.dayofyear + (timestamps.hour / 24.0) + (timestamps.minute / 1440.0) + (timestamps.second / 86400.0)

    return python_time

def calcTime_Date2DOY(date):

    ####        date should be forematted as YYYYmmDD

    print ('Converting date to DOY:')

    mm = date[4:6]      #### month
    DD = date[6:8]      #### day
    refDateAug = 226       #### Aug reference date for drift: 14th Aug 2018
    refDateSep = 243       #### Sep reference date for drift: 1st Sep 2018

    if mm == '08':
        doy = (float(DD) - 14.0) + refDateAug
    elif mm == '09':
        doy = float(DD) + refDateSep
    else:
        print ('****Date not valid with this function****')

    print ('----')
    print ('Date = ', date)
    print ('DOY = ', doy)
    print ('')

    return doy


text:
#!/usr/bin/env python3

import rospy
import numpy as np
from nav_msgs.msg import OccupancyGrid
from nav_msgs.msg import Odometry

def convert_world2map(world_coors, map_shape, map_scale):
    """
    World coordinate system:
        Agent starts at (0, 0) facing upward along X. Y is rightward.
    Map coordinate system:
        Agent starts at (W/2, H/2) with X rightward and Y downward.

    Inputs:
        world_coors: (bs, 2) --- (x, y) in world coordinates
        map_shape: tuple with (H, W)
        map_scale: scalar indicating the cell size in the map
    """
    H, W = map_shape
    Hby2 = (H - 1) / 2 if H % 2 == 1 else H // 2
    Wby2 = (W - 1) / 2 if W % 2 == 1 else W // 2

    x_world = world_coors[:, 0]
    y_world = world_coors[:, 1]

    # x_map = torch.clamp((Wby2 + y_world / map_scale), 0, W - 1).round()
    # y_map = torch.clamp((Hby2 - x_world / map_scale), 0, H - 1).round()
    x_map = np.clip((Hby2 - y_world / map_scale), 0, H - 1).round()
    y_map = np.clip((Wby2 + x_world / map_scale), 0, W - 1).round()

    map_coors = np.stack([x_map, y_map], dim=1)  # (bs, 2)

    return map_coors

def callback(data, prev_path_map):
    path_pub = rospy.Publisher('path_map', OccupancyGrid, queue_size=1)
    odomX = data.pose.pose.position.x
    odomY = data.pose.pose.position.y
    prev_map = pre_path_map


text:
from espy.channelNoiseSimulator import channel_noise_simulator
import numpy

numpy.random.seed(42)

def test_bitGen():
    cns = channelNoiseSimulator()
    res = numpy.alltrue(cns.createRandomBits(10) == [0, 1, 0, 0, 0, 1, 0, 0, 0, 1])
    assert res

def test_simpleRandom():
    cns = channelNoiseSimulator()
    res = numpy.alltrue(
        cns.randomiseBits([0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 0.5)
        == [1, 0, 0, 1, 0, 0, 0, 1, 1, 0]
    )
    assert res

# test_bitGen()
# test_simpleRandom()

# cns = channelNoiseSimulator()

max_stars_repo_name: Yomikron/espy

max_issues_repo_name: Yomikron/espy

max_forks_repo_name: Yomikron/espy


text:
from __future__ import print_function, division
import numpy as np
import torch

import torch
import os
import numpy as np
import torch.nn as nn

def zy_deconv(img, in_channels, out_channels, kernels,bias, stride=1, padding=0,output_padding=0):
    #得到参数
    N, C, H, W = img.shape
    kc,kc_in,kh, kw = kernels.shape
    p = padding

    #间隔填充
    if  stride>1:
       gap_img=np.zeros([N,in_channels,stride*H-1,stride*W-1])
       for n in range(N):
           for in_c in range(in_channels):
               for  x in range(H):
                   for y in range(W):
                        gap_img[n][in_c][stride*x][stride*y]=img[n][in_c][x][y]
       N, C, H, W = gap_img.shape
       img=gap_img

    #四周填充
    p=kh-p-1
    if p:
        img = np.pad(img, ((0, 0), (0, 0), (p, p), (p, p)), 'constant')  # padding along with all axis

    out_h = (H + 2 * p - kh) + 1
    out_w = (W + 2 * p - kw) + 1

    #右边和下方填充
    if output_padding:
        temp = np.zeros([N,in_channels, H+2*p+output_padding, W+2*p+output_padding])
        #先将原有的数据填入
        for n in range(N):
             for in_c in range(in_channels):
                for  x in range(H+2*p):
                   for y in range(W+2*p):
                     temp[n][in_c][x][y] =img[n][in_c][x][y]

                for  a  in range(output_padding):
                    #对下方与右方填入output_padding行（列）的0
                    for  i in range (W+2*p) :
                        temp[n][in_c][H+2*p+a][i]=0
                    for  j  in range (H+2*p+output_padding) :
                        temp[n][in_c][j][W+2*p+a]=0
        img=temp
        out_h=  out_h+output_padding
        out_w=  out_w+output_padding

    #卷积
    outputs = np.zeros([N, out_channels, out_h, out_w])
    for n in range(N):
        for out_c in range(out_channels):
            for in_c in range(in_channels):
                for h in range(out_h):
                    for w in range(out_w):
                             for x in range(kh):
                                 for y in range(kw):
                                     #这里的矩阵旋转了180度
                                    outputs[n][out_c][h][w] += img[n][in_c][h + x][w + y] * kernels[in_c][out_c][kh-x-1][kw-y-1]
            #添加偏置
            if in_c == in_channels - 1:
                            outputs[n][out_c][:][:] += bias[out_c]

    #转化为tensor格式，内部数据为float32的形式
    outputs = torch.tensor(outputs, dtype=torch.float32)
    return outputs

img = np.asarray(
        [[
            [
                [1, 2, 3],
                [6, 5, 4],
                [7, 8, 9]
            ],
            [
                [1, 2, 3],
                [6, 5, 4],
                [7, 8, 9]
            ],
            [
                [1, 2, 3],
                [6, 5, 4],
                [7, 8, 9]
            ],
        ]]
)

#将img转化为tensor类型
img= torch.tensor(img, dtype=torch.float32)
deconv= nn.ConvTranspose2d(3, 2, 3, 2, 1,output_padding=1)#输入通道，输出通道，卷积核大小，步长，padding,output_padding

#进行反卷积操作
out=deconv(img)
print("卷积核：")
print(deconv.weight)
print("偏置：")
print(deconv.bias)
print("pytorch自带反卷积函数运行结果：")
print(out)

#提取出卷积核与偏置矩阵
kernels=deconv.weight
kernals=kernels.detach().numpy()
bias=deconv.bias
bias=bias.detach().numpy()

in_channels = 3
out_channels = 2

#将提取出来的卷积核与偏置矩阵代入自编的卷积函数中，对比自编函数与pytorch自带函数的结果
outputs = zy_deconv(img, in_channels, out_channels, kernels,bias, stride=2, padding=1, output_padding=1)
print("自编反卷积函数运行结果：")
print(outputs)

max_stars_repo_name: orange-eng/internship

max_issues_repo_name: orange-eng/internship

max_forks_repo_name: orange-eng/internship