text:
import numpy as np
import re

# Thanks to Anton Goloborodko, Lev Levitsky, as code below is initially from Pyteomics project.
# http://hg.theorchromo.ru/

_comments = '#;!/'

class PyteomicsError(Exception):
    """Exception raised for errors in Pyteomics library.

    Attributes
    ----------
    message : str
        Error message.
    """

    def __init__(self, msg):
        self.message = msg

    def __str__(self):
        return "Pyteomics error, message: %s" % (repr(self.message),)

class Charge(int):
    """A subclass of :py:class:`int`. Can be constructed from strings in "N+"
    or "N-" format, and the string representation of a :py:class:`Charge` is
    also in that format.
    """
    def __new__(cls, *args, **kwargs):
        try:
            return super(Charge, cls).__new__(cls, *args)
        except ValueError as e:
            if isinstance(args[0], str):
                try:
                    num, sign = re.match(r'^(\d+)(\+|-)$', args[0]).groups()
                    return super(Charge, cls).__new__(cls,
                        sign + num, *args[1:], **kwargs)
                except:
                    pass
            raise PyteomicsError(*e.args)

    def __str__(self):
        return str(abs(self)) + '+-'[self<0]

class ChargeList(list):
    """Just a list of :py:class:`Charge`s. When printed, looks like an
    enumeration of the list contents. Can also be constructed from such
    strings (e.g. "2+, 3+ and 4+").
    """
    def __init__(self, *args, **kwargs):
        if args and isinstance(args[0], str):
            self.extend(map(Charge,
                re.split(r'(?:,\s*)|(?:\s*and\s*)', args[0])))
        else:
            try:
                super(ChargeList, self).__init__(
                        sorted(set(args[0])), *args[1:], **kwargs)
            except:
                super(ChargeList, self).__init__(*args, **kwargs)
            self[:] = map(Charge, self)

    def __str__(self):
        if len(self) > 1:
            return ', '.join(map(str, self[:-1])) + ' and {}'.format(self[-1])
        elif self:
            return str(self[0])
        return super(ChargeList, self).__str__()

def parse_charge(s, list_only=False):
    if not list_only:
        try:
            return Charge(s)
        except PyteomicsError:
            pass
    return ChargeList(s)

def parseMGF(m_data):
    data = m_data.split('\n')
    reading_spectrum = False
    params = {}
    masses = []
    intensities = []
    charges = []
    for line in data:
        if not reading_spectrum:
            if line.strip() == 'BEGIN IONS':
                reading_spectrum = True
                # otherwise we are not interested; do nothing, just move along
        else:
            if not line.strip() or any(
                    line.startswith(c) for c in _comments):
                pass
            elif line.strip() == 'END IONS':
                reading_spectrum = False
                if 'pepmass' in params:
                    try:
                        pepmass = tuple(map(float, params['pepmass'].split()))
                    except ValueError:
                        raise PyteomicsError('MGF format error: cannot parse '
                                                 'PEPMASS = {}'.format(params['pepmass']))
                    else:
                        params['pepmass'] = pepmass + (None,) * (2 - len(pepmass))
                if isinstance(params.get('charge'), str):
                    params['charge'] = parse_charge(params['charge'], True)
                out = {'params': params,
                       'm/z array': np.array(masses),
                       'intensity array': np.array(intensities),
                       'charge array': np.ma.masked_equal(charges, 0)}
                return out
            else:
                l = line.split('=', 1)
                if len(l) > 1:  # spectrum-specific parameters!
                    params[l[0].lower()] = l[1].strip()
                elif len(l) == 1:  # this must be a peak list
                    l = line.split()
                    if len(l) >= 2:
                        try:
                            masses.append(float(l[0]))  # this may cause
                            intensities.append(float(l[1]))  # exceptions...
                            # charges.append(aux._parse_charge(l[2]) if len(l) > 2 else 0)
                        except ValueError:
                            raise PyteomicsError(
                                'Error when parsing %s. Line:\n%s' %
                                (data, line))

max_stars_repo_name: gurvindersingh/mgfspark

max_issues_repo_name: gurvindersingh/mgfspark

max_forks_repo_name: gurvindersingh/mgfspark


text:
import math
import numpy as np
from abc import ABCMeta, abstractmethod

class Estimator_Base():
	__metaclass__ = ABCMeta

	estimator_name = ''
	estimator_type = '' # EKF,UKF,RLS etc.

	@abstractmethod
	def __init__(self):
		pass

	@abstractmethod
	def estimate(self):
		'''
		This function calculates the next state  each time step
		'''
		pass

	@abstractmethod
	def reset(self):
		'''
		return TRUE for success, FALSE for failure
		'''
		pass

	def get_name(self):
		return self.estimator_name

	def get_type(self):
		return self.estimator_type

class Zero_Estimator(Estimator_Base):
	'''
	This is a blank estimator class.
	The Zero_Estimator always returns 0 as estimator output.
	'''
	def __init__(self, spec, model):
		self.name = 'Zero'
		self.type = 'None'
		self.spec = spec
		self.model = model

	def reset(self):
		return True

	def estimate(self, x,covariance,z):
		return np.zeros(self.model.x_shape)

class Estimator(Estimator_Base):
	'''
    This is the unified estimator class used in Agent Class.
    Estimator specs should be given in the following format:
    {'Extended Kalman Filter': EFK, 'Unscented Kalman Filter': 'UKF'}
    The estimator key can be ignored if it's not used, it will be filled with an zero estimator automatically.

    E.g. EKF Estimator specs:
    {"Extended Kalman filter": "EKF", "params": {"Rww": [1,2], "Rvv": [0, 0]}}
    '''
	def __init__(self,spec,model):
		self.spec  = spec
		self.model = model

		if 'EKF' in self.spec:
			self.EKF = globals()[self.spec['EKF']](self.spec['params'],self.model)
			# this line will take input from spec, check reflection in python. it takes input from spec and
			# translates a string used in spec and creates a class object deepnds on string
			# example : If I define a feedback controller, with PID, when I change PID to something else like adatpive controller
			# it will automatically create an ARC class object.

		else:
			self.EKF = Zero_Estimator(self.spec,self.model)

class Unscented_Kalman_Filter(Estimator_Base):

	def __init__(self,estimator_params,model):
		self.name                = 'UKF'
		self.params              = estimator_params
		self.model               = model
		self._Rww                = estimator_params['Rww'] # passing covariances as a dictionary
		self._Rvv                = estimator_params['Rvv'] # passing covariances as a dictionary
		self._alpha_ukf          = estimator_params['alpha_ukf']
		self._kappa_ukf          = estimator_params['kappa_ukf']
		self._beta_ukf           = estimator_params['beta_ukf']

	def param_tuning(self,params):
		self._Rww                = params['Rww']
		self._Rvv                = params['Rvv']
		return True

	def estimate(self,posterior_pos,posterior_state_cov,z_meas):
		# This is the main estimate function, it takes the state and state covariance and measuremnets
		# and returns state and covariance at the next time step
		X                        = self.get_sigma_points(posterior_pos,posterior_state_cov)
		priori_pos_sigma         = self.forward_propagate_dynamics(X)
		weighted_pos_mean        = self.compute_weighted_mean(priori_pos_sigma)
		weighted_pos_covariance  = self.compute_weighted_covariance(weighted_pos_mean,priori_pos_sigma,self._Rww)
		X                        = self.get_sigma_points(weighted_pos_mean,weighted_pos_covariance);
		Z                        = self.measurement_function(X) ;
		weighted_meas_mean       = self.compute_weighted_mean(Z);
		weighted_meas_covariance = self.compute_weighted_covariance(weighted_meas_mean,Z,Rvv);
		F                        = self.compute_other_covariance(weighted_pos_mean,X,weighted_meas_mean,Z);
		K                        = np.matmul(F,np.linalg.inv(weighted_meas_covariance));
		posterior_pos            = weighted_pos_mean + np.matmul(K,z_meas-weighted_meas_mean)          ;
		P_post                   = weighted_pos_covariance - np.matmul(np.matmul(K,weighted_meas_covariance),K.transpose())    ;

		return posterior_pos,P_post

	def get_sigma_points(self,mu,E):
		# This function gets 2N+1 sigma points centered around the mean mu with variance in multiples of E
		# They are collected in X
		N                       = len(mu)
		X                       = np.zeros((N,(2*N)+1))
		X[:,0]                  = mu
		lambda_ukf              = ((self._alpha_ukf**2)*(N + self._kappa_ukf))-N;
		Z                       = sqrtm((N + lambda_ukf)*E);

		for i in range(N):
			X[:,i+1]            = mu + Z[:,i]

		for i in range(N):
			X[:,i+1+N]          = mu - Z[:,i]

		return X

	def compute_weighted_mean(self,X):
		# This function calculates the weighted mean of the sigma points X,
		# X[0,:] is the mean
		N,C                     = X.shape
		lambda_ukf              = ((self._alpha_ukf**2)*(N + self._kappa_ukf))-N;
		w                       = (0.5/(N+lambda_ukf))*np.ones((C,))
		w[0]                    = lambda_ukf/(N+lambda_ukf);
		mu                      = np.zeros((N,))

		for i in range(C):
			mu                  = mu + w[i]*X[:,i]

		return mu

	def compute_weighted_covariance(self,mu,X,F):
		N,C                     = X.shape
		lambda_ukf              = ((self._alpha_ukf**2)*(N + self._kappa_ukf))-N;
		w                       = (0.5/(N+lambda_ukf))*np.ones((C,))
		w[0]                    = (lambda_ukf/(N+lambda_ukf)) + (1-(self._alpha_ukf**2) + self._beta_ukf);
		P                       = np.zeros((N,N))
		for i in range(C):
			Z = X[:,i] - mu
			P = P + w[i]*np.outer(Z,Z)
		P = P + F

		return P

	def compute_other_covariance(self,weighted_pos_mean,X,weighted_meas_mean,Z):
		# This function computes statistical covariance based on sigma points
		Nx,N                        = X.shape
		Nz,rr                       = Z.shape
		P                           = np.zeros((Nx,Nz))
		lambda_ukf                  = ((self._alpha_ukf**2)*(N + self._kappa_ukf))-N;
		w                           = (0.5/(N+lambda_ukf))*np.ones((N,))
		w[0]                        = (lambda_ukf/(N+lambda_ukf)) + (1-(self._alpha_ukf**2) + self._beta_ukf);

		for i in range(N):
			G = X[:,i] - weighted_pos_mean
			H = Z[:,i] - weighted_meas_mean
			W = w[i]*np.outer(G,H)
			P = P + W

		return P

	def reset(self):
		return True

