text:
import numpy as onp

from jax.numpy import lax_numpy as np
from jax.numpy import linalg as la
from jax.numpy.lax_numpy import _wraps

def _isEmpty2d(arr):
  # check size first for efficiency
  return arr.size == 0 and np.product(arr.shape[-2:]) == 0

def _assertNoEmpty2d(*arrays):
  for a in arrays:
    if _isEmpty2d(a):
      raise onp.linalg.LinAlgError("Arrays cannot be empty")

def _assertRankAtLeast2(*arrays):
  for a in arrays:
    if a.ndim < 2:
      raise onp.linalg.LinAlgError(
          '%d-dimensional array given. Array must be '
          'at least two-dimensional' % a.ndim)

def _assertNdSquareness(*arrays):
  for a in arrays:
    m, n = a.shape[-2:]
    if m != n:
      raise onp.linalg.LinAlgError(
          'Last 2 dimensions of the array must be square')

def _assert2d(*arrays):
    for a in arrays:
        if a.ndim != 2:
            raise ValueError(f'{a.ndim}-dimensional array given. '
                             'Array must be two-dimensional')

@_wraps(onp.linalg.cond)
def cond(x, p=None):
  _assertNoEmpty2d(x)
  if p in (None, 2):
    s = la.svd(x, compute_uv=False)
    return s[..., 0] / s[..., -1]
  elif p == -2:
    s = la.svd(x, compute_uv=False)
    r = s[..., -1] / s[..., 0]
  else:
    _assertRankAtLeast2(x)
    _assertNdSquareness(x)
    invx = la.inv(x)
    r = la.norm(x, ord=p, axis=(-2, -1)) * la.norm(invx, ord=p, axis=(-2, -1))

  # Convert nans to infs unless the original array had nan entries
  orig_nan_check = np.full_like(r, ~np.isnan(r).any())
  nan_mask = np.logical_and(np.isnan(r), ~np.isnan(x).any(axis=(-2, -1)))
  r = np.where(orig_nan_check, np.where(nan_mask, np.inf, r), r)
  return r

@_wraps(onp.linalg.tensorinv)
def tensorinv(a, ind=2):
  a = np.asarray(a)
  oldshape = a.shape
  prod = 1
  if ind > 0:
    invshape = oldshape[ind:] + oldshape[:ind]
    for k in oldshape[ind:]:
      prod *= k
  else:
    raise ValueError("Invalid ind argument.")
  a = a.reshape(prod, -1)
  ia = la.inv(a)
  return ia.reshape(*invshape)

@_wraps(onp.linalg.tensorsolve)
def tensorsolve(a, b, axes=None):
  a = np.asarray(a)
  b = np.asarray(b)
  an = a.ndim
  if axes is not None:
    allaxes = list(range(0, an))
    for k in axes:
      allaxes.remove(k)
      allaxes.insert(an, k)

    a = a.transpose(allaxes)

  Q = a.shape[-(an - b.ndim):]

  prod = 1
  for k in Q:
    prod *= k

  a = a.reshape(-1, prod)
  b = b.ravel()

  res = np.asarray(la.solve(a, b))
  res = res.reshape(Q)

  return res

@_wraps(onp.linalg.multi_dot)
def multi_dot(arrays, *, precision=None):
    n = len(arrays)
    # optimization only makes sense for len(arrays) > 2
    if n < 2:
        raise ValueError("Expecting at least two arrays.")
    elif n == 2:
        return np.dot(arrays[0], arrays[1], precision=precision)

    arrays = [np.asarray(a) for a in arrays]

    # save original ndim to reshape the result array into the proper form later
    ndim_first, ndim_last = arrays[0].ndim, arrays[-1].ndim
    # Explicitly convert vectors to 2D arrays to keep the logic of the internal
    # _multi_dot_* functions as simple as possible.
    if arrays[0].ndim == 1:
        arrays[0] = np.atleast_2d(arrays[0])
    if arrays[-1].ndim == 1:
        arrays[-1] = np.atleast_2d(arrays[-1]).T
    _assert2d(*arrays)

    # _multi_dot_three is much faster than _multi_dot_matrix_chain_order
    if n == 3:
        result = _multi_dot_three(*arrays, precision)
    else:
        order = _multi_dot_matrix_chain_order(arrays)
        result = _multi_dot(arrays, order, 0, n - 1, precision)

    # return proper shape
    if ndim_first == 1 and ndim_last == 1:
        return result[0, 0]  # scalar
    elif ndim_first == 1 or ndim_last == 1:
        return result.ravel()  # 1-D
    else:
        return result

def _multi_dot_three(A, B, C, precision):
    """
    Find the best order for three arrays and do the multiplication.
    For three arguments `_multi_dot_three` is approximately 15 times faster
    than `_multi_dot_matrix_chain_order`
    """
    a0, a1b0 = A.shape
    b1c0, c1 = C.shape
    # cost1 = cost((AB)C) = a0*a1b0*b1c0 + a0*b1c0*c1
    cost1 = a0 * b1c0 * (a1b0 + c1)
    # cost2 = cost(A(BC)) = a1b0*b1c0*c1 + a0*a1b0*c1
    cost2 = a1b0 * c1 * (a0 + b1c0)

    if cost1 < cost2:
        return np.dot(np.dot(A, B, precision=precision), C, precision=precision)
    else:
        return np.dot(A, np.dot(B, C, precision=precision), precision=precision)

def _multi_dot_matrix_chain_order(arrays, return_costs=False):
    """
    Return a np.array that encodes the optimal order of mutiplications.
    The optimal order array is then used by `_multi_dot()` to do the
    multiplication.
    Also return the cost matrix if `return_costs` is `True`
    The implementation CLOSELY follows Cormen, "Introduction to Algorithms",
    Chapter 15.2, p. 370-378.  Note that Cormen uses 1-based indices.
        cost[i, j] = min([
            cost[prefix] + cost[suffix] + cost_mult(prefix, suffix)
            for k in range(i, j)])
    """
    n = len(arrays)
    # p stores the dimensions of the matrices
    # Example for p: A_{10x100}, B_{100x5}, C_{5x50} --> p = [10, 100, 5, 50]
    p = [a.shape[0] for a in arrays] + [arrays[-1].shape[1]]
    # m is a matrix of costs of the subproblems
    # m[i,j]: min number of scalar multiplications needed to compute A_{i..j}
    m = onp.zeros((n, n), dtype=onp.double)
    # s is the actual ordering
    # s[i, j] is the value of k at which we split the product A_i..A_j
    s = onp.empty((n, n), dtype=onp.intp)

    for l in range(1, n):
        for i in range(n - l):
            j = i + l
            m[i, j] = np.inf
            for k in range(i, j):
                q = m[i, k] + m[k+1, j] + p[i]*p[k+1]*p[j+1]
                if q < m[i, j]:
                    m[i, j] = q
                    s[i, j] = k  # Note that Cormen uses 1-based index

    return (s, m) if return_costs else s

def _multi_dot(arrays, order, i, j, precision):
    """Actually do the multiplication with the given order."""
    if i == j:
        return arrays[i]
    else:
        return np.dot(_multi_dot(arrays, order, i, order[i, j], precision),
                      _multi_dot(arrays, order, order[i, j] + 1, j, precision),
                      precision=precision)

max_stars_repo_name: nirum/jax

max_issues_repo_name: nirum/jax

max_forks_repo_name: nirum/jax

reason: import numpy,from jax


text:
import math
import itertools
import sys

import numpy as np

import perimeter
from util import manhattanDistance, removeDupsFromPointList

def toIntersectingLines(mesh, height):
    relevantTriangles = list(filter(lambda tri: isAboveAndBelow(tri, height), mesh))
    notSameTriangles = filter(lambda tri: not isIntersectingTriangle(tri, height), relevantTriangles)
    lines = list(map(lambda tri: triangleToIntersectingLines(tri, height), notSameTriangles))
    return lines

def drawLineOnPixels(p1, p2, pixels):
    lineSteps = math.ceil(manhattanDistance(p1, p2))
    if lineSteps == 0:
        pixels[int(p1[0]), int(p2[1])] = True
        return
    for j in range(lineSteps + 1):
        point = linearInterpolation(p1, p2, j / lineSteps)
        pixels[int(point[0]), int(point[1])] = True

def linearInterpolation(p1, p2, distance):
    '''
    :param p1: Point 1
    :param p2: Point 2
    :param distance: Between 0 and 1, Lower numbers return points closer to p1.
    :return: A point on the line between p1 and p2
    '''
    slopex = (p1[0] - p2[0])
    slopey = (p1[1] - p2[1])
    slopez = p1[2] - p2[2]
    return (
        p1[0] - distance * slopex,
        p1[1] - distance * slopey,
        p1[2] - distance * slopez
    )

def isAboveAndBelow(pointList, height):
    '''

    :param pointList: Can be line or triangle
    :param height:
    :return: true if any line from the triangle crosses or is on the height line,
    '''
    above = list(filter(lambda pt: pt[2] > height, pointList))
    below = list(filter(lambda pt: pt[2] < height, pointList))
    same = list(filter(lambda pt: pt[2] == height, pointList))
    if len(same) == 3 or len(same) == 2:
        return True
    elif (above and below):
        return True
    else:
        return False

def isIntersectingTriangle(triangle, height):
    assert (len(triangle) == 3)
    same = list(filter(lambda pt: pt[2] == height, triangle))
    return len(same) == 3

def triangleToIntersectingLines(triangle, height):
    assert (len(triangle) == 3)
    above = list(filter(lambda pt: pt[2] > height, triangle))
    below = list(filter(lambda pt: pt[2] < height, triangle))
    same = list(filter(lambda pt: pt[2] == height, triangle))
    assert len(same) != 3
    if len(same) == 2:
        return same[0], same[1]
    elif len(same) == 1:
        side1 = whereLineCrossesZ(above[0], below[0], height)
        return side1, same[0]
    else:
        lines = []
        for a in above:
            for b in below:
                lines.append((b, a))
        side1 = whereLineCrossesZ(lines[0][0], lines[0][1], height)
        side2 = whereLineCrossesZ(lines[1][0], lines[1][1], height)
        return side1, side2

