text:
import argparse, os, sys

from random import randint

# imports and basic notebook setup
from cStringIO import StringIO
import numpy as np
import scipy.ndimage as nd
import PIL.Image
from IPython.display import clear_output, Image, display
from google.protobuf import text_format

import caffe

import os

# If your GPU supports CUDA and Caffe was built with CUDA support,
# uncomment the following to run Caffe operations on the GPU.
# caffe.set_mode_gpu()
# caffe.set_device(0) # select GPU device if multiple devices exist

def output_path():
    """ Create an output filename: look into folder dreams,
        return lowest INTEGER.jpg with leading zeros, e.g. 00020.jpg """
    # faster with sort

    index=0
    output_file = "dreams/%06d.jpg"%index

    while os.path.exists(output_file):
        index += 1
        output_file = "dreams/%06d.jpg"%index

    return output_file

def create_net(model_file):
    net_fn = os.path.join(os.path.split(model_file)[0], 'deploy.prototxt')
    param_fn = model_file

    # Patching model to be able to compute gradients.
    # Note that you can also manually add "force_backward: true" line to "deploy.prototxt".
    model = caffe.io.caffe_pb2.NetParameter()
    text_format.Merge(open(net_fn).read(), model)
    model.force_backward = True
    open('tmp.prototxt', 'w').write(str(model))

    net = caffe.Classifier('tmp.prototxt', param_fn,
                           mean = np.float32([104.0, 116.0, 122.0]), # ImageNet mean, training set dependent
                           channel_swap = (2,1,0)) # the reference model has channels in BGR order instead of RGB
    return net

# a couple of utility functions for converting to and from Caffe's input image layout
def preprocess(net, img):
    return np.float32(np.rollaxis(img, 2)[::-1]) - net.transformer.mean['data']

def deprocess(net, img):
    return np.dstack((img + net.transformer.mean['data'])[::-1])

# regular, non-guided objective
def objective_L2(dst):
    dst.diff[:] = dst.data

class Dreamer(object):
    def __init__(self, net, source_path, iterations, end, guide_path, octaves):
        self.img = np.float32(PIL.Image.open(source_path))
        self.net = net
        self.iterations = iterations
        self.objective = objective_L2
        self.octave_n = octaves

        self.end = end

        if guide_path:
            self.guide_features = self.create_guide(guide_path)
            self.objective = self.objective_guide

    # make this a product of a generator function with guide_features in its scope
    def objective_guide(self, dst):
        x = dst.data[0].copy()
        # guide_features is global here
        y = self.guide_features
        ch = x.shape[0]
        x = x.reshape(ch,-1)
        y = y.reshape(ch,-1)
        A = x.T.dot(y) # compute the matrix of dot-products with guide features
        dst.diff[0].reshape(ch,-1)[:] = y[:,A.argmax(1)] # select ones that match best

    # use self.end instead of end
    def create_guide(self, guide_path):
        guide = np.float32(PIL.Image.open(guide_path))
        h, w = guide.shape[:2]
        src, dst = net.blobs['data'], net.blobs[self.end]
        src.reshape(1,3,h,w)
        src.data[0] = preprocess(net, guide)
        self.net.forward(end=self.end)

        return dst.data[0].copy()

    def iterated_dream(self):
        self.net.blobs.keys()

        frame = self.img

        h, w = frame.shape[:2]
        s = 0.05 # scale coefficient

        for i in xrange(self.iterations):
            if self.end:
                frame = self.deepdream(frame, end=self.end, octave_n=self.octave_n)
            else:
                frame = self.deepdream(frame, octave_n=self.octave_n)

            PIL.Image.fromarray(np.uint8(frame)).save(output_path())
            frame = nd.affine_transform(frame, [1-s,1-s,1], [h*s/2,w*s/2,0], order=1)

    def make_step(self, step_size=1.5, end='inception_4c/output',
                  jitter=32, clip=True):
        """Basic gradient ascent step."""

        src = self.net.blobs['data'] # input image is stored in Net's 'data' blob
        dst = self.net.blobs[end]

        ox, oy = np.random.randint(-jitter, jitter+1, 2)
        src.data[0] = np.roll(np.roll(src.data[0], ox, -1), oy, -2) # apply jitter shift

        self.net.forward(end=end)
        self.objective(dst)  # specify the optimization objective
        self.net.backward(start=end)
        g = src.diff[0]
        # apply normalized ascent step to the input image
        src.data[:] += step_size/np.abs(g).mean() * g

        src.data[0] = np.roll(np.roll(src.data[0], -ox, -1), -oy, -2) # unshift image

        if clip:
            bias = self.net.transformer.mean['data']
            src.data[:] = np.clip(src.data, -bias, 255-bias)

    def deepdream(self, base_img, iter_n=10, octave_n=4, octave_scale=1.4,
                  end='inception_4c/output', clip=True, **step_params):
        # prepare base images for all octaves
        octaves = [preprocess(self.net, base_img)]
        for i in xrange(octave_n-1):
            octaves.append(nd.zoom(octaves[-1], (1, 1.0/octave_scale,1.0/octave_scale), order=1))

        src = self.net.blobs['data']
        detail = np.zeros_like(octaves[-1]) # allocate image for network-produced details
        for octave, octave_base in enumerate(octaves[::-1]):
            h, w = octave_base.shape[-2:]
            if octave > 0:
                # upscale details from the previous octave
                h1, w1 = detail.shape[-2:]
                detail = nd.zoom(detail, (1, 1.0*h/h1,1.0*w/w1), order=1)

            src.reshape(1,3,h,w) # resize the network's input image size
            src.data[0] = octave_base+detail
            for i in xrange(iter_n):
                self.make_step(end=end, clip=clip, **step_params)

                # visualization
                vis = deprocess(self.net, src.data[0])
                if not clip: # adjust image contrast if clipping is disabled
                    vis = vis*(255.0/np.percentile(vis, 99.98))
                # is octave, i the depth?
                print octave, i, end, vis.shape
                clear_output(wait=True)

            # extract details produced on the current octave
            detail = src.data[0]-octave_base
        # returning the resulting image
        return deprocess(self.net, src.data[0])

def parse_arguments(sysargs):
    """ Setup the command line options. """

    description = ''' deepdreaming.py is a handler script to simplify the usage
        of Googles DeepDream algorithm. The DeepDream algorithm takes an image
        as input and runs an overexpressed pattern recognition in form of
        a convolutional neural network over it.
        See the original Googleresearch blog post
        http://googleresearch.blogspot.ch/2015/06/inceptionism-going-deeper-into-neural.html
        for more information or follow this
        http://www.knight-of-pi.org/installing-the-google-deepdream-software/
        tutorial for installing DeepDream on Ubuntu.
        Try guided dreams with the options -g FILE and -d 2 or shallow dreams
        with the options -d 2 -t 5.'''

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('-s', '--source', nargs='?', const='sky_1024.jpg',
                                     default='sky_1024.jpg', help='input filename')
    parser.add_argument('-g', '--guide', nargs='?', default=None,
                                    help='Target for guided dreams')
    parser.add_argument('-i', '--iterations', nargs='?', type=int, const=1,
                                     default=1, help='Number of iterations')
    parser.add_argument('-d', '--depth', nargs='?', metavar='int', type=int,
                                    choices=xrange(1, 10),  const=5, default=5,
                                    help='Depth of the dream as an value between 1 and 10')
    parser.add_argument('-t', '--type', nargs='?', metavar='int', type=int,
                                    choices=xrange(1, 10),
                                    const=4, default=4, help='Layer type as an value between 1 and 6')
    parser.add_argument('-o', '--octaves', nargs='?', metavar='int', type=int,
                                         choices=xrange(1, 12),
                                         const=5, default=5,
                                         help='The number of scales the algorithm is applied to')
    parser.add_argument('-r', '--random', action='store_true',
                                         help='Overwrite depth, layer type and octave with random values ')
    #parser.add_argument('-m', '--model', nargs='?', metavar='int', type=int,
    #                                choices=xrange(1, 6), help='model 1..5',
    #                                const=1, default=1)

    return parser.parse_args(sysargs)

if __name__ == "__main__":

    args = parse_arguments(sys.argv[1:])

    models_base = '../caffe/models'
    net = create_net(os.path.join(models_base, 'bvlc_googlenet/bvlc_googlenet.caffemodel'))

    # model selection turned off since i found no way of producing good
    # dreams with different models
    '''
    models = ('bvlc_googlenet/bvlc_googlenet.caffemodel',
                    'bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',
                    'bvlc_reference_rcnn_ilsvrc13/bvlc_reference_rcnn_ilsvrc13.caffemodel',
                    'finetune_flickr_style/finetune_flickr_style.caffemodel',
                    'bvlc_alexnet/bvlc_alexnet.caffemodel')

    net = create_net(os.path.join(models_base, models[args.model-1]))
    '''

    numbering = ['3a', '3b', '4a', '4b', '4c', '4d', '4e', '5a', '5b']
    layer_types = ['1x1', '3x3', '5x5', 'output', '5x5_reduce', '3x3_reduce']

    layer = 'inception_' + numbering[args.depth-1] + '/' + layer_types[args.type-1]
    octaves = args.octaves

    # overwrite octaves and layer with random values
    if args.random == True:
        octaves = randint(1, 11)
        layer = ('inception_' + numbering[randint(0, len(numbering)-1)] + '/' +
                      layer_types[randint(0, len(layer_types)-1)])

    dreamer = Dreamer(net=net, source_path=args.source,
                                   iterations=args.iterations, end=layer,
                                   guide_path=args.guide, octaves=octaves)
    dreamer.iterated_dream()

max_stars_repo_name: JoBergs/deepdream

max_issues_repo_name: JoBergs/deepdream

max_forks_repo_name: JoBergs/deepdream

reason: import numpy,import scipy


text:
# -*- coding: utf-8 -*-
from __future__ import division
import numpy
import PyAstronomy.funcFit as fuf
from PyAstronomy.pyaC import pyaErrors as PE
from PyAstronomy.modelSuite.XTran import _ZList

# Check whether mpmath and/or the elliptical integrals from the boost library
# can be imported.

mpmath_imported = False
boostEll_imported = False

try:
    import mpmath
    mpmath_imported = True
except ImportError:
    mpmath_imported = False

try:
    import ell
    boostEll_imported = True
except ImportError:
    boostEll_imported = False

class _Case:
    """
    *Case-designation* according to Pal 2008 Table 1.

    Parameters
    ----------
    s : int
        Step identifier in Pal 2008 Table 1.
    c : string
        Case identifier in Pal 2008 Table 1.
    """

    def __init__(self, s, c):
        self.step = s
        self.case = c

    def _str_(self):
        return "Step: " + str(self.step) + "  case: " + str(self.case)