# put underscore to internal variables
class Extended_Kalman_Filter(Estimator_Base):

	def __init__(self,estimator_params,model):
		self.name 				 = 'EKF'
		self.params 			 = estimator_params
		self.model  			 = model
		self._Rww    			 = estimator_params['Rww']
		self._Rvv    			 = estimator_params['Rvv']

	def param_tuning(self,params):
		self._Rww 				 = params['Rww']
		self._Rvv 				 = params['Rvv']
		return True

	def estimate(self,posterior_pos,posterior_state_cov,z_meas):
		# check Chase's code, see which method gives next state
		priori_pos               = self.forward_propagate_dynamics(posterior_pos)      							; # need to call dynamics of the paritcular robot model
		A                        = self.linearization_forward_dynamics(posterior_pos)  							; # need to call linearization of dynamics of the paritcular robot model
		priori_cov               = np.matmul(np.matmul(A,posterior_state_cov),np.transpose(A)) + self._Rww      ; # P- = APA' + Q
		n                        = len(posterior_pos)                                           				; # get size of EKF state
		I                        = np.identity(n)                                               				; # initialize identity
		H                        = self.linearization_measurement_function(priori_pos)   						; # linearization of robot's measurement function
		innovation               = z_meas - self.measurement_function(priori_pos)        						; # calculate innovation, needs measurement function
		S                        = np.matmul(np.matmul(H,priori_cov),np.transpose(H)) + self._Rvv  				; # S = HP-H' + R
		Sinv                     = np.linalg.inv(S)																; # Calculate Sinverse
		K                        = np.matmul(np.matmul(priori_cov,np.transpose(H)),Sinv)                  		; # Calculate Kalman Gain
		next_posterior_state     = priori_pos + np.matmul(K,innovation)                         				; # Get next pos estimate
		next_posterior_state_cov = np.matmul(I-(np.matmul(K,H)),priori_cov)                     				; # Get next pos covaraince, this is posteriori covariance

		return next_posterior_state,next_posterior_state_cov

	def reset(self):
		return True

ext: py

reason: import numpy


text:
#!/usr/bin/env python

"""
Usage example employing Lasagne for digit recognition using the MNIST dataset.

This example is deliberately structured as a long flat file, focusing on how
to use Lasagne, instead of focusing on writing maximally modular and reusable
code. It is used as the foundation for the introductory Lasagne tutorial:
http://lasagne.readthedocs.org/en/latest/user/tutorial.html

More in-depth examples and reproductions of paper results are maintained in
a separate repository: https://github.com/Lasagne/Recipes
"""

from __future__ import print_function

import sys
import os
import time
import re
from collections import defaultdict

import numpy as np
from scipy.ndimage import imread
import theano
import theano.tensor as T

import lasagne

##############################
## DATASET GLOBALS
IM_WIDTH  = 128
IM_HEIGHT = 128
NUM_CLASSES = 3
BATCH_SIZE = 15
##############################

# ################## Download and prepare the MNIST dataset ##################
# This is just some way of getting the MNIST dataset from an online location
# and loading it into numpy arrays. It doesn't involve Lasagne at all.

def load_dataset():
   # Load all the images
   inputDir = 'imgs'
   labelsFile = 'whales.csv'
   output = np.genfromtxt(labelsFile, skip_header=1, dtype=[('image', 'S10'), ('label', 'S11')], delimiter=',')
   labels = [x[1] for x in output]
   numberOfClasses = len(set(labels))

   # Read the images into 4D array (numImages, numChannels, width, height)
   # Assume all files in dir are images and all are the same size
   fileNames = os.listdir(inputDir)
   im = imread(os.path.join(inputDir, fileNames[0]))
   w, h = im.shape

   # Initialize the ndarray
   images = np.ndarray((len(fileNames), 1, w, h))
   # Load the first image into it's slot
   images[0,0,:,:] = im

   for i in xrange(1, len(fileNames)):
      im = imread(os.path.join(inputDir, fileNames[i]))
      images[i,0:,:] = im

   labelsDict = {int(re.search("w_(\\d+)\.jpg", x[0]).group(1)):int(re.search("whale_(\\d+)", x[1]).group(1)) for x in output}

   # to provide default values
   labelsDict = defaultdict(lambda:0, labelsDict)
   examples = [int(re.search("w_(\\d+).jpg",x).group(1)) for x in fileNames]
   labels = [labelsDict[x] for x in examples]
   labels = np.array(labels)

   origTrainLabels = labels[labels > 0]

   # Renumber the labels to have consecutive numnbers
   l = np.zeros(max(origTrainLabels))
   i = 0
   for k in sorted(set(origTrainLabels)):
      l[k-1] = i
      i += 1
   trainLabels = np.array([l[x-1] for x in origTrainLabels])
   trainLabels = trainLabels.astype(np.uint8)

   # We can now download and read the training and test set images and labels.
   X_train = images
   y_train = trainLabels
   X_test = images
   y_test = trainLabels

   X_val = images
   y_val = trainLabels
   # We reserve the last 10000 training examples for validation.
#    X_train, X_val = X_train[:-10000], X_train[-10000:]
#    y_train, y_val = y_train[:-10000], y_train[-10000:]

   # We just return all the arrays in order, as expected in main().
   # (It doesn't matter how we do this as long as we can read them again.)
   return X_train, y_train, X_val, y_val, X_test, y_test

def build_cnn(input_var=None):
   # As a third model, we'll create a CNN of two convolution + pooling stages
   # and a fully-connected hidden layer in front of the output layer.

   # Input layer, as usual:
   network = lasagne.layers.InputLayer(shape=(None, 1, IM_HEIGHT, IM_WIDTH),
                                       input_var=input_var)
   # This time we do not apply input dropout, as it tends to work less well
   # for convolutional layers.

   # Convolutional layer with 32 kernels of size 5x5. Strided and padded
   # convolutions are supported as well; see the docstring.
   network = lasagne.layers.Conv2DLayer(
           network, num_filters=32, filter_size=(5, 5),
           nonlinearity=lasagne.nonlinearities.rectify,
           W=lasagne.init.GlorotUniform())
   # Expert note: Lasagne provides alternative convolutional layers that
   # override Theano's choice of which implementation to use; for details
   # please see http://lasagne.readthedocs.org/en/latest/user/tutorial.html.

   # Max-pooling layer of factor 2 in both dimensions:
   network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))

   # Another convolution with 32 5x5 kernels, and another 2x2 pooling:
   network = lasagne.layers.Conv2DLayer(
           network, num_filters=32, filter_size=(5, 5),
           nonlinearity=lasagne.nonlinearities.rectify)
   network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))

   # And another convolution with 32 5x5 kernels, and another 2x2 pooling:
   network = lasagne.layers.Conv2DLayer(
           network, num_filters=32, filter_size=(5, 5),
           nonlinearity=lasagne.nonlinearities.rectify)
   network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))

   # And another convolution with 32 5x5 kernels, and another 2x2 pooling:
   network = lasagne.layers.Conv2DLayer(
           network, num_filters=32, filter_size=(5, 5),
           nonlinearity=lasagne.nonlinearities.rectify)
   network = lasagne.layers.MaxPool2DLayer(network, pool_size=(2, 2))

   # A fully-connected layer of 512 units with 50% dropout on its inputs:
   network = lasagne.layers.DenseLayer(
           lasagne.layers.dropout(network, p=.5),
           num_units=512,
           nonlinearity=lasagne.nonlinearities.rectify)

   # And, finally, the 3-unit output layer with 50% dropout on its inputs:
   network = lasagne.layers.DenseLayer(
           lasagne.layers.dropout(network, p=.5),
           num_units=NUM_CLASSES,
           nonlinearity=lasagne.nonlinearities.softmax)

   return network

# ############################# Batch iterator ###############################
# This is just a simple helper function iterating over training data in
# mini-batches of a particular size, optionally in random order. It assumes
# data is available as numpy arrays. For big datasets, you could load numpy
# arrays as memory-mapped files (np.load(..., mmap_mode='r')), or write your
# own custom data iteration function. For small datasets, you can also copy
# them to GPU at once for slightly improved performance. This would involve
# several changes in the main program, though, and is not demonstrated here.

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
   assert len(inputs) == len(targets)
   if shuffle:
      indices = np.arange(len(inputs))
      np.random.shuffle(indices)
   for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
      if shuffle:
         excerpt = indices[start_idx:start_idx + batchsize]
      else:
         excerpt = slice(start_idx, start_idx + batchsize)
      yield inputs[excerpt], targets[excerpt]

# ############################## Main program ################################
# Everything else will be handled in our main program now. We could pull out
# more functions to better separate the code, but it wouldn't make it any
# easier to read.

def main(model='mlp', num_epochs=500):
   # Load the dataset
   print("Loading data...")
   X_train, y_train, X_val, y_val, X_test, y_test = load_dataset()

   # Prepare Theano variables for inputs and targets
   input_var = T.tensor4('inputs')
   target_var = T.ivector('targets')

   # Create neural network model (depending on first command line parameter)
   network = build_cnn(input_var)

   # Create a loss expression for training, i.e., a scalar objective we want
   # to minimize (for our multi-class problem, it is the cross-entropy loss):
   prediction = lasagne.layers.get_output(network)
   loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
   loss = loss.mean()
   # We could add some weight decay as well here, see lasagne.regularization.

   # Create update expressions for training, i.e., how to modify the
   # parameters at each training step. Here, we'll use Stochastic Gradient
   # Descent (SGD) with Nesterov momentum, but Lasagne offers plenty more.
   params = lasagne.layers.get_all_params(network, trainable=True)
   updates = lasagne.updates.nesterov_momentum(
           loss, params, learning_rate=0.01, momentum=0.9)

   # Create a loss expression for validation/testing. The crucial difference
   # here is that we do a deterministic forward pass through the network,
   # disabling dropout layers.
   test_prediction = lasagne.layers.get_output(network, deterministic=True)
   test_loss = lasagne.objectives.categorical_crossentropy(test_prediction, target_var)
   test_loss = test_loss.mean()
   # As a bonus, also create an expression for the classification accuracy:
   test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var), dtype=theano.config.floatX)

   # Compile a function performing a training step on a mini-batch (by giving
   # the updates dictionary) and returning the corresponding training loss:
   train_fn = theano.function([input_var, target_var], loss, updates=updates)

   # Compile a second function computing the validation loss and accuracy:
   val_fn = theano.function([input_var, target_var], [test_loss, test_acc])

   # Finally, launch the training loop.
   print("Starting training...")
   # We iterate over epochs:
   for epoch in range(num_epochs):
      # In each epoch, we do a full pass over the training data:
      train_err = 0
      train_batches = 0
      start_time = time.time()
      for batch in iterate_minibatches(X_train, y_train, BATCH_SIZE, shuffle=True):
         inputs, targets = batch
         train_err += train_fn(inputs, targets)
         train_batches += 1

         # And a full pass over the validation data:
         val_err = 0
         val_acc = 0
         val_batches = 0
         for batch in iterate_minibatches(X_val, y_val, BATCH_SIZE, shuffle=False):
            inputs, targets = batch
            err, acc = val_fn(inputs, targets)
            val_err += err
            val_acc += acc
            val_batches += 1

      # Then we print the results for this epoch:
      print("Epoch {} of {} took {:.3f}s".format(epoch + 1, num_epochs, time.time() - start_time))
      print("  training loss:\t\t{:.6f}".format(train_err / train_batches))
      print("  validation loss:\t\t{:.6f}".format(val_err / val_batches))
      print("  validation accuracy:\t\t{:.2f} %".format(
          val_acc / val_batches * 100))

   # After training, we compute and print the test error:
   test_err = 0
   test_acc = 0
   test_batches = 0
   for batch in iterate_minibatches(X_test, y_test, BATCH_SIZE, shuffle=False):
      inputs, targets = batch
      err, acc = val_fn(inputs, targets)
      test_err += err
      test_acc += acc
      test_batches += 1
   print("Final results:")
   print("  test loss:\t\t\t{:.6f}".format(test_err / test_batches))
   print("  test accuracy:\t\t{:.2f} %".format(
       test_acc / test_batches * 100))

   # Optionally, you could now dump the network weights to a file like this:
   # np.savez('model.npz', *lasagne.layers.get_all_param_values(network))
   #
   # And load them again later on like this:
   # with np.load('model.npz') as f:
   #     param_values = [f['arr_%d' % i] for i in range(len(f.files))]
   # lasagne.layers.set_all_param_values(network, param_values)