def whereLineCrossesZ(p1, p2, z):
    if (p1[2] > p2[2]):
        t = p1
        p1 = p2
        p2 = t
    # now p1 is below p2 in z
    if p2[2] == p1[2]:
        distance = 0
    else:
        distance = (z - p1[2]) / (p2[2] - p1[2])
    return linearInterpolation(p1, p2, distance)

def calculateScaleAndShift(mesh, resolution):
    """
    Calculates the bounding box in voxel units, the shift vector to bring
    one corner to zero and a scale factor based on the XY resolution.
    """
    allPoints = np.array([item for sublist in mesh for item in sublist])
    mins = allPoints.min(axis=0)
    maxs = allPoints.max(axis=0)
    print("Physical Bounding box found between {} and {}".format(mins, maxs), file=sys.stderr)
    # shift: vector which can be added to the mins vector to get to (0,0,0)
    # --> physical units
    shift = -mins

    # --> voxel units per physical units
    scale = [resolution] * 3
    # Returns the scaled bounding box --> this is now in voxel units
    bounding_box = np.ceil(maxs - mins).astype(np.int) * resolution
    print("Image Size: {} voxels".format(bounding_box), file=sys.stderr)
    return (scale, shift, bounding_box)

def scaleAndShiftMesh(mesh, scale, shift):
    for tri in mesh:
        newTri = []
        for pt in tri:
            newpt = [0, 0, 0]
            for i in range(3):
                newpt[i] = (pt[i] + shift[i]) * scale[i]
            newTri.append(tuple(newpt))
        if len(removeDupsFromPointList(newTri)) == 3:
            yield newTri
        else:
            pass

ext: py

max_stars_repo_name: reox/stl-to-voxel

max_issues_repo_name: reox/stl-to-voxel

max_forks_repo_name: reox/stl-to-voxel

reason: import numpy


text:
import os
import argparse
import random
import json
import math
from tqdm import tqdm
import numpy as np
import torch
# import torch.nn as nn
from torch.utils.data import RandomSampler, SequentialSampler
from torch.utils.data.dataloader import DataLoader

from transformers import BartTokenizer, BartForConditionalGeneration, AutoConfig
from transformers.optimization import AdamW, get_linear_schedule_with_warmup

from dataset_simmc import SimmcDataset, SimmcDataCollator

import logging
logging.basicConfig(level = logging.INFO)
logger = logging.getLogger(__name__)

def set_seed(seed: int):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)

def training_step(model, inputs, args, loss_fn):
        model.train()
        for k, v in inputs.items():
            if isinstance(v, torch.Tensor):
                inputs[k] = v.to(args.device)

        # compute loss
        if "labels" in inputs:
            labels = inputs.pop("labels")

        # force training to ignore pad token
        output = model(**inputs, use_cache=False)
        logits = output.logits

        loss = loss_fn(logits.view(-1, logits.shape[-1]), labels.view(-1))

        if args.n_gpu > 1:
            loss = loss.mean()  # mean() to average on multi-gpu parallel training

        if args.gradient_accumulation_steps > 1:
            loss = loss / args.gradient_accumulation_steps

        loss.backward()

        return loss.detach()

def prediction_step(model, inputs, loss_fn, prediction_loss_only=True):
        for k, v in inputs.items():
            if isinstance(v, torch.Tensor):
                inputs[k] = v.to(args.device)

        if "labels" in inputs:
            labels = inputs.pop("labels")

        with torch.no_grad():
            # compute loss on predict data
            output = model(**inputs, use_cache=False)
            logits = output.logits

            loss = loss_fn(logits.view(-1, logits.shape[-1]), labels.view(-1))

        return loss.mean().detach()

def train(model, args, train_dataset, eval_dataset, data_collator, tokenizer, config=None):
        # Create output directory if needed
        os.makedirs(args.output_dir, exist_ok=True)

        if config is None:
            config = model.config

        # vocab_size = config.vocab_size
        loss_fn = torch.nn.CrossEntropyLoss(ignore_index=config.pad_token_id)

        # Data loader and number of training steps
        train_sampler = RandomSampler(train_dataset)
        train_dataloader = DataLoader(train_dataset,
            batch_size=args.train_batch_size,
            sampler=train_sampler,
            collate_fn=data_collator,
        )

        eval_sampler = SequentialSampler(eval_dataset)
        eval_dataloader = DataLoader(
            eval_dataset,
            sampler=eval_sampler,
            batch_size=args.eval_batch_size,
            collate_fn=data_collator,
        )

        # Multi-gpu training (should be after apex fp16 initialization)
        if args.n_gpu > 1:
            model = torch.nn.DataParallel(model)

        model = model.to(args.device)

        # Setting up training control variables:
        # number of training epochs: num_train_epochs
        # number of training steps per epoch: num_update_steps_per_epoch
        # total number of training steps to execute: max_steps
        num_update_steps_per_epoch = len(train_dataloader) // args.gradient_accumulation_steps
        num_update_steps_per_epoch = max(num_update_steps_per_epoch, 1)
        max_steps = math.ceil(args.num_train_epochs * num_update_steps_per_epoch)
        num_train_epochs = math.ceil(args.num_train_epochs)

        # create optimizer and scheduler
        no_decay = ["bias", "LayerNorm.weight"]
        optimizer_grouped_parameters = [
            {
                "params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
                "weight_decay": args.weight_decay,
            },
            {
                "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
                "weight_decay": 0.0,
            },
        ]
        optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate)
        lr_scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=max_steps)

        # Train
        total_train_batch_size =  args.train_batch_size * args.gradient_accumulation_steps

        num_examples = len(train_dataloader.dataset)

        logger.info("***** Running training *****")
        logger.info("  Num examples = %d", num_examples)
        logger.info("  Num Epochs = %d", num_train_epochs)
        logger.info("  Instantaneous batch size per device = %d", args.train_batch_size)
        logger.info("  Total train batch size (w. parallel, distributed & accumulation) = %d", total_train_batch_size)
        logger.info("  Gradient Accumulation steps = %d", args.gradient_accumulation_steps)
        logger.info("  Total optimization steps = %d", max_steps)

        train_loss = []
        best_eval_loss = float('inf')
        best_epoch = 0
        model.zero_grad()
        for epoch in range(num_train_epochs):
            steps_in_epoch = len(train_dataloader)

            for step, inputs in enumerate(tqdm(train_dataloader, desc="Training")):
                tr_loss = training_step(model, inputs, args, loss_fn)
                train_loss.append(tr_loss.item())

                if (step + 1) % args.gradient_accumulation_steps == 0 or (
                    # last step in epoch but step is always smaller than gradient_accumulation_steps
                    steps_in_epoch <= args.gradient_accumulation_steps
                    and (step + 1) == steps_in_epoch
                ):
                    torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
                    optimizer.step()
                    lr_scheduler.step()
                    model.zero_grad()

            # evaluate
            eval_loss = []
            model.eval()
            for step, inputs in enumerate(tqdm(eval_dataloader, desc="Evaluation")):
                loss = prediction_step(model, inputs, loss_fn)
                eval_loss.append(loss.item())

            eval_loss = np.mean(eval_loss)
            print("Epoch:", epoch+1, "Train loss:", np.mean(train_loss), "Eval loss:", eval_loss)

            if eval_loss < best_eval_loss:
                best_eval_loss = eval_loss
                best_epoch = epoch
                # model.save_pretrained(args.output_dir)
                # torch.save(model,args.output_dir+"/check_saving")
                torch.save(model,args.output_dir+"/pytorch_model.bin")
                tokenizer.save_pretrained(args.output_dir)
                print("Saving best model at epoch", epoch+1)

            if args.patience > 0 and (epoch - best_epoch) > args.patience:
                print("Early stopping")
                break

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    # Data arguments
    parser.add_argument('--data_dir', type=str, default='data', help='Input data directory')
    parser.add_argument('--output_dir', type=str, default='ckpt', help='Output directory where the model predictions and checkpoints will be written')

    # Model arguments
    parser.add_argument('--config_name', type=str, default=None, help='Pretrained config name or path if not the same as model_name')
    parser.add_argument('--model_name_or_path', type=str, default='facebook/bart-large', help='Path to pretrained model or model identifier from huggingface.co/models')

    # Training arguments
    parser.add_argument('--seed', type=int, default=42, help='Random seed for initialization')
    parser.add_argument('--train_batch_size', type=int, default=8, help='Batch size for training')
    parser.add_argument('--eval_batch_size', type=int, default=32, help='Batch size for evaluation')
    parser.add_argument('--learning_rate', type=float, default=1e-5, help='Learning rate')
    parser.add_argument('--num_train_epochs', type=int, default=50, help='Number of training epochs')
    parser.add_argument('--gradient_accumulation_steps', type=int, default=1, help='Number of updates steps to accumulate the gradients for')
    parser.add_argument('--max_grad_norm', type=float, default=1.0, help='Maximum gradient norm (for gradient clipping)')
    parser.add_argument('--weight_decay', type=float, default=0.0, help='The weight decay to apply (if not zero)')
    parser.add_argument('--patience', type=int, default=3, help='Number of epochs with no improvement for early stopping. Set to 0 to disable early stopping')
    args = parser.parse_args()

    args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    args.n_gpu = torch.cuda.device_count()

    # set seed
    if args.seed is not None:
        set_seed(args.seed)

    # init model
    config = AutoConfig.from_pretrained(args.model_name_or_path)
    tokenizer = BartTokenizer.from_pretrained(args.model_name_or_path)

    # add special tokens
    special_tokens = None
    special_tokens_file = os.path.join(args.data_dir, "simmc2_special_tokens.json")
    if os.path.exists(special_tokens_file):
        with open(special_tokens_file, "r") as f:
            special_tokens = json.load(f)

        tokenizer.add_special_tokens(special_tokens)
    print("Vocab size:", len(tokenizer)) # 50281

    model = BartForConditionalGeneration.from_pretrained(args.model_name_or_path, config=config)
    if special_tokens is not None:
        model.resize_token_embeddings(len(tokenizer))
        model.vocab_size = len(tokenizer)

    # init dataset
    train_src_file = os.path.join(args.data_dir, "train_predict.txt")
    train_tgt_file = os.path.join(args.data_dir, "train_target.txt")
    eval_src_file = os.path.join(args.data_dir, "dev_predict.txt")
    eval_tgt_file = os.path.join(args.data_dir, "dev_target.txt")

    train_dataset = SimmcDataset(tokenizer, train_src_file, train_tgt_file, prefix=config.prefix or "", is_lm=False)
    eval_dataset = SimmcDataset(tokenizer, eval_src_file, eval_tgt_file, prefix=config.prefix or "", is_lm=False)
    data_collator = SimmcDataCollator(tokenizer)

    # run train
    train(model, args, train_dataset, eval_dataset, data_collator, tokenizer, config)