class PalLC(_ZList, fuf.OneDFit):
    """
    Calculate and fit analytical transit light-curves using the formulae
    provided by Pal 2008.

    This class uses a **circular planetary orbit**.

    .. note :: The **evaluation of elliptical integrals** is essential
               in calculating the transit model. While both
               the *mpmath* module and the *Boost* libraries implement
               those integrals, it is the Boost library, which
               evaluates them by far more quickly. Yet, the support
               for Boost has to be added manually.

    *Model parameters*:
      - `p` - Radius ratio between planet and star.
      - `a` - Semi-major axis of planetary orbit [stellar radii].
      - `i` - Inclination of orbit in degrees (90 deg is *edge on* view).
      - `linLib` - Linear limb-darkening coefficient.
      - `quadLimb` - Quadratic limb-darkening coefficient.
      - `T0` - Time offset of transit center.
      - `per` - Period of planetary orbit.
      - `b` - Describes the flux ratio between a stellar companion and the main star (default is 0).

    This class inherits the functionality of funcFit's OneDFit object.
    You can, therefore, set and get the parameter using the brackets:
      e.g., pallc["p"] = 0.12345

    .. warning::
        Time units have to be consistent.
    """

    def __init__(self):
        _ZList.__init__(self, "circular")
        fuf.OneDFit.__init__(
            self, ["p", "a", "i", "linLimb", "quadLimb", "T0", "per", "b"])
        self.freeze(["p", "a", "i", "linLimb", "quadLimb", "T0", "per", "b"])
        self.setRootName("PalCirc08")

        self._zlist = None

        if (not mpmath_imported) and (not boostEll_imported):
            raise(PE.PyARequiredImport("Neither mpmath nor the elliptical integrals from the boost library could be found!",
                                       where="PalLC::__init__", solution="Install mpmath or make Boost library available (more complicated - see documentation)"))
        self.useBoost = False
        if boostEll_imported:
            self.useBoost = True
            self.ell = ell.ell()

    def _selectCases(self):
        """
        Determines which 'case' (according to Pal '08, Table 1) must be
        attributed to an individual value of 'z' and returns a
        list of 'Case' class objects.
        """

        result = []
        p = self["p"]
        for z in self._zlist:
            if z == 0.0 and p < 1.0:
                result.append(_Case(1, 'A'))
                continue
            if z <= p - 1.0:
                result.append(_Case(2, 'Ag'))
                continue
            if z < p and z < (1.0 - p):
                result.append(_Case(3, 'B'))
                continue
            if z < p and z == 1.0 - p:
                result.append(_Case(4, 'Bt'))
                continue
            if z < p:
                result.append(_Case(5, 'Bg'))
                continue
            if z == p and z < 1.0 - p:
                result.append(_Case(6, 'C'))
                continue
            if z == p and z == 0.5:
                result.append(_Case(7, 'Ct'))
                continue
            if z == p:
                result.append(_Case(8, 'Cg'))
                continue
            if z < 1.0 - p:
                result.append(_Case(9, 'D'))
                continue
            if z == (1.0 - p):
                result.append(_Case(10, 'E'))
                continue
            if z < 1.0 + p:
                result.append(_Case(11, 'F'))
                continue
            # This means that there is no occultation at all
            result.append(_Case(12, 'G'))
        return result

    def _ci(self, z, a):
        return 2 / (9.0 * numpy.pi * numpy.sqrt(1.0 - a))

    def _cik(self, z, a, b):
        return 1.0 - 5.0 * z**2.0 + self["p"]**2.0 + a * b

    def _cie(self, z, a):
        return (z**2.0 + 7 * self["p"]**2.0 - 4.0) * (1.0 - a)

    def _cipi(self, z):
        return -3.0 * (self["p"] + z) / (self["p"] - z)

    def _cg(self, z):
        return 1.0 / (9.0 * numpy.pi * numpy.sqrt(self["p"] * z))

    def _cgk(self, z):
        return 3.0 - 6.0 * (1.0 - self["p"] * self["p"])**2.0 - 2.0 * self["p"] * z * (z * z + 7 * self["p"] * self["p"] - 4.0 + 5.0 * self["p"] * z)

    def _cge(self, z):
        return 4.0 * self["p"] * z * (z * z + 7.0 * self["p"] * self["p"] - 4.0)

    def _cgpi(self, z):
        return -3.0 * (self["p"] + z) / (self["p"] - z)

    def _ti(self, z):
        phat = numpy.sqrt(self["p"] * (1.0 - self["p"]))
        return 2.0 / (3.0 * numpy.pi) * numpy.arccos(1.0 - 2.0 * self["p"]) - 4.0 / (9.0 * numpy.pi) * (3.0 + 2.0 * self["p"] - 8.0 * self["p"]**2.0) * phat

    def _g0(self, z):
        k0 = numpy.arccos(
            (self["p"]**2.0 + z**2.0 - 1.0) / (2.0 * self["p"] * z))
        k1 = numpy.arccos((z**2.0 + 1.0 - self["p"]**2.0) / (2.0 * z))
        return (self["p"] * self["p"] * k0 + k1 - numpy.sqrt(z * z - 0.25 * (1 + z * z - self["p"] * self["p"])**2.0)) / numpy.pi

    def _g2(self, z, a, b):
        k0 = numpy.arccos(
            (self["p"]**2.0 + z**2.0 - 1.0) / (2.0 * self["p"] * z))
        k1 = numpy.arccos((z**2.0 + 1.0 - self["p"]**2.0) / (2.0 * z))
        return (k1 + self["p"] * self["p"] * (self["p"] * self["p"] + 2.0 * z * z) * k0 - 0.25 * (1.0 + 5.0 * self["p"] * self["p"] + z * z) * numpy.sqrt((1.0 - a) * (b - 1.0))) / (2.0 * numpy.pi)

    def _returnCoeff(self, step, z):
        a = (self["p"] - z) * (self["p"] - z)
        b = (self["p"] + z) * (self["p"] + z)
        if (step == 1):
            pprime = numpy.sqrt(1.0 - self["p"]**2.0)
            f0 = self["p"]**2.0
            f1 = 2.0 / 3.0 * (1.0 - pprime**3.0)
            fk = 0.0
            fe = 0.0
            fpi = 0.0
            f2 = 0.5 * self["p"]**4.0
            k = 0.0
            n = 0.0
        elif (step == 2):
            f0 = 1.0
            f1 = 2.0 / 3.0
            fk = 0.0
            fe = 0.0
            fpi = 0.0
            f2 = 0.5
            k = 0.0
            n = 0.0
        elif (step == 3):
            f0 = self["p"]**2.0
            f1 = 2.0 / 3.0
            fk = self._ci(z, a) * self._cik(z, a, b)
            fe = self._ci(z, a) * self._cie(z, a)
            fpi = self._ci(z, a) * self._cipi(z)
            f2 = 0.5 * self["p"]**2.0 * (self["p"]**2.0 + 2.0 * z**2.0)
            k = numpy.sqrt((4.0 * self["p"] * z) / (1.0 - a))
            n = -4.0 * self["p"] * z / a
        elif (step == 4):
            f0 = self["p"]**2.0
            f1 = self._ti(z)
            fk = 0.0
            fe = 0.0
            fpi = 0.0
            f2 = 0.5 * self["p"]**2.0 * (self["p"]**2.0 + 2.0 * z**2.0)
            k = 0.0
            n = 0.0
        elif (step == 5):
            f0 = self._g0(z)
            f1 = 2.0 / 3.0
            fk = self._cg(z) * self._cgk(z)
            fe = self._cg(z) * self._cge(z)
            fpi = self._cg(z) * self._cgpi(z)
            f2 = self._g2(z, a, b)
            k = numpy.sqrt((1.0 - a) / (4.0 * self["p"] * z))
            n = (a - 1.0) / a
        elif (step == 6):
            f0 = self["p"]**2.0
            f1 = 1.0 / 3.0
            fk = 2.0 / (9.0 * numpy.pi) * (1.0 - 4.0 * self["p"]**2.0)
            fe = 8.0 / (9.0 * numpy.pi) * (2.0 * self["p"]**2.0 - 1.0)
            fpi = 0.0
            f2 = 3.0 / 2.0 * self["p"]**4.0
            k = 2 * self["p"]
            n = 0.0
        elif (step == 7):
            f0 = 0.25
            f1 = 1.0 / 3.0 - 4.0 / (9.0 * numpy.pi)
            fk = 0.0
            fe = 0.0
            fpi = 0.0
            f2 = 3.0 / 32.0
            k = 0.0
            n = 0.0
        elif (step == 8):
            f0 = self._g0(z)
            f1 = 1.0 / 3.0
            fk = -1.0 / (9.0 * numpy.pi * self["p"]) * (1.0 -
                                                        4.0 * self["p"]**2.0) * (3.0 - 8.0 * self["p"]**2.0)
            fe = 1.0 / (9.0 * numpy.pi) * 16.0 * \
                self["p"] * (2.0 * self["p"]**2.0 - 1.0)
            fpi = 0.0
            f2 = self._g2(z, a, b)
            k = 1.0 / (2.0 * numpy.pi)
            n = 0.0
        elif (step == 9):
            f0 = self["p"]**2.0
            f1 = 0.0
            fk = self._ci(z, a) * self._cik(z, a, b)
            fe = self._ci(z, a) * self._cie(z, a)
            fpi = self._ci(z, a) * self._cipi(z)
            f2 = 0.5 * self["p"]**2.0 * (self["p"]**2.0 + 2.0 * z**2.0)
            k = numpy.sqrt((4.0 * self["p"] * z) / (1.0 - a))
            n = -4.0 * self["p"] * z / a
        elif (step == 10):
            f0 = self["p"]**2.0
            f1 = self._ti(z)
            fk = 0.0
            fe = 0.0
            fpi = 0.0
            f2 = 0.5 * self["p"]**2.0 * (self["p"]**2.0 + 2.0 * z**2.0)
            k = 0.0
            n = 0.0
        elif (step == 11):
            f0 = self._g0(z)
            f1 = 0.0
            fk = self._cg(z) * self._cgk(z)
            fe = self._cg(z) * self._cge(z)
            fpi = self._cg(z) * self._cgpi(z)
            f2 = self._g2(z, a, b)
            k = numpy.sqrt((1.0 - a) / (4.0 * self["p"] * z))
            n = (a - 1.0) / a
        elif (step == 12):
            f0 = 0.0
            f1 = 0.0
            fk = 0.0
            fe = 0.0
            fpi = 0.0
            f2 = 0.0
            k = 0.0
            n = 0.0
        return [f0, f1, fk, fe, fpi, f2, float(k), float(n)]

    def whichEllInts(self):
        """
        Says which module is used for the evaluation of the elliptical functions.

        .. note::
           The support for BOOST has to be added manually!

        Returns
        -------
        Identifier : string
            Either 'boost' or 'mpmath' depending on whether the *BOOST*
            libraries are used or the *mpmath* python module.
        """
        if self.useBoost:
            return "boost"
        return "mpmath"

    def evaluate(self, time):
        """
        Calculate a light curve according to the analytical models
        given by Pal 2008.

        Parameters
        ----------
        time : array
            An array of time points at which the light curve
            shall be calculated.

        .. note:: time = 0 -> Planet is exactly in the line of sight (phase = 0).

        Returns
        -------
        Model : array
            The analytical light curve is stored in the property `lightcurve`.
        """

        # Translate the given parameters into an orbit and, finally,
        # into a projected, normalized distance (z-parameter)
        self._calcZList(time - self["T0"])

        # 'W' parameters corresponding to notation in Pal '08
        w = 6. - 2. * self["linLimb"] - self["quadLimb"]
        w0 = (6. - 6. * self["linLimb"] - 12. * self["quadLimb"]) / w
        w1 = (6. * self["linLimb"] + 12. * self["quadLimb"]) / w
        w2 = 6. * self["quadLimb"] / w

        # Initialize flux decrease array
        df = numpy.zeros(len(time))
        # Get a list of 'cases' (according to Pal '08). Depends on radius ratio and 'z'-parameter along the orbit
        ca = self._selectCases()
        # Loop through z-list, and calculate the light curve at each point in z (->time)
        for i in self._intrans:
            # Calculate the coefficients to be substituted into the Pal '08 equation
            c = self._returnCoeff(ca[i].step, self._zlist[i])
            # Substitute the coefficients and get 'flux decrease'
            if ca[i].step != 12:
                # Calculate flux decrease only if there is an occultation
                if not self.useBoost:
                    df[i] = w0 * c[0] + w2 * c[5] + w1 * (c[1] + c[2] * mpmath.ellipk(
                        c[6]**2) + c[3] * mpmath.ellipe(c[6]**2) + c[4] * mpmath.ellippi(c[7], c[6]**2))
                else:
                    df[i] = w0 * c[0] + w2 * c[5] + w1 * (c[1] + c[2] * self.ell.ell1(
                        c[6]) + c[3] * self.ell.ell2(c[6]) + c[4] * self.ell.ell3(c[7], c[6]))
        self.lightcurve = (1. - df) * 1. / \
            (1. + self["b"]) + self["b"] / (1.0 + self["b"])
        return self.lightcurve