if __name__ == '__main__':
   if ('--help' in sys.argv) or ('-h' in sys.argv):
      print("Trains a neural network on MNIST using Lasagne.")
      print("Usage: %s [MODEL [EPOCHS]]" % sys.argv[0])
      print()
      print("MODEL: 'mlp' for a simple Multi-Layer Perceptron (MLP),")
      print("       'custom_mlp:DEPTH,WIDTH,DROP_IN,DROP_HID' for an MLP")
      print("       with DEPTH hidden layers of WIDTH units, DROP_IN")
      print("       input dropout and DROP_HID hidden dropout,")
      print("       'cnn' for a simple Convolutional Neural Network (CNN).")
      print("EPOCHS: number of training epochs to perform (default: 500)")
   else:
      kwargs = {}
      if len(sys.argv) > 1:
         kwargs['model'] = sys.argv[1]
      if len(sys.argv) > 2:
         kwargs['num_epochs'] = int(sys.argv[2])
   main(**kwargs)

reason: import numpy,from scipy,import theano


text:
###############################################################################
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
###############################################################################
import numbers
import collections
import numpy as np
from ..common import cvtfunc
from ..common.onnx_ops import apply_transpose, apply_reshape
from ..proto import onnx_proto
from .common import extract_recurrent_activation

def _calculate_keras_lstm_output_shapes(operator):
    op = operator.raw_operator
    if isinstance(op.output_shape[0], collections.Iterable):
        operator.outputs[0].type.shape = list(i if isinstance(i, numbers.Integral) else 'None'
                                              for i in op.output_shape[0])
    else:
        operator.outputs[0].type.shape = list(i if isinstance(i, numbers.Integral) else 'None' for i in op.output_shape)

@cvtfunc(shape_infer=_calculate_keras_lstm_output_shapes)
def convert_keras_lstm(scope, operator, container):
    op = operator.raw_operator
    hidden_size = op.units
    input_size = op.input_shape[-1]
    seq_length = op.input_shape[-2]
    output_seq = op.return_sequences
    output_state = op.return_state
    reverse_input = op.go_backwards

    # Keras: [W_x, W_h, b] each in I F C O
    # ONNX: W[iofc] I O F C
    W_h = np.empty(shape=(4, hidden_size, hidden_size))
    W_x = np.empty(shape=(4, hidden_size, input_size))
    K_W_h = op.get_weights()[1].T
    W_h[0:] = K_W_h[0 * hidden_size:][:hidden_size]
    W_h[1:] = K_W_h[3 * hidden_size:][:hidden_size]
    W_h[2:] = K_W_h[1 * hidden_size:][:hidden_size]
    W_h[3:] = K_W_h[2 * hidden_size:][:hidden_size]

    K_W_x = op.get_weights()[0].T
    W_x[0:] = K_W_x[0 * hidden_size:][:hidden_size]
    W_x[1:] = K_W_x[3 * hidden_size:][:hidden_size]
    W_x[2:] = K_W_x[1 * hidden_size:][:hidden_size]
    W_x[3:] = K_W_x[2 * hidden_size:][:hidden_size]

    b = None
    if op.use_bias:
        b = np.zeros(shape=(8, hidden_size))
        keras_b = op.get_weights()[2]
        b[0:] = keras_b[0 * hidden_size:][:hidden_size]
        b[1:] = keras_b[3 * hidden_size:][:hidden_size]
        b[2:] = keras_b[1 * hidden_size:][:hidden_size]
        b[3:] = keras_b[2 * hidden_size:][:hidden_size]

    # Declare essential attributes of ONNX LSTM
    lstm__type = 'LSTM'
    lstm_input_names = []
    lstm_output_names = []
    lstm_attrs = {'name': operator.full_name}

    # Because of the format difference between Keras and ONNX LSTM's, we set up a preprocessing node to match them.
    lstm_x_name = scope.get_unique_variable_name('lstm_x')
    lstm_input_names.append(lstm_x_name)
    apply_reshape(scope, operator.inputs[0].full_name, lstm_x_name, container, desired_shape=[-1, 1, input_size])

    # Add the weights to the final model's initializer list so that our LSTM operator can use it
    tensor_w_name = scope.get_unique_variable_name('W')
    container.add_initializer(tensor_w_name, onnx_proto.TensorProto.FLOAT,
                              [1, 4 * hidden_size, input_size], W_x.flatten())
    lstm_input_names.append(tensor_w_name)

    # Add the recursion weights to the final model's initializer list so that our LSTM operator can use it
    tensor_r_name = scope.get_unique_variable_name('R')
    container.add_initializer(tensor_r_name, onnx_proto.TensorProto.FLOAT,
                              [1, 4 * hidden_size, hidden_size], W_h.flatten())
    lstm_input_names.append(tensor_r_name)

    if len(b) > 0:
        tensor_b_name = scope.get_unique_variable_name('B')
        container.add_initializer(tensor_b_name, onnx_proto.TensorProto.FLOAT, [1, 8 * hidden_size], b.flatten())
        lstm_input_names.append(tensor_b_name)
    else:
        lstm_input_names.append('')

    # sequence_lens
    lstm_input_names.append('')
    # TODO initial_h (optional) : T
    lstm_input_names.append('')
    # TODO initial_c (optional) : T
    lstm_input_names.append('')
    # P (optional) : No peep hole in keras.
    lstm_input_names.append('')

    activation_types = []
    alphas = []
    betas = []
    extracted_activations = [
        extract_recurrent_activation(op.recurrent_activation),
        extract_recurrent_activation(op.activation),
        extract_recurrent_activation(op.activation)]

    for (activation_type, alpha, beta) in extracted_activations:
        activation_types.append(activation_type.encode('utf-8'))
        if alpha is not None:
            alphas.append(alpha)
        if beta is not None:
            betas.append(beta)

    lstm_attrs['activations'] = activation_types
    if alphas:
        lstm_attrs['activation_alpha'] = alphas
    if betas:
        lstm_attrs['activation_beta'] = betas

    # Set up other attributes
    lstm_attrs['direction'] = 'reverse' if reverse_input else 'forward'
    lstm_attrs['hidden_size'] = hidden_size

    # Set up version-dependent attributes
    if container.target_opset <= 5:
        lstm_attrs['output_sequence'] = 1 if output_seq else 0
        op_version = 1
    else:
        op_version = 7

    # We declare some names to store the outputs produced by ONNX LSTM. Then, create ONNX LSTM. Subsequently, its
    # outputs may be adjusted to match Keras format.
    lstm_y_name = scope.get_unique_variable_name('lstm_y')
    lstm_output_names.append(lstm_y_name)
    lstm_h_name = scope.get_unique_variable_name('lstm_h')
    lstm_output_names.append(lstm_h_name)
    lstm_c_name = scope.get_unique_variable_name('lstm_c')
    lstm_output_names.append(lstm_c_name)
    container.add_node(lstm__type, lstm_input_names, lstm_output_names, op_version=op_version, **lstm_attrs)

    # Create output-adjusting operators
    if output_seq:
        lstm_y_name_transposed = scope.get_unique_variable_name('lstm_y_transposed')
        perm = [1, 0, 2] if container.target_opset <= 5 else [2, 0, 1, 3]
        apply_transpose(scope, lstm_y_name, lstm_y_name_transposed, container, perm=perm)
        apply_reshape(scope, lstm_y_name_transposed, operator.outputs[0].full_name, container,
                      desired_shape=[-1, seq_length, hidden_size])
    else:
        apply_reshape(scope, lstm_h_name, operator.outputs[0].full_name, container, desired_shape=[-1, hidden_size])

    if output_state:
        # state_h
        apply_reshape(scope, lstm_h_name, operator.outputs[1].full_name, container, desired_shape=[-1, hidden_size])
        # state_c
        apply_reshape(scope, lstm_c_name, operator.outputs[2].full_name, container, desired_shape=[-1, hidden_size])

max_stars_repo_name: kohei-us/keras-onnx

max_issues_repo_name: kohei-us/keras-onnx

max_forks_repo_name: kohei-us/keras-onnx


text:
import logging
import simplejson
import uuid
import tempfile
import shutil
import os
import time
import sys
import numpy as np
from .reactopya_serialize import reactopya_serialize, reactopya_deserialize

logger = logging.getLogger('reactopya')

class ReactopyaHostedWidget:
    """Reactopya hosted widget"""
    def __init__(self, *, project_name, type, initial_children, props, key=''):
        self._project_name = project_name
        self._type = type
        self._children = dict()
        self._child_ids = []
        for child_id, ch in enumerate(initial_children):
            self._children[str(child_id)] = ReactopyaHostedWidget(
                project_name=ch.get('project_name', self._project_name),
                type=ch['type'],
                initial_children=ch.get('children', []),
                props=ch.get('props', {}),
                key=ch.get('key', '')
            )
            self._child_ids.append(str(child_id))
        self._props = props
        self._key = key
        self._javascript_state_changed_handlers = []
        self._iterate_handlers = []
        self._custom_message_handlers = []
        self._add_child_handlers = []
        self._message_index = 100000
        self._snapshot_html = None
        self._session_dir = None
        self._session_id = None

    def _set_session_id(self, session_id):
        self._session_id = session_id

    def _set_snapshot_html(self, html):
        self._snapshot_html = html

    def _set_session_dir(self, path):
        self._session_dir = path

    def show(self):
        with open(os.path.join(self._session_dir, 'index.html'), 'w') as f:
            f.write(self._snapshot_html)

        while True:
            if not os.path.exists(self._session_dir):
                return
            self._iterate()
            time.sleep(0.1)

    def set_python_state(self, state):
        msg = dict(
            name='setPythonState',
            state=state
        )
        self._send_message_to_javascript(msg)

    def send_custom_message(self, message):
        msg = dict(
            name='customMessage',
            message=message
        )
        self._send_message_to_javascript(msg)

    def on_javascript_state_changed(self, handler):
        self._javascript_state_changed_handlers.append(handler)

    def on_iterate(self, handler):
        self._iterate_handlers.append(handler)

    def on_custom_message(self, handler):
        self._custom_message_handlers.append(handler)

    def on_add_child(self, handler):
        self._add_child_handlers.append(handler)

    def _iterate(self):
        sys.stdout.flush()
        sys.stderr.flush()
        messages = take_js_messages(self._session_dir)
        for msg in messages:
            self._handle_message(msg)
        for h in self._iterate_handlers:
            h()

    def _handle_message(self, msg):
        # handle message from javascript
        name = msg.get('name', '')
        if name == 'setJavaScriptState':
            state = msg['state']
            for handler in self._javascript_state_changed_handlers:
                handler(state)
        elif name == 'customMessage':
            message = msg['message']
            for handler in self._custom_message_handlers:
                handler(message)
        elif name == 'addChild':
            data = msg.get('data')
            for handler in self._add_child_handlers:
                handler(data)
        else:
            raise Exception('Unexpected message name: {}'.format(name))

    def _send_message_to_javascript(self, msg):
        msg = reactopya_serialize(msg)
        self._message_index = self._message_index + 1
        write_py_message(self._session_dir, self._message_index, msg)