text:
"""
* Assignment: Numpy Slice 1
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
    1. Print inner 2x2 elements
    2. Run doctests - all must succeed

Polish:
    1. Wybierz wewnętrzne 2x2 elementy
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is np.ndarray, \
    'Variable `result` has invalid type, expected: np.ndarray'

    >>> result
    array([[8, 4],
           [5, 2]])
"""

import numpy as np

DATA = np.array([
    [2, 8, 1, 5],
    [8, 8, 4, 4],
    [5, 5, 2, 5],
    [1, 0, 6, 0],
])

result = ...


text:
#!/usr/bin/python

"""
G2_RIGHTS.

This module defines utility functions to generate graph properties for a Mininet network.

"""

from collections import defaultdict
import networkx as nx
import json
import os
from itertools import combinations
import operator
from mininet.log import info

def writeAdjList(mnet, outfile):
    """Generate adjacency list for a graph given by a Mininet topology and write it to an output file.

    Args:
        mnet (mininet.net.Mininet): Mininet network object.
        outfile (str): Output file path.

    """

    allLinks = mnet.topo.iterLinks() # list of (src,dst) tuples
    G = nx.Graph(allLinks)
    nx.write_adjlist(G, outfile)

def generatePaths(adjfile, outfile, trafficEndPoints, traversedNodes, configuredLinks):
    """Generate shortest path between each pair of nodes of a graph represented by its adjacency list.

    Args:
        adjfile (str): Path to a file containing adjacency list of a graph.
        outfile (str): Output file path.
        trafficEndPoints (list): List of traffic flow end points (src_host, dst_host).
        traversedNodes (list): List of traversed nodes for each specified flow path.
        configuredLinks (list): List of tuples, each representing a user-specified non-default link.

    Returns:
        dict: With (key, value) = (each node as src, dictionary containing dst-path as key-value)

    """

    G = nx.read_adjlist(adjfile)
    paths = (nx.shortest_path(G))

    # If there are multiple paths of same (shortest) length between a pair of nodes,
    # nx.shortest_path() can pick any one. Therefore, we make sure that
    # each reverse path is literally the reverse of the forward path.
    for n1, n2 in combinations(paths.keys(), 2):
        fwd = paths[n1][n2]
        dwf = fwd[::-1]
        rev = paths[n2][n1]
        if dwf != rev:
            paths[n2][n1] = dwf

    # Enforce to choose shortest paths which traverse the configured links, wherever applicable.
    # ['src']['dst'][idx] -> number_of_configured_links
    if configuredLinks:
        pathDict = {}
        for job_id, (nodes, (tSrc,tDst)) in enumerate(zip(traversedNodes, trafficEndPoints)):
            if tSrc not in pathDict:
                pathDict[tSrc] = dict()
            pathDict[tSrc][tDst] = defaultdict(int)

            # If the specified path is not 'N/A' and the specified path is a simple path, use the specified path
            useShortestPath = True
            if 'N/A' not in nodes:
                srcDstPaths = [p for p in nx.shortest_simple_paths(G, tSrc, tDst)]
                if nodes in srcDstPaths:
                    pth = nodes
                    useShortestPath = False
                else:
                    info("**** [G2]: The specified path for flow %d is not a simple path; will use a shortest path \n" %(job_id))

            if useShortestPath:
                srcDstPaths = [p for p in nx.all_shortest_paths(G, tSrc, tDst)]
                for idx, pth in enumerate(srcDstPaths):
                    for bSrc, bDst in configuredLinks:
                        try:
                            if abs(pth.index(bSrc) - pth.index(bDst)) == 1: # configured link (bSrc,bDst) is present on path 'pth'
                                pathDict[tSrc][tDst][idx] += 1
                        except ValueError:
                            # ValueError occurs when configured link is not part of any of the shortest paths.
                            continue
                # Get the path having max configured link on it.
                # Which means -- get key with max value in dictionary pathDict[tSrc][tDst].
                maxIdx = max(pathDict[tSrc][tDst].items(), key=operator.itemgetter(1))[0]
                pth = srcDstPaths[maxIdx]
            paths[tSrc][tDst] = pth
            # Again, set the reverse path.
            paths[tDst][tSrc] = pth[::-1]

    with open(outfile, "w") as write_file:
        json.dump(paths, write_file, indent=1, sort_keys=False)
    write_file.close()
    return paths

def readFromPathFile(infile):
    """Read path information from a file.

    Args:
        infile (str): Path to input file.

    """

    with open(infile, "r") as read_file:
        data = json.load(read_file)
    read_file.close()
    return data

def generateRoutingConf(mnet, paths, outfile):
    """For a given Mininet network and given paths (flows), generate routing config file.

    Args:
        mnet (mininet.net.Mininet): Mininet network object.
        paths (dict): Dictionary containing paths.
        outfile (str): Output file path.

    """

    topo = mnet.topo
    links = topo.iterLinks()
    srcs = paths.keys()
    routingInfo = defaultdict(list)
    for src in srcs:
        if not topo.isSwitch(src):
            dsts = paths[src].keys()
            for dst in dsts:
                if not topo.isSwitch(dst):
                    if src != dst:
                        hoplist = paths[src][dst]
                        for idx in range(len(hoplist) - 2):
                            firsthop = hoplist[idx]
                            secondhop = hoplist[idx+1]
                            thirdhop = hoplist[idx+2]
                            rcvPort = topo.port(firsthop, secondhop)[1] # dst switch side port
                            sendPort = topo.port(secondhop,thirdhop)[0] # source switch side port towards nexthop
                            routingInfo[secondhop].append(src + '-' + dst + ': ' + str(rcvPort) + '-' + str(sendPort))

    with open(outfile, 'w') as wf:
        sections = routingInfo.keys()
        for sc in sections:
            wf.write('[%s]\n' %sc)
            for entry in routingInfo[sc]:
                wf.write(entry+'\n')
            wf.write('\n')
    wf.close()

def getPathFeasibility(mnet, adjfile, paths):
    """Check the dictionary 'paths' on the graph represented by Mininet network.
    The paths dictionary is in specific format, that matches with the outputs of networkx.shortest_path(G) and our generateShortestPaths(adjfile, outfile) functions.

    Args:
        mnet (mininet.net.Mininet): Mininet network object.
        paths (dict): Dictionary containing paths
        adjfile (str): Path to file containing adjacency list of the network (in a format feasible for Python networkx)

    Returns:
        bool: Returns True if every path in 'paths' is feasible on the graph. False otherwise.

    """

    # If adjacency list file is not present, create it and then create a graph out of it.
    if not os.path.exists(adjfile):
        writeAdjList(mnet, adjfile)
    G = nx.read_adjlist(adjfile)

    for src in paths.keys():
        for dst in paths[src].keys():
            path = paths[src][dst] # list of nodes on the path, including src and dst.
            pair_list = zip(path[:-1], path[1:])
            # For single node path (i.e., src == dst), pair_list will be [], so below check is bypassed.
            for n1, n2 in pair_list:
                if not G.has_edge(n1, n2):
                    print("Routing conf not feasible. Non-existent edge (%s,%s) as specified in conf, path %s.\n"
                          %(n1, n2, "-".join(path)))
                    return False
    return True

def getG2Inputs(conf, mnet):
    """Get dictionaries C, F, and flow info (RTT and path) for each traffic flow.
    Write these dictionaries to JSON files.

    Args:
        conf (ConfigHandler): ConfigHandler object containing user-specified configurations.
        mnet (NetworkSimulator): The NetworkSimulator object.

    Returns:
        C (dict)    [linkID] -> linkCapacity
        F (dict)    [flowID] -> [linkIDs]
        flowInfo (dict) [flowID] -> {linkStr, links, rtt}

    Note:
        -- All IDs are integers starting at 1, prefixed with 'l' or 'f'' (for link or flow respectively).
        -- C, F, and L are written to c_dict.json, f_dict.json, and l_dict.json files in the output directory (as specified in config).

    """

    flows = conf.trace.jobs
    topoConf = conf.topoData
    paths = mnet.paths
    topo = mnet.net.topo
    # The capacity dictionary.
    C = {}
    # The flow dictionary.
    # A flow is the list of links traversed by it.
    F = {}
    # linkID -> [linkStr]. Example, {'l1': 's1-s2'}.
    L = conf.topoData['L']
    # [linkStr] -> linkID. Example, {'s1-s2': 'l1'}.
    reverseL = dict((v,k) for k,v in L.items())
    # Dictionary to hold flow information and RTT.
    flowInfo = {}

    # Link specifications are provided by the user under either 'link_info' or 'default_link_info' parameters.
    linkInfos = topoConf['linkInfos']
    defaultBW = float(topoConf['defaultLinkInfo']['bw'].strip())
    links = topo.iterLinks()
    for n1, n2 in links:
        # For C, consider only switch-switch links.
        if topo.isSwitch(n1) and topo.isSwitch(n2):
            linkStr = n1 + '-' + n2
            linkBW = defaultBW
            for lif in linkInfos:
                if (n1 == lif['src'] and n2 == lif['dst']) or (n2 == lif['src'] and n1 == lif['dst']):
                    linkBW = float(lif['bw'].strip())
                    break
            C[reverseL[linkStr]] = linkBW

    # At this point, C and L dictionaries will have data for all the relevant links.
    # Now prepare F and flowInfo
    defaultDelay = float(topoConf['defaultLinkInfo']['delay'].strip('ms')) # remove 'ms' from end
    for flow in flows:
        flowID = 'f' + str(flow['id'])
        flowInfo[flowID] = {}
        src = flow['src']
        dst = flow['dst']
        flowStr = src + '-' + dst
        flowInfo[flowID]['flowStr'] = flowStr
        # Generate pairs of consecutive nodes on the path - we consider complete end-to-end path.
        pathList = paths[src][dst]
        flowLinks = [(x,y) for x,y in zip(pathList, pathList[1:])]
        flowInfo[flowID]['links'] = flowLinks # all links, both host-switch and switch-switch.

        # Add to F, the details for this flow, consider only switch-switch links for that.
        F[flowID] = []
        for (x,y) in flowLinks:
            if topo.isSwitch(x) and topo.isSwitch(y):
                if x+'-'+y in reverseL:
                    F[flowID].append(reverseL[x+'-'+y])
                elif y+'-'+x in reverseL:
                    F[flowID].append(reverseL[y+'-'+x])
                else:
                    # This would happen only when 'paths' do not correspond to the same network, e.g., due to (un)intentional data corruption.
                    F[flowID] = []
                    break
        if not F[flowID]:
            # Again, the extreme case of data corruption, as above.
            F = {}
            break
        # RTT computation.
        rtt = 0.0
        for n1, n2 in flowLinks:
            linkDelay = defaultDelay
            for lif in linkInfos:
                if (n1 == lif['src'] and n2 == lif['dst']) or (n2 == lif['src'] and n1 == lif['dst']):
                    linkDelay = float(lif['delay'].strip('ms'))
                    break
            rtt += linkDelay
        rtt *= 2.0
        flowInfo[flowID]['rtt'] = rtt

    # Write C, F, and L to files in output directory.
    cfile = os.path.join(conf.outPath, "c_dict.json")
    ffile = os.path.join(conf.outPath, "f_dict.json")
    lfile = os.path.join(conf.outPath, "l_dict.json")
    with open(cfile, "w") as write_file:
        json.dump(C, write_file, indent=1)
    with open(ffile, "w") as write_file:
        json.dump(F, write_file, indent=1)
    with open(lfile, "w") as write_file:
        json.dump(L, write_file, indent=1)

    return (C, F, flowInfo)