class PalLCKep(PalLC):
    """
    Analytical transit light-curves using the formulae provided by Pal 2008.

    More information on the Keplerian orbit can be found here: :ref:`keplerorbitpyasl`

    .. note :: The **evaluation of elliptical integrals** is essential
               in calculating the transit model. While both
               the *mpmath* module and the *Boost* libraries implement
               those integrals, it is the Boost library, which
               evaluates them by far more quickly. Yet, the support
               for Boost has to be added manually.

    *Model parameters*:
      - `p` - Radius ratio between planet and star.
      - `a` - Semi-major axis of planetary orbit [stellar radii].
      - `w` - Longitude of periastron [deg].
      - `Omega` - Longitude of the ascending node [deg].
      - `e` - Orbital eccentricity.
      - `i` - Inclination of orbit in degrees (90 deg is *edge on* view).
      - `linLib` - Linear limb-darkening coefficient.
      - `quadLimb` - Quadratic limb-darkening coefficient.
      - `tau` - Time of periastron passage.
      - `per` - Period of planetary orbit.
      - `b` - Describes the flux ratio between a stellar companion and the main star (default is 0).

    This class inherits the functionality of funcFit's OneDFit object.
    You can, therefore, set and get the parameter using the brackets:
      e.g., pallc["p"] = 0.12345

    .. warning::
        Time units have to be consistent.

    Parameters
    ----------
    ke : None, optional
        If an instance of KeplerEllipse is given, the parameters a, i, tau, per, w, Omega,
        and e will be adopted from it.
    collisionCheck : boolean, optional
        If set True, it will be checked whether the two bodies collide on the current orbit.
    """

    def __init__(self, ke=None, collisionCheck=False):
        _ZList.__init__(self, "keplerian", collisionCheck)
        fuf.OneDFit.__init__(self, ["p", "a", "i", "linLimb", "quadLimb", "tau", "per", "b",
                                    "w", "Omega", "e"])
        self.freeze(["p", "a", "i", "linLimb", "quadLimb", "tau", "per", "b",
                     "w", "Omega", "e"])
        self.setRootName("Pal08")
        if ke is None:
            self["per"] = 1.0
            self["w"] = -90
        else:
            self["per"] = ke.per
            self["w"] = ke.w
            self["i"] = ke.i
            self["tau"] = ke.tau
            self["a"] = ke.a
            self["Omega"] = ke.Omega
            self["e"] = ke.e

        self._zlist = None

        if (not mpmath_imported) and (not boostEll_imported):
            raise(PE.PyARequiredImport("Neither mpmath nor the elliptical integrals from the boost library could be found!",
                                       where="PalLC::__init__", solution="Install mpmath or make Boost library available (more complicated - see documentation)"))
        self.useBoost = False
        if boostEll_imported:
            self.useBoost = True
            self.ell = ell.ell()

        self.collisionCheck = collisionCheck

        # Wrap get/setitem to inform about change in parameter name
        T0paE = PE.PyADeprecationError("The parameter 'T0pa' in PalLCKep had to be renamed 'tau'.",
                                       solution="Use 'tau' instead of 'T0pa'.")

        def getitem(specifier, **kwargs):
            if specifier == "T0pa":
                PE.warn(T0paE)
            return PalLC.__getitem__(self, specifier, **kwargs)
        self.__getitem__ = getitem

        def setitem(specifier, value):
            if specifier == "T0pa":
                PE.warn(T0paE)
            PalLC.__setitem__(self, specifier, value)
        self.__setitem__ = setitem

    def evaluate(self, time):
        """
        Calculate a light curve according to the analytical models
        given by Pal 2008.

        Parameters
        ----------
        time : array
            An array of time points at which the light curve
            shall be calculated.

        Returns
        -------
        Model : array
            The analytical light curve is stored in the property `lightcurve`.
        """

        # Translate the given parameters into an orbit and, finally,
        # into a projected, normalized distance (z-parameter)
        self._calcZList(time)

        # 'W' parameters corresponding to notation in Pal '08
        w = 6. - 2. * self["linLimb"] - self["quadLimb"]
        w0 = (6. - 6. * self["linLimb"] - 12. * self["quadLimb"]) / w
        w1 = (6. * self["linLimb"] + 12. * self["quadLimb"]) / w
        w2 = 6. * self["quadLimb"] / w

        # Initialize flux decrease array
        df = numpy.zeros(len(time))
        # Get a list of 'cases' (according to Pal '08). Depends on radius ratio and 'z'-parameter along the orbit
        ca = self._selectCases()
        # Loop through in-transit points in z-list,
        # and calculate the light curve at each point in z (->time)
        for i in self._intrans:
            # Calculate the coefficients to be substituted into the Pal '08 equation
            c = self._returnCoeff(ca[i].step, self._zlist[i])
            # Substitute the coefficients and get 'flux decrease'
            if ca[i].step != 12:
                # Calculate flux decrease only if there is an occultation
                if not self.useBoost:
                    df[i] = w0 * c[0] + w2 * c[5] + w1 * (c[1] + c[2] * mpmath.ellipk(
                        c[6]**2) + c[3] * mpmath.ellipe(c[6]**2) + c[4] * mpmath.ellippi(c[7], c[6]**2))
                else:
                    df[i] = w0 * c[0] + w2 * c[5] + w1 * (c[1] + c[2] * self.ell.ell1(
                        c[6]) + c[3] * self.ell.ell2(c[6]) + c[4] * self.ell.ell3(c[7], c[6]))
        self.lightcurve = (1. - df) * 1. / \
            (1. + self["b"]) + self["b"] / (1.0 + self["b"])
        return self.lightcurve

PalLC_Rebin = fuf.turnIntoRebin(PalLC)

ext: py

max_stars_repo_name: mirofedurco/PyAstronomy

max_issues_repo_name: mirofedurco/PyAstronomy

max_forks_repo_name: mirofedurco/PyAstronomy

reason: import numpy,import mpmath


text:
import threading
from functools import partial
import cv2
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from utils import backbone
import tensorflow as tf
import csv
import cv2
import numpy as np
from utils import visualization_utils as vis_util

# Variables
total_passed_objects = 0  # using it to count objects
from api import object_counting_api

class MainScreen(Screen):
    pass

class Manager(ScreenManager):
    pass

Builder.load_string('''
<MainScreen>:
    name: "Test"

    FloatLayout:
        Label:
            text: "Webcam from OpenCV?"
            pos_hint: {"x":0.0, "y":0.8}
            size_hint: 1.0, 0.2

        Image:
            # this is where the video will show
            # the id allows easy access
            id: vid
            size_hint: 1, 0.6
            allow_stretch: True  # allow the video image to be scaled
            keep_ratio: True  # keep the aspect ratio so people don't look squashed
            pos_hint: {'center_x':0.5, 'top':0.8}

        Button:
            text: 'Stop Video'
            pos_hint: {"x":0.0, "y":0.0}
            size_hint: 1.0, 0.2
            font_size: 50
            on_release: app.stop_vid()
''')

class Main(App):
    def build(self):

        # start the camera access code on a separate thread
        # if this was done on the main thread, GUI would stop
        # daemon=True means kill this thread when app stops
        threading.Thread(target=self.doit, daemon=True).start()

        sm = ScreenManager()
        self.main_screen = MainScreen()
        sm.add_widget(self.main_screen)
        return sm

    def doit(self):
        # this code is run in a separate thread
        self.do_vid = True  # flag to stop loop

        # make a window for use by cv2
        # flags allow resizing without regard to aspect ratio
        cv2.namedWindow('Hidden', cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)

        # resize the window to (0,0) to make it invisible
        cv2.resizeWindow('Hidden', 0, 0)
        cam = cv2.VideoCapture(0)
        detection_graph, category_index = backbone.set_model('ssd_mobilenet_v1_coco_2018_01_28',
                                                             'mscoco_label_map.pbtxt')

        is_color_recognition_enabled = False  # set it to true for enabling the color prediction for the detected objects

        # start processing loop
        while (self.do_vid):

            color = "waiting..."
            with detection_graph.as_default():
                with tf.compat.v1.Session(graph=detection_graph) as sess:
                    # Definite input and output Tensors for detection_graph
                    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

                    # Each box represents a part of the image where a particular object was detected.
                    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

                    # Each score represent how level of confidence for each of the objects.
                    # Score is shown on the result image, together with the class label.
                    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
                    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
                    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

                    #cap = cv2.VideoCapture(0)
                    (ret, frame) = cam.read()

                    # for all the frames that are extracted from input video
                    while True:
                        # Capture frame-by-frame
                        (ret, frame) = cam.read()

                        if not ret:
                            print("end of the video file...")
                            break

                        input_frame = frame

                        # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                        image_np_expanded = np.expand_dims(input_frame, axis=0)

                        # Actual detection.
                        (boxes, scores, classes, num) = sess.run(
                            [detection_boxes, detection_scores, detection_classes, num_detections],
                            feed_dict={image_tensor: image_np_expanded})

                        # insert information text to video frame
                        font = cv2.FONT_HERSHEY_SIMPLEX

                        # Visualization of the results of a detection.
                        counter, csv_line, counting_result = vis_util.visualize_boxes_and_labels_on_image_array(
                            cam.get(1),
                            input_frame,
                            is_color_recognition_enabled,
                            np.squeeze(boxes),
                            np.squeeze(classes).astype(np.int32),
                            np.squeeze(scores),
                            category_index,
                            use_normalized_coordinates=True,
                            line_thickness=4)
                        if (len(counting_result) == 0):
                            cv2.putText(input_frame, "...", (10, 35), font, 0.8, (0, 255, 255), 2,
                                        cv2.FONT_HERSHEY_SIMPLEX)
                        else:
                            cv2.putText(input_frame, counting_result, (10, 35), font, 0.8, (0, 255, 255), 2,
                                        cv2.FONT_HERSHEY_SIMPLEX)

                       # cv2.imshow('object counting', input_frame)

                        if cv2.waitKey(1) & 0xFF == ord('q'):
                            break

                            # ...
                            # more code
                            # ...

                            # send this frame to the kivy Image Widget
                            # Must use Clock.schedule_once to get this bit of code
                            # to run back on the main thread (required for GUI operations)
                            # the partial function just says to call the specified method with the provided argument (Clock adds a time argument)
                        Clock.schedule_once(partial(self.display_frame, input_frame))

                        cv2.imshow('Hidden', input_frame)
                        cv2.waitKey(1)

                    cam.release()
                    cv2.destroyAllWindows()

    def stop_vid(self):
        # stop the video capture loop
        self.do_vid = False

    def display_frame(self, frame, dt):
        # display the current video frame in the kivy Image widget

        # create a Texture the correct size and format for the frame
        texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')

        # copy the frame data into the texture
        texture.blit_buffer(frame.tobytes(order=None), colorfmt='bgr', bufferfmt='ubyte')

        # flip the texture (otherwise the video is upside down
        texture.flip_vertical()

        # actually put the texture in the kivy Image widget
        self.main_screen.ids.vid.texture = texture