def write_py_message(dirname, message_index, msg):
    fname = os.path.join(dirname, '{}.msg-from-py'.format(message_index))
    with open(fname + '.tmp', 'w') as f:
        simplejson.dump(msg, f, ignore_nan=True)
    os.rename(fname + '.tmp', fname)

def take_js_messages(dirname):
    messages = []
    files = os.listdir(dirname)
    files = sorted(files)
    for file in files:
        if file.endswith('.msg-from-js'):
            fname = os.path.join(dirname, file)
            with open(fname, 'r') as f:
                msg = simplejson.load(f)
            msg = reactopya_deserialize(msg)
            messages.append(msg)
            os.remove(fname)
    return messages

max_stars_repo_name: flatironinstitute/reactopy

max_issues_repo_name: flatironinstitute/reactopy

max_forks_repo_name: flatironinstitute/reactopy


text:
"""CrossEntropyLoss"""

import numpy as np
import torch
from torch.nn import CrossEntropyLoss as _CrossEntrypyLoss

# pylint: disable=too-few-public-methods

class CrossEntropyLoss:
    """
        Applies a Cross Entropy Loss function to the model.

        Cross Entropy Loss automatically applies a Softmax
        Layer at the end of the model, so there is no need to add a Softmax layer.

        Supported Arguments
            weight=None : (Numpy Array | List) Manual rescaling of classes
            ignore_index=-100 : (Integer) Specifies a target value that is
                ignored and does not contribute to the input gradient.
            reduction='mean' : (String) Specifies the reduction that is to be
                applied to the output.
    """

    def __init__(self, weight=None, ignore_index=-100, reduction='mean'):
        """
            __init__ method for CrossEntropyLoss

            Supported Arguments
                weight=None : (Numpy Array | List) Manual rescaling of classes
                ignore_index=-100 : (Integer) Specifies a target value that is
                    ignored and does not contribute to the input gradient.
                reduction='mean' : (String) Specifies the reduction that is to be
                    applied to the output.
        """
        if weight is not None and not (
                isinstance(weight, list) or
                type(weight).__module__ == np.__name__):
            raise ValueError("Invalid weight")

        if reduction not in ["none", "mean", "sum"]:
            raise ValueError("Invalid reduction")

        if isinstance(ignore_index, bool) or not isinstance(ignore_index, int):
            raise ValueError("Invalid ignore index")

        self.__weight = weight
        self.__ignore_index = ignore_index
        self.__reduction = reduction

    def get_loss_function(self):
        """
            Returns the details of the loss function

            There is no need to call this method as this is used by the
            Sequential model to build the model
        """
        # If weight provided, then converting it into torch tensor
        # pylint: disable=not-callable
        weight = None

        if self.__weight is not None:
            weight = torch.tensor(self.__weight).float()

        return {
            'loss_function': _CrossEntrypyLoss,
            'keyword_arguments': {
                # pylint: disable=not-callable
                'weight': weight,
                'ignore_index': self.__ignore_index,
                'reduction': self.__reduction
            }
        }


text:
import numpy as np
import pickle
import datetime
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import dgl
from dgl.nn.pytorch import Set2Set
from dgllife.model import MPNNGNN

# pylint: disable=W0221
class MPNNPredictor(nn.Module):
    """MPNN for regression and classification on graphs.
    MPNN is introduced in `Neural Message Passing for Quantum Chemistry
    <https://arxiv.org/abs/1704.01212>`__.
    Parameters
    ----------
    node_in_feats : int
        Size for the input node features.
    edge_in_feats : int
        Size for the input edge features.
    node_out_feats : int
        Size for the output node representations. Default to 64.
    edge_hidden_feats : int
        Size for the hidden edge representations. Default to 128.
    n_tasks : int
        Number of tasks, which is also the output size. Default to 1.
    num_step_message_passing : int
        Number of message passing steps. Default to 6.
    num_step_set2set : int
        Number of set2set steps. Default to 6.
    num_layer_set2set : int
        Number of set2set layers. Default to 3.
    """
    def __init__(self,
                 node_in_feats=3,
                 edge_in_feats=6,
                 node_out_feats=64,#32,##node hidden dim
                 edge_hidden_feats=128,#32,#
                 n_tasks=1,
                 num_step_message_passing=4,#6,
                 num_step_set2set=4,#6,
                 num_layer_set2set=2):#3):
        super(MPNNPredictor, self).__init__()

        self.gnn = MPNNGNN(node_in_feats=node_in_feats,
                           node_out_feats=node_out_feats,
                           edge_in_feats=edge_in_feats,
                           edge_hidden_feats=edge_hidden_feats,
                           num_step_message_passing=num_step_message_passing)
        self.readout = Set2Set(input_dim=node_out_feats,
                               n_iters=num_step_set2set,
                               n_layers=num_layer_set2set)
        #self.predict = nn.Sequential(
        #    nn.Linear(2 * node_out_feats, node_out_feats),
        #    nn.ReLU(),
        #    nn.Linear(node_out_feats, n_tasks)
        #)
        #v0
        #self.lin1 = nn.Linear(2 * node_out_feats, node_out_feats)
        #self.lin_last1 = nn.Linear(node_out_feats, 40)
        #self.lin_last2 = nn.Linear(node_out_feats, 1)
        #v1
        self.predict = nn.Sequential(
            nn.Linear(2 * node_out_feats, node_out_feats),
            nn.ReLU()
        )
        self.lin_last1 = nn.Linear(node_out_feats, 40)
        self.lin_last2 = nn.Linear(node_out_feats, 1)

    def forward(self, g, node_feats, edge_feats):
        """Graph-level regression/soft classification.
        Parameters
        ----------
        g : DGLGraph
            DGLGraph for a batch of graphs.
        node_feats : float32 tensor of shape (V, node_in_feats)
            Input node features.
        edge_feats : float32 tensor of shape (E, edge_in_feats)
            Input edge features.
        Returns
        -------
        float32 tensor of shape (G, n_tasks)
            Prediction for the graphs in the batch. G for the number of graphs.
        """
        node_feats = self.gnn(g, node_feats, edge_feats)
        graph_feats = self.readout(g, node_feats)
        #return self.predict(graph_feats)
        #v0
        #out = F.relu(self.lin1(graph_feats))
        #out1=self.lin_last1(out)#for classification
        #out2=self.lin_last2(out)#for regression
        #v1
        out = self.predict(graph_feats)
        out1=self.lin_last1(out)#for classification
        out2=self.lin_last2(out)#for regression
        return [out1,out2]

# define NN architecture
class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()

        self.fc1 = nn.Linear(10, 2048)#1024
        self.fc2 = nn.Linear(2048,2048)
        self.fc3 = nn.Linear(2048,2048)
        self.fc4 = nn.Linear(2048,2048)
        #self.fc5 = nn.Linear(2048,2048)
        #self.fc6 = nn.Linear(2048,2048)

        #self.fc_last1_1 = nn.Linear(4096,4096)
        #self.fc_last1_2 = nn.Linear(4096,4096)
        #self.fc_last1_3 = nn.Linear(4096,4096)

        #self.fc_last2_1 = nn.Linear(4096,4096)
        #self.fc_last2_2 = nn.Linear(4096,4096)

        self.fc_output1 = nn.Linear(2048,40)
        self.fc_output2 = nn.Linear(2048,1)
        self.droput = nn.Dropout(0.2)
        #self.sig=nn.Sigmoid()

    def forward(self,x):
        x = F.relu(self.fc1(x))
        x = self.droput(x)
        x = F.relu(self.fc2(x))
        x = self.droput(x)
        x = F.relu(self.fc3(x))
        x = self.droput(x)
        x = F.relu(self.fc4(x))
        x = self.droput(x)

        #x = F.relu(self.fc5(x))
        #x = F.relu(self.fc6(x))

        #x1 = F.relu(self.fc_last1_1(x))
        #x1 = F.relu(self.fc_last1_2(x1))
        #x1 = F.relu(self.fc_last1_3(x1))

        #x2 = F.relu(self.fc_last2_1(x))
        #x2 = F.relu(self.fc_last2_2(x2))

        x1 = self.fc_output1(x)
        x2 = self.fc_output2(x)#self.sig(self.fc_output2(x2))#x2 = self.fc4_2(x)
        return [x1,x2]#x

class MLPClassification(nn.Module):
    def __init__(self):
        super(MLPClassification,self).__init__()

        self.fc1 = nn.Linear(10, 2048)#1024
        self.fc2 = nn.Linear(2048,2048)
        self.fc3 = nn.Linear(2048,2048)
        self.fc4 = nn.Linear(2048,2048)

        self.fc_output1 = nn.Linear(2048,40)
        #self.fc_output2 = nn.Linear(2048,1)
        self.droput = nn.Dropout(0.2)
        #self.sig=nn.Sigmoid()

    def forward(self,x):
        x = F.relu(self.fc1(x))
        x = self.droput(x)
        x = F.relu(self.fc2(x))
        x = self.droput(x)
        x = F.relu(self.fc3(x))
        x = self.droput(x)
        x = F.relu(self.fc4(x))
        x = self.droput(x)

        x1 = self.fc_output1(x)
        #x2 = self.fc_output2(x)
        return x1

class MLPRegression(nn.Module):
    def __init__(self):
        super(MLPRegression,self).__init__()

        self.fc1 = nn.Linear(10, 2048)#1024
        self.fc2 = nn.Linear(2048,2048)
        self.fc3 = nn.Linear(2048,2048)
        self.fc4 = nn.Linear(2048,2048)

        #self.fc_output1 = nn.Linear(2048,40)
        self.fc_output2 = nn.Linear(2048,1)
        self.droput = nn.Dropout(0.2)
        #self.sig=nn.Sigmoid()

    def forward(self,x):
        x = F.relu(self.fc1(x))
        x = self.droput(x)
        x = F.relu(self.fc2(x))
        x = self.droput(x)
        x = F.relu(self.fc3(x))
        x = self.droput(x)
        x = F.relu(self.fc4(x))
        x = self.droput(x)

        #x1 = self.fc_output1(x)
        x2 = self.fc_output2(x)
        return x2