reason: import networkx


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: J. Massey
@description: Res test for flat plate experiment with analysis Kurt
@contact: jmom1n15@soton.ac.uk
"""

# Imports
import numpy as np
import postproc.visualise.plotter
import postproc.io as io
import postproc.frequency_spectra
import matplotlib.pyplot as plt
import os
import seaborn as sns

plt.style.use(['science', 'grid'])
cases = ['full_bumps', 'smooth']
labels = [r'$ 75\% $', r'Smooth']

def read_rot(data_root, c):
    force_file = '3D/fort.9'
    names = ['t', 'dt', 'px', 'py', 'pz']
    interest = 'p'
    fos = (io.unpack_flex_forces(os.path.join(data_root, str(c), force_file), names))
    forces_dic = dict(zip(names, fos))
    t = forces_dic['t'] / c
    u = np.array((forces_dic[interest + 'x'], forces_dic[interest + 'y']))
    ux, uy = np.squeeze(np.array(u[0])), np.squeeze(np.array(u[1]))

    return t, ux, uy

def plot_coeffs(**kwargs):
    drag = kwargs.get('drag', False)
    lift = kwargs.get('lift', True)

    tit = r'Power spectra comparison'

    colors = sns.color_palette("husl", len(cases))

    # How long from 2D to 3D, and when to crop TS
    init = 40
    snip = 200

    fs, uks_labelled, uks = [], [], []
    # Plot TSs and save spectra
    fig1, ax1 = plt.subplots(figsize=(7, 5))
    ax1.set_title(r'Resolution test')
    ax1.tick_params(bottom="on", top="on", right="on", which='both', direction='in', length=2)
    ax1.set_xlabel(r'$ t/c $')

    if drag: label = r'$ C_{D} $'
    if lift: label = r'$ C_{L} $'

    ax1.set_ylabel(label)
    for idx2, case in enumerate(cases):
        data_root = '/home/masseyjmo/Workspace/Lotus/projects/waving_plate/' + case

        t, ux, uy = read_rot(data_root, c)

        if lift:
            u = uy
        if drag:
            u = ux

        t, u = t[t < snip], u[t < snip]
        t, u = t[t > init], u[t > init]

        # Append the Welch spectra to a list in order to compare
        criteria = postproc.frequency_spectra.FreqConv(t, u, n=5, OL=0.5)
        f, uk = criteria.welch()
        fs.append(f)
        uks.append(uk)
        uks_labelled.append((labels[idx2], uk))
        ax1.plot_fill(t, u, label=labels[idx2], color=colors[idx2])

    ax1.legend()
    if drag:
        fig1.savefig(os.path.join(data_root, '../comparisons/cd_comparison.pdf'),
                     bbox_inches='tight', dpi=30, transparent=False)
        postproc.visualise.plotter.plotLogLogTimeSpectra_list(
            os.path.join(data_root, '../comparisons/log_spectra_cd_comparison.pdf'),
            uks_labelled, fs,
            title=tit,
            ylabel=r'$PS$ ' + label,
            colors=colors)
    if lift:
        fig1.savefig(os.path.join(data_root, '../comparisons/cl_comparison.pdf'),
                     bbox_inches='tight', dpi=30, transparent=False)
        postproc.visualise.plotter.plotLogLogTimeSpectra_list(
            os.path.join(data_root, '../comparisons/log_spectra_cl_comparison.pdf'),
            uks_labelled, fs,
            title=tit,
            ylabel=r'$PS$ ' + label,
            colors=colors)
    plt.close()

def plot_cl_cd(aoas, theta):

    colors = sns.color_palette("husl", len(cases))

    fig, ax = plt.subplots(figsize=(7, 5))
    ax.set_title(r'$C_{L}/C_{D}$')
    ax.tick_params(bottom="on", top="on", right="on", which='both', direction='in', length=2)
    ax.set_xlabel(r'Roughness case')
    ax.set_xticks(range(len(cases)))
    ax.set_xticklabels(labels)

    init = 100
    snip = 200
    for idx, alpha in enumerate(aoas):
        for idx2, case in enumerate(cases):
            data_root = '/home/masseyjmo/Workspace/Lotus/projects/waving_plate/' + str(alpha) + '/' + case
            t, ux, uy = read_rot(data_root, c, theta[idx])
            u = uy / ux
            t, u = t[t < snip], u[t < snip]
            t, u = t[t > init], u[t > init]

            if idx2 == 3:
                ax.axhline(np.mean(u), c=colors[idx], ls='--')

            if idx2 == 0:
                lab = r'AoA $' + str(theta[idx]) + r'$'
            else:
                lab = None
            ax.scatter(idx2, np.mean(u), color=colors[idx], label=lab)
    ax.legend()
    fig.savefig(os.path.join(data_root, '../../figures/cl_cd.pdf'),
                bbox_inches='tight', dpi=30, transparent=True)
    plt.close()

def res_test(cs, case, **kwargs):

    drag = kwargs.get('drag', False)
    lift = kwargs.get('lift', False)
    mean = kwargs.get('mean', False)
    rms = kwargs.get('rms', False)

    colors = sns.color_palette("husl", len(cases))

    fig, ax = plt.subplots(figsize=(5, 3))

    if drag:
        ax.set_title(r'$C_{D}$')
    if lift:
        ax.set_title(r'$C_{L}$')

    ax.tick_params(bottom="on", top="on", right="on", which='both', direction='in', length=2)
    ax.set_xlabel(r'h')
    if mean:
        ax.set_ylabel(r'Mean')
    else:
        ax.set_ylabel(r'RMS')

    init = 30
    snip = 200

    hs = np.max(np.array(cs[-1]))/np.array(cs)
    for idx, res in enumerate(cs):
        data_root = '/home/masseyjmo/Workspace/Lotus/projects/waving_plate/' + case
        t, ux, uy = read_rot(data_root, res)

        if drag:
            u = ux
        if lift:
            u = uy

        t, u = t[t < snip], u[t < snip]
        t, u = t[t > init], u[t > init]
        if res == np.max(np.array(cs[-1])):
            if mean:
                ax.fill_between(hs, np.mean(u)*1.03, np.mean(u)*.97, color='green', zorder=0, label=r'$\pm 3\%$')
            else:
                ax.fill_between(hs,
                                np.sqrt(np.sum((u-np.mean(u))**2)/len(u)) * 1.03,
                                np.sqrt(np.sum((u-np.mean(u))**2)/len(u)) * .97,
                                color='green', zorder=0, label=r'$\pm 3\%$')

        if mean:
            if idx > 3:
                co = 'white'
            else:
                co = 'k'
            ax.scatter(hs[idx], np.mean(u), color=co, marker='X', s=104)
            print(np.mean(u))
        if rms:
            if idx > 3:
                co = 'white'
            else:
                co = 'k'
            ax.scatter(hs[idx], np.sqrt(np.sum((u-np.mean(u))**2)/len(u)), color=co, marker='P', s=104)
            print(np.sqrt(np.sum((u - np.mean(u)) ** 2) / len(u)))
    ax.legend()
    if mean:
        ext = 'mean'
    if rms:
        ext = 'rms'
        ax.set_title(r'RMS $C_{D_p}$')

    if drag:
        fig.savefig(os.path.join(data_root, 'figures/cd_'+ext+'.png'),
                    bbox_inches='tight', dpi=300, transparent=True)
    if lift:
        fig.savefig(os.path.join(data_root, 'figures/cl_'+ext+'.png'),
                    bbox_inches='tight', dpi=300, transparent=True)

    plt.close()

if __name__ == "__main__":
    c = [32, 64, 96, 128, 192, 256]
    # plot_coeffs(drag=True)
    res_test(c, 'smooth', drag=True, mean=True)
    res_test(c, 'smooth', drag=True, rms=True)
    # plot_cl_cd(AoAs, thetas)
    # plot_coeffs_rms(AoAs, thetas, lift=True, rms=True)
    # plot_coeffs_rms(AoAs, thetas, lift=True, mean=True)

max_stars_repo_name: J-Massey/postproc

max_issues_repo_name: J-Massey/postproc

max_forks_repo_name: J-Massey/postproc


text:
import networkx as nx
from matplotlib import pyplot as plt
import queue

# from networkx.classes.function import neighbors
# from networkx.classes.graph import Graph

class PuzzleProblem():
    def __init__(self, size, agent_loc, target_loc, wall_loc) -> None:
        self.size = size
        self.agent_loc = agent_loc
        self.target_loc = target_loc
        self.wall_locations = wall_loc
        self.path = []
        self.dls_path = []

        self.search_G = nx.grid_2d_graph(self.size, self.size)
        self.search_pos = {(x,y):(y,-x) for x,y in self.search_G.nodes()}
        # self.search_color_map = ['lightgreen' for _ in range(self.size * self.size)]

        self.G = nx.grid_2d_graph(self.size, self.size)
        self.pos = {(x,y):(y,-x) for x,y in self.G.nodes()}
        self.color_map = ['lightgreen' for _ in range(self.size * self.size)]

        self.color_map[self.agent_loc[0]*8 + self.agent_loc[1]] = 'orange'
        self.color_map[self.target_loc[0]*8 + self.target_loc[1]] = 'lightblue'

        for wall in self.wall_locations:
            self.color_map[wall[0]*8+wall[1]] = 'red'

            self.search_G.remove_node(wall)
            del self.search_pos[wall]

        self.search_G.nodes()[self.agent_loc]["start"] = True
        self.search_G.nodes()[self.target_loc]["end"] = True

    def drawPuzzle(self):
        if not self.path and not self.dls_path:
            nx.draw(self.G, pos=self.pos,
            node_color=self.color_map,
            with_labels=True,
            node_size=600)
        else:
            if self.path:
                self.color_map_bfs = self.color_map.copy()
                for node in self.path:
                  self.color_map_bfs[node[0]*8 + node[1]] = 'black'
                nx.draw(self.G, pos=self.pos,
                node_color=self.color_map_bfs,
                with_labels=True,
                node_size=600)
            if self.dls_path:
                self.color_map_dls = self.color_map.copy()
                for node in self.dls_path[1:-1]:
                    self.color_map_dls[node[0]*8 + node[1]] = 'blue'
                nx.draw(self.G, pos=self.pos,
                node_color=self.color_map_dls,
                with_labels=True,
                node_size=600)

    def drawSearchPuzzle(self):
        nx.draw(self.search_G,
        pos=self.search_pos,
        with_labels=True,
        node_size=600)

    def __returnPath(self):
        path = []
        current = self.target_loc
        while current != self.agent_loc:
            father = self.search_G.nodes()[current]["from"]
            current = father
            path.append(current)
        self.path = [i for i in reversed(path)][1:]
        return self.path

    def bfs(self):
        self.dls_path = []
        current = self.agent_loc
        visited = []
        visited.append(current)
        q = queue.Queue()
        if current == self.target_loc:
            return 2
        while current != self.target_loc:
            neighbors = [e[1] for e in self.search_G.edges(current)]
            for n in neighbors:
                if n == self.target_loc:
                    self.search_G.nodes()[n]["from"] = current
                    return self.__returnPath()

                if n not in visited:
                    self.search_G.nodes()[n]["from"] = current
                    visited.append(n)
                    q.put(n)
            if q.empty():
                return 0
            current = q.get()

    def DLS(self, node, depth, visited: list):
        if node == self.target_loc:
            self.dls_path.append(node)
            return 1
        if depth == 0:
            return -1
        if node not in visited:
            visited.append(node)
            neighbors = [e[1] for e in self.search_G.edges(node)]
            for n in neighbors:
                if n not in visited:
                    if self.DLS(node=n, depth=depth-1, visited=visited.copy()) == 1:
                        self.dls_path.append(node)
                        return 1
        return 0

    def IDS(self):
        i = 0
        self.path = []
        while self.DLS(node=self.agent_loc, depth=i, visited=[]) != 1:
            i = i+1
        return [i for i in reversed(self.dls_path[1:-1])]

reason: import networkx,from networkx


text:
import cv2
import numpy as np

def prepare_image(image, target_size=(300, 300), target_layout="NCWH"):

    # Resize image, [H, W, C] -> [300, 300, C]
    image_copy = cv2.resize(image, target_size)

    # Swap axes, [H, W, C] -> [C, H, W]
    if target_layout == "NCHW":
        image_copy = np.swapaxes(image_copy, 0, 2)
        image_copy = np.swapaxes(image_copy, 1, 2)

    # Expand dimensions, [1, C, H, W]
    image_copy = np.expand_dims(image_copy, 0)

    return image_copy

def draw_bounding_boxes(image, detections, classes, threshold=0.5, box_color=(255, 0, 0)):

    image_copy = np.copy(image)

    # Get image dimensions
    image_height = image_copy.shape[0]
    image_width = image_copy.shape[1]

    # Iterate through detections
    no_detections = detections.shape[2]
    for i in range(no_detections):

        detection = detections[0, 0, i]

        # Skip detections with confidence below threshold
        confidence = detection[2]
        if confidence < threshold:
            continue

        # Draw bounding box
        x_min = int(detection[3]*image_width)
        y_min = int(detection[4]*image_height)

        x_max = int(detection[5]*image_width)
        y_max = int(detection[6]*image_height)

        top_left = (x_min, y_min)
        bottom_right = (x_max, y_max)

        cv2.rectangle(image_copy, top_left, bottom_right, box_color, 2)

        # Get class text
        class_ = classes[str(int(detection[1]))]

        # Draw text background
        text_size = cv2.getTextSize(class_, cv2.FONT_HERSHEY_PLAIN, 2, 1)

        top_left = (x_min, y_max-text_size[0][1])
        bottom_right = (x_min+text_size[0][0], y_max)

        cv2.rectangle(image_copy, top_left, bottom_right, box_color, cv2.FILLED)

        # Draw text
        cv2.putText(image_copy, class_, (x_min,y_max), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 1)

    return image_copy

max_stars_repo_name: RuneSkovrupHansen/openvino-examples

max_issues_repo_name: RuneSkovrupHansen/openvino-examples

max_forks_repo_name: RuneSkovrupHansen/openvino-examples


text:
import os
import cv2
import json
import scipy.io as sio
import numpy as np
import torch
from torchvision.transforms import transforms
import matplotlib.pyplot as plt

from lib.utils.logging import setup_logging, SmoothedValue
from lib.models.multi_depth_model_auxiv2 import RelDepthModel
from lib.utils.net_tools import load_ckpt
from lib.utils.evaluate_depth_error import evaluate_rel_err, recover_metric_depth
from lib.configs.config import cfg, merge_cfg_from_file
from tools.parse_arg_test import TestOptions

logger = setup_logging(__name__)

def scale_torch(img, scale):
    """
    Scale the image and output it in torch.tensor.
    :param img: input image. [C, H, W]
    :param scale: the scale factor. float
    :return: img. [C, H, W]
    """
    img = np.transpose(img, (2, 0, 1))
    img = img.astype(np.float32)
    img /= scale
    img = torch.from_numpy(img.copy())
    img = transforms.Normalize(cfg.DATASET.RGB_PIXEL_MEANS, cfg.DATASET.RGB_PIXEL_VARS)(img)
    return img

if __name__ == '__main__':
    test_args = TestOptions().parse()
    test_args.thread = 1
    test_args.batchsize = 1

    # load model
    model = RelDepthModel()
    model.eval()

    # load checkpoint
    if test_args.load_ckpt:
        load_ckpt(test_args, model)
    model.cuda()
    model = torch.nn.DataParallel(model)

    # base_dir = '/home/yvan/DeepLearning/all-datasets'
    # annos_path = os.path.join(base_dir, test_args.dataset_list[0], 'annotations/test_annotations.json')
    # f = open(annos_path)
    # annos = json.load(f)
    # f.close()

    imgs = sio.loadmat('../datasets/test.mat')
    rgbs = imgs['rgbs']
    depths = imgs['depths']

    test_datasize = rgbs.shape[0]
    logger.info('{:>15}: {:<30}'.format('test_data_size', test_datasize))
    # test
    smoothed_absRel = SmoothedValue(test_datasize)
    smoothed_rms = SmoothedValue(test_datasize)
    smoothed_logRms = SmoothedValue(test_datasize)
    smoothed_squaRel = SmoothedValue(test_datasize)
    smoothed_silog = SmoothedValue(test_datasize)
    smoothed_silog2 = SmoothedValue(test_datasize)
    smoothed_log10 = SmoothedValue(test_datasize)
    smoothed_delta1 = SmoothedValue(test_datasize)
    smoothed_delta2 = SmoothedValue(test_datasize)
    smoothed_delta3 = SmoothedValue(test_datasize)
    smoothed_whdr = SmoothedValue(test_datasize)

    smoothed_criteria = {'err_absRel': smoothed_absRel, 'err_squaRel': smoothed_squaRel, 'err_rms': smoothed_rms,
                         'err_silog': smoothed_silog, 'err_logRms': smoothed_logRms, 'err_silog2': smoothed_silog2,
                         'err_delta1': smoothed_delta1, 'err_delta2': smoothed_delta2, 'err_delta3': smoothed_delta3,
                         'err_log10': smoothed_log10, 'err_whdr': smoothed_whdr}

    for i in range(test_datasize):
        if i % 100 == 0:
            logger.info('processing : ' + str(i) + ' / ' + str(test_datasize))
        rgb = rgbs[i].transpose((2, 1, 0))  #rgb
        depth = depths[i].transpose((1, 0))
        mask_invalid = depth < 1e-8
        mask_invalid[45:471, 41:601] = 1
        mask_invalid = mask_invalid.astype(np.bool)

        # resize input to [385, 385], same to training setting
        rgb_resize = cv2.resize(rgb, (448, 448))

        img_torch = scale_torch(rgb_resize, 255)
        img_torch = img_torch[None, :, :, :].cuda()
        with torch.no_grad():
            pred_depth, pred_disp = model.module.depth_model(img_torch)
        pred_depth_resize = cv2.resize(pred_depth.cpu().numpy().squeeze(), (rgb.shape[1], rgb.shape[0]))

        # Recover metric depth
        pred_depth_metric = recover_metric_depth(pred_depth_resize, depth)
        # evaluate
        smoothed_criteria = evaluate_rel_err(pred_depth_metric, depth, smoothed_criteria)

        model_name = test_args.load_ckpt.split('/')[-1].split('.')[0]
        image_dir = os.path.join(cfg.ROOT_DIR, './evaluation', cfg.MODEL.ENCODER, model_name + '_nyu')
        os.makedirs(image_dir, exist_ok=True)
        img_name = '%04d.png' %i

        plt.imsave(os.path.join(image_dir, img_name.replace('.png', '_pred.png')), pred_depth_metric, cmap='rainbow')
        cv2.imwrite(os.path.join(image_dir, img_name.replace('.png', '_rgb.png')), np.squeeze(rgb)[:, :, ::-1])
        plt.imsave(os.path.join(image_dir, img_name.replace('.png', '_gt.png')), np.squeeze(depth), cmap='rainbow')
        # cv2.imwrite(os.path.join(image_dir, img_name.replace('.png', '_gtraw.png')), (pred_depth_metric * 6000).astype(np.uint16))

    print("###############WHDR ERROR: %f", smoothed_criteria['err_whdr'].GetGlobalAverageValue())
    print("###############absREL ERROR: %f", smoothed_criteria['err_absRel'].GetGlobalAverageValue())
    print("###############silog ERROR: %f", np.sqrt(smoothed_criteria['err_silog2'].GetGlobalAverageValue() - (
        smoothed_criteria['err_silog'].GetGlobalAverageValue()) ** 2))
    print("###############log10 ERROR: %f", smoothed_criteria['err_log10'].GetGlobalAverageValue())
    print("###############RMS ERROR: %f", np.sqrt(smoothed_criteria['err_rms'].GetGlobalAverageValue()))
    print("###############delta_1 ERROR: %f", smoothed_criteria['err_delta1'].GetGlobalAverageValue())
    print("###############delta_2 ERROR: %f", smoothed_criteria['err_delta2'].GetGlobalAverageValue())
    print("###############delta_3 ERROR: %f", smoothed_criteria['err_delta3'].GetGlobalAverageValue())
    print("###############squaRel ERROR: %f", smoothed_criteria['err_squaRel'].GetGlobalAverageValue())
    print("###############logRms ERROR: %f", np.sqrt(smoothed_criteria['err_logRms'].GetGlobalAverageValue()))

max_stars_repo_name: chensjtu/AdelaiDepth

max_issues_repo_name: chensjtu/AdelaiDepth

max_forks_repo_name: chensjtu/AdelaiDepth

reason: import numpy,import scipy


text:
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/master/LICENSE

from __future__ import absolute_import

import sys
import json
import distutils.version

import numpy
import pytest
import skhep_testdata

import uproot4

def test_awkward_strings():
    awkward1 = pytest.importorskip("awkward1")
    with uproot4.open(skhep_testdata.data_path("uproot-stl_containers.root"))[
        "tree"
    ] as tree:
        assert awkward1.to_list(tree["string"].array(library="ak")) == [
            "one",
            "two",
            "three",
            "four",
            "five",
        ]

def test_pandas_strings():
    pandas = pytest.importorskip("pandas")
    with uproot4.open(skhep_testdata.data_path("uproot-stl_containers.root"))[
        "tree"
    ] as tree:
        assert tree["string"].array(library="pd").values.tolist() == [
            "one",
            "two",
            "three",
            "four",
            "five",
        ]

def test_leaflist_numpy():
    with uproot4.open(skhep_testdata.data_path("uproot-leaflist.root"))[
        "tree/leaflist"
    ] as branch:
        result = branch.array(library="np")
        assert result.dtype.names == ("x", "y", "z")
        assert result.tolist() == [
            (1.1, 1, 97),
            (2.2, 2, 98),
            (3.3, 3, 99),
            (4.0, 4, 100),
            (5.5, 5, 101),
        ]
        assert branch.typename == "struct {double x; int32_t y; int8_t z;}"

def test_leaflist_awkward():
    awkward1 = pytest.importorskip("awkward1")
    with uproot4.open(skhep_testdata.data_path("uproot-leaflist.root"))[
        "tree/leaflist"
    ] as branch:
        result = branch.array(library="ak")
        assert str(awkward1.type(result)) == '5 * {"x": float64, "y": int32, "z": int8}'
        assert awkward1.to_list(result) == [
            {"x": 1.1, "y": 1, "z": 97},
            {"x": 2.2, "y": 2, "z": 98},
            {"x": 3.3, "y": 3, "z": 99},
            {"x": 4.0, "y": 4, "z": 100},
            {"x": 5.5, "y": 5, "z": 101},
        ]

def test_leaflist_pandas():
    pandas = pytest.importorskip("pandas")
    with uproot4.open(skhep_testdata.data_path("uproot-leaflist.root"))["tree"] as tree:
        result = tree["leaflist"].array(library="pd")

        if distutils.version.LooseVersion(
            pandas.__version__
        ) < distutils.version.LooseVersion("0.21"):
            assert list(result.columns) == ["x", "y", "z"]
            assert result["x"].values.tolist() == [1.1, 2.2, 3.3, 4.0, 5.5]
            assert result["y"].values.tolist() == [1, 2, 3, 4, 5]
            assert result["z"].values.tolist() == [97, 98, 99, 100, 101]

            result = tree.arrays("leaflist", library="pd")
            assert list(result.columns) == [
                ("leaflist", "x"),
                ("leaflist", "y"),
                ("leaflist", "z"),
            ]
            assert result["leaflist", "x"].values.tolist() == [1.1, 2.2, 3.3, 4.0, 5.5]
            assert result["leaflist", "y"].values.tolist() == [1, 2, 3, 4, 5]
            assert result["leaflist", "z"].values.tolist() == [97, 98, 99, 100, 101]

        else:
            assert list(result.columns) == [("x",), ("y",), ("z",)]
            assert result["x",].values.tolist() == [1.1, 2.2, 3.3, 4.0, 5.5]
            assert result["y",].values.tolist() == [1, 2, 3, 4, 5]
            assert result["z",].values.tolist() == [97, 98, 99, 100, 101]

            result = tree.arrays("leaflist", library="pd")
            assert list(result.columns) == [
                ("leaflist", "x"),
                ("leaflist", "y"),
                ("leaflist", "z"),
            ]
            assert result["leaflist", "x"].values.tolist() == [1.1, 2.2, 3.3, 4.0, 5.5]
            assert result["leaflist", "y"].values.tolist() == [1, 2, 3, 4, 5]
            assert result["leaflist", "z"].values.tolist() == [97, 98, 99, 100, 101]

def test_fixed_width():
    with uproot4.open(
        skhep_testdata.data_path("uproot-sample-6.20.04-uncompressed.root")
    )["sample"] as tree:
        assert tree["ai4"].array(library="np").tolist() == [
            [i, i + 1, i + 2] for i in range(-14, 16)
        ]
        assert tree["ai4"].typename == "int32_t[3]"

def test_fixed_width_awkward():
    awkward1 = pytest.importorskip("awkward1")
    with uproot4.open(
        skhep_testdata.data_path("uproot-sample-6.20.04-uncompressed.root")
    )["sample"] as tree:
        assert awkward1.to_list(tree["ai4"].array(library="ak")) == [
            [i, i + 1, i + 2] for i in range(-14, 16)
        ]

def test_fixed_width_pandas():
    pandas = pytest.importorskip("pandas")
    with uproot4.open(
        skhep_testdata.data_path("uproot-sample-6.20.04-uncompressed.root")
    )["sample"] as tree:
        result = tree["ai4"].array(library="pd")
        assert list(result.columns) == ["[0]", "[1]", "[2]"]
        assert result["[0]"].values.tolist() == list(range(-14, 16))
        assert result["[1]"].values.tolist() == list(range(-13, 17))
        assert result["[2]"].values.tolist() == list(range(-12, 18))

        result = tree.arrays("ai4", library="pd")
        assert list(result.columns) == ["ai4[0]", "ai4[1]", "ai4[2]"]
        assert result["ai4[0]"].values.tolist() == list(range(-14, 16))
        assert result["ai4[1]"].values.tolist() == list(range(-13, 17))
        assert result["ai4[2]"].values.tolist() == list(range(-12, 18))

def test_fixed_width_pandas_2():
    pandas = pytest.importorskip("pandas")
    with uproot4.open(
        skhep_testdata.data_path("uproot-small-evnt-tree-fullsplit.root")
    )["tree"] as tree:
        result = tree["ArrayI32[10]"].array(library="pd")
        assert list(result.columns) == ["[" + str(i) + "]" for i in range(10)]
        for i in range(10):
            assert result["[" + str(i) + "]"].values.tolist() == list(range(100))

        result = tree.arrays(
            "xyz", aliases={"xyz": "get('ArrayI32[10]')"}, library="pd"
        )
        assert list(result.columns) == ["xyz[" + str(i) + "]" for i in range(10)]
        for i in range(10):
            assert result["xyz[" + str(i) + "]"].values.tolist() == list(range(100))

def hook(self, **kwargs):
    print("ENTER")
    for k, v in kwargs.items():
        print(k, v)


text:
#!/bin/python
import sys
sys.path.append("..")

from common import read_csv, normalize_data, prepare_regression, get_features, get_labels, calculate_precision_recall
import numpy as np
import matplotlib.pyplot as plt
import argparse

def choices_unique(seq, k=1):
    from random import shuffle
    shuffle(seq)
    a = np.array(seq)
    assert a.shape == (n,)
    return a[:k]

def train(features, labels, step, k, iterations=100):
    n, nfeatures = features.shape
    assert k <= n
    sign_labels, w = prepare_regression(features, labels)
    assert w.shape == (nfeatures,)
    it = 0

    best_ein = 1e+50
    best_w = np.zeros((nfeatures,))
    kmul = -1. / k
    while it < iterations:
        cur_labels = np.sign(features.dot(w))
        diff = cur_labels != sign_labels
        ein = np.sum(diff, dtype=float) / n
        if ein < best_ein:
            best_ein = ein
            best_w = w
        # print "it = ", it, "ein = ", ein
        # print ein
        assert cur_labels.shape == (n,)
        choices = choices_unique(range(n), k)
        ii = np.sort(choices)
        xi, wx, yi = features[ii, :], cur_labels[ii], sign_labels[ii]
        assert yi.shape == (k,)
        yitile = np.tile(yi, (nfeatures, 1)).T
        assert yitile.shape == xi.shape
        nominator = yx = xi * yitile
        assert yx.shape == (k, nfeatures)
        ywx = yi * wx
        assert ywx.shape == (k, )
        denominator = 1 + np.exp(ywx)
        assert denominator.shape == (k, )

        denominator = np.tile(denominator, (nfeatures, 1)).T
        assert denominator.shape == (k, nfeatures)
        assert nominator.shape == (k, nfeatures)
        wdelta = kmul * np.sum(nominator / denominator, axis=0)
        assert wdelta.shape == (nfeatures,)
        w = w - step * wdelta
        it += 1
    print "best_ein = ", best_ein
    return best_w

def plot_roc(features, labels, w):
    # labels[np.where(labels == 0)] = -1
    assert list(set(labels)) == [0, 1]
    res = features.dot(w)
    threshold =  np.arange(-1, 1, 0.01)
    n = len(labels)
    def calc_metrics(t):
        rest = res > t
        acc = np.sum(rest != labels, dtype=float) / n
        precision, recall, tpr, fpr = calculate_precision_recall(rest, labels)
        return tpr, fpr, acc

    metrics = np.array([calc_metrics(t) for t in threshold])
    plt.figure("threshold")
    plt.plot(threshold, metrics[:,2], "o", label="threshold")
    plt.legend()

    plt.figure("ROC")
    plt.plot(metrics[:,1], metrics[:,0], label="ROC")
    plt.legend()
    plt.show()
    print "best accuracy is ", np.min(metrics[:, 2]), "at threshold = ", threshold[np.argmin(metrics[:,2])]

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Stochastic linear regression.")
    parser.add_argument("--train", dest="train", required=True, help="Train filename")
    parser.add_argument("--test", dest="test", required=True, help="Test filename")

    args = parser.parse_args()
    train_data = read_csv(args.train)
    # normalize_data(train_data)

    train_features, train_labels = get_features(train_data), get_labels(train_data)
    n = train_labels.shape[0]
    w = train(train_features, train_labels, k=10, step=0.1, iterations=1000)
    # plot_roc(train_features, train_labels, w)

    test_data = read_csv(args.test)
    normalize_data(test_data)
    test_features, test_labels = get_features(test_data), get_labels(test_data)
    plot_roc(test_features, test_labels, w)

max_stars_repo_name: kravtsun/au-ml

max_issues_repo_name: kravtsun/au-ml

max_forks_repo_name: kravtsun/au-ml


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

import numpy as np

def test_func(x, y, txt, logger):
    logger.info('info of test func ------------------------------------------')
    logger.info('get input x: {}, y: {}.'.format(x, y))
    logger.warning(txt)
    logger.error('some error may find here.')
    try:
        logger.debug('x div y : {}'.format(str(x/y)))
    except:
        logger.error('x cannot div y, please check y.', exc_info=True)
        # raise
    logger.fatal('program may crash here.')
    logger.info('test function finished.')

def rand_div(x):
    y = np.random.randint(-1, 1)
    try:
        return x / y
    except:
        raise

max_stars_repo_name: Genlovy-Hoo/dramkit

max_issues_repo_name: Genlovy-Hoo/dramkit

max_forks_repo_name: Genlovy-Hoo/dramkit


text:
import cv2
import os
from tensorflow.python.keras.models import load_model
import numpy as np
from time import sleep
import threading
import RPi.GPIO as GPIO
#Disable warnings (optional)
GPIO.setwarnings(False)
#Select GPIO mode
GPIO.setmode(GPIO.BCM)
#Set buzzer - pin 2 as output
#Set IR sensor - pin 5 as input
buzzer=2
light=5
lightInp=0
GPIO.setup(buzzer,GPIO.OUT)
GPIO.output(buzzer,GPIO.HIGH)
GPIO.setup(light,GPIO.IN)
beeperActive=False
#Function to activate beeping
def beeping(name):
    while beeperActive:
        GPIO.output(buzzer,GPIO.LOW)
        sleep(0.5)
        GPIO.output(buzzer,GPIO.HIGH)
        sleep(0.5)
face = cv2.CascadeClassifier('haar_cascade_files/haarcascade_frontalface_alt.xml')
leye = cv2.CascadeClassifier('haar_cascade_files/haarcascade_lefteye_2splits.xml')
reye = cv2.CascadeClassifier('haar_cascade_files/haarcascade_righteye_2splits.xml')

threshold = 4

model = load_model('model/eye_model.h5')
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
score=0
rpred=[99]
lpred=[99]
msg = ['Driver Alert','Driver Drowsy','Seatbelt Fastened','Please Fasten Seatbelt']

while(True):
    ret, frame = cap.read()
    height,width = frame.shape[:2]
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame_ROI=frame
    gray_ROI=gray
    faces = face.detectMultiScale(gray,minNeighbors=5,scaleFactor=1.1,minSize=(25,25))
    for (x,y,w,h) in faces:
        cv2.rectangle(frame, (x,y) , (x+w,y+h) , (0,0,255) if score>threshold or lightInp else (100,100,100) , 1 )
        frame_ROI=frame[y:y+h,x:x+w]
        gray_ROI=gray[y:y+h,x:x+w]
        break
    left_eye = leye.detectMultiScale(gray_ROI)
    right_eye =  reye.detectMultiScale(gray_ROI)

    cv2.rectangle(frame, (0,height-50) , (width,height) , (0,0,0) , thickness=cv2.FILLED )

    lightInp = GPIO.input(light)

    for (x,y,w,h) in right_eye:
        driver_right_eye=frame_ROI[y:y+h,x:x+w]
        driver_right_eye = cv2.cvtColor(driver_right_eye,cv2.COLOR_BGR2GRAY)
        driver_right_eye = cv2.resize(driver_right_eye,(24,24))
        driver_right_eye= driver_right_eye/255
        driver_right_eye=  driver_right_eye.reshape(24,24,-1)
        driver_right_eye = np.expand_dims(driver_right_eye,axis=0)
        predict_r=model.predict(driver_right_eye)
        rpred=np.argmax(predict_r,axis=1)
        break

    for (x,y,w,h) in left_eye:
        driver_left_eye=frame_ROI[y:y+h,x:x+w]
        driver_left_eye = cv2.cvtColor(driver_left_eye,cv2.COLOR_BGR2GRAY)
        driver_left_eye = cv2.resize(driver_left_eye,(24,24))
        driver_left_eye= driver_left_eye/255
        driver_left_eye=driver_left_eye.reshape(24,24,-1)
        driver_left_eye = np.expand_dims(driver_left_eye,axis=0)
        predict_l=model.predict(driver_left_eye)
        lpred=np.argmax(predict_l,axis=1)
        break

    if(rpred[0]==0 and lpred[0]==0):
        score=score+1
        cv2.putText(frame,"Closed",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
    else:
        score=score-2
        cv2.putText(frame,"Open",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)

    if(score<0):
        score=0
    cv2.putText(frame,msg[1] if score>=threshold else msg[0],(100,height-20), font, 1,(0,0,255) if score>threshold else (0,255,0),1,cv2.LINE_AA)
    cv2.putText(frame,msg[3] if lightInp else msg[2],(300,height-20), font, 1,(0,0,255) if lightInp else (0,255,0),1,cv2.LINE_AA)
    if(score>=threshold or lightInp) :
        if(not beeperActive):
            buzzThread = threading.Thread(target=beeping, args=(1,), daemon=True)
            beeperActive=True
            buzzThread.start()
        cv2.rectangle(frame,(0,0),(width,height),(0,0,255),4)
    else:
        beeperActive=False
    cv2.imshow('Demo Output',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

max_stars_repo_path: drowsiness detection_v2.py

max_issues_repo_path: drowsiness detection_v2.py

max_forks_repo_path: drowsiness detection_v2.py


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 Ryan L. Collins <rlcollins@g.harvard.edu>
# and the Talkowski Laboratory
# Distributed under terms of the MIT license.

"""
Preprocess GTEx gene X sample matrix and distill various summary matrices
"""

import pandas as pd
import numpy as np
from os import path
import gzip
from scipy.stats import median_absolute_deviation as mad
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import subprocess
import argparse

def get_genelist(infile):
    """
    Extract list of genes and transcripts to consider
    """

    df = pd.read_csv(infile, delimiter='\t', header=None)

    ensg_ids = [x.split('.')[0] for x in df.iloc[:, 0].tolist()]
    # enst_ids = df.iloc[:, 1].tolist()
    genes = df.iloc[:, 4].tolist()

    return genes, ensg_ids

def load_gtex_samples(manifest_in):
    """
    Load GTEx sample manifest and return dict of samples per tissue
    """

    tissues = {}

    df = pd.read_csv(manifest_in, delimiter='\t')

    for tissue in sorted(list(set(df.SMTSD.tolist()))):
        samples = sorted(list(set(df.SAMPID[df.SMTSD == tissue].tolist())))
        tissues[tissue] = samples

    return tissues

def scale_expression(values):
    """
    Apply log10(x+1) scaling to a list of expression values
    """

    values = [float(x) for x in values]
    nonzero = [x for x in values if x > 0]
    if len(nonzero) > 0:
        # noise = np.min(nonzero) / 10
        return np.log10([x + 1 for x in values])
    else:
        return np.array([0 for x in values])

def get_gene_stats(xvals, col_idxs, tissues):
    """
    Compute summary stats across all samples for a given gene & tissue
    """

    xmin, xq1, xmed, xmean, xq3, xmax, xsd, xmad = [], [], [], [], [], [], [], []

    for tissue in tissues.keys():
        tidx = [col_idxs[s] for s in tissues[tissue] if s in col_idxs.keys()]
        if len(tidx) > 0:
            tvals = xvals[tidx]
            xmin.append(np.nanmin(tvals))
            xq1.append(np.nanquantile(tvals, q=0.25))
            xmed.append(np.nanmedian(tvals))
            xmean.append(np.nanmean(tvals))
            xq3.append(np.nanquantile(tvals, q=0.75))
            xmax.append(np.nanmax(tvals))
            xsd.append(np.nanstd(tvals))
            xmad.append(mad(tvals))
        else:
            xmin.append(np.nan)
            xq1.append(np.nan)
            xmed.append(np.nan)
            xmean.append(np.nan)
            xq3.append(np.nan)
            xmax.append(np.nan)
            xsd.append(np.nan)
            xmad.append(np.nan)

    return xmin, xq1, xmed, xmean, xq3, xmax, xsd, xmad

def load_gtex_matrix(gtex_in, genes, ensg_ids, tissues, quiet=False):
    """
    Reads GTEx expression matrix and filters to passing genes
    """

    # Note: for memory purposes, each line is processed one at a time

    df_cols = ['gene'] + list(tissues.keys())
    gtex_min = pd.DataFrame(columns=df_cols)
    gtex_q1 = pd.DataFrame(columns=df_cols)
    gtex_median = pd.DataFrame(columns=df_cols)
    gtex_mean = pd.DataFrame(columns=df_cols)
    gtex_q3 = pd.DataFrame(columns=df_cols)
    gtex_max = pd.DataFrame(columns=df_cols)
    gtex_sd = pd.DataFrame(columns=df_cols)
    gtex_mad = pd.DataFrame(columns=df_cols)

    if path.splitext(gtex_in)[-1] in '.gz .bz .bgz .bgzip .gzip'.split():
        infile = gzip.open(gtex_in, 'rt')
    else:
        infile = open(gtex_in)

    col_idxs = {}

    r = 0
    for k, line in enumerate(infile):
        # Skip descriptive header lines
        if len(line.split('\t')) < 3:
            continue

        vals = line.rstrip().split('\t')

        # Store indexes corresponding to sample IDs
        if vals[0] == 'Name':
            for i, sid in enumerate(vals):
                if i > 1:
                    col_idxs[sid] = i - 2

        # Process each gene if canonical
        else:
            if not any([vals[0].split('.')[0] in ensg_ids, vals[1] in genes]):
                continue

            # DEBUG: print line number
            if not quiet:
                r += 1
                print('\t'.join([str(r), str(k), vals[1]]))

            # Compute expression stats
            xvals = scale_expression(vals[2:])
            xmin, xq1, xmedian, xmean, xq3, xmax, xsd, xmad = \
                get_gene_stats(xvals, col_idxs, tissues)

            # Add gene stats to each summary df
            gtex_min = gtex_min.append(pd.Series([vals[1]] + xmin, index=gtex_min.columns),
                                       ignore_index=True)
            gtex_q1 = gtex_q1.append(pd.Series([vals[1]] + xq1, index=gtex_q1.columns),
                                     ignore_index=True)
            gtex_median = gtex_median.append(pd.Series([vals[1]] + xmedian,
                                                       index=gtex_median.columns),
                                             ignore_index=True)
            gtex_mean = gtex_mean.append(pd.Series([vals[1]] + xmean,
                                                   index=gtex_mean.columns),
                                         ignore_index=True)
            gtex_q3 = gtex_q3.append(pd.Series([vals[1]] + xq3,
                                                index=gtex_q3.columns),
                                     ignore_index=True)
            gtex_max = gtex_max.append(pd.Series([vals[1]] + xmax,
                                                 index=gtex_max.columns),
                                       ignore_index=True)
            gtex_sd = gtex_sd.append(pd.Series([vals[1]] + xsd,
                                                 index=gtex_sd.columns),
                                       ignore_index=True)
            gtex_mad = gtex_mad.append(pd.Series([vals[1]] + xmad,
                                                 index=gtex_mad.columns),
                                       ignore_index=True)

    return gtex_min, gtex_q1, gtex_median, gtex_mean, gtex_q3, gtex_max, gtex_sd, gtex_mad

def pca_expression(matrix, n_pcs=20):
    """
    Reduce an expression matrix to its n_pcs top principal components
    """

    # Clean input matrix
    X = StandardScaler().fit_transform(matrix.drop(columns='gene').fillna(value=0))

    # PCA
    pca = PCA(n_components=n_pcs).fit_transform(X)
    pc_names = ['expression_component_' + str(i) for i in range(1, n_pcs+1)]
    pcadf = pd.DataFrame(pca, columns=pc_names)

    # Convert back to dataframe with genes as first column
    return pd.concat([matrix.loc[:, 'gene'], pcadf], axis=1)

def write_matrix(matrix, filename, gzip):
    """
    Write matrix to file and gzip if optioned
    """

    matrix.rename(columns={'gene' : '#gene'}, inplace=True)

    matrix.to_csv(filename, sep='\t', index=False, na_rep='NA')

    if gzip:
        subprocess.run(['gzip', '-f', filename])

def main():
    # Parse command line arguments and options
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('canonical', help='tsv of canonical transcripts')
    parser.add_argument('gtex_matrix', help='GTEx sample X gene expression matrix')
    parser.add_argument('gtex_sample_manifest', help='GTEx sample manifest tsv')
    parser.add_argument('-p', '--prefix', default='GTEx_summary', help='Prefix ' +
                        'for output matrices')
    parser.add_argument('--n-pcs', default=20, help='Number of principal components ' +
                        'to retain', type=int)
    parser.add_argument('-z', '--gzip', action='store_true', help='Gzip outputs')
    args = parser.parse_args()

    # Load list of genes & transcripts to allow
    genes, ensg_ids = get_genelist(args.canonical)

    # Load GTEx sample manifest
    tissues = load_gtex_samples(args.gtex_sample_manifest)

    # Load sample X gene expression matrix
    gtex_min, gtex_q1, gtex_median, gtex_mean, gtex_q3, gtex_max, gtex_sd, gtex_mad = \
        load_gtex_matrix(args.gtex_matrix, genes, ensg_ids, tissues)

    # PCA of gene-level average expression levels across tissues
    gtex_pca = pca_expression(gtex_mean, args.n_pcs)

    # Write matrices to output files
    write_matrix(gtex_min, args.prefix + '.min.tsv', args.gzip)
    write_matrix(gtex_q1, args.prefix + '.q1.tsv', args.gzip)
    write_matrix(gtex_median, args.prefix + '.median.tsv', args.gzip)
    write_matrix(gtex_mean, args.prefix + '.mean.tsv', args.gzip)
    write_matrix(gtex_q3, args.prefix + '.q3.tsv', args.gzip)
    write_matrix(gtex_max, args.prefix + '.max.tsv', args.gzip)
    write_matrix(gtex_sd, args.prefix + '.sd.tsv', args.gzip)
    write_matrix(gtex_mad, args.prefix + '.mad.tsv', args.gzip)
    write_matrix(gtex_pca, args.prefix + '.pca.tsv', args.gzip)

if __name__ == '__main__':
    main()

reason: import numpy,from scipy


text:
import numpy as np

from qtmoderngl import QModernGLWidget
import sys

from PyQt5 import QtWidgets

from renderer_example import HelloWorld2D, PanTool

def vertices():
    x = np.linspace(-1.0, 1.0, 50)
    y = np.random.rand(50) - 0.5
    r = np.ones(50)
    g = np.zeros(50)
    b = np.zeros(50)
    a = np.ones(50)
    return np.dstack([x, y, r, g, b, a])

verts = vertices()
pan_tool = PanTool()

class MyWidget(QModernGLWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.scene = None

    def init(self):
        self.resize(512, 512)
        self.ctx.viewport = (0, 0, 512, 512)
        self.scene = HelloWorld2D(self.ctx)

    def render(self):
        self.screen.use()
        self.scene.clear()
        self.scene.plot(verts)

    def mousePressEvent(self, evt):
        pan_tool.start_drag(evt.x() / 512, evt.y() / 512)
        self.scene.pan(pan_tool.value)
        self.update()

    def mouseMoveEvent(self, evt):
        pan_tool.dragging(evt.x() / 512, evt.y() / 512)
        self.scene.pan(pan_tool.value)
        self.update()

    def mouseReleaseEvent(self, evt):
        pan_tool.stop_drag(evt.x() / 512, evt.y() / 512)
        self.scene.pan(pan_tool.value)
        self.update()

app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())

max_stars_repo_name: asnt/moderngl

max_issues_repo_name: asnt/moderngl

max_forks_repo_name: asnt/moderngl


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 31 16:01:25 2015.

@author: rc,alex
"""
import os
import sys
if __name__ == '__main__' and __package__ is None:
    filePath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(filePath)