if __name__ == '__main__':
    Main().run()

reason: import numpy


text:
import unittest

import numpy as np

from pytorch_common.additional_configs import BaseDatasetConfig
from pytorch_common.datasets import create_dataset
from pytorch_common.datasets_dl import (
    BasePyTorchDataset,
    DummyMultiClassDataset,
    DummyMultiLabelDataset,
    DummyRegressionDataset,
)
from pytorch_common.types import Optional, _StringDict

class TestModels(unittest.TestCase):
    @classmethod
    def setUp(cls):
        """
        Set up default config dictionary for a dataset.
        """
        cls.default_config_dict = {"size": 30, "dim": 4, "num_classes": 3}

    def test_create_dataset(self):
        """
        Test creation of all datasets.
        """
        size, in_dim, out_dim, num_classes = 5, 4, 2, 2

        config = BaseDatasetConfig({"size": size, "dim": in_dim, "num_classes": num_classes})
        self.assertIsInstance(create_dataset("multi_class_dataset", config), DummyMultiClassDataset)

        config = BaseDatasetConfig({"size": size, "dim": in_dim, "num_classes": num_classes})
        self.assertIsInstance(create_dataset("multi_label_dataset", config), DummyMultiLabelDataset)

        config = BaseDatasetConfig({"size": size, "in_dim": in_dim, "out_dim": out_dim})
        self.assertIsInstance(create_dataset("regression_dataset", config), DummyRegressionDataset)

    def test_oversample_class(self):
        """
        Test oversampling the targets of a dataset.
        """
        oversampling_factor = 2
        dataset = self._get_dataset(dictionary={"oversampling_factor": oversampling_factor})

        # Get original class info
        minority_info_pre = dataset._get_class_info(minority=True)
        majority_class_pre = dataset._get_class_info(minority=False)

        # Oversample minority class
        dataset.sample_class(oversampling_factor=oversampling_factor)

        # Get class info after oversampling
        # Ensure consistency in labels (order might have changed after sampling)
        minority_class_post = dataset._get_class_info(class_to_sample=minority_info_pre["label"])
        majority_class_post = dataset._get_class_info(class_to_sample=majority_class_pre["label"])

        # Ensure correct number of rows for both classes
        self.assertEqual(minority_class_post["count"], minority_info_pre["count"] * oversampling_factor)
        self.assertEqual(majority_class_post["count"], majority_class_pre["count"])

    def test_undersample_class(self):
        """
        Test undersampling the targets of a dataset.
        """
        undersampling_factor = 2
        dataset = self._get_dataset(dictionary={"undersampling_factor": undersampling_factor})

        # Get original class info
        minority_info_pre = dataset._get_class_info(minority=True)
        majority_class_pre = dataset._get_class_info(minority=False)

        # Undersample minority class
        dataset.sample_class(undersampling_factor=undersampling_factor)

        # Get class info after undersampling
        # Ensure consistency in labels (order might have changed after sampling)
        minority_class_post = dataset._get_class_info(class_to_sample=minority_info_pre["label"])
        majority_class_post = dataset._get_class_info(class_to_sample=majority_class_pre["label"])

        # Ensure correct number of rows for both classes
        self.assertEqual(minority_class_post["count"], minority_info_pre["count"])
        self.assertEqual(
            majority_class_post["count"],
            np.ceil(majority_class_pre["count"] / undersampling_factor),
        )

    def _get_dataset(
        self,
        dataset_name: Optional[str] = "multi_class_dataset",
        dictionary: Optional[_StringDict] = None,
    ) -> BasePyTorchDataset:
        """
        Merge the provided `dictionary` with the default
        one and create and return the desired dataset.
        """
        if dictionary is None:
            dictionary = {}
        dictionary = {**self.default_config_dict, **dictionary}
        return create_dataset(dataset_name, BaseDatasetConfig(dictionary))

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


text:
import os
import numpy as np

try:
    import flopy
except:
    msg = 'Error. FloPy package is not available.\n'
    msg += 'Try installing using the following command:\n'
    msg += ' pip install flopy'
    raise Exception(msg)

from framework import testing_framework, running_on_CI
from simulation import Simulation

ex = ['csub_dbgeo01a']
exdirs = []
for s in ex:
    exdirs.append(os.path.join('temp', s))
ddir = 'data'

ndcell = [19]
strt = [0.]
chdh = [0]
gso = [True]
bso = [True]
# sstate = [None, True]

# run all examples on Travis
# continuous_integration = [True for idx in range(len(exdirs))]
# the delay bed problems only run on the development version of MODFLOW-2005
# set travis to True when version 1.13.0 is released
continuous_integration = [False for idx in range(len(exdirs))]

# set replace_exe to None to use default executable
replace_exe = {'mf2005': 'mf2005devdbl'}

# comparison data
compdataa = [0.6829965295, 1.3552260620, 2.0163615680, 2.6662194730,
             3.3047615550, 3.9320942150, 4.5484650870, 5.1542570520,
             5.7499797990, 6.3362591640, 6.9138245770, 7.4834950010,
             8.0461637950, 8.6027829980, 9.1543475090, 9.7018796520,
             10.2464145700, 10.7889868300, 11.3306186100, 11.8723096200,
             12.4150289800, 12.9597090600, 13.5072412800, 14.0584737000,
             14.6142102900, 15.1752116300, 15.7421966800, 16.3158455400,
             16.8968027900, 17.4856812200, 18.0830657300, 18.6895172900,
             19.3055766500, 19.9317679500, 20.5686019500, 21.2165789800,
             21.8761915600, 22.5479266700, 23.2322678200, 23.9296967200,
             24.6406948800, 25.3657448400, 26.1053314000, 26.8599425100,
             27.6300702200, 28.4162113200, 29.2188679600, 30.0385480600,
             30.8757655600, 31.7310404400, 32.6048985000, 33.4978707400,
             34.4104923500, 35.3433011400, 36.2968353000, 37.2716304000,
             38.2682154600, 39.2871078800, 40.3288072900, 41.3937880000,
             42.4824901100, 43.5953091600, 44.7325843800, 45.8945855500,
             47.0814986400, 48.2934103700, 49.5302919900, 50.7919824900,
             52.0781717600, 53.3883838800, 54.7219611100, 56.0780489100,
             57.4555824900, 58.8532751700, 60.2696090000, 61.7028279600,
             63.1509339500, 64.6116859300, 66.0826023000, 67.5609666300,
             69.0438370300, 70.5280589700, 72.0102819100, 73.4869793800,
             74.9544728300, 76.4089589400, 77.8465405400, 79.2632606700,
             80.6551399000, 82.0182164100, 83.3485884600, 84.6424588900,
             85.8961809100, 87.1063045100, 88.2696228000, 89.3832170600,
             90.4444998100, 91.4512546100, 92.4016715000, 93.2943770700]

compdata = {0: compdataa}

# static model data
# spatial discretization
nlay, nrow, ncol = 1, 1, 3
delr, delc = 1., 1.
top = 0.
bots = [-100.]
botm = [top] + bots

# temporal discretization
nper = 1
perlen = [1000. for i in range(nper)]
nstp = [100 for i in range(nper)]
tsmult = [1.05 for i in range(nper)]
steady = [False for i in range(nper)]
tdis_rc = []
for idx in range(nper):
    tdis_rc.append((perlen[idx], nstp[idx], tsmult[idx]))

hnoflo = 1e30
hdry = -1e30

# idomain data
ib = 1

# npf and sto data
hk = 1e6
laytyp = [1]
ss = 0.
sy = 0.2

# solver data
nouter, ninner = 50, 100
hclose, rclose, relax = 1e-6, 1e-6, 0.97

# sub data
cc = 100.
cr = 1.
void = 0.82
theta = void / (1. + void)
kv = 0.025
sgm = 1.7
sgs = 2.2
ini_stress = 1.0
thick = 1.

# calculate geostatic and effective stress
def calc_stress(sgm0, sgs0, h, bt):
    geo = []
    for k in range(nlay):
        top = bt[k]
        bot = bt[k + 1]
        ht = h
        if ht > top:
            gs = (top - bot) * sgs0
        elif ht < bot:
            gs = (top - bot) * sgm0
        else:
            gs = ((top - ht) * sgm0) + ((ht - bot) * sgs0)
        geo.append(gs)
    # calculate total geostatic stress at bottom of layer
    for k in range(1, nlay):
        geo[k] += geo[k - 1]
    # calculate effective stress at the bottom of the layer
    es = []
    for k in range(nlay):
        es.append(geo[k] - (h - bt[k + 1]))
    return geo, es

def get_model(idx, dir):
    c6 = []
    for j in range(0, ncol, 2):
        c6.append([(0, 0, j), chdh[idx]])
    cd6 = {0: c6}

    geo, es = calc_stress(sgm, sgs, strt[idx], botm)
    sub6 = [[0, (0, 0, 1), 'delay', -1., thick,
             1., cc, cr, theta, kv, 1.]]

    name = ex[idx]

    # build MODFLOW 6 files
    ws = dir
    sim = flopy.mf6.MFSimulation(sim_name=name, version='mf6',
                                 exe_name='mf6',
                                 sim_ws=ws)
    # create tdis package
    tdis = flopy.mf6.ModflowTdis(sim, time_units='DAYS',
                                 nper=nper, perioddata=tdis_rc)

    # create gwf model
    gwf = flopy.mf6.ModflowGwf(sim, modelname=name)

    # create iterative model solution and register the gwf model with it
    ims = flopy.mf6.ModflowIms(sim, print_option='SUMMARY',
                               outer_dvclose=hclose,
                               outer_maximum=nouter,
                               under_relaxation='NONE',
                               inner_maximum=ninner,
                               inner_dvclose=hclose, rcloserecord=rclose,
                               linear_acceleration='CG',
                               scaling_method='NONE',
                               reordering_method='NONE',
                               relaxation_factor=relax)
    sim.register_ims_package(ims, [gwf.name])

    dis = flopy.mf6.ModflowGwfdis(gwf, nlay=nlay, nrow=nrow, ncol=ncol,
                                  delr=delr, delc=delc,
                                  top=top, botm=bots,
                                  filename='{}.dis'.format(name))

    # initial conditions
    ic = flopy.mf6.ModflowGwfic(gwf, strt=strt[idx],
                                filename='{}.ic'.format(name))

    # node property flow
    npf = flopy.mf6.ModflowGwfnpf(gwf, save_flows=False,
                                  icelltype=laytyp,
                                  k=hk,
                                  k33=hk)
    # storage
    sto = flopy.mf6.ModflowGwfsto(gwf, save_flows=False, iconvert=laytyp,
                                  ss=ss, sy=sy,
                                  storagecoefficient=True,
                                  transient={0: True})

    # chd files
    chd = flopy.mf6.modflow.mfgwfchd.ModflowGwfchd(gwf,
                                                   maxbound=len(c6),
                                                   stress_period_data=cd6,
                                                   save_flows=False)

    # csub files
    opth = '{}.csub.obs'.format(name)
    ibcsv = '{}.ib.strain.csv'.format(name)
    skcsv = '{}.sk.strain.csv'.format(name)
    csub = flopy.mf6.ModflowGwfcsub(gwf,
                                    print_input=True,
                                    ndelaycells=ndcell[idx],
                                    strainib_filerecord=ibcsv,
                                    straincg_filerecord=skcsv,
                                    effective_stress_lag=True,
                                    # compression_indices=True,
                                    ninterbeds=1,
                                    sgs=sgs, sgm=sgm, packagedata=sub6,
                                    beta=0., cg_ske_cr=0.)
    orecarray = {}
    orecarray['csub_obs.csv'] = [('tcomp', 'interbed-compaction', (0, 0, 1)),
                                 ('gs', 'gstress-cell', (0, 0, 1)),
                                 ('es', 'estress-cell', (0, 0, 1)),
                                 ('pcs', 'delay-preconstress', (0, 0)),
                                 ('sk', 'sk', (0, 0, 1))]
    csub_obs_package = csub.obs.initialize(filename=opth, digits=10,
                                           print_input=True,
                                           continuous=orecarray)

    # output control
    oc = flopy.mf6.ModflowGwfoc(gwf,
                                budget_filerecord='{}.cbc'.format(name),
                                head_filerecord='{}.hds'.format(name),
                                headprintrecord=[
                                    ('COLUMNS', 10, 'WIDTH', 15,
                                     'DIGITS', 6, 'GENERAL')],
                                saverecord=[('HEAD', 'ALL')],
                                printrecord=[('HEAD', 'LAST'),
                                             ('BUDGET', 'ALL')])

    mc = None

    return sim, mc