max_stars_repo_name: kidrabit/Data-Visualization-Lab-RND

max_issues_repo_name: kidrabit/Data-Visualization-Lab-RND

max_forks_repo_name: kidrabit/Data-Visualization-Lab-RND


text:
import cv2
import numpy as np
import os

def load_image(path: str) -> np.ndarray:
    """Загрузка ихображения

    :param path: путь к файлу с изображением
    :return: загруженное изображение
    """
    if type(path) != str:
        raise TypeError(f'Тип переменной path {type(path)} не является строкой')

    if not os.path.exists(path):
        raise FileNotFoundError(f'Файла {path} не существует.')

    image = cv2.imread(path)

    return image

def extract_contours(image: np.ndarray) -> np.ndarray:
    """Поиск контуров на изображени

    :param image: предварительно обработанное изображение с нанесенными рамками
    :return: контуры на изображении
    """
    # Диапазон цвета которого не может быть в документе (в нашем случае - синий)
    lower_range = np.array([110, 50, 50])
    upper_range = np.array([130, 255, 255])

    image_mask = cv2.inRange(image, lower_range, upper_range)

    thresh = cv2.Canny(image_mask, 10, 250)
    contours_of_frames, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    return contours_of_frames

def extract_frames(contours_of_frames: np.ndarray) -> list:
    """Поиск рамок на изображении

    :param contours_of_frames: контуры на изображении
    :return: полученные координаты рамок
    """
    frames = []

    # перебираем все найденные контуры в цикле
    for contours_of_frame in contours_of_frames:
        rect = cv2.minAreaRect(contours_of_frame)  # пытаемся вписать прямоугольник
        box = cv2.boxPoints(rect)  # поиск четырех вершин прямоугольника
        box = np.int0(box)  # округление координат
        area = int(rect[1][0] * rect[1][1])  # вычисление площади

        if area > 250:
            frames.append(box[[1, 3]])

    return np.array(frames).tolist()

def save_frames(path_to_image_with_fields: str,
                create_debug_form: bool = False, path_to_blank_image: str = None) -> (dict, np.ndarray):
    """По полученному изображению с нанесенными на него рамками (прямоугольники синего цвета по контурам мест, где
    пользователь будет вводить свои данные) вывести координаты полей, а также изображение с нанесенными на него заново
    рамками для проверки. Полученный в ходе работы данной программы массив значений можно использовать напрямую для
    подачи в текущую версию основного скрипта в качестве sogl{number}_fields.json.

    :param path_to_image_with_fields: путь к изображению с нанесенными рамками, которые выделяют нужные поля
    :param create_debug_form: если True, на загруженное пустое изображение наносятся рамки в соответствии с полученными
                            их координатами. это действие осуществляется для проверки, что все сработало нормально
    :param path_to_blank_image: пусть к исходному пустому изображению без нанесенных рамок
    :return: словарь с координатами полей, а также изображение с нанесенными заново рамками
    """
    image_with_frames = load_image(path_to_image_with_fields)
    image_with_frames = cv2.cvtColor(image_with_frames, cv2.COLOR_BGR2HSV)

    contours_of_frames = extract_contours(image_with_frames)
    frames = extract_frames(contours_of_frames)

    dict_with_values = {str(i): frame for i, frame in enumerate(frames)}

    if create_debug_form:
        template = load_image(path_to_blank_image)  # пустой бланк

        for location in frames:
            y = (location[0][1], location[1][1])
            x = (location[0][0], location[1][0])

            # нанесение рамок на форму по инструкции
            cv2.rectangle(template, (x[0], y[0]), (x[1], y[1]), (0, 255, 0), 3)

        return dict_with_values, template

    return dict_with_values, None

max_stars_repo_name: azarenkovgd/consents-recognition

max_issues_repo_name: azarenkovgd/consents-recognition

max_forks_repo_name: azarenkovgd/consents-recognition


text:
import warnings

import torch
import torch.nn
import scipy
import scipy.special

from numpy import euler_gamma

from ..base import BaseARD
from ...utils.sparsity import SparsityStats

from .base import CplxLinearGaussian, CplxBilinearGaussian
from .base import CplxConv1dGaussian, CplxConv2dGaussian, CplxConv3dGaussian

class ExpiFunction(torch.autograd.Function):
    r"""Pythonic differentiable port of scipy's Exponential Integral Ei.
    $$
        Ei
            \colon \mathbb{R} \to \mathbb{R} \cup \{\pm \infty\}
            \colon x \mapsto \int_{-\infty}^x \tfrac{e^t}{t} dt
        \,. $$

    Notes
    -----
    This may potentially introduce a memory transfer and compute bottleneck
    during the forward pass due to CPU-GPU device switch. Backward pass does
    not suffer from this issue and is computed on-device.
    """
    @staticmethod
    def forward(ctx, x):
        ctx.save_for_backward(x)

        x_cpu = x.data.cpu().numpy()
        output = scipy.special.expi(x_cpu, dtype=x_cpu.dtype)
        return torch.from_numpy(output).to(x.device)

    @staticmethod
    def backward(ctx, grad_output):
        x = ctx.saved_tensors[-1]
        return grad_output * torch.exp(x) / x

torch_expi = ExpiFunction.apply

class RelevanceMixin(SparsityStats):
    __sparsity_ignore__ = ("log_sigma2",)

    def relevance(self, *, threshold, **kwargs):
        """Get the relevance mask based on the threshold."""
        with torch.no_grad():
            return torch.le(self.log_alpha, threshold).to(self.log_alpha)

    def sparsity(self, *, threshold, **kwargs):
        relevance = self.relevance(threshold=threshold)

        weight = self.weight
        n_dropped = float(weight.real.numel()) - float(relevance.sum().item())
        return [(id(weight.real), n_dropped), (id(weight.imag), n_dropped)]

class CplxVDMixin:
    r"""Trait class with kl-divergence penalty of the cplx variational dropout.

    Details
    -------
    This module assumes the standard loss-minimization framework. Hence
    instead of -ve KL divergence for ELBO and log-likelihood maximization,
    this property computes and returns the divergence as is, which implies
    minimization of minus log-likelihood (and, thus, minus ELBO).

    Attributes
    ----------
    penalty : computed torch.Tensor, read-only
        The Kullback-Leibler divergence between the mean field approximate
        complex variational posterior of the weights and the scale-free
        log-uniform complex prior:
        $$
            KL(\mathcal{CN}(w\mid \theta, \alpha \theta \bar{\theta}, 0) \|
                    \tfrac1{\lvert w \rvert^2})
                = 2 \mathbb{E}_{\xi \sim \mathcal{CN}(1, \alpha, 0)}
                    \log{\lvert \xi \rvert}
                  + C - \log \alpha
                = C - \log \alpha - Ei( - \tfrac1{\alpha})
            \,, $$
        where $Ei(x) = \int_{-\infty}^x e^t t^{-1} dt$ is the exponential
        integral. Unlike real-valued variational dropout, this KL divergence
        does not need an approximation, since it can be computed exactly via
        a special function. $Ei(x)$ behaves well on the -ve values, and near
        $0-$. The constant $C$ is fixed to Euler's gamma, so that the divergence
        is +ve.
    """

    @property
    def penalty(self):
        """Exact complex KL divergence."""
        n_log_alpha = - self.log_alpha
        return euler_gamma + n_log_alpha - torch_expi(- torch.exp(n_log_alpha))

class CplxLinearVD(CplxVDMixin, RelevanceMixin, CplxLinearGaussian, BaseARD):
    """Complex-valued linear layer with variational dropout."""
    pass

class CplxBilinearVD(CplxVDMixin, RelevanceMixin, CplxBilinearGaussian, BaseARD):
    """Complex-valued bilinear layer with variational dropout."""
    pass

class CplxConv1dVD(CplxVDMixin, RelevanceMixin, CplxConv1dGaussian, BaseARD):
    """1D complex-valued convolution layer with variational dropout."""
    pass

class CplxConv2dVD(CplxVDMixin, RelevanceMixin, CplxConv2dGaussian, BaseARD):
    """2D complex-valued convolution layer with variational dropout."""
    pass

class CplxConv3dVD(CplxVDMixin, RelevanceMixin, CplxConv3dGaussian, BaseARD):
    """3D complex-valued convolution layer with variational dropout."""
    pass

class CplxLinearARD(object):
    def __new__(cls, in_features, out_features, bias=True):
        warnings.warn("Importing complex-valued Automatic Relevance Determination"
                      " layers (ARD) from `cplxmodule.nn.relevance.complex` has"
                      " been deprecated due to misleading name. Starting with"
                      " version `2021` the `.complex` submodule will export complex-"
                      "valued Variational Dropout (VD) layers only. Please"
                      " import ARD layers from `relevance.ard`.",
                      FutureWarning)

        return CplxLinearVD(in_features, out_features, bias)

class CplxBilinearARD(object):
    def __new__(cls, in1_features, in2_features, out_features, bias=True,
                conjugate=True):
        warnings.warn("Importing complex-valued Automatic Relevance Determination"
                      " layers (ARD) from `cplxmodule.nn.relevance.complex` has"
                      " been deprecated due to misleading name. Starting with"
                      " version `2021` the `.complex` submodule will export complex-"
                      "valued Variational Dropout (VD) layers only. Please"
                      " import ARD layers from `relevance.ard`.",
                      FutureWarning)

        return CplxBilinearVD(in1_features, in2_features, out_features, bias,
                              conjugate)

class CplxConv1dARD(object):
    def __new__(cls, in_channels, out_channels, kernel_size, stride=1,
                padding=0, dilation=1, groups=1,
                bias=True, padding_mode='zeros'):
        warnings.warn("Importing complex-valued Automatic Relevance Determination"
                      " layers (ARD) from `cplxmodule.nn.relevance.complex` has"
                      " been deprecated due to misleading name. Starting with"
                      " version `2021` the `.complex` submodule will export complex-"
                      "valued Variational Dropout (VD) layers only. Please"
                      " import ARD layers from `relevance.ard`.",
                      FutureWarning)

        return CplxConv1dVD(in_channels, out_channels, kernel_size, stride,
                            padding, dilation, groups, bias, padding_mode)

class CplxConv2dARD(object):
    def __new__(cls, in_channels, out_channels, kernel_size, stride=1,
                padding=0, dilation=1, groups=1,
                bias=True, padding_mode='zeros'):
        warnings.warn("Importing complex-valued Automatic Relevance Determination"
                      " layers (ARD) from `cplxmodule.nn.relevance.complex` has"
                      " been deprecated due to misleading name. Starting with"
                      " version `2021` the `.complex` submodule will export complex-"
                      "valued Variational Dropout (VD) layers only. Please"
                      " import ARD layers from `relevance.ard`.",
                      FutureWarning)

        return CplxConv2dVD(in_channels, out_channels, kernel_size, stride,
                            padding, dilation, groups, bias, padding_mode)