from glob import glob
import numpy as np
import pandas as pd
from sklearn.metrics import roc_auc_score

from preprocessing.aux import getEventNames

cols = getEventNames()
ids = np.load('../infos_test.npy')
subject_test = ids[:, 1]
series_test = ids[:, 2]
ids = ids[:, 0]

labels = np.load('../infos_val.npy')
subjects = labels[:, -2]
series = labels[:, -1]
labels = labels[:, :-2]

subs_val = []
subs_test = []

# get all submissions
subs = glob('models/*.yml')
# remove folder and extension
subs = [sub[7:-4] for sub in subs]

# load subs
print 'Computing an average of %d submissions:' % len(subs)
for sub in subs:
    print sub
    subs_val.append(np.load('val/val_%s.npy' % sub)[0])
    subs_test.append(pd.read_csv('submissions/%s.csv' % sub,
                                 index_col=0).values)

# average all models
ens_val = np.mean(subs_val, axis=0)
ens_test = np.mean(subs_test, axis=0)

# stats of the average
aucs_val = [np.mean(roc_auc_score(labels[series == s], ens_val[series == s]))
            for s in [7, 8]]
print 'AUC: %.5f (SD: %.5f), s7: %.5f ; s8: %.5f' % (np.mean(aucs_val),
                                                     np.std(aucs_val),
                                                     aucs_val[0], aucs_val[1])

# save
np.save('val/val_YOLO.npy', [ens_val])
sub = pd.DataFrame(data=ens_test, index=ids, columns=cols)
sub.to_csv('submissions/YOLO.csv', index_label='id', float_format='%.8f')