def eval_sub(sim):
    print('evaluating subsidence...')

    # MODFLOW 6 total compaction results
    fpth = os.path.join(sim.simpath, 'csub_obs.csv')
    try:
        tc = np.genfromtxt(fpth, names=True, delimiter=',')
    except:
        assert False, 'could not load data from "{}"'.format(fpth)

    # set comparison data
    tc0 = compdata[sim.idxsim]

    # calculate maximum absolute error
    diff = tc['TCOMP'] - tc0[:]
    diffmax = np.abs(diff).max()
    dtol = 1e-6
    msg = 'maximum absolute total-compaction difference ({}) '.format(diffmax)

    # write summary
    fpth = os.path.join(sim.simpath,
                        '{}.comp.cmp.out'.format(os.path.basename(sim.name)))
    f = open(fpth, 'w')
    line = '{:>15s}'.format('TOTIM')
    line += ' {:>15s}'.format('CSUB')
    line += ' {:>15s}'.format('MF')
    line += ' {:>15s}'.format('DIFF')
    f.write(line + '\n')
    for i in range(diff.shape[0]):
        line = '{:15g}'.format(tc0[i])
        line += ' {:15g}'.format(tc['TCOMP'][i])
        line += ' {:15g}'.format(tc0[i])
        line += ' {:15g}'.format(diff[i])
        f.write(line + '\n')
    f.close()

    if diffmax > dtol:
        sim.success = False
        msg += 'exceeds {}'.format(dtol)
        assert diffmax < dtol, msg
    else:
        sim.success = True
        print('    ' + msg)

    return

# - No need to change any code below
def build_models():
    for idx, dir in enumerate(exdirs):
        sim, mc = get_model(idx, dir)
        sim.write_simulation()
        if mc is not None:
            mc.write_input()
    return

def test_mf6model():
    # determine if running on Travis or GitHub actions
    is_CI = running_on_CI()
    r_exe = None
    if not is_CI:
        if replace_exe is not None:
            r_exe = replace_exe

    # initialize testing framework
    test = testing_framework()

    # build the models
    build_models()

    # run the test models
    for idx, dir in enumerate(exdirs):
        if is_CI and not continuous_integration[idx]:
            continue
        yield test.run_mf6, Simulation(dir, exfunc=eval_sub,
                                       exe_dict=r_exe, idxsim=idx)

    return

def main():
    # initialize testing framework
    test = testing_framework()

    # build the models
    build_models()

    # run the test models
    for idx, dir in enumerate(exdirs):
        sim = Simulation(dir, exfunc=eval_sub, exe_dict=replace_exe,
                         idxsim=idx)
        test.run_mf6(sim)
    return

# use python testmf6_csub_sub01.py --mf2005 mf2005devdbl
if __name__ == "__main__":
    # print message
    print('standalone run of {}'.format(os.path.basename(__file__)))

    # run main routine
    main()


text:
# -*- coding: utf-8 -*-
"""
/*------------------------------------------------------*
|     Spatial Uncertainty Research Framework            |
|                                                       |
| Author: Charles Wang,  UC Berkeley, c_w@berkeley.edu  |
|                                                       |
| Date:    04/07/2019                                   |
*------------------------------------------------------*/
"""

import numpy as np

def exponential(d, s, sill):
    '''
    Inputs: d, distance
            s, scaler
            sill, sill
    Outputs: exp
    Description: Exponential model.
    '''

    if np.ndim( s ) > 1:
        h = np.sum(np.square(d/s))**0.5
    else:
        h = d / s
    return sill * ( 1.0 - np.exp( -3.0 * h ) )

def cov(f, pars):
    '''
    Inputs: f,   model func
            pars, pars taken by f
    Outputs:
    '''
    def func( d ):
        return pars[-1] - f(d,*pars)
    return func

max_stars_repo_name: claudioperez/SURF

max_issues_repo_name: claudioperez/SURF

max_forks_repo_name: claudioperez/SURF


text:
from frameworks.factory import Framework
import numpy as np
from itertools import product
from metamodels.model_selection import prepare_data

class Framework6(Framework):
    def __init__(self,
                 framework_id=None,
                 problem=None,
                 algorithm=None,
                 model_list=None,
                 ref_dirs=None,
                 curr_ref_id=None,
                 m6_fg_aggregate_func='asfcv',
                 *args,
                 **kwargs
                 ):
        super().__init__(framework_id=framework_id,
                         problem=problem,
                         algorithm=algorithm,
                         model_list=model_list,
                         ref_dirs=ref_dirs,
                         curr_ref_id=curr_ref_id,
                         m6_fg_aggregate_func=m6_fg_aggregate_func,
                         *args,
                         **kwargs)
        self.type = 2

    def train(self, x, f, g, *args, **kwargs):

        for i in range(len(self.ref_dirs)):
            string = "fg_M6_" + str(i + 1) + "_" + str(self.m6_fg_aggregate_func)
            d = prepare_data(f=f, g=g, acq_func=[string], ref_dirs=self.ref_dirs,
                             curr_ref_id=i)

            self.model_list[string].train(x, d[string])

    def predict(self, x, out, *args, **kwargs):
        f = []
        g = []
        for i in range(len(self.ref_dirs)):
            _f = self.model_list["fg_M6_" + str(i + 1) + "_" + str(self.m6_fg_aggregate_func)].predict(x)
            f.append(_f)

        _g = np.zeros([x.shape[0], self.problem.n_constr])
        g.append(_g)

        out["F"] = np.column_stack(f)
        out["G"] = np.column_stack(g)

    def calculate_sep(self, problem, actual_data, prediction_data, n_split):

        err = []
        for partition in range(n_split):

            # compute average error over all reference directions
            temp_err = 0
            count = 0
            for j in range(len(self.ref_dirs)):
                string = "fg_M6_"+str(j + 1) + "_" + str(self.m6_fg_aggregate_func)
                I_temp = np.arange(0, actual_data[string][partition].shape[0])
                I = np.asarray(list(product(I_temp, I_temp)))
                cv = np.zeros([I_temp.shape[0], 1])
                cv_pred = np.zeros([I_temp.shape[0], 1])

                f = actual_data[string][partition]
                f_pred = prediction_data[string][partition]

                for i in range(I.shape[0]):
                    count = count + 1
                    d1 = self.constrained_domination(f[I[i, 0]], f[I[i, 1]], cv[I[i, 0]], cv[I[i, 1]])
                    d2 = self.constrained_domination(f_pred[I[i, 0]], f_pred[I[i, 1]], cv_pred[I[i, 0]], cv_pred[I[i, 1]])
                    if d1 != d2:
                        temp_err = temp_err + 1
            temp_err = temp_err/count
            err.append(temp_err)

        return np.asarray(err)

max_stars_repo_name: proteekroy/samoo

max_issues_repo_name: proteekroy/samoo

max_forks_repo_name: proteekroy/samoo


text:
import tensorflow as tf
import numpy as np
from rl.acktr.utils import Scheduler
from rl.acktr.utils import fc

disc_types = ['decentralized', 'centralized', 'single']

class Discriminator(object):
    def __init__(self, sess, ob_spaces, ac_spaces,
                 nstack, index, disc_type='decentralized', hidden_size=128, gp_coef=5,
                 lr_rate=5e-4, total_steps=50000, scope="discriminator"):
        self.lr = Scheduler(v=lr_rate, nvalues=total_steps * 20, schedule='linear')
        self.disc_type = disc_type
        if disc_type not in disc_types:
            assert False
        self.scope = scope
        self.index = index
        self.sess = sess
        ob_space = ob_spaces[index]
        ac_space = ac_spaces[index]
        self.ob_shape = ob_space.shape[0] * nstack
        nact = ac_space.n
        self.ac_shape = nact * nstack
        self.all_ob_shape = sum([obs.shape[0] for obs in ob_spaces]) * nstack
        self.all_ac_shape = sum([ac.n for ac in ac_spaces]) * nstack
        self.hidden_size = hidden_size

        if disc_type == 'decentralized':
            input_shape = self.all_ob_shape + self.ac_shape
        elif disc_type == 'centralized':
            input_shape = self.all_ob_shape + self.all_ac_shape
        elif disc_type == 'single':
            input_shape = self.all_ob_shape + self.all_ac_shape
        else:
            assert False

        self.g = tf.placeholder(tf.float32, (None, input_shape))
        self.e = tf.placeholder(tf.float32, (None, input_shape))
        self.lr_rate = tf.placeholder(tf.float32, ())

        num_outputs = len(ob_spaces) if disc_type == 'centralized' else 1
        self.bias = tf.get_variable(name=scope + '_bias', shape=(num_outputs,),
                                    initializer=tf.zeros_initializer, trainable=False)
        self.bias_ph = tf.placeholder(tf.float32, (num_outputs,))
        self.update_bias = tf.assign(self.bias, self.bias_ph * 0.01 + self.bias * 0.99)

        generator_logits = self.build_graph(self.g, num_outputs, reuse=False)
        expert_logits = self.build_graph(self.e, num_outputs, reuse=True)

        self.generator_loss = tf.reduce_mean(generator_logits, axis=0)
        self.expert_loss = tf.reduce_mean(expert_logits, axis=0)

        ddg = tf.gradients(generator_logits, [self.g])
        ddg = tf.sqrt(tf.reduce_sum(tf.square(ddg[0]), axis=1))
        self.ddg = tf.reduce_mean(tf.square(ddg - 1.))

        dde = tf.gradients(expert_logits, [self.e])
        dde = tf.sqrt(tf.reduce_sum(tf.square(dde[0]), axis=1))
        self.dde = tf.reduce_mean(tf.square(dde - 1.))

        epsilon = tf.random_uniform([], 0.0, 1.0)
        ge = self.g * epsilon + self.e * (1 - epsilon)
        gel = self.build_graph(ge, num_outputs, reuse=True)
        ddd = tf.gradients(gel, [ge])
        ddd = tf.norm(ddd, axis=1)
        self.ddd = tf.reduce_mean(tf.square(ddd - 1.))

        self.total_loss = self.generator_loss - self.expert_loss + gp_coef * self.ddd #(self.ddg + self.dde)
        self.reward_op = generator_logits

        self.var_list = self.get_trainable_variables()
        self.d_optim = tf.train.AdamOptimizer(self.lr_rate, beta1=0.5, beta2=0.9).minimize(self.total_loss, var_list=self.var_list)
        self.saver = tf.train.Saver(self.get_variables())

    def build_graph(self, x, num_outputs=1, reuse=False):
        with tf.variable_scope(self.scope):
            if reuse:
                tf.get_variable_scope().reuse_variables()
            p_h1 = fc(x, 'fc1', nh=self.hidden_size)
            p_h2 = fc(p_h1, 'fc2', nh=self.hidden_size)
            p_h3 = fc(p_h2, 'fc3', nh=self.hidden_size)
            logits = fc(p_h3, 'out', nh=num_outputs, act=lambda x: x)
            logits -= self.bias
        return logits

    def get_variables(self):
        return tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, self.scope)

    def get_trainable_variables(self):
        return tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.scope)

    def get_reward(self, all_obs, acs):
        if len(all_obs.shape) == 1:
            all_obs = np.expand_dims(all_obs, 0)
        if len(acs.shape) == 1:
            acs = np.expand_dims(acs, 0)
        feed_dict = {self.g: np.concatenate([all_obs, acs], axis=1)}
        return self.sess.run(self.reward_op, feed_dict)

    def train(self, g_all_obs, g_acs, e_all_obs, e_acs):
        feed_dict = {self.g: np.concatenate([g_all_obs, g_acs], axis=1),
                     self.e: np.concatenate([e_all_obs, e_acs], axis=1), self.lr_rate: self.lr.value()}
        gl, el, _ = self.sess.run([self.generator_loss, self.expert_loss, self.d_optim], feed_dict)
        # self.sess.run(self.update_bias, feed_dict={self.bias_ph: (gl + el) / 2.0})
        return self.sess.run([self.generator_loss, self.expert_loss, self.ddg, self.dde], feed_dict)

    def restore(self, path):
        print('restoring from:' + path)
        self.saver.restore(self.sess, path)