reason: from numpy,import scipy


text:
#GENERAL IMPORTS
import cv2
import cv2 as cv
import numpy
import numpy as np
import math
from enum import Enum
import imutils
import argparse
import sys
import time
import logging
from networktables import NetworkTables
from networktables import NetworkTablesInstance
import RPi.GPIO as GPIO

#CAMERA IMPORTS
import picamera
from imutils.video import VideoStream

#CLASS IMPORTS
import VisionProcessing as vp

#Set up controllable LED
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_PIN = 23
GPIO.setup(GPIO_PIN, GPIO.OUT)

#Set up the network table
ntinst = NetworkTablesInstance.getDefault()
#ntinst.startClientTeam(3176)
netTable = NetworkTables.getTable("SmartDashBoard")
#netTable.initialize(server='10.12.34.2') not sure we need this

logging.basicConfig(
    filename="vision.log",
    level=logging.DEBUG,
    format="%(asctime)s:%(levelname)s:%(messages)s"
    )

ap = argparse.ArgumentParser()
ap.add_argument("-p", "--picamera", type=int, default=1, help="whether or not the Raspi camera should be used")
args = vars(ap.parse_args())

#vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(320, 240),framerate=60).start()
vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(640, 480),framerate=60).start()
#vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(640, 480),framerate=90).start()
#vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(1280, 720),framerate=60).start()
#vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(1640, 922),framerate=40).start()
#vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(1920, 1080),framerate=30).start()
#vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(1640, 1232),framerate=40).start()
#vs = VideoStream(usePiCamera=args["picamera"] > 0, resolution=(3280, 2464),framerate=15).start()

time.sleep(2.0)
vs.camera.brightness = 30
vs.camera.contrast = 100
vs.camera.saturation = 100

def daMastaLoop():
    isLightOn = False
    while True:
    #Grab the frame
        frame = vs.read()

        #frame = VisionProcessing(frame)
        cv2.imshow("VP", frame)
        #---------------------#
        #STEP 1: GET TABLE DATA
        #---------------------#
        #netTable =

        #-----------------------------------#
        #STEP 2: MAKE DECISIONS BASED ON DATA
        #-----------------------------------#
        g2 = vp.findG2InFrame(frame)
        if g2 is not None:
            frame = g2.drawFittingOnFrame(frame)
            angle = g2.calcAngle()
            dist = g2.calcRange()
            #print("Ang: ", angle, " Range(in): ", dist[0] / 0.0254)
            print("R1: ", dist[0] / 0.0254, " R2: ", dist[1] / 0.0254)

        key = cv2.waitKey(1) & 0xFF

        if key == ord("l"): #LED
            isLightOn = not isLightOn
        elif key == ord("k"): #Kill
            break

        if isLightOn:
            GPIO.output(GPIO_PIN, GPIO.HIGH)
        else:
           GPIO.output(GPIO_PIN, GPIO.LOW)

        #------------------------#
        #STEP 3: UPDATE TABLE DATA
        #------------------------#
        ntinst.flush()

    GPIO.output(GPIO_PIN, GPIO.LOW)

daMastaLoop()
cv2.destroyAllWindows()
vs.stop()


text:
import xgboost as xgb
import numpy as np
import sys
import json
from collections import OrderedDict
model = xgb.Booster({'nthread': 1})
model.load_model('python/model.bin')
data = json.loads(sys.argv[1], object_pairs_hook=OrderedDict)
new_data = xgb.DMatrix(np.array([list(data.values())], dtype='float64'),
                       feature_names=list(data.keys()))
print('%0.3f' % model.predict(new_data)[0])

max_stars_repo_name: tonydifranco/xgboostjson

max_issues_repo_name: tonydifranco/xgboostjson

max_forks_repo_name: tonydifranco/xgboostjson


text:
# Import GDSII Library
from gdsii.library import Library
from gdsii.elements import *

# Import Custom Modules
import debug_prints as dbg
from polygon import *
from net     import *
from layout  import *

# Other Imports
import time
import sys
import inspect
import copy
import numpy
import multiprocessing as mp
import functools as ft

# Import matplotlib
# import matplotlib.pyplot as plt

# Possible ERROR Codes:
# 1 = Error loading input load_files
# 2 = Unknown GDSII object attributes/attribute types
# 3 = Unhandled feature
# 4 = Usage Error

# ------------------------------------------------------------------
# Critical Net Blockage Metric
# ------------------------------------------------------------------
# Color adjacent layer bitmap by setting bits inside any polygon to 1
def color_bitmap_al(bitmap, offset, poly):
	for row in range(bitmap.shape[0]):
		for col in range(bitmap.shape[1]):
			if poly.is_point_inside(Point(col + offset.x + 0.5, row + offset.y + 0.5)):
				bitmap[row, col] = 1

# Color same layer bitmap by setting bits inside any polygon to 1
def color_bitmap_sl(bitmap, curr_pitch_pt, curr_overlap_pt, end_pitch_pt, nearby_polys, layout, direction):
	num_sites = max(bitmap.shape[0], bitmap.shape[1])
	step_size = layout.net_blockage_step
	sites_ind = 0
	while curr_pitch_pt != end_pitch_pt:
		for poly in nearby_polys:
			if poly.is_point_inside(curr_pitch_pt) or poly.is_point_inside(curr_overlap_pt):
				for i in range(sites_ind, min(sites_ind + layout.net_blockage_step, num_sites)):
					if direction == 'N' or direction == 'S':
						bitmap[0, i] = 1
					elif direction == 'E' or direction == 'W':
						bitmap[i, 0] = 1
					else:
						print "ERROR %s: unknown scan direction (%s)." % (inspect.stack()[0][3], direction)
						sys.exit(4)
				break

		if direction == 'N' or direction == 'S':
			curr_pitch_pt.shift(step_size, 0)
			curr_overlap_pt.shift(step_size, 0)
		elif direction == 'E' or direction == 'W':
			curr_pitch_pt.shift(0, step_size)
			curr_overlap_pt.shift(0, step_size)
		else:
			print "ERROR %s: unknown scan direction (%s)." % (inspect.stack()[0][3], direction)
			sys.exit(4)

		sites_ind += step_size

	return bitmap

# Computes number of bits colored
def bits_colored(bitmap):
	return numpy.count_nonzero(bitmap)

def compute_windows_blocked(bitmap, layout, net_segment, offset, side, num_nearby_polys):
	windows_scanned        = 0
	windows_blocked        = 0
	num_rows               = bitmap.shape[0]
	num_cols               = bitmap.shape[1]
	sl_required_open_width = layout.lef.layers[net_segment.layer_name].rogue_wire_width

	# Get minimum wire spacing rules
	if side == 'T':
		# Get minimum wire width/spacing constraints for the adjacent layer
		min_wire_width      = layout.lef.layers[net_segment.layer_num + 1].min_width_db
		required_open_width = layout.lef.layers[net_segment.layer_num + 1].rogue_wire_width
	elif side == 'B':
		# Get minimum wire width/spacing constraints for the adjacent layer
		min_wire_width      = layout.lef.layers[net_segment.layer_num - 1].min_width_db
		required_open_width = layout.lef.layers[net_segment.layer_num - 1].rogue_wire_width

	# Configure Scan Window
	if side == 'N' or side == 'S':
		scan_window = Window(Point(0,0), sl_required_open_width, 1, 'H')
		windows_scanned_precompute = num_cols - sl_required_open_width + 1
	elif side == 'E' or side == 'W':
		scan_window = Window(Point(0,0), 1, sl_required_open_width, 'V')
		windows_scanned_precompute = num_rows - sl_required_open_width + 1
	elif side == 'T' or side == 'B':
		# Get direction of wire (horizontal or vertical)
		if net_segment.polygon.bbox.get_width() > net_segment.polygon.bbox.get_height():
			# Horizontal Path or Boundary
			if net_segment.polygon.bbox.get_width() > min_wire_width:
				# Wide enough for 1 width of AL wire
				scan_window                = Window(Point(0, 0), required_open_width, num_rows, 'H')
				windows_scanned_precompute = num_cols - required_open_width + 1
			else:
				# Width of entire path segment --> TODO: constrain with via enclosure rules
				scan_window                = Window(Point(0, 0), num_cols, num_rows, 'H')
				windows_scanned_precompute = 1
		elif net_segment.polygon.bbox.get_width() < net_segment.polygon.bbox.get_height():
			# Vertical Path or Boundary
			if net_segment.polygon.bbox.get_height() > min_wire_width:
				# Tall enough for 1 width of AL wire
				scan_window                = Window(Point(0, 0), num_cols, required_open_width, 'V')
				windows_scanned_precompute = num_rows - required_open_width + 1
			else:
				# Height of entire path segment --> TODO: constrain with via enclosure rules
				scan_window                = Window(Point(0, 0), num_cols, num_rows, 'V')
				windows_scanned_precompute = 1
		else:
			# Path or Boundary is a Square --> get direction from LEF file
			routing_direction = layout.lef.layers[net_segment.layer_name].direction
			if routing_direction == "H":
				# Routing Direction is HORIZONTAL
				scan_window                = Window(Point(0, 0), required_open_width, num_rows, 'H')
				windows_scanned_precompute = num_cols - required_open_width + 1
			elif routing_direction == "V":
				# Routing Direction is VERTICAL
				scan_window                = Window(Point(0, 0), num_cols, required_open_width, 'V')
				windows_scanned_precompute = num_rows - required_open_width + 1
			else:
				print "UNSUPPORTED %s: routing direction is not H or V." % (inspect.stack()[0][3])
				sys.exit(3)

	# ---------------------------------------
	# Eearly Termination Optimizations
	# ---------------------------------------
	# CASE #1: If no nearbly polygons, skip calculation and
	# return number of windows that would have been scanned.
	if num_nearby_polys == 0:
		if side == 'T':
			net_segment.unblocked_windows[side].append(Window.from_bbox(net_segment.nearby_al_bbox, scan_window.direction))
		elif side == 'B':
			net_segment.unblocked_windows[side].append(Window.from_bbox(net_segment.nearby_bl_bbox, scan_window.direction))
		elif side == 'N' or side == 'S':
			net_segment.unblocked_windows[side].append(Window(copy.deepcopy(offset), num_cols, 1, 'H'))
		elif side == 'E' or side == 'W':
			net_segment.unblocked_windows[side].append(Window(copy.deepcopy(offset), 1, num_rows, 'V'))
		else:
			print "UNSUPPORTED %s: side to scan is invalid." % (inspect.stack()[0][3])
			sys.exit(3)
		return windows_scanned_precompute, 0
	# ---------------------------------------

	# Keep track of patching points
	prev_window_blocked = True
	unblocked_window    = None

	# print "			Window Start:"
	# scan_window.print_window()
	# print "			Num. Cols/Rows:", num_cols, "/", num_rows

	# Scan bitmap rows
	while scan_window.get_end_pt().y <= num_rows:
		# Reset scan window X position
		scan_window.reset_x_position()

		# Scan bitmap cols
		while scan_window.get_end_pt().x <= num_cols:
			if scan_window.get_bitmap_splice(bitmap).any():
				# Current Window is BLOCKED
				windows_blocked += 1
				prev_window_blocked = True

				# If end of OPEN window, save
				if unblocked_window:
					# Adjust window location to real location on chip
					unblocked_window.offset(offset)

					# Append window to list of unblocked windows
					net_segment.unblocked_windows[side].append(unblocked_window)
					unblocked_window = None

			elif prev_window_blocked:
				# Previous Window is BLOCKED and Current Window is OPEN
				prev_window_blocked = False
				unblocked_window    = Window(scan_window.get_start_pt_copy(), scan_window.width, scan_window.height, scan_window.direction)

			else:
				# Previous Window is OPEN and Current Window is OPEN
				if scan_window.direction == 'H':
					unblocked_window.increase_width(1)
				elif scan_window.direction == 'V':
					unblocked_window.increase_height(1)
				else:
					print "UNSUPPORTED %s: wire patching direction." % (inspect.stack()[0][3], token)
					sys.exit(3)

			windows_scanned += 1
			scan_window.shift_horizontal(1)
		scan_window.shift_vertical(1)

	if unblocked_window:
		# Adjust window location to real location on chip
		unblocked_window.offset(offset)

		# Append window to list of unblocked windows
		net_segment.unblocked_windows[side].append(unblocked_window)
		unblocked_window = None

	# print "			Window End:"
	# scan_window.print_window()
	# print "			Windows Blocked: %d / %d" % (windows_blocked, windows_scanned)
	# print "			Windows Precomputed:  %d" % (windows_scanned_precompute)

	return windows_scanned, windows_blocked

def check_blockage_constrained(layout, net_segment):
	num_same_layer_units_checked = 0
	same_layer_units_blocked     = 0
	num_diff_layer_units_checked = 0
	diff_layer_units_blocked     = 0
	sides_unblocked 			 = []
	sl_min_wire_spacing          = layout.lef.layers[net_segment.layer_name].min_spacing_db - 1
	check_distance               = (layout.lef.layers[net_segment.layer_name].pitch - (0.5 * layout.lef.layers[net_segment.layer_name].width)) * layout.lef.database_units
	# print "		Check Distance (uM):", (layout.lef.layers[net_segment.layer_name].pitch - (0.5 * layout.lef.layers[net_segment.layer_name].width))

	# Scan all 4 perimeter sides to check for blockages
	for direction in ['N', 'E', 'S', 'W', 'T', 'B']:
		if direction == 'N':
			start_scan_coord         = net_segment.polygon.bbox.ll.x - sl_min_wire_spacing
			end_scan_coord           = net_segment.polygon.bbox.ur.x + sl_min_wire_spacing
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ur.y + check_distance
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ur.y + 2
			start_pitch_pt           = Point(start_scan_coord, curr_fixed_coord_pitch)
			curr_pitch_pt            = Point(start_scan_coord, curr_fixed_coord_pitch)
			end_pitch_pt             = Point(end_scan_coord, curr_fixed_coord_pitch)
			curr_overlap_pt          = Point(start_scan_coord, curr_fixed_coord_overlap)
		elif direction == 'E':
			start_scan_coord         = net_segment.polygon.bbox.ll.y - sl_min_wire_spacing
			end_scan_coord           = net_segment.polygon.bbox.ur.y + sl_min_wire_spacing
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ur.x + check_distance
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ur.x + 2
			start_pitch_pt           = Point(curr_fixed_coord_pitch, start_scan_coord)
			curr_pitch_pt            = Point(curr_fixed_coord_pitch, start_scan_coord)
			end_pitch_pt             = Point(curr_fixed_coord_pitch, end_scan_coord)
			curr_overlap_pt          = Point(curr_fixed_coord_overlap, start_scan_coord)
		elif direction == 'S':
			start_scan_coord         = net_segment.polygon.bbox.ll.x - sl_min_wire_spacing
			end_scan_coord           = net_segment.polygon.bbox.ur.x + sl_min_wire_spacing
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ll.y - check_distance
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ll.y - 2
			start_pitch_pt           = Point(start_scan_coord, curr_fixed_coord_pitch)
			curr_pitch_pt            = Point(start_scan_coord, curr_fixed_coord_pitch)
			end_pitch_pt             = Point(end_scan_coord, curr_fixed_coord_pitch)
			curr_overlap_pt          = Point(start_scan_coord, curr_fixed_coord_overlap)
		elif direction == 'W':
			start_scan_coord         = net_segment.polygon.bbox.ll.y - sl_min_wire_spacing
			end_scan_coord           = net_segment.polygon.bbox.ur.y + sl_min_wire_spacing
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ll.x - check_distance
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ll.x - 2
			start_pitch_pt           = Point(curr_fixed_coord_pitch, start_scan_coord)
			curr_pitch_pt            = Point(curr_fixed_coord_pitch, start_scan_coord)
			end_pitch_pt             = Point(curr_fixed_coord_pitch, end_scan_coord)
			curr_overlap_pt          = Point(curr_fixed_coord_overlap, start_scan_coord)
		elif direction != 'T' and direction != 'B':
			print "ERROR %s: unknown scan direction (%s)." % (inspect.stack()[0][3], direction)
			sys.exit(4)

		# Analyze blockage along the perimeter on the same layer
		if direction != 'T' and direction != 'B':
			curr_scan_coord    = start_scan_coord
			num_points_to_scan = (float(end_scan_coord - curr_scan_coord) / float(layout.net_blockage_step))
			# print "		Checking %.2f units along %s edge (%d/%f units/microns away)..." % (num_points_to_scan, direction, check_distance, float(check_distance / layout.lef.database_units))
			# print "		Start Scan Coord = %d; End Scan Coord = %d; Num Points to Scan = %d" % (curr_scan_coord, end_scan_coord, num_points_to_scan)

			# Create Bitmap
			if direction == 'N' or direction == 'S':
				sl_bitmap = numpy.zeros(shape=(1, end_scan_coord - start_scan_coord), dtype=bool)
			elif direction == 'E' or direction == 'W':
				sl_bitmap = numpy.zeros(shape=(end_scan_coord - start_scan_coord, 1), dtype=bool)

			# Color Bitmap
			sl_bitmap = color_bitmap_sl(sl_bitmap, curr_pitch_pt, curr_overlap_pt, end_pitch_pt, net_segment.nearby_sl_polygons, layout, direction)

			# Calculate windows blocked
			windows_scanned, windows_blocked = compute_windows_blocked(sl_bitmap, layout, net_segment, start_pitch_pt, direction, len(net_segment.nearby_sl_polygons))
			# windows_scanned, windows_blocked = compute_windows_blocked(sl_bitmap, layout, net_segment, start_pitch_pt, direction, 0)
			num_same_layer_units_checked     += windows_scanned
			same_layer_units_blocked         += windows_blocked

			# Record Sides Unblocked
			if windows_blocked < windows_scanned:
				sides_unblocked.append(direction)

			# print "		Windows blocked %d/%d on %s edge" % (windows_blocked, windows_scanned, direction)

		# Analyze blockage along the adjacent layers (top and bottom)
		else:
			# Only analyze if top/bottom adjacent layer is routable
			if   direction == 'T' and (net_segment.layer_num < layout.lef.top_routing_layer_num):
				# Get nearby polygons to analyze
				nearby_polys = net_segment.nearby_al_polygons
				nearby_bbox  = net_segment.nearby_al_bbox

			elif direction == 'B' and (net_segment.layer_num > layout.lef.bottom_routing_layer_num):
				# Get nearby polygons to analyze
				nearby_polys = net_segment.nearby_bl_polygons
				nearby_bbox  = net_segment.nearby_bl_bbox

			else:
				continue

			# Create bitmap
			al_bitmap = numpy.zeros(shape=(nearby_bbox.get_height(), nearby_bbox.get_width()), dtype=bool)
			# print "		Checking (%d) nearby polygons along %s side (GDSII Layer:) ..." % (len(nearby_polys), direction)

			# Color the bitmap
			for poly in nearby_polys:
				color_bitmap_al(al_bitmap, nearby_bbox.ll, poly)

			# Calculate windows blocked
			windows_scanned, windows_blocked = compute_windows_blocked(al_bitmap, layout, net_segment, nearby_bbox.ll, direction, len(nearby_polys))
			# windows_scanned, windows_blocked = compute_windows_blocked(al_bitmap, layout, net_segment, nearby_bbox.ll, direction, 0)

			# Updated sides unblocked
			if windows_blocked < windows_scanned:
				sides_unblocked.append(direction)

			# Updated different layer blockage stats
			num_diff_layer_units_checked += windows_scanned
			diff_layer_units_blocked     += windows_blocked

			# Free bitmap memory
			del al_bitmap

	return num_same_layer_units_checked, same_layer_units_blocked, sides_unblocked, num_diff_layer_units_checked, diff_layer_units_blocked