max_issues_repo_name: genipap/multiagent-gail

max_forks_repo_name: genipap/multiagent-gail


text:
"""
@brief Dark current task: compute 95th percentile dark current in
units of e-/sec/pixel.

@author J. Chiang <jchiang@slac.stanford.edu>
"""
import os
from collections import defaultdict
import warnings
import numpy as np
import pandas as pd
import astropy.io.fits as fits
from astropy.utils.exceptions import AstropyWarning, AstropyUserWarning
import lsst.afw.math as afwMath
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
from lsst.eotest.fitsTools import fitsWriteto
import lsst.eotest.image_utils as imutils
from .MaskedCCD import MaskedCCD
from .EOTestResults import EOTestResults

class DarkCurrentConfig(pexConfig.Config):
    """Configuration for DarkCurrentTask"""
    temp_set_point = pexConfig.Field("Required temperature (C) set point",
                                     float, default=-95.)
    temp_set_point_tol = pexConfig.Field("Required temperature set point tolerance (degrees C)",
                                         float, default=1.)
    output_dir = pexConfig.Field("Output directory", str, default=".")
    eotest_results_file = pexConfig.Field('EO test results filename',
                                          str, default=None)
    verbose = pexConfig.Field("Turn verbosity on", bool, default=True)

class DarkCurrentTask(pipeBase.Task):
    """Task to evaluate dark current quantiles."""
    ConfigClass = DarkCurrentConfig
    _DefaultName = "DarkCurrentTask"

    @pipeBase.timeMethod
    def run(self, sensor_id, dark_files, mask_files, gains, bias_frame=None,
            linearity_correction=None, dark_files_linear_fit=None):
        if dark_files_linear_fit is not None:
            self.linear_fit(sensor_id, dark_files_linear_fit, mask_files,
                            gains, bias_frame=bias_frame,
                            linearity_correction=linearity_correction)
        return self.compute_percentiles(sensor_id, dark_files, mask_files,
                                        gains, bias_frame=bias_frame,
                                        linearity_correction=linearity_correction)

    def linear_fit(self, sensor_id, dark_files, mask_files, gains,
                   bias_frame=None, linearity_correction=None):
        """Fit a slope and intercept for a dark frame data set that has
        a range of different exposure times.
        """
        data = defaultdict(list)
        for item in dark_files:
            ccd = MaskedCCD(item, mask_files=mask_files, bias_frame=bias_frame,
                            linearity_correction=linearity_correction)
            for amp in ccd:
                data['amp'].append(amp)
                data['darktime'].append(ccd.md.get('DARKTIME'))
                image = ccd.unbiased_and_trimmed_image(amp)
                stats = afwMath.makeStatistics(image, afwMath.MEDIAN)
                data['median'].append(stats.getValue(afwMath.MEDIAN)*gains[amp])
        df0 = pd.DataFrame(data=data)
        slopes, intercepts = dict(), dict()
        for amp in ccd:
            df = df0.query(f'amp == {amp}')
            slopes[amp], intercepts[amp] \
                = np.polyfit(df['darktime'], df['median'], 1)

        results_file = self.config.eotest_results_file
        if results_file is None:
            results_file = os.path.join(self.config.output_dir,
                                        '%s_eotest_results.fits' % sensor_id)
        results = EOTestResults(results_file, namps=len(ccd))
        # Write slopes and intercepts for each amp
        for amp in ccd:
            results.add_seg_result(amp, 'DARK_CURRENT_SLOPE', slopes[amp])
            results.add_seg_result(amp, 'DARK_CURRENT_INTERCEPT',
                                   intercepts[amp])
        results.write(clobber=True)

    def compute_percentiles(self, sensor_id, dark_files, mask_files, gains,
                            bias_frame=None, linearity_correction=None):
        """Compute median and 95th percentiles of pixel values for dark frame
        data sets that have the same integration times."""
        imutils.check_temperatures(dark_files, self.config.temp_set_point_tol,
                                   setpoint=self.config.temp_set_point,
                                   warn_only=True)
        median_images = {}
        md = imutils.Metadata(dark_files[0])
        for amp in imutils.allAmps(dark_files[0]):
            median_images[amp] = imutils.fits_median(dark_files,
                                                     imutils.dm_hdu(amp))
        medfile = os.path.join(self.config.output_dir,
                               '%s_median_dark_current.fits' % sensor_id)
        imutils.writeFits(median_images, medfile, dark_files[0])

        ccd = MaskedCCD(medfile, mask_files=mask_files, bias_frame=bias_frame,
                        linearity_correction=linearity_correction)

        dark95s = {}
        dark_medians = {}
        try:
            exptime = md.get('DARKTIME')
        except:
            exptime = md.get('EXPTIME')
        if self.config.verbose:
            self.log.info("Amp        95 percentile    median")
        dark_curr_pixels = []
        dark_curr_pixels_per_amp = {}
        for amp in ccd:
            mi = ccd.unbiased_and_trimmed_image(amp)
            imarr = mi.getImage().array
            mskarr = mi.getMask().array
            pixels = imarr.reshape(1, imarr.shape[0]*imarr.shape[1])[0]
            masked = mskarr.reshape(1, mskarr.shape[0]*mskarr.shape[1])[0]
            unmasked = [pixels[i] for i in range(len(pixels)) if masked[i] == 0]
            unmasked.sort()
            unmasked = np.array(unmasked)*gains[amp]/exptime
            dark_curr_pixels_per_amp[amp] = unmasked
            dark_curr_pixels.extend(unmasked)
            try:
                dark95s[amp] = unmasked[int(len(unmasked)*0.95)]
                dark_medians[amp] = unmasked[len(unmasked)//2]
            except IndexError as eobj:
                print(str(eobj))
                dark95s[amp] = -1.
                dark_medians[amp] = -1.
            if self.config.verbose:
                self.log.info("%2i         %.2e         %.2e"
                              % (amp, dark95s[amp], dark_medians[amp]))
        #
        # Compute 95th percentile dark current for CCD as a whole.
        #
        dark_curr_pixels = sorted(dark_curr_pixels)
        darkcurr95 = dark_curr_pixels[int(len(dark_curr_pixels)*0.95)]
        dark95mean = np.mean(list(dark95s.values()))
        if self.config.verbose:
            #self.log.info("CCD: mean 95 percentile value = %s" % dark95mean)
            self.log.info("CCD-wide 95 percentile value = %s" % darkcurr95)
        #
        # Update header of dark current median image file with dark
        # files used and dark95 values, and write dark95 values to the
        # eotest results file.
        #
        results_file = self.config.eotest_results_file
        if results_file is None:
            results_file = os.path.join(self.config.output_dir,
                                        '%s_eotest_results.fits' % sensor_id)
        results = EOTestResults(results_file, namps=len(ccd))
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=UserWarning, append=True)
            warnings.filterwarnings('ignore', category=AstropyWarning,
                                    append=True)
            warnings.filterwarnings('ignore', category=AstropyUserWarning,
                                    append=True)

            with fits.open(medfile) as output:
                for i, dark in enumerate(dark_files):
                    output[0].header['DARK%02i' % i] = os.path.basename(dark)
                # Write overall dark current 95th percentile
                results.output['AMPLIFIER_RESULTS'].header['DARK95'] \
                    = darkcurr95
                for amp in ccd:
                    output[0].header['DARK95%s'%imutils.channelIds[amp]] \
                        = dark95s[amp]
                    results.add_seg_result(amp, 'DARK_CURRENT_95', dark95s[amp])
                    results.add_seg_result(amp, 'DARK_CURRENT_MEDIAN',
                                           dark_medians[amp])
                fitsWriteto(output, medfile, overwrite=True, checksum=True)
                results.write(clobber=True)

        return dark_curr_pixels_per_amp, dark95s

max_stars_repo_name: tguillemLSST/eotest

max_issues_repo_name: tguillemLSST/eotest

max_forks_repo_name: tguillemLSST/eotest

reason: import numpy,import astropy,from astropy


text:
#!/usr/bin/env python
"""
Created on Thu Jun 18 10:42:49 2015

Author: Oren Freifeld
Email: freifeld@csail.mit.edu
"""

from of.utils import ObsoleteError
raise ObsoleteError("Moved to Tessellation.py")

import numpy as np
from itertools import product # since we have an unknown number of lists
from numpy import binary_repr

from of.utils import ipshell

def create_cells(nCs,nC,XMINS,XMAXS,tess='II'):
    N = len(nCs)
    if len(XMINS)!=N:
        raise ValueError(XMINS)
    if len(XMAXS)!=N:
        raise ValueError(XMAXS)

    if tess != 'II':
        raise ValueError(tess)

    if np.prod(nCs) != nC:
        raise ValueError(tess,np.prod(nCs), nCs)

    nCs = map(int,nCs)

    Vs = [np.linspace(m,M,nc+1) for (m,M,nc) in zip(XMINS,XMAXS,nCs)]

    cells_verts=[]
    cells_multiidx=[]

    lists = map(range,nCs)
    lists = lists[::-1]
    Vs=Vs[::-1]

    brs = [binary_repr(i,N) for i in range(2**N)]

    for idx, items in enumerate(product(*lists)):
#        print idx,'---',items
        items=np.asarray(items)
        verts_of_this_cell =[]
        for i in range(2**N):
            inc = np.asarray(map(int,brs[i]))

            indices = items+inc

            tmp = [Vs[j][indices[j]] for j in range(N)][::-1]

            verts_of_this_cell.append(tmp+[1])

        cells_multiidx.append( tuple(items.tolist()))

        verts_of_this_cell = map(tuple,verts_of_this_cell)
        verts_of_this_cell = tuple(verts_of_this_cell)
        cells_verts.append(verts_of_this_cell)

#    ipshell('hi')
#    2/0

    if len(cells_multiidx) != nC:
        raise ValueError( len(cells_multiidx) , nC)
    if len(cells_verts) != nC:
        raise ValueError( len(cells_verts) , nC)

    if tess == 'II':
        # every cell should be made of 8 vertices (cube)
        if not all([x==2**N for x in map(len,map(set,cells_verts))]):
            raise ValueError
    else:
        raise ValueError(tess)

    return  cells_multiidx,cells_verts

max_stars_repo_name: freifeld/cpabDiffeo

max_issues_repo_name: freifeld/cpabDiffeo

max_forks_repo_name: freifeld/cpabDiffeo

reason: import numpy,from numpy


text:
import numpy as np

def test_parameters_optimizers():
    """
    Initialize paramaters to be used in the tests.

    Returns
    -------
    weights : numpy.array
    bias : numpy.array
    dW : numpy.array
    db : numpy.array
    """
    weights = np.array(
        [[-0.17242821, -0.87785842,  0.04221375],
       [ 0.58281521, -1.10061918,  1.14472371]]
    )
    bias = np.array([[0.90159072], [0.50249434]])

    dW = np.array(
        [[ 0.90085595, -0.68372786, -0.12289023],
       [-0.93576943, -0.26788808,  0.53035547]]
    )
    db = np.array([[-0.69166075], [-0.39675353]])

    return weights, bias, dW, db

def test_parameters_linearlayer():
    """
    Initialize paramaters to be used in the tests.

    Returns
    -------
    weights : numpy.array
    bias : numpy.array
    A : numpy.array
    dZ : numpy.array
    """
    weights = np.array([[-0.5, -1, 0.4], [2, 1, 4]])
    bias = np.array([[0.5], [0.5]])

    A = np.array([[-5], [20], [10]])
    dZ = np.array([[-1], [2]])

    return weights, bias, A, dZ

def test_parameters_convolution():
    """
    Initialize parameters to be used in the test.
    """
    image = np.array(
        [[[[3]], [[4]], [[0]], [[1]]],
        [[[3]], [[0]], [[0]], [[1]]],
        [[[4]], [[4]], [[1]], [[2]]],
        [[[4]], [[2]], [[4]], [[3]]]]
    )
    weights = np.array([[[[4, 2, 1], [2, 4, 0], [4, 1, 1]]]])
    bias = np.array([[1]])

    return image, weights, bias

def test_parameters_rnncell():
    """
    Initialize paramaters to be used in the tests.

    Returns
    -------
    weights : numpy.array
    bias : numpy.array
    A : numpy.array
    dZ : numpy.array
    """
    weights = np.array(
        [[ 0.81217268, -0.30587821, -0.26408588, -0.53648431],
       [ 0.43270381, -1.15076935,  0.87240588, -0.38060345]]
    )

    dZ = np.array([[ 1.74481176], [-0.7612069]])

    return weights, dZ

max_stars_repo_name: Javicadserres/dnetworks

max_issues_repo_name: Javicadserres/dnetworks

max_forks_repo_name: Javicadserres/DNet


text:
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from typing import List, Tuple
import json

import cv2 as cv
import numpy as np
from skimage.feature import (
    greycomatrix,
    greycoprops,
    hog,
    local_binary_pattern,
)
from skimage.filters import sobel
from skimage.filters.rank import entropy
from skimage.measure import shannon_entropy
from skimage.morphology import disk
from sklearn import decomposition
from tornado.web import RequestHandler

from ..types import DataObject
from .utils import get_image, resize_SVD

def raw_flatten(imgs: np.ndarray) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for each image by simply flattening the normalized image.

    The images are normalized by converting to grey image and resized to (8, 8).

    Args
    ----
    imgs : np.ndarray
        The images to extract features.

    Returns
    -------
    X : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name

    # normalized the images to gray scale 8 x 8
    h, w = 8, 8
    X_raw_normalized = []
    for img in imgs:
        img_gray = img if len(img.shape) == 2 else cv.cvtColor(
            img, cv.COLOR_BGR2GRAY)
        img_resized = cv.resize(img_gray, (h, w), interpolation=cv.INTER_AREA)
        X_raw_normalized.append(img_resized)
    X_raw_normalized = np.array(X_raw_normalized)

    # simply flatten the image
    X = X_raw_normalized.reshape((-1, h * w))
    feature_names = [f'raw[{i}][{j}]' for i in range(h) for j in range(w)]
    return X, feature_names

def color_descriptors_single(img: np.ndarray,
                             return_feature_names: bool = True,
                             ) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for a single image by handcrafted color features.

    The images are normalized by converting to color image.

    Args
    ----
    img : np.ndarray
        The image to extract features.
    return_feature_names : bool
        Whether to return feature names.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name, too-many-locals

    # normalize the image to color image
    img_color = img if len(img.shape) == 3 else cv.cvtColor(
        img, cv.COLOR_GRAY2BGR)

    img_hsv = cv.cvtColor(img_color, cv.COLOR_BGR2HSV)
    channels = {
        'gray': cv.cvtColor(img_color, cv.COLOR_BGR2GRAY),
        'b': img_color[:, :, 0],
        'g': img_color[:, :, 1],
        'r': img_color[:, :, 2],
        'h': img_hsv[:, :, 0],
        's': img_hsv[:, :, 1],
        'v': img_hsv[:, :, 2],
    }

    n_hist_bins = 16

    x = []
    for channel in channels:
        img_channel = channels[channel]
        h, w = img_channel.shape

        # histogram
        hist, _ = np.histogram(img_channel, bins=n_hist_bins, range=(0, 256))
        # normalize with image size to make the histogram invariant to image scaling
        hist = hist / (h * w)

        # median, mean, std
        median = np.median(img_channel[:, :])
        mean = np.mean(img_channel[:, :])
        std = np.std(img_channel[:, :])

        x += [*hist, median, mean, std]
    x = np.array(x)

    if return_feature_names:
        dims = [f'hist[{i}]' for i in range(n_hist_bins)]\
            + ['median', 'mean', 'std']
        feature_names = [f'color-{channel}-{dim}'
                         for channel in channels for dim in dims]
        return x, feature_names
    return x

def color_descriptors(imgs: np.ndarray) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for each image by handcrafted color features.

    The images are normalized by converting to color image.

    Args
    ----
    img : np.ndarray
        The image to extract features.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name

    X = np.array([color_descriptors_single(img, False) for img in imgs])

    channels = ['gray', 'b', 'g', 'r', 'h', 's', 'v']
    n_hist_bins = 16
    dims = [f'hist[{i}]' for i in range(
        n_hist_bins)] + ['median', 'mean', 'std']
    feature_names = [
        f'color-{channel}-{dim}' for channel in channels for dim in dims]
    return X, feature_names

def edge_descriptors_single(img: np.ndarray,
                            return_feature_names: bool = True) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for a single image by handcrafted edge features.

    The images are normalized by converting to color image.

    Args
    ----
    img : np.ndarray
        The image to extract features.
    return_feature_names : bool
        Whether to return feature names.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name

    # normalize the image to gray image
    img_gray = img if len(img.shape) == 2 else cv.cvtColor(
        img, cv.COLOR_BGR2GRAY)
    # Note: in sobel edge detection implemenetation,
    # the image is first converted to float image on [0, 1]
    # then sobel filter is run on the float image
    # where the filter result is bounded in [0, 1]
    img_edge = sobel(img_gray)

    n_hist_bins = 16

    h, w = img_edge.shape

    # histogram
    hist, _ = np.histogram(img_edge, bins=n_hist_bins, range=(0, 1))
    # normalize with image size to make the histogram invariant to image scaling
    hist = hist / (h * w)

    # median, mean, std
    median = np.median(img_edge[:, :])
    mean = np.mean(img_edge[:, :])
    std = np.std(img_edge[:, :])

    x = np.array([*hist, median, mean, std])

    if return_feature_names:
        dims = [f'hist[{i}]' for i in range(
            n_hist_bins)] + ['median', 'mean', 'std']
        feature_names = [f'edge-{dim}' for dim in dims]
        return x, feature_names
    return x

def edge_descriptors(imgs: np.ndarray) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for each image by handcrafted edge features.

    The images are normalized by converting to color image.

    Args
    ----
    img : np.ndarray
        The image to extract features.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name

    X = np.array([edge_descriptors_single(img, False) for img in imgs])

    n_hist_bins = 16
    dims = [f'hist[{i}]' for i in range(
        n_hist_bins)] + ['median', 'mean', 'std']
    feature_names = [f'edge-{dim}' for dim in dims]
    return X, feature_names

def edge_direction_descriptors_single(img: np.ndarray,
                                      return_feature_names: bool = True,
                                      ) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for a single image by handcrafted edge features.

    The images are normalized by converting to color image.

    Args
    ----
    img : np.ndarray
        The image to extract features.
    return_feature_names : bool
        Whether to return feature names.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name

    # normalize the image to color image of size 96 x 96
    h, w = 96, 96
    img_color = img if len(img.shape) == 3 else cv.cvtColor(
        img, cv.COLOR_GRAY2BGR)
    img_resized = cv.resize(img_color, (h, w), interpolation=cv.INTER_AREA)

    # hog descriptor of size (96/6, 96/6, 1, 1, 8) and then flattened
    x = hog(img_resized, orientations=8, pixels_per_cell=(6, 6),
            cells_per_block=(1, 1), visualize=False,
            feature_vector=True, multichannel=True)

    if return_feature_names:
        feature_names = [f'edge-hog-{i}' for i in range(len(x))]
        return x, feature_names
    return x

def edge_direction_descriptors(imgs: np.ndarray) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for each image by handcrafted edge features.

    The images are normalized by converting to color image.

    Args
    ----
    img : np.ndarray
        The image to extract features.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name

    X = np.array([edge_direction_descriptors_single(img, False)
                  for img in imgs])

    # reduce the dimension of hog features to save space
    n_components = 50

    n_samples, n_features = X.shape
    n_components_actual = min(n_samples, n_features, n_components)

    reducer = decomposition.TruncatedSVD(n_components=n_components_actual)
    X_proj = reducer.fit_transform(X)
    if n_components > n_components_actual:
        zeros = np.zeros((n_samples, n_components -
                         n_components_actual), dtype=float)
        X_proj = np.hstack((X_proj, zeros))

    feature_names = [f'edge-hog-{i}' for i in range(n_components)]
    return X_proj, feature_names

def texture_descriptors_single(img: np.ndarray,
                               return_feature_names: bool = True) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for a single image by handcrafted texture features.

    The images are normalized by converting to gray image.

    Args
    ----
    img : np.ndarray
        The image to extract features.
    return_feature_names : bool
        Whether to return feature names.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name, too-many-locals

    # normalize the image to gray image
    img_gray = img if len(img.shape) == 2 else cv.cvtColor(
        img, cv.COLOR_BGR2GRAY)

    # local binary pattern
    radius = 3
    n_points = 8 * radius
    img_lbp = local_binary_pattern(
        img_gray, n_points, radius, method='uniform')
    lbp_hist, _ = np.histogram(img_lbp, bins=n_points+1, range=(0, n_points+1))

    # grey level co-occurrence matrix
    distances = [1, 2]
    angles = [0, np.pi/4, np.pi/2, 3*np.pi/4]
    glcm = greycomatrix(img_gray, distances, angles,
                        256, symmetric=True, normed=True)
    contrast = greycoprops(glcm, 'contrast').reshape(-1)
    dissimilarity = greycoprops(glcm, 'dissimilarity').reshape(-1)
    homogeneity = greycoprops(glcm, 'homogeneity').reshape(-1)
    energy = greycoprops(glcm, 'energy').reshape(-1)
    correlation = greycoprops(glcm, 'correlation').reshape(-1)

    # entropy
    global_entropy = shannon_entropy(img_gray)

    # local entropy
    n_hist_bins = 20
    mask = disk(7)
    local_entropy = entropy(img_gray, mask)
    # for images with value range [0, 255], the maximum local entropy is 8
    entropy_upper_bound = 8
    local_entropy_hist, _ = np.histogram(
        local_entropy, bins=n_hist_bins, range=(0, entropy_upper_bound))

    x = [*lbp_hist, *contrast, *dissimilarity, *homogeneity, *
         energy, *correlation, global_entropy, *local_entropy_hist]

    if return_feature_names:
        feature_names = [f'texture-lbp[{i}]' for i in range(len(lbp_hist))]\
            + [f'texture-contrast[{i}]' for i in range(len(contrast))]\
            + [f'texture-dissimilarity[{i}]' for i in range(len(dissimilarity))]\
            + [f'texture-homogeneity[{i}]' for i in range(len(homogeneity))]\
            + [f'texture-energy[{i}]' for i in range(len(energy))]\
            + [f'texture-correlation[{i}]' for i in range(len(correlation))]\
            + ['entropy']\
            + [f'texture-localentropy[{i}]' for i in range(len(local_entropy_hist))]
        return x, feature_names
    return x