def check_blockage(layout, net_segment):
	num_same_layer_units_checked = 0
	same_layer_units_blocked     = 0
	num_diff_layer_units_checked = 0
	diff_layer_units_blocked     = 0
	sides_unblocked 			 = []
	check_distance               = (layout.lef.layers[net_segment.layer_name].pitch - (0.5 * layout.lef.layers[net_segment.layer_name].width)) * layout.lef.database_units
	# print "		Check Distance (uM):", (layout.lef.layers[net_segment.layer_name].pitch - (0.5 * layout.lef.layers[net_segment.layer_name].width))

	# Scan all 4 perimeter sides to check for blockages
	for direction in ['N', 'E', 'S', 'W', 'T', 'B']:
		if direction == 'N':
			curr_scan_coord          = net_segment.polygon.bbox.ll.x
			end_scan_coord           = net_segment.polygon.bbox.ur.x
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ur.y + 1
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ur.y + check_distance
		elif direction == 'E':
			curr_scan_coord          = net_segment.polygon.bbox.ll.y
			end_scan_coord           = net_segment.polygon.bbox.ur.y
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ur.x + 1
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ur.x + check_distance
		elif direction == 'S':
			curr_scan_coord          = net_segment.polygon.bbox.ll.x
			end_scan_coord           = net_segment.polygon.bbox.ur.x
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ll.y - 1
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ll.y - check_distance
		elif direction == 'W':
			curr_scan_coord          = net_segment.polygon.bbox.ll.y
			end_scan_coord           = net_segment.polygon.bbox.ur.y
			curr_fixed_coord_overlap = net_segment.polygon.bbox.ll.x - 1
			curr_fixed_coord_pitch   = net_segment.polygon.bbox.ll.x - check_distance
		elif direction != 'T' and direction != 'B':
			print "ERROR %s: unknown scan direction (%s)." % (inspect.stack()[0][3], direction)
			sys.exit(4)

		# Analyze blockage along the perimeter on the same layer
		if direction != 'T' and direction != 'B':
			num_points_to_scan      = (float(end_scan_coord - curr_scan_coord) / float(layout.net_blockage_step))
			same_side_units_blocked = 0
			# print "		Checking %.2f units along %s edge (%d/%f units/microns away)..." % (num_points_to_scan, direction, check_distance, float(check_distance / layout.lef.database_units))
			# print "		Start Scan Coord = %d; End Scan Coord = %d; Num Points to Scan = %d" % (curr_scan_coord, end_scan_coord, num_points_to_scan)
			while curr_scan_coord < end_scan_coord:
				for poly in net_segment.polygon.nearby_sl_polygons:
					if direction == 'N' or direction == 'S':
						if poly.is_point_inside(Point(curr_scan_coord, curr_fixed_coord_pitch)) or poly.is_point_inside(Point(curr_scan_coord, curr_fixed_coord_overlap)):
							same_layer_units_blocked += 1
							same_side_units_blocked  += 1
							break
					else:
						if poly.is_point_inside(Point(curr_fixed_coord_pitch, curr_scan_coord)) or poly.is_point_inside(Point(curr_fixed_coord_overlap, curr_scan_coord)):
							same_layer_units_blocked += 1
							same_side_units_blocked  += 1
							break
				num_same_layer_units_checked += 1
				curr_scan_coord              += layout.net_blockage_step
				if same_side_units_blocked < num_points_to_scan:
					sides_unblocked.append(direction)

		# Analyze blockage along the adjacent layers (top and bottom)
		else:
			# Create bitmap of net segment
			net_segment_bitmap = numpy.zeros(shape=(net_segment.polygon.bbox.get_height(), net_segment.polygon.bbox.get_width()), data_type=bool)

			# Choose nearby polygons to analyze
			if direction == 'T' and (net_segment.layer_num < layout.lef.top_routing_layer_num):
				nearby_polys = net_segment.nearby_al_polygons
				# print "		Checking (%d) nearby polygons along %s side (Layer: %d) ..." % (len(nearby_polys), direction, net_segment.layer_num + 1)
			elif direction == 'B' and (net_segment.layer_num > layout.lef.bottom_routing_layer_num):
				nearby_polys = net_segment.nearby_bl_polygons
				# print "		Checking (%d) nearby polygons along %s side (Layer: %d) ..." % (len(nearby_polys), direction, net_segment.layer_num - 1)
			else:
				continue

			# Color the bitmap
			for poly in nearby_polys:
				# First check if bounding boxes overlap
				if net_segment.polygon.bbox.overlaps_bbox(poly.bbox):
					color_bitmap_al(net_segment_bitmap, net_segment.polygon.bbox.ll, poly)

			# Calculate colored area
			diff_layer_units_blocked     += bits_colored(net_segment_bitmap)
			num_diff_layer_units_checked += net_segment.polygon.get_area()

			# Free bitmap memory
			del net_segment_bitmap

	return num_same_layer_units_checked, same_layer_units_blocked, sides_unblocked, num_diff_layer_units_checked, diff_layer_units_blocked

def launch_net_blockage(layout, net):
	for net_segment in net.segments:
		# Start Computation Timer
		net_segment.nb_compute_time = time.time()

		# Compute Net Blockage for each Net Segment
		if layout.net_blockage_type == 1:
			num_same_layer_units_checked, \
			same_layer_units_blocked, \
			sides_unblocked, \
			num_diff_layer_units_checked, \
			diff_layer_units_blocked = check_blockage_constrained(layout, net_segment)
		else:
			num_same_layer_units_checked, \
			same_layer_units_blocked, \
			sides_unblocked, \
			num_diff_layer_units_checked, \
			diff_layer_units_blocked = check_blockage(layout, net_segment)

		# Update Net Segment Properties
		net_segment.same_layer_units_blocked = same_layer_units_blocked
		net_segment.diff_layer_units_blocked = diff_layer_units_blocked
		net_segment.same_layer_units_checked = num_same_layer_units_checked
		net_segment.diff_layer_units_checked = num_diff_layer_units_checked
		net_segment.sides_unblocked          = sides_unblocked

		# End Computation Timer
		net_segment.nb_compute_time = time.time() - net_segment.nb_compute_time

	return net

def print_net_segment_blockage_info(net_segment, segment_num, verbose, layout):
	# Get GDSII element type
	if isinstance(net_segment.polygon.gdsii_element, Path):
		gdsii_element_type = "Path"
	elif isinstance(net_segment.polygon.gdsii_element, Boundary):
		gdsii_element_type = "Boundary"
	else:
		gdsii_element_type = "Unknown"
	# Report Path Segment Condition
	if verbose:
		print "	Analyzing Net Segment", segment_num
		print "		Layer:                    ", net_segment.layer_num
		print "		GDSII Element:            ", gdsii_element_type
		print "		Perimeter (dbu):          ", net_segment.polygon.bbox.get_perimeter()
		print "		Step Size (dbu):          ", layout.net_blockage_step
		print "		Pitch (uM):               ", layout.lef.layers[net_segment.layer_name].pitch
		print "		Default Width (uM):       ", layout.lef.layers[net_segment.layer_name].width
		print "		Min SL Width (uM):        ", layout.lef.layers[net_segment.layer_num].min_width
		if net_segment.layer_num < layout.lef.top_routing_layer_num:
			print "		Min TL Width (uM):        ", layout.lef.layers[net_segment.layer_num + 1].min_width
		if net_segment.layer_num > layout.lef.bottom_routing_layer_num:
			print "		Min BL Width (uM):        ", layout.lef.layers[net_segment.layer_num - 1].min_width
		print "		Top and Bottom Area (dbu):", (net_segment.polygon.get_area() * 2)
		print "		BBox (M-Units):           ", net_segment.polygon.bbox.get_bbox_as_list()
		print "		Nearby SL BBox (M-Units): ", net_segment.nearby_sl_bbox.get_bbox_as_list()
		if net_segment.layer_num < layout.lef.top_routing_layer_num:
			print "		Nearby TL BBox (M-Units): ", net_segment.nearby_al_bbox.get_bbox_as_list()
		if net_segment.layer_num > layout.lef.bottom_routing_layer_num:
			print "		Nearby BL BBox (M-Units): ", net_segment.nearby_bl_bbox.get_bbox_as_list()
		print "		Num. Nearby Polygons:     ", len(net_segment.nearby_al_polygons) + len(net_segment.nearby_bl_polygons) + len(net_segment.nearby_sl_polygons)
		if gdsii_element_type == "Path":
			print "		Klayout Query:       "
			print "			paths on layer %d/%d of cell %s where" % (net_segment.polygon.gdsii_element.layer, net_segment.polygon.gdsii_element.data_type, layout.top_level_name)
			print "			shape.path.bbox.left==%d &&"  % (net_segment.polygon.bbox.ll.x)
			print "			shape.path.bbox.right==%d &&" % (net_segment.polygon.bbox.ur.x)
			print "			shape.path.bbox.top==%d &&"   % (net_segment.polygon.bbox.ur.y)
			print "			shape.path.bbox.bottom==%d"   % (net_segment.polygon.bbox.ll.y)
		elif gdsii_element_type == "Boundary":
			print "		Klayout Query:       "
			print "			boxes on layer %d/%d of cell %s where" % (net_segment.polygon.gdsii_element.layer, net_segment.polygon.gdsii_element.data_type, layout.top_level_name)
			print "			shape.box.left==%d &&"  % (net_segment.polygon.bbox.ll.x)
			print "			shape.box.right==%d &&" % (net_segment.polygon.bbox.ur.x)
			print "			shape.box.top==%d &&"   % (net_segment.polygon.bbox.ur.y)
			print "			shape.box.bottom==%d"   % (net_segment.polygon.bbox.ll.y)

		if net_segment.sides_unblocked:
			print "		Sides Unblocked:", net_segment.sides_unblocked
		print "		Perimeter Units Blocked:  %d / %d" % (net_segment.same_layer_units_blocked, net_segment.same_layer_units_checked)
		print "		Top/Bottom Units Blocked: %d / %d" % (net_segment.diff_layer_units_blocked, net_segment.diff_layer_units_checked)
		print "		Done - Time Elapsed:", net_segment.nb_compute_time, "seconds."
		print "		----------------------------------------------"

def analyze_critical_net_blockage(layout, verbose):
	total_perimeter_units     = 0
	total_top_bottom_area     = 0
	total_same_layer_blockage = 0
	total_diff_layer_blockage = 0

	# Extract all GDSII elements near security-critical nets
	layout.extract_nearby_polygons()
	# layout.extract_nearby_polygons_parallel()

	# Distribute Workload Among Multiple Processes
	worker_pool                   = mp.Pool(processes=layout.num_processes)
	analyze_critical_nets_partial = ft.partial(launch_net_blockage, layout)
	layout.critical_nets          = worker_pool.map(analyze_critical_nets_partial, layout.critical_nets)

	# Print/Aggregate Detailed Results
	for net in layout.critical_nets:
		print "Analyzing Net: ", net.fullname
		segment_num = 1

		for net_segment in net.segments:
			# Print Blockage Results
			print_net_segment_blockage_info(net_segment, segment_num, verbose, layout)

			# Aggregate Blockage Results
			total_same_layer_blockage += net_segment.same_layer_units_blocked
			total_diff_layer_blockage += net_segment.diff_layer_units_blocked
			total_perimeter_units     += net_segment.same_layer_units_checked
			total_top_bottom_area     += net_segment.diff_layer_units_checked

			segment_num += 1

	# Calculate raw and weighted blockage percentages.
	# Weighted accounts for area vs. perimeter blockage
	# print "Total Same Layer Blockage:", total_same_layer_blockage
	# print "Total Perimeter Units    :", total_perimeter_units
	# print "Total Diff Layer Blockage:", total_diff_layer_blockage
	# print "Total Top/Bottom Area    :", total_top_bottom_area
	perimeter_blockage_percentage  = (float(total_same_layer_blockage) / float(total_perimeter_units)) * 100.0
	top_bottom_blockage_percentage = (float(total_diff_layer_blockage) / float(total_top_bottom_area)) * 100.0
	raw_blockage_percentage        = (float(total_same_layer_blockage + total_diff_layer_blockage) / float(total_perimeter_units + total_top_bottom_area)) * 100.0
	weighted_blockage_percentage   = (((float(total_same_layer_blockage) / float(total_perimeter_units)) * float(4.0/6.0)) + ((float(total_diff_layer_blockage) / float(total_top_bottom_area)) * float(2.0/6.0))) * 100.0

	# Print calculations
	print "Perimeter Blockage Percentage:  %4.2f%%" % (perimeter_blockage_percentage)
	print "Top/Bottom Blockage Percentage: %4.2f%%" % (top_bottom_blockage_percentage)
	print "Raw Blockage Percentage:        %4.2f%%" % (raw_blockage_percentage)
	print "Weighted Blockage Percentage:   %4.2f%%" % (weighted_blockage_percentage)

	# Set completed flag
	layout.net_blockage_done = True