def texture_descriptors(imgs: np.ndarray) -> Tuple[np.ndarray, List[str]]:
    """
    Extract features for each image by handcrafted edge features.

    The images are normalized by converting to color image.

    Args
    ----
    img : np.ndarray
        The image to extract features.

    Returns
    -------
    x : np.ndarray
        The extracted feature values.
    feature_names : List[str]
        The names of features.
    """
    # pylint: disable=invalid-name

    X = np.array([texture_descriptors_single(img, False) for img in imgs])

    feature_names = [f'texture-lbp[{i}]' for i in range(25)]\
        + [f'texture-contrast[{i}]' for i in range(8)]\
        + [f'texture-dissimilarity[{i}]' for i in range(8)]\
        + [f'texture-homogeneity[{i}]' for i in range(8)]\
        + [f'texture-energy[{i}]' for i in range(8)]\
        + [f'texture-correlation[{i}]' for i in range(8)]\
        + ['entropy']\
        + [f'texture-localentropy[{i}]' for i in range(20)]
    return X, feature_names

def extract_features(data_objects: List[DataObject]
                     ) -> Tuple[List[List[float]], List[str]]:
    imgs = [get_image(data_object) for data_object in data_objects]

    extractors = [
        raw_flatten,
        resize_SVD,
        color_descriptors,
        edge_descriptors,
        edge_direction_descriptors,
        texture_descriptors,
    ]

    X = None
    feature_names = []
    for extractor in extractors:
        X_tmp, feature_names_tmp = extractor(imgs)
        X = np.hstack((X, X_tmp)) if X is not None else X_tmp
        feature_names = feature_names + feature_names_tmp\
            if feature_names is not None else feature_names_tmp
    return X.tolist(), feature_names

class Handler(RequestHandler):
    """
    The handler for feature extraction - image bag of words.
    """

    def post(self):
        self.set_header('Access-Control-Allow-Origin', '*')
        json_data = json.loads(self.request.body)

        # process input: (dataObjects)
        data_objects: List[DataObject] = json_data['dataObjects']

        features, feature_names = extract_features(data_objects)

        self.write({
            'features': features,
            'featureNames': feature_names,
        })

max_stars_repo_name: microsoft/OneLabeler

max_issues_repo_name: microsoft/OneLabeler

max_forks_repo_name: microsoft/OneLabeler


text:
from __future__ import print_function, division

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import time
import os
import copy
import pickle

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
plt.ion()

import get_mean_std_dataset as gm
#-------------------------------------------------------------------
#Loading data.
#Chose source file
file = "../../Datasets/mini"

#get mean and std for Data normalisation
params = gm.get_mean_std(file)
mymean, mystderr = params["mean"], params["stderr"]

#Define transformation for each dataset, training and validation.
data_transform = {
    'train': transforms.Compose([
    transforms.RandomResizedCrop(96, scale=(0.5, 1)),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(mean=mymean,
                        std=mystderr)
]),
    'val': transforms.Compose([
    transforms.Resize(96),
    transforms.CenterCrop(96),
    transforms.ToTensor(),
    transforms.Normalize(mean=mymean,
                        std=mystderr)
])
    }

#Defines two datasets:
#amphiprion_dataset_t["train"]
#amphiprion_dataset_t["val"]
#apply the appropriate transformation for each of them

amphiprion_dataset_t = {X: datasets.ImageFolder(root=os.path.join(file,X),
                                           transform=data_transform[X])
                        for X in ["train", "val"]}

#Loader allow to retrieve transformed images by batch, randomly sampled

dataset_loader_t = {X: torch.utils.data.DataLoader(amphiprion_dataset_t[X],
                                             batch_size=16, shuffle=True,
                                             num_workers=4)
                    for X in ["train", "val"]}

#Extract parameter of the data
dataset_sizes = {X: len(amphiprion_dataset_t[X]) for X in ['train', 'val']}

class_names = amphiprion_dataset_t["train"].classes

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

#-------------------------------------------------------------------
#Define a few useful function

#Take a tensor, the format in which the images are "stored" in the loader
#and add it to a plot.
def imshow(inp, title=None):
    """Imshow for Tensor.
    Tensor stores image as (C x H x W) in the range [0.0, 1.0].
    Numpy as (C x H x W) in the range [0.0, 1.0]"""

    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array(mymean)
    std = np.array(mystderr)
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    plt.imshow(inp)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # pause a bit so that plots are updated

# Make a grid from the bach images.
def Mkgrid():
    """Makes a grid with a dataloade batch, takes no argument,requires Imshow"""
    inputs, classes = next(iter(dataset_loader_t['train']))
    out = torchvision.utils.make_grid(inputs)
    half = round(len(classes)/2) -1
    print(half)
    titles = [class_names[x].split("_")[1][0:3] for x in classes]
    titles[half] = "".join([titles[half], "\n"])
    title_joined = ", ".join(titles)

    imshow(out, title=title_joined)
    plt.savefig("grid.jpg")

#------------------------------------------------------------------
#Define the architecture of the network.

class Net96(nn.Module):
    def __init__(self): #only defines the architecture of the net
        super(Net96, self).__init__()
        self.conv1 = nn.Conv2d(3,70, 5, stride = 1)
        # 3 input image channel,
        # 70 output
        # 5x5 square convolution kernel
        self.conv2 = nn.Conv2d(70, 110, 3)
        #70 inputs as it is the output of conv1
        self.conv3 = nn.Conv2d(110,180,3)
        #110 input, 180 out
        self.fc1 = nn.Linear(180*9*9, 2000)
        # an affine operation: y = Wx + b
        #400 input, 120 output
        self.fc2 = nn.Linear(2000, 200)
        self.fc3 = nn.Linear(200, 2)

    def forward(self, x): #defines the method, how the input goes throught the element described above
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), 3, stride = 2) #passes through con1, relu, and max pooled
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 3, stride = 2) #now through conv2
        x = F.max_pool2d(F.relu(self.conv3(x)), 3, stride = 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

#Instantiate the model
net = Net96()

#defines the loss function, the optimizer and schduler.
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
scheduler = lr_scheduler.StepLR(optimizer,step_size=7, gamma=0.1 )

def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
    since = time.time()

    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0

    for epoch in range(num_epochs): #50 x
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        # Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            if phase == 'train':
                scheduler.step()
                model.train()  # Set model to training mode
            else:
                model.eval()   # Set model to evaluate mode

            running_loss = 0.0
            running_corrects = 0

            # Iterate over data.
            for inputs, labels in dataset_loader_t[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model(inputs)
                    _, preds = torch.max(outputs, 1)
                    loss = criterion(outputs, labels)

                    # backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                # statistics
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss / dataset_sizes[phase]
            epoch_acc = running_corrects.double() / dataset_sizes[phase]

            if phase == "train":
                loss_train.append(epoch_loss)
                acc_train.append(epoch_acc)
            else:
                loss_val.append(epoch_loss)
                acc_val.append(epoch_acc)

            print('{} Loss: {:.4f} Acc: {:.4f}'.format(
                phase, epoch_loss, epoch_acc))

            # deep copy the model
            if phase == 'val' and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_model_wts = copy.deepcopy(model.state_dict())

        print()

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))
    print('Best val Acc: {:4f}'.format(best_acc))

    # load best model weights
    model.load_state_dict(best_model_wts)
    return model

#creates a list to  store the loss value at each Epoch
loss_train = []
loss_val = []
acc_train = []
acc_val = []

#train the instanciated model
train_model(net, criterion, optimizer, scheduler, num_epochs=50)

#save the best parameters of the model.
torch.save(net.state_dict(), "trainedRI_2fam1.pt")

#save the loss and accuracy
pickle.dump(loss_train, open("loss_train", "wb"))
pickle.dump(loss_val, open("loss_val", "wb"))

pickle.dump(acc_train, open("loss_val", "wb"))
pickle.dump(acc_val, open("acc_val", "wb"))

# plot the loss
fig, ax = plt.subplots()
ax.plot(range(len(loss_train)), loss_train)

ax.set(xlabel='Epoch', ylabel='loss',
       title='Training loss')
ax.grid()

fig.savefig("training_process.png")

fig, ax = plt.subplots()
ax.plot(range(len(loss_val)), loss_val)

ax.set(xlabel='Epoch', ylabel='loss',
       title='Validation loss')
ax.grid()

fig.savefig("validation_loss.png")


text:
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import numpy as np

from openvino.tools.mo.ops.elementwise import Mul, Add
from openvino.tools.mo.front.common.replacement import FrontReplacementPattern
from openvino.tools.mo.front.tf.graph_utils import create_op_with_const_inputs
from openvino.tools.mo.graph.graph import Graph, Node, rename_node

class Sub(FrontReplacementPattern):
    # This transformation is called directly from the 'openvino/tools/mo/middle/fusings.py' transformation
    enabled = False

    @staticmethod
    def sub_to_add_replacement(sub: Node):
        # we execute this transformation for V10 IR later on middle phase despite graph_condition
        # so we prevent Sub replacement on shape-calculating sub-graphs
        if sub.in_port(0).data.get_value() is not None and sub.in_port(1).data.get_value() is not None:
            return

        graph = sub.graph
        name = sub.soft_get('name', sub.id)

        # keep Add name the same as Sub -- because of mathematical equality of output tensors
        rename_node(node=sub, name=name + '/to_be_removed')

        # reconnect Sub in(out)puts to Add
        add = Add(graph, {'name': name}).create_node()
        rename_node(add, name)

        sub.in_port(0).get_connection().set_destination(add.in_port(0))
        sub.in_port(1).get_connection().set_destination(add.in_port(1))
        sub.out_port(0).get_connection().set_source(add.out_port(0))

        # restore mathematical equivalence to Sub operation: Sub(A, B) = Add(A, Mul(B, -1))
        const_dtype = sub.soft_get('data_type', np.float32)
        negate = create_op_with_const_inputs(graph, Mul, {1: np.array(-1, dtype=const_dtype)}, {'name': name + '/neg_'})
        add.in_port(1).get_connection().insert_node(negate)

    def find_and_replace_pattern(self, graph: Graph):
        for sub in graph.get_op_nodes(op='Sub'):
            self.sub_to_add_replacement(sub)

max_stars_repo_name: ytorzuk-altran/openvino

max_issues_repo_name: ytorzuk-altran/openvino

max_forks_repo_name: ytorzuk-altran/openvino