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

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

import cv2
import numpy as np

def forward_convert(coordinate, with_label=True):
    """
    :param coordinate: format [x_c, y_c, w, h, theta]
    :return: format [x1, y1, x2, y2, x3, y3, x4, y4]
    """
    boxes = []
    if with_label:
        for rect in coordinate:
            box = cv2.boxPoints(((rect[0], rect[1]), (rect[2], rect[3]), rect[4]))
            box = np.reshape(box, [-1, ])
            boxes.append([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7], rect[5]])
    else:
        for rect in coordinate:
            box = cv2.boxPoints(((rect[0], rect[1]), (rect[2], rect[3]), rect[4]))
            box = np.reshape(box, [-1, ])
            boxes.append([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7]])

    return np.array(boxes, dtype=np.float32)

def back_forward_convert(coordinate, with_label=True):
    """
    :param coordinate: format [x1, y1, x2, y2, x3, y3, x4, y4, (label)]
    :param with_label: default True
    :return: format [x_c, y_c, w, h, theta, (label)]
    """

    boxes = []
    if with_label:
        for rect in coordinate:
            box = np.int0(rect[:-1])
            box = box.reshape([4, 2])
            rect1 = cv2.minAreaRect(box)

            x, y, w, h, theta = rect1[0][0], rect1[0][1], rect1[1][0], rect1[1][1], rect1[2]
            boxes.append([x, y, w, h, theta, rect[-1]])

    else:
        for rect in coordinate:
            box = np.int0(rect)
            box = box.reshape([4, 2])
            rect1 = cv2.minAreaRect(box)

            x, y, w, h, theta = rect1[0][0], rect1[0][1], rect1[1][0], rect1[1][1], rect1[2]
            boxes.append([x, y, w, h, theta])

    return np.array(boxes, dtype=np.float32)

if __name__ == '__main__':
    coord = np.array([[150, 150, 50, 100, -90, 1],
                      [150, 150, 100, 50, -90, 1],
                      [150, 150, 50, 100, -45, 1],
                      [150, 150, 100, 50, -45, 1]])

    coord1 = np.array([[150, 150, 100, 50, 0],
                      [150, 150, 100, 50, -90],
                      [150, 150, 100, 50, 45],
                      [150, 150, 100, 50, -45]])

    coord2 = forward_convert(coord)
    # coord3 = forward_convert(coord1, mode=-1)
    print(coord2)
    # print(coord3-coord2)
    # coord_label = np.array([[167., 203., 96., 132., 132., 96., 203., 167., 1.]])
    #
    # coord4 = back_forward_convert(coord_label, mode=1)
    # coord5 = back_forward_convert(coord_label)

    # print(coord4)
    # print(coord5)

    # coord3 = coordinate_present_convert(coord, -1)
    # print(coord3)
    # coord4 = coordinate_present_convert(coord3, mode=1)
    # print(coord4)


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

import re
import traceback

from absl.testing import absltest
from absl.testing import parameterized

import jax
from jax import core, grad, jit, vmap, lax
import jax.numpy as jnp
from jax import test_util as jtu
from jax._src import source_info_util
from jax._src import traceback_util

from jax.config import config
config.parse_flags_with_absl()
FLAGS = config.FLAGS

def get_exception(etype, f):
  try:
    f()
  except etype as e:
    return e
  assert False

def check_filtered_stack_trace(test, etype, f, frame_patterns=[],
                               filter_mode="remove_frames"):
  with jax._src.config.traceback_filtering(filter_mode):
    test.assertRaises(etype, f)
    e = get_exception(etype, f)
  c = e.__cause__
  if filter_mode == "remove_frames":
    test.assertIsInstance(c, traceback_util.UnfilteredStackTrace)
  else:
    test.assertFalse(isinstance(c, traceback_util.UnfilteredStackTrace))

  if frame_patterns:
    frames = []
    for frame, lineno in traceback.walk_tb(e.__traceback__):
      if filter_mode == "tracebackhide":
        if "__tracebackhide__"  in frame.f_locals.keys():
          continue
      frames.append((frame, lineno))

    c_tb = traceback.format_list(traceback.StackSummary.extract(frames))
    for (fname_pat, line_pat), frame_fmt in zip(
        reversed(frame_patterns), reversed(c_tb)):
      file = re.escape(__file__)
      fname_pat = re.escape(fname_pat)
      line_pat = re.escape(line_pat)
      full_pat = (
          f'  File "{file}", line ' r'[0-9]+'
          f', in {fname_pat}' r'\n\s*' f'{line_pat}')
      test.assertRegex(frame_fmt, full_pat)

@parameterized.named_parameters(
  {"testcase_name": f"_{f}", "filter_mode": f}
  for f in ("tracebackhide", "remove_frames"))
class FilteredTracebackTest(jtu.JaxTestCase):

  def test_nested_jit(self, filter_mode):
    @jit
    def innermost(x):
      assert False
    @jit
    def inbetween(x):
      return 1 + innermost(x)
    @jit
    def outermost(x):
      return 2 + inbetween(x)

    f = lambda: outermost(jnp.array([1, 2]))

    check_filtered_stack_trace(self, AssertionError, f, [
        ('<lambda>', 'f = lambda: outermost'),
        ('outermost', 'return 2 + inbetween(x)'),
        ('inbetween', 'return 1 + innermost(x)'),
        ('innermost', 'assert False')],
        filter_mode=filter_mode)

  def test_nested_jit_and_vmap(self, filter_mode):
    @jit
    def innermost(x):
      assert False
    @jit
    def inbetween(x):
      return 1 + vmap(innermost)(x)
    @jit
    def outermost(x):
      return 2 + inbetween(x)

    f = lambda: outermost(jnp.array([1, 2]))

    check_filtered_stack_trace(self, AssertionError, f, [
        ('<lambda>', 'f = lambda: outermost'),
        ('outermost', 'return 2 + inbetween(x)'),
        ('inbetween', 'return 1 + vmap(innermost)(x)'),
        ('innermost', 'assert False')],
        filter_mode=filter_mode)

  def test_nested_jit_and_grad(self, filter_mode):
    @jit
    def innermost(x):
      assert False
    @jit
    def inbetween(x):
      return 1 + grad(innermost)(x)
    @jit
    def outermost(x):
      return 2 + inbetween(x)

    f = lambda: outermost(jnp.array([1, 2]))

    check_filtered_stack_trace(self, TypeError, f, [
        ('<lambda>', 'f = lambda: outermost'),
        ('outermost', 'return 2 + inbetween(x)'),
        ('inbetween', 'return 1 + grad(innermost)(x)'),
    ], filter_mode=filter_mode)

  def test_lax_cond(self, filter_mode):
    def err(_):
      assert False
      return ()

    def f():
      return lax.cond(True, err, lambda _: (), ())

    check_filtered_stack_trace(self, AssertionError, f, [
        ('f', 'return lax.cond(True, err, lambda _: (), ())'),
        ('err', 'assert False')],
        filter_mode=filter_mode)

  def test_lax_switch(self, filter_mode):
    def err(_):
      assert False
      return ()

    def f():
      branches = [lambda _: (), err, lambda _: ()]
      return lax.switch(1, branches, ())

    check_filtered_stack_trace(self, AssertionError, f, [
        ('f', 'return lax.switch(1, branches, ())'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_lax_scan(self, filter_mode):
    def err(*_):
      assert False
      return ()

    def f():
      return lax.scan(err, (), (), 3)

    check_filtered_stack_trace(self, AssertionError, f, [
        ('f', 'return lax.scan(err, (), (), 3)'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_lax_fori_loop(self, filter_mode):
    def err(*_):
      assert False
      return ()

    def f():
      return lax.fori_loop(0, 3, err, ())

    check_filtered_stack_trace(self, AssertionError, f, [
        ('f', 'return lax.fori_loop(0, 3, err, ())'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_lax_while_loop(self, filter_mode):
    def err(*_):
      assert False
      return ()

    def f():
      pred = lambda _: False
      return lax.while_loop(pred, err, ())

    check_filtered_stack_trace(self, AssertionError, f, [
        ('f', 'return lax.while_loop(pred, err, ())'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_lax_map(self, filter_mode):
    def err(_):
      assert False
      return ()

    def f():
      xs = jnp.ones(3)
      return lax.map(err, xs)

    check_filtered_stack_trace(self, AssertionError, f, [
        ('f', 'return lax.map(err, xs)'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_lax_custom_root(self, filter_mode):
    def err(*_):
      assert False
      return ()

    def g(x): return (x - 1.) ** 2.
    def solve(*_): return 1.

    def f1():
      return lax.custom_root(g, 0., err, solve)
    def f2():
      return lax.custom_root(g, 0., solve, err)
    def f3():
      return lax.custom_root(err, 0., solve, solve)

    check_filtered_stack_trace(self, AssertionError, f1, [
        ('f1', 'return lax.custom_root(g, 0., err, solve)'),
        ('err', 'assert False')], filter_mode=filter_mode)
    check_filtered_stack_trace(self, AssertionError, f2, [
        ('f2', 'return lax.custom_root(g, 0., solve, err)'),
        ('err', 'assert False')], filter_mode=filter_mode)
    check_filtered_stack_trace(self, AssertionError, f3, [
        ('f3', 'return lax.custom_root(err, 0., solve, solve)'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_lax_custom_linear_solve(self, filter_mode):
    def err(*_):
      assert False
      return ()

    matvec = lambda v: v
    solve = lambda mv, b: 1.
    b = 1.

    def f1():
      return lax.custom_linear_solve(err, b, solve)
    def f2():
      return lax.custom_linear_solve(matvec, b, err)

    check_filtered_stack_trace(self, AssertionError, f1, [
        ('f1', 'return lax.custom_linear_solve(err, b, solve)'),
        ('err', 'assert False')], filter_mode=filter_mode)
    check_filtered_stack_trace(self, AssertionError, f2, [
        ('f2', 'return lax.custom_linear_solve(matvec, b, err)'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_lax_associative_scan(self, filter_mode):
    def err(*_):
      assert False
      return ()

    def f():
      xs = jnp.arange(4.)
      return lax.associative_scan(err, xs)

    check_filtered_stack_trace(self, AssertionError, f, [
        ('f', 'return lax.associative_scan(err, xs)'),
        ('err', 'assert False')], filter_mode=filter_mode)

  def test_cause_chain(self, filter_mode):
    @jit
    def inner(x):
      raise ValueError('inner')
    @jit
    def outer(x):
      try:
        inner(x)
      except ValueError as e:
        raise TypeError('outer') from e

    f = lambda: outer(1.)

    check_filtered_stack_trace(self, TypeError, f, [
        ('<lambda>', 'f = lambda: outer'),
        ('outer', 'raise TypeError')], filter_mode=filter_mode)
    e = get_exception(TypeError, f)
    self.assertIsInstance(e.__cause__, traceback_util.UnfilteredStackTrace)
    self.assertIsInstance(e.__cause__.__cause__, ValueError)

class UserContextTracebackTest(jtu.JaxTestCase):

  def test_grad_norm(self):
    e = None
    try:
      with jax.debug_nans(True):
        jax.grad(jnp.linalg.norm)(jnp.zeros((3, 3), jnp.float32))
    except FloatingPointError as exc:
      e = exc
    self.assertIsNot(e, None)
    self.assertIn("invalid value", str(e))
    self.assertIsInstance(
        e.__cause__.__cause__,
        source_info_util.JaxStackTraceBeforeTransformation)

class CustomErrorsTest(jtu.JaxTestCase):
  @parameterized.named_parameters(jtu.cases_from_list(
    {"testcase_name": "_{}".format(errorclass), "errorclass": errorclass}
     for errorclass in dir(jax.errors)
     if errorclass.endswith('Error') and errorclass not in ['JaxIndexError', 'JAXTypeError']))
  def testErrorsURL(self, errorclass):
    class FakeTracer(core.Tracer):
      aval = None
    ErrorClass = getattr(jax.errors, errorclass)
    err = ErrorClass(FakeTracer(None))

    self.assertIn(f'https://jax.readthedocs.io/en/latest/errors.html#jax.errors.{errorclass}', str(err))

if __name__ == '__main__':
  absltest.main(testLoader=jtu.JaxTestLoader())

ext: py

max_stars_repo_name: adam-lewis-gcorp/jax

max_issues_repo_name: adam-lewis-gcorp/jax

max_forks_repo_name: adam-lewis-gcorp/jax

reason: import jax,from jax


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import numpy as np
from astropy.stats import sigma_clip

__all__ = ['find_imgcuts', 'img_stats', 'rescale_img', 'scale_linear',
           'scale_sqrt', 'scale_power', 'scale_log', 'scale_asinh',]

# from scikit-image
DTYPE_RANGE = {np.bool_: (False, True),
               np.bool8: (False, True),
               np.uint8: (0, 255),
               np.uint16: (0, 65535),
               np.int8: (-128, 127),
               np.int16: (-32768, 32767),
               np.float32: (-1, 1),
               np.float64: (-1, 1)}
DTYPE_RANGE.update((d.__name__, limits) for d, limits in list(DTYPE_RANGE.items()))
DTYPE_RANGE.update({'uint10': (0, 2**10 - 1), 'uint12': (0, 2**12 - 1),
                    'uint14': (0, 2**14 - 1), 'bool': DTYPE_RANGE[np.bool_],
                    'float': DTYPE_RANGE[np.float64]})

def find_imgcuts(image, min_cut=None, max_cut=None, min_percent=None,
                 max_percent=None, percent=None):
    """
    Find minimum and maximum image cut levels from percentiles of the
    image values.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    min_cut : float, optional
        The minimum cut level.  Data values less than ``min_cut`` will
        set to ``min_cut`` before scaling the image.

    max_cut : float, optional
        The maximum cut level.  Data values greater than ``max_cut``
        will set to ``max_cut`` before scaling the image.

    min_percent : float, optional
        The minimum cut level as a percentile of the values in the
        image.  If ``min_cut`` is input, then ``min_percent`` will be
        ignored.

    max_percent : float, optional
        The maximum cut level as a percentile of the values in the
        image.  If ``max_cut`` is input, then ``max_percent`` will be
        ignored.

    percent : float, optional
        The percentage of the image values to scale.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  This value overrides the values of ``min_percent``
        and ``max_percent``, but is ignored if ``min_cut`` and
        ``max_cut`` are both input.

    Returns
    -------
    out : tuple
        Returns a tuple containing (``min_cut``, ``max_cut``) image cut
        levels.
    """

    if min_cut is not None and max_cut is not None:
        return min_cut, max_cut
    if percent:
        assert (percent >= 0) and (percent <= 100.0), 'percent must be >= 0 and <= 100.0'
        if not min_percent and not max_percent:
            min_percent = (100.0 - float(percent)) / 2.0
            max_percent = 100.0 - min_percent
    if min_cut is None:
        if min_percent is None:
            min_percent = 0.0
        assert min_percent >= 0, 'min_percent must be >= 0'
        min_cut = np.percentile(image, min_percent)
    if max_cut is None:
        if max_percent is None:
            max_percent = 100.0
        assert max_percent <= 100.0, 'max_percent must be <= 100.0'
        max_cut = np.percentile(image, max_percent)
    assert min_cut <= max_cut, 'min_cut must be <= max_cut'
    return min_cut, max_cut

def img_stats(image, image_mask=None, mask_val=None, sig=3.0, iters=None):
    """
    Perform sigma-clipped statistics on an image.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    image_mask : array_like, bool, optional
        A boolean mask with the same shape as ``image``, where a `True`
        value indicates the corresponding element of ``image`` is
        invalid.  Masked pixels are ignored when computing the image
        statistics.

    mask_val : float, optional
        An image data value (e.g., ``0.0``) that is ignored when
        computing the image statistics.  ``mask_val`` will be ignored if
        ``image_mask`` is input.

    sig : float, optional
        The number of standard deviations to use as the clipping limit.

    iters : float, optional
       The number of iterations to perform clipping, or `None` to clip
       until convergence is achieved (i.e. continue until the last
       iteration clips nothing).

    Returns
    -------
    stats : tuple
        Returns a tuple of the (``mean``, ``median``, ``stddev``) of the
        sigma-clipped image.
    """

    if image_mask:
        image = image[~image_mask]
    if mask_val and not image_mask:
        idx = (image != mask_val).nonzero()
        image = image[idx]
    image_clip = sigma_clip(image, sig=sig, iters=iters)
    goodvals = image_clip.data[~image_clip.mask]
    return np.mean(goodvals), np.median(goodvals), np.std(goodvals)

def rescale_img(image, min_cut=None, max_cut=None, min_percent=None,
                max_percent=None, percent=None):
    """
    Rescale image values between minimum and maximum cut levels to
    values between 0 and 1, inclusive.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    min_cut : float, optional
        The minimum cut level.  Data values less than ``min_cut`` will
        set to ``min_cut`` before scaling the image.

    max_cut : float, optional
        The maximum cut level.  Data values greater than ``max_cut``
        will set to ``max_cut`` before scaling the image.

    min_percent : float, optional
        The minimum cut level as a percentile of the values in the
        image.  If ``min_cut`` is input, then ``min_percent`` will be
        ignored.

    max_percent : float, optional
        The maximum cut level as a percentile of the values in the
        image.  If ``max_cut`` is input, then ``max_percent`` will be
        ignored.

    percent : float, optional
        The percentage of the image values to scale.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  This value overrides the values of ``min_percent``
        and ``max_percent``, but is ignored if ``min_cut`` and
        ``max_cut`` are both input.

    Returns
    -------
    out : tuple
        Returns a tuple containing (``outimg``, ``min_cut``,
        ``max_cut``), which are the output scaled image and the minimum
        and maximum cut levels.
    """

    image = image.astype(np.float64)
    min_cut, max_cut = find_imgcuts(image, min_cut=min_cut, max_cut=max_cut,
                                    min_percent=min_percent,
                                    max_percent=max_percent, percent=percent)
    outimg = rescale_intensity(image, in_range=(min_cut, max_cut),
                               out_range=(0, 1))
    return outimg, min_cut, max_cut

def scale_linear(image, min_cut=None, max_cut=None, min_percent=None,
                 max_percent=None, percent=None):
    """
    Perform linear scaling of an image between minimum and maximum cut
    levels.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    min_cut : float, optional
        The minimum cut level.  Data values less than ``min_cut`` will
        set to ``min_cut`` before scaling the image.

    max_cut : float, optional
        The maximum cut level.  Data values greater than ``max_cut``
        will set to ``max_cut`` before scaling the image.

    min_percent : float, optional
        The minimum cut level as a percentile of the values in the
        image.  If ``min_cut`` is input, then ``min_percent`` will be
        ignored.

    max_percent : float, optional
        The maximum cut level as a percentile of the values in the
        image.  If ``max_cut`` is input, then ``max_percent`` will be
        ignored.

    percent : float, optional
        The percentage of the image values to scale.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  This value overrides the values of ``min_percent``
        and ``max_percent``, but is ignored if ``min_cut`` and
        ``max_cut`` are both input.

    Returns
    -------
    scaled_image : array_like
        The 2D array of the scaled/stretched image.
    """

    result = rescale_img(image, min_cut=min_cut, max_cut=max_cut,
                         min_percent=min_percent, max_percent=max_percent,
                         percent=percent)
    return result[0]

def scale_sqrt(image, min_cut=None, max_cut=None, min_percent=None,
               max_percent=None, percent=None):
    """
    Perform square-root scaling of an image between minimum and maximum
    cut levels.  This is equivalent to using `scale_power` with a
    ``power`` of ``0.5``.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    min_cut : float, optional
        The minimum cut level.  Data values less than ``min_cut`` will
        set to ``min_cut`` before scaling the image.

    max_cut : float, optional
        The maximum cut level.  Data values greater than ``max_cut``
        will set to ``max_cut`` before scaling the image.

    min_percent : float, optional
        The minimum cut level as a percentile of the values in the
        image.  If ``min_cut`` is input, then ``min_percent`` will be
        ignored.

    max_percent : float, optional
        The maximum cut level as a percentile of the values in the
        image.  If ``max_cut`` is input, then ``max_percent`` will be
        ignored.

    percent : float, optional
        The percentage of the image values to scale.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  This value overrides the values of ``min_percent``
        and ``max_percent``.

    Returns
    -------
    scaled_image : array_like
        The 2D array of the scaled/stretched image.
    """

    result = rescale_img(image, min_cut=min_cut, max_cut=max_cut,
                         min_percent=min_percent, max_percent=max_percent,
                         percent=percent)
    return np.sqrt(result[0])

def scale_power(image, power, min_cut=None, max_cut=None, min_percent=None,
                max_percent=None, percent=None):
    """
    Perform power scaling of an image between minimum and maximum cut
    levels.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    power : float
        The power index for the image scaling.

    min_cut : float, optional
        The minimum cut level.  Data values less than ``min_cut`` will
        set to ``min_cut`` before scaling the image.

    max_cut : float, optional
        The maximum cut level.  Data values greater than ``max_cut``
        will set to ``max_cut`` before scaling the image.

    min_percent : float, optional
        The minimum cut level as a percentile of the values in the
        image.  If ``min_cut`` is input, then ``min_percent`` will be
        ignored.

    max_percent : float, optional
        The maximum cut level as a percentile of the values in the
        image.  If ``max_cut`` is input, then ``max_percent`` will be
        ignored.

    percent : float, optional
        The percentage of the image values to scale.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  This value overrides the values of ``min_percent``
        and ``max_percent``, but is ignored if ``min_cut`` and
        ``max_cut`` are both input.

    Returns
    -------
    scaled_image : array_like
        The 2D array of the scaled/stretched image.
    """

    result = rescale_img(image, min_cut=min_cut, max_cut=max_cut,
                         min_percent=min_percent, max_percent=max_percent,
                         percent=percent)
    return (result[0])**power

def scale_log(image, min_cut=None, max_cut=None, min_percent=None,
              max_percent=None, percent=None):
    """
    Perform logarithmic (base 10) scaling of an image between minimum and
    maximum cut levels.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    min_cut : float, optional
        The minimum cut level.  Data values less than ``min_cut`` will
        set to ``min_cut`` before scaling the image.

    max_cut : float, optional
        The maximum cut level.  Data values greater than ``max_cut``
        will set to ``max_cut`` before scaling the image.

    min_percent : float, optional
        The minimum cut level as a percentile of the values in the
        image.  If ``min_cut`` is input, then ``min_percent`` will be
        ignored.

    max_percent : float, optional
        The maximum cut level as a percentile of the values in the
        image.  If ``max_cut`` is input, then ``max_percent`` will be
        ignored.

    percent : float, optional
        The percentage of the image values to scale.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  This value overrides the values of ``min_percent``
        and ``max_percent``, but is ignored if ``min_cut`` and
        ``max_cut`` are both input.

    Returns
    -------
    scaled_image : array_like
        The 2D array of the scaled/stretched image.
    """

    result = rescale_img(image, min_cut=min_cut, max_cut=max_cut,
                         min_percent=min_percent, max_percent=max_percent,
                         percent=percent)
    outimg = np.log10(result[0] + 1.0) / np.log10(2.0)
    return outimg

def scale_asinh(image, noise_level=None, sigma=2.0, min_cut=None,
                max_cut=None, min_percent=None, max_percent=None,
                percent=None):
    """
    Perform inverse hyperbolic sine (arcsinh) scaling of an image
    between minimum and maximum cut levels.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    noise_level: float, optional
        The noise level of the image.  Levels less than noise_level will
        approximately be linearly scaled, while levels greater than
        noise_level will approximately be logarithmically scaled.

    sigma: float, optional
        The number of standard deviations of the background noise used
        to estimate the absolute noise level.  This value is ignored if
        ``noise_level`` is input.

    min_cut : float, optional
        The minimum cut level.  Data values less than ``min_cut`` will
        set to ``min_cut`` before scaling the image.

    max_cut : float, optional
        The maximum cut level.  Data values greater than ``max_cut``
        will set to ``max_cut`` before scaling the image.

    min_percent : float, optional
        The minimum cut level as a percentile of the values in the
        image.  If ``min_cut`` is input, then ``min_percent`` will be
        ignored.

    max_percent : float, optional
        The maximum cut level as a percentile of the values in the
        image.  If ``max_cut`` is input, then ``max_percent`` will be
        ignored.

    percent : float, optional
        The percentage of the image values to scale.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  This value overrides the values of ``min_percent``
        and ``max_percent``, but is ignored if ``min_cut`` and
        ``max_cut`` are both input.

    Returns
    -------
    scaled_image : array_like
        The 2D array of the scaled/stretched image.
    """

    result = rescale_img(image, min_cut=min_cut, max_cut=max_cut,
                         min_percent=min_percent, max_percent=max_percent,
                         percent=percent)
    outimg, min_cut, max_cut = result
    if not noise_level:
        mean, median, stddev = img_stats(outimg)
        noise_level = mean + (sigma * stddev)
    z = (noise_level - min_cut) / (max_cut - min_cut)
    outimg = np.arcsinh(outimg / z) / np.arcsinh(1.0 / z)
    return outimg

# from scikit-image
def rescale_intensity(image, in_range=None, out_range=None):
    """Return image after stretching or shrinking its intensity levels.

    The image intensities are uniformly rescaled such that the minimum and
    maximum values given by `in_range` match those given by `out_range`.

    Parameters
    ----------
    image : array
        Image array.

    in_range : 2-tuple (float, float) or str
        Min and max *allowed* intensity values of input image. If None, the
        *allowed* min/max values are set to the *actual* min/max values in the
        input image. Intensity values outside this range are clipped.
        If string, use data limits of dtype specified by the string.

    out_range : 2-tuple (float, float) or str
        Min and max intensity values of output image. If None, use the min/max
        intensities of the image data type. See `skimage.util.dtype` for
        details. If string, use data limits of dtype specified by the string.

    Returns
    -------
    out : array
        Image array after rescaling its intensity. This image is the same dtype
        as the input image.

    Examples
    --------
    By default, intensities are stretched to the limits allowed by the dtype:

    >>> image = np.array([51, 102, 153], dtype=np.uint8)
    >>> rescale_intensity(image)
    array([  0, 127, 255], dtype=uint8)

    It's easy to accidentally convert an image dtype from uint8 to float:

    >>> 1.0 * image
    array([  51.,  102.,  153.])

    Use `rescale_intensity` to rescale to the proper range for float dtypes:

    >>> image_float = 1.0 * image
    >>> rescale_intensity(image_float)
    array([ 0. ,  0.5,  1. ])

    To maintain the low contrast of the original, use the `in_range` parameter:

    >>> rescale_intensity(image_float, in_range=(0, 255))
    array([ 0.2,  0.4,  0.6])

    If the min/max value of `in_range` is more/less than the min/max image
    intensity, then the intensity levels are clipped:

    >>> rescale_intensity(image_float, in_range=(0, 102))
    array([ 0.5,  1. ,  1. ])

    If you have an image with signed integers but want to rescale the image to
    just the positive range, use the `out_range` parameter:

    >>> image = np.array([-10, 0, 10], dtype=np.int8)
    >>> rescale_intensity(image, out_range=(0, 127))
    array([  0,  63, 127], dtype=int8)

    """

    dtype = image.dtype.type

    if in_range is None:
        imin = np.min(image)
        imax = np.max(image)
    elif in_range in DTYPE_RANGE:
        imin, imax = DTYPE_RANGE[in_range]
    else:
        imin, imax = in_range

    if out_range is None or out_range in DTYPE_RANGE:
        out_range = dtype if out_range is None else out_range
        omin, omax = DTYPE_RANGE[out_range]
        if imin >= 0:
            omin = 0
    else:
        omin, omax = out_range

    image = np.clip(image, imin, imax)

    image = (image - imin) / float(imax - imin)
    return dtype(image * (omax - omin) + omin)

max_stars_repo_name: larrybradley/findobj

max_issues_repo_name: larrybradley/findobj

max_forks_repo_name: larrybradley/findobj

reason: import numpy,from astropy


text:
import numpy

from .._common import lhs, messages, optimizer, selection_async, selection_sync
from .._helpers import OptimizeResult, register
from ._constraints import _constraints_map
from ._strategy import _strategy_map

__all__ = [
    "minimize",
]

def minimize(
    fun,
    bounds,
    x0=None,
    args=(),
    maxiter=100,
    popsize=10,
    mutation=0.5,
    recombination=0.9,
    strategy="best1bin",
    seed=None,
    xtol=1.0e-8,
    ftol=1.0e-8,
    constraints=None,
    updating="immediate",
    workers=1,
    backend=None,
    return_all=False,
):
    """
    Minimize an objective function using Differential Evolution (DE).

    Parameters
    ----------
    fun : callable
        The objective function to be minimized. Must be in the form ``f(x, *args)``, where ``x`` is the argument in the form of a 1-D array and args is a tuple of any additional fixed parameters needed to completely specify the function.
    bounds : array_like
        Bounds for variables. ``(min, max)`` pairs for each element in ``x``, defining the finite lower and upper bounds for the optimizing argument of ``fun``. It is required to have ``len(bounds) == len(x)``. ``len(bounds)`` is used to determine the number of parameters in ``x``.
    x0 : array_like or None, optional, default None
        Initial population. Array of real elements with shape (``popsize``, ``ndim``), where ``ndim`` is the number of independent variables. If ``x0`` is not specified, the population is initialized using Latin Hypercube sampling.
    args : tuple, optional, default None
        Extra arguments passed to the objective function.
    maxiter : int, optional, default 100
        The maximum number of generations over which the entire population is evolved.
    popsize : int, optional, default 10
        Total population size.
    mutation : scalar, optional, default 0.5
        The mutation constant. In the literature this is also known as differential weight, being denoted by F. It should be in the range [0, 2]. Increasing the mutation constant increases the search radius, but will slow down convergence.
    recombination : scalar, optional, default 0.9
        The recombination constant, should be in the range [0, 1]. In the literature this is also known as the crossover probability, being denoted by CR. Increasing this value allows a larger number of mutants to progress into the next generation, but at the risk of population stability.
    strategy : str, optional, default 'best1bin'
        The differential evolution strategy to use. Should be one of:

         - 'rand1bin'
         - 'rand2bin'
         - 'best1bin'
         - 'best2bin'

    seed : int or None, optional, default None
        Seed for random number generator.
    xtol : scalar, optional, default 1.0e-8
        Solution tolerance for termination.
    ftol : scalar, optional, default 1.0e-8
        Objective function value tolerance for termination.
    constraints : str or None, optional, default None
        Constraints definition:

         - None: no constraint
         - 'Random': infeasible solutions are resampled in the feasible space defined by `bounds`

    updating : str {'immediate', 'deferred'}, optional, default 'immediate'
        If ``'immediate'``, the best solution vector is continuously updated within a single generation. This can lead to faster convergence as candidate solutions can take advantage of continuous improvements in the best solution. With ``'deferred'``, the best solution vector is updated once per generation. Only ``'deferred'`` is compatible with parallelization, and is overridden when ``workers`` is not ``0`` or ``1`` or ``backend == 'mpi'``.
    workers : int, optional, default 1
        The population is subdivided into workers sections and evaluated in parallel (uses :class:`joblib.Parallel`). Supply -1 to use all available CPU cores.
    backend : str {'loky', 'threading', 'mpi'}, optional, default 'threading'
        Parallel backend to use when ``workers`` is not ``0`` or ``1``:

         - 'loky': disable threading
         - 'threading': enable threading
         - 'mpi': use MPI (uses :mod:`mpi4py`)

    return_all : bool, optional, default False
        Set to True to return an array with shape (``nit``, ``popsize``, ``ndim``) of all the solutions at each iteration.

    Returns
    -------
    :class:`stochopy.optimize.OptimizeResult`
        The optimization result represented as a :class:`stochopy.optimize.OptimizeResult`. Important attributes are:

         - ``x``: the solution array
         - ``fun``: the solution function value
         - ``success``: a Boolean flag indicating if the optimizer exited successfully
         - ``message``: a string which describes the cause of the termination

    References
    ----------
    .. [1] R. Storn and K. Price, *Differential Evolution - A Simple and Efficient Heuristic for global Optimization over Continuous Spaces*, Journal of Global Optimization, 1997, 11(4): 341-359

    """
    # Cost function
    if not hasattr(fun, "__call__"):
        raise TypeError()

    # Dimensionality and search space
    if numpy.ndim(bounds) != 2:
        raise ValueError()

    # Initial guess x0
    if x0 is not None:
        if numpy.ndim(x0) != 2 or numpy.shape(x0)[1] != len(bounds):
            raise ValueError()

    # Population size
    if popsize < 2:
        raise ValueError()

    if x0 is not None and len(x0) != popsize:
        raise ValueError()

    # DE parameters
    if not 0.0 <= mutation <= 2.0:
        raise ValueError()

    if not 0.0 <= recombination <= 1.0:
        raise ValueError()

    if updating not in {"immediate", "deferred"}:
        raise ValueError()

    F = mutation
    CR = recombination
    mut = _strategy_map[strategy]

    # Synchronize
    sync = updating == "deferred"
    sync = sync or workers not in {0, 1}
    sync = sync or backend == "mpi"

    # Seed
    if seed is not None:
        numpy.random.seed(seed)

    # Run in serial or parallel
    optargs = (
        bounds,
        x0,
        maxiter,
        popsize,
        F,
        CR,
        mut,
        constraints,
        xtol,
        ftol,
        return_all,
    )
    res = de(fun, args, sync, workers, backend, *optargs)

    return res

@optimizer
def de(
    fun,
    args,
    sync,
    workers,
    backend,
    bounds,
    x0,
    maxiter,
    popsize,
    F,
    CR,
    mut,
    constraints,
    xtol,
    ftol,
    return_all,
):
    """Optimize with DE."""
    ndim = len(bounds)
    lower, upper = numpy.transpose(bounds)

    # Constraints
    cons = _constraints_map[constraints](lower, upper)

    # Iteration
    de_iter = de_sync if sync else de_async

    # Initial population
    X = x0 if x0 is not None else lhs(popsize, ndim, bounds)
    U = numpy.empty((popsize, ndim))

    # Evaluate initial population
    pfit = fun(X)
    pbestfit = pfit.copy()

    # Initial best solution
    gbidx = numpy.argmin(pbestfit)
    gfit = pbestfit[gbidx]
    gbest = X[gbidx].copy()

    # Initialize arrays
    if return_all:
        xall = numpy.empty((maxiter, popsize, ndim))
        funall = numpy.empty((maxiter, popsize))
        xall[0] = X.copy()
        funall[0] = pfit.copy()

    # Iterate until one of the termination criterion is satisfied
    it = 1
    converged = False
    while not converged:
        it += 1

        r1 = numpy.random.rand(popsize, ndim)
        X, gbest, pbestfit, gfit, pfit, status = de_iter(
            it,
            X,
            U,
            gbest,
            pbestfit,
            gfit,
            pfit,
            F,
            CR,
            r1,
            maxiter,
            xtol,
            ftol,
            fun,
            mut,
            cons,
        )

        if return_all:
            xall[it - 1] = X.copy()
            funall[it - 1] = pbestfit.copy()

        converged = status is not None

    res = OptimizeResult(
        x=gbest,
        success=status >= 0,
        status=status,
        message=messages[status],
        fun=gfit,
        nfev=it * popsize,
        nit=it,
    )
    if return_all:
        res["xall"] = xall[:it]
        res["funall"] = funall[:it]

    return res

def delete_shuffle_sync(popsize):
    """Delete current solution from population for mutation (synchronous)."""
    return numpy.transpose([delete_shuffle_async(i, popsize) for i in range(popsize)])

def delete_shuffle_async(i, popsize):
    """Delete current solution from population for mutation (asynchronous)."""
    return numpy.random.permutation(numpy.delete(numpy.arange(popsize), i))

def de_sync(
    it,
    X,
    U,
    gbest,
    pbestfit,
    gfit,
    pfit,
    F,
    CR,
    r1,
    maxiter,
    xtol,
    ftol,
    fun,
    mut,
    cons,
):
    """Synchronous DE."""
    popsize, ndim = X.shape

    # Mutation
    V = mut(delete_shuffle_sync(popsize), F, X, gbest)

    # Recombination
    mask = numpy.zeros_like(r1, dtype=bool)
    irand = numpy.random.randint(ndim, size=popsize)
    for i in range(popsize):
        mask[i, irand[i]] = True

    U[:] = cons(numpy.where(numpy.logical_or(mask, r1 <= CR), V, X))

    # Selection
    gbest, gfit, pfit, status = selection_sync(
        it, U, gbest, X, pbestfit, maxiter, xtol, ftol, fun
    )

    return X, gbest, pbestfit, gfit, pfit, status

def de_async(
    it,
    X,
    U,
    gbest,
    pbestfit,
    gfit,
    pfit,
    F,
    CR,
    r1,
    maxiter,
    xtol,
    ftol,
    fun,
    mut,
    cons,
):
    """Asynchronous DE."""
    popsize, ndim = X.shape

    for i in range(popsize):
        # Mutation
        V = mut(delete_shuffle_async(i, popsize), F, X, gbest)

        # Recombination
        mask = numpy.zeros(ndim, dtype=bool)
        irand = numpy.random.randint(ndim)
        mask[irand] = True
        U[i] = cons(numpy.where(numpy.logical_or(mask, r1[i] <= CR), V, X[i]))

        # Selection
        gbest, gfit, pfit[i], status = selection_async(
            it, U, gbest, gfit, X, pbestfit, maxiter, xtol, ftol, fun, i
        )

    # Stop if maximum iteration is reached
    if status is None and it >= maxiter:
        status = -1

    return X, gbest, pbestfit, gfit, pfit, status

register("de", minimize)

max_stars_repo_name: keurfonluu/stochopy

max_issues_repo_name: keurfonluu/stochopy

max_forks_repo_name: keurfonluu/stochopy

reason: import numpy


text:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# from tinymce.models import HTMLField
# from pyuploadcare.dj.models import ImageField
# from django.db.models import Avg, Max, Min
# from pyuploadcare.dj.forms import FileWidget
from django.conf import settings
import numpy as np
import datetime as dt

class Profile(models.Model):
    prof_pic = models.ImageField('avatar', default='default.jpg')
    bio = models.TextField(max_length=140, blank=True, default='')
    user = models.OneToOneField(User,on_delete=models.CASCADE, primary_key=True)

    def save_profile(self):
        self.save()

    @classmethod
    def search_profile(cls, name):
        profile = Profile.objects.filter(user__username__icontains = name)
        return profile

    @classmethod
    def get_by_id(cls, id):
        profile = Profile.objects.get(user = id)
        return profile

    @classmethod
    def filter_by_id(cls, id):
        profile = Profile.objects.filter(user = id).first()
        return profile

@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    instance.profile.save()

class Image(models.Model):
    photo = models.ImageField(upload_to='pics', height_field=None, width_field=None, max_length=None)
    profile = models.ForeignKey(User, on_delete=models.CASCADE)
    def save_image(self):
        self.save()

    @classmethod
    def get_profile_images(cls, profile):
        images = Image.objects.filter(profile__pk = profile)
        return images

class Project(models.Model):
    '''
    class that contains Project properties
    '''
    title = models.CharField(max_length=40)
    image = models.ImageField(upload_to='pics', height_field=None, width_field=None, max_length=None)
    description = models.TextField()
    link = models.URLField(max_length=70)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, default="", blank=True, null=True)
    profile = models.ForeignKey(
        Profile, on_delete=models.CASCADE, default="", blank=True, null=True)
    rating = models.TextField()
    post_date = models.DateTimeField(auto_now_add=True, blank=True, null=True)

    def save_project(self):
        self.save()

    def update_project(self):
        self.update()

    def delete_project(self):
        self.delete()

    @classmethod
    def search_project(cls, title):
        project = cls.objects.filter(title__icontains=title)
        return project

    @classmethod
    def get_posted_projects(cls):
        projects = Project.objects.all()
        return projects

    @classmethod
    def get_projects_on_profile(cls, profile):
        projects = Project.objects.filter(profile__pk=profile)
        return projects

    @classmethod
    def get_project_by_id(cls, id):
        project = Project.objects.filter(user_id=id).all()
        return project

    def average_design(self):
        total_ratings = list(
            map(lambda x: x.rating, self.designrating_set.all()))
        return np.mean(total_ratings)

    def average_usability(self):
        total_ratings = list(
            map(lambda x: x.rating, self.usabilityrating_set.all()))
        return np.mean(total_ratings)

    def average_content(self):
        total_ratings = list(
            map(lambda x: x.rating, self.contentrating_set.all()))
        return np.mean(total_ratings)

    def __str__(self):
        return self.title

class DesignRating(models.Model):
    CHOICES = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5'),
        (6, '6'),
        (7, '7'),
        (8, '8'),
        (9, '9'),
        (10, '10')
    )
    project = models.ForeignKey(Project)
    pub_date = models.DateTimeField(auto_now_add=True)
    profile = models.ForeignKey(Profile)
    comment = models.CharField(max_length=200)
    rating = models.IntegerField(choices=CHOICES, default=0)

class UsabilityRating(models.Model):
    CHOICES = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5'),
        (6, '6'),
        (7, '7'),
        (8, '8'),
        (9, '9'),
        (10, '10')
    )
    project = models.ForeignKey(Project)
    pub_date = models.DateTimeField(auto_now_add=True)
    profile = models.ForeignKey(Profile)
    comment = models.CharField(max_length=200)
    rating = models.IntegerField(choices=CHOICES, default=0)

class ContentRating(models.Model):
    CHOICES = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5'),
        (6, '6'),
        (7, '7'),
        (8, '8'),
        (9, '9'),
        (10, '10')
    )
    project = models.ForeignKey(Project)
    pub_date = models.DateTimeField(auto_now_add=True,)
    profile = models.ForeignKey(Profile)
    comment = models.CharField(max_length=200)
    rating = models.IntegerField(choices=CHOICES, default=0)


text:
"""

Edited for multiclass boxes

Takes a JSON file and visualizes the annotation boxes on images.

"""

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

import sys
sys.path.append('./tools')
import numpy as np
import os, cv2
import argparse
import os.path as osp
import time
import skvideo.io
import json
import csv
from six.moves import xrange
from PIL import Image
from tqdm import tqdm

from matplotlib import pyplot as plt

JSON_FILE = '' #'data/CS6_annot/cs6-subset-gt_face_train_annot_coco_style.json'
# OUT_DIR = '/mnt/nfs/work1/elm/arunirc/Data/CS6_annots'
OUT_DIR = '' #'Outputs/visualizations/'

DEBUG = False

def parse_args():
    parser = argparse.ArgumentParser(description='Creating CS6 ground truth data')
    parser.add_argument(
        '--output_dir', help='directory for saving outputs',
        default=OUT_DIR, type=str
    )
    parser.add_argument(
        '--json_file', help='Name of JSON file', default=JSON_FILE
    )
    parser.add_argument(
        '--imdir', help="root directory for loading dataset images",
        default='data/CS6_annot', type=str)
    return parser.parse_args()

_GREEN = (18, 127, 15)
color_dict = {'red': (0,0,225), 'green': (0,255,0), 'yellow': (0,255,255),
              'blue': (255,0,0), '_GREEN':(18, 127, 15), '_GRAY': (218, 227, 218), 'COL1': (255,255,0),'COL2':(255,0,255)}
# -----------------------------------------------------------------------------------
def draw_detection_list(im, dets, labels, sources=None):
# -----------------------------------------------------------------------------------
    """ Draw bounding boxes on a copy of image and return it.
        [x0 y0 w h conf_score]
    """
    im_det = im.copy()
    if dets.size == 0:
        return im_det
    if dets.ndim == 1:
        dets = dets[np.newaxis,:] # handle single detection case

    # format into [xmin, ymin, xmax, ymax]
    dets[:, 2] = dets[:, 2] + dets[:, 0]
    dets[:, 3] = dets[:, 3] + dets[:, 1]

    for i, det in enumerate(dets):
        bbox = dets[i, :4]
        x0, y0, x1, y1 = [int(x) for x in bbox]

        # TEMP: if 'source' is available, set yellow for HP and green for dets -- assuming 1 class
        if not (sources is None):
            if sources[i] == 1:
                col = 'green'
            elif sources[i] == 2:
                col = 'yellow'
        else:
            col = list(sorted(color_dict.keys()))[labels[i]] #-- standard multi class, not considering the source
        line_color = color_dict[col]
        cv2.rectangle(im_det, (x0, y0), (x1, y1), line_color, thickness=2)

    return im_det

if __name__ == '__main__':

    args = parse_args()

    with open(args.json_file) as f:
        ann_dict = json.load(f)
    print(ann_dict.keys())
    #input(ann_dict['images'][0])
    #input(ann_dict['annotations'][0])

    #out_dir = osp.join(args.output_dir,
    #                   osp.splitext(osp.basename(args.json_file))[0])
    out_dir = args.output_dir

    if not osp.exists(out_dir):
        os.makedirs(out_dir, exist_ok=True)

    i = 0
    np.random.seed(0)
    imlist = ann_dict['images']
    np.random.shuffle(imlist)
    for img_annot in tqdm(imlist):#ann_dict['images']):
        image_name = img_annot['file_name']
        image_id = img_annot['id']
        bboxes = [x['bbox'] for x in ann_dict['annotations'] if x['image_id'] == image_id]
        labels = [x['category_id'] for x in ann_dict['annotations'] if x['image_id'] == image_id]

        # get source -- gt, dets or hp
        sources = None
        if 'source' in ann_dict['annotations'][0].keys():
            sources = [x['source'] for x in ann_dict['annotations'] if x['image_id'] == image_id]
            #input('-->'+str(sources[0]))

        #print('>>>',osp.join(args.imdir, image_name))
        im = cv2.imread(osp.join(args.imdir, image_name))
        if im is None:
            print('>>>',osp.join(args.imdir, image_name))
            continue
        assert im.size > 0
        im_det = draw_detection_list(im, np.array(bboxes), labels, sources=sources)

        #if im_det is None:
        #    print('>>>',image_name)
        #    plt.imshow(im)
        #    plt.show()

        out_path = osp.join(out_dir, image_name.replace('/', '_'))
        cv2.imwrite(out_path, im_det)
        i += 1
        if i == 5000:
            break

max_stars_repo_name: AruniRC/detectron-self-train

max_issues_repo_name: AruniRC/detectron-self-train

max_forks_repo_name: AruniRC/detectron-self-train


text:
import numpy as np
from scipy.integrate import quad
from scipy.interpolate import interp2d
import scipy.special
import os

phi_interp = None

#------- Velocity distribution stuff----------
#----------------------------------------------

vesc = 544.0 #Escape velocity
sigmav = 156.0  #Velocity dispersion (v_0/sqrt(2))
ve = 232.0  #Earth peculiar velocity around GC

# Nesc - normalisation constant
Nesc = (scipy.special.erf(vesc/(np.sqrt(2.0)*sigmav)) - np.sqrt(2.0/np.pi)*(vesc/sigmav)*np.exp(-vesc**2/(2.0*sigmav**2)))
NNORM = Nesc*156.0**3*np.sqrt(2.0*np.pi)*2.0*np.pi

#Load an interpolation function for the integral
#of the Maxwell-Boltzmann velocity distribution
#over phi
#*This is called as soon as the module is loaded*
def loadPhiInterp():
    global phi_interp
    curr_dir = os.path.dirname(os.path.realpath(__file__)) + '/'
    fname = curr_dir + "../data/PhiIntegrals.dat"
    xvals = np.arange(-7, 7.001, 0.05)
    phivals = np.arange(0, np.pi+0.1, 0.05)
    xlen = len(xvals)
    ylen = len(phivals)
    #print xlen, ylen
    if (os.path.isfile(fname)):
        data = np.loadtxt(fname, usecols=(2,))
        z = data.reshape((xlen,ylen))

    else:
        print(">MAXWELLBOLTZMANN: File '../data/PhiIntegrals.dat' doesn't exist...")
        print(">MAXWELLBOLTZMANN: Calculating from scratch...")
        z = np.zeros((xlen, ylen))
        for i,x in enumerate(xvals):
            for j,phi in enumerate(phivals):
                z[i,j] = quad(lambda y: np.exp(x*np.cos(y)), 0, phi)[0]

        xgrid, phigrid = np.meshgrid(xvals, phivals, indexing='ij')
        np.savetxt(fname, zip(xgrid.flatten(), phigrid.flatten(),z.flatten()))

    phi_interp = interp2d(xvals,phivals,z.T)

loadPhiInterp()

def IntegralOverPhi(x, phi_max):
    if (phi_max < 0):
        return 0
    if (phi_max >= np.pi):
        return 2.0*np.pi*scipy.special.i0(x)
    else:
        return 2.0*phi_interp(x, phi_max)

IntegralOverPhiVec = np.vectorize(IntegralOverPhi)

#Integrand for integrating over the velocity distribution
#The phi integral has already been performed, so all you have
#left is v and theta.
def calcf_integ(v, theta, gamma):

    if (v*np.sin(gamma)*np.sin(theta) <= 1e-10):
        return 2.0*np.pi*VelDist(v, theta, 0, gamma)

    delsq = v**2 + ve**2 - 2*v*ve*np.cos(gamma)*np.cos(theta)

    cosmin = (v**2 + ve**2 - vesc**2)/(2*v*ve*np.sin(gamma)*np.sin(theta))\
         - (np.cos(gamma)*np.cos(theta))/(np.sin(gamma)*np.sin(theta))

    x0 = np.sin(theta)*np.sin(gamma)*v*ve/(sigmav**2)
    phi_max = np.arccos(np.clip(cosmin, -1.0, 1.0))
    A = IntegralOverPhiVec(x0, phi_max)*np.exp(-delsq/(2.0*sigmav**2))

    return A*1.0/NNORM

#Full 3-D velocity distribution (v, theta, phi)
def VelDist(v, theta, phi, gamma):
    cdel = np.sin(gamma)*np.sin(theta)*np.cos(phi) + np.cos(gamma)*np.cos(theta)
    dsq = v**2 - 2*v*ve*cdel + ve**2
    A = np.exp(-dsq/(2*sigmav**2))/NNORM
    if hasattr(A, "__len__"):
        A[np.where(dsq > vesc**2)] = A[np.where(dsq > vesc**2)]*0.0
    else:
        if (dsq > vesc**2):
            A = 0
    return A

#Calculate the free MB speed distribution
#after integrating over all angles
def calcf_SHM(v):
    beta = ve/(sigmav**2)
    N1 = 1.0/(Nesc*sigmav**3*np.sqrt(2*np.pi))
    f = v*0.0

    a = (v <= vesc-ve)
    f[a] = np.exp(-(v[a]**2 + ve**2)/(2.0*sigmav**2))*(np.exp(beta*v[a])-np.exp(-beta*v[a]))

    b = (vesc-ve < v)&(v < vesc+ve)
    f[b] = np.exp(-(v[b]**2 + ve**2)/(2.0*sigmav**2))*(np.exp(beta*v[b]) - np.exp((v[b]**2 + ve**2 -vesc**2)/(2*sigmav**2)))
    return f*v*N1/beta

#Minimum velocity required for a recoil of energy E_R
def vmin(E, m_N, m_x):
    res = E*0.0
    m_N2 = m_N*0.9315
    mu = (m_N2*m_x)/(m_N2+m_x)
    res =  3e5*np.sqrt((E/1e6)*(m_N2)/(2*mu*mu))
    return res

max_stars_repo_name: bradkav/verne

max_issues_repo_name: bradkav/verne

max_forks_repo_name: bradkav/verne

reason: import numpy,import scipy,from scipy


text:
# -*- coding: utf-8 -*-
# native
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import figure, show, rc, grid
# fucction
def someFunc():  #(val_int):
  pass
  #print hello_ext.greet()
  print("clicked")
  x = np.arange(0, 10, 0.05);
  y = np.sin(4*x)
  e = np.fft.fft(y)
  print(e)
  plt.plot(x, 20*np.log10(np.abs(e)))
  plt.show()
  #radar green, solid grid lines


text:
from sympy.core.numbers import igcd
from primetest import isprime
from factor_ import factorint, trailing, totient

def int_tested(*j, **hint):
    """
    Return all args as Python integers.

    In some cases a routine needs to work with integers
    but it is convenient to allow the user to pass a non-integer
    value or expression. In this case, the flag ``strict`` can be set
    to False. The default behavior is to raise an error if any argument
    cannot pass an int(arg) == arg test.

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import int_tested
    >>> from sympy import sqrt
    >>> n = sqrt(10)
    >>> int_tested(n, strict=False)
    3
    >>> int_tested(n)
    Traceback (most recent call last):
    ...
    ValueError: All arguments were not integers

    """
    i = tuple([int(i) for i in j])
    if hint.get('strict', True):
        if i != j:
            raise ValueError('all arguments were not integers')
    if len(i) == 1:
        return i[0]
    return i

def n_order(a, n):
    """Returns the order of ``a`` modulo ``n``.

    The order of ``a`` modulo ``n`` is the smallest integer
    ``k`` such that ``a**k`` leaves a remainder of 1 with ``n``.

    Examples
    ========

    >>> from sympy.ntheory import n_order
    >>> n_order(3, 7)
    6
    >>> n_order(4, 7)
    3
    """
    a, n = int_tested(a, n)
    if igcd(a, n) != 1:
        raise ValueError("The two numbers should be relatively prime")
    group_order = totient(n)
    factors = factorint(group_order)
    order = 1
    if a > n:
        a = a % n
    for p, e in factors.iteritems():
        exponent = group_order
        for f in xrange(0, e + 1):
            if (a ** (exponent)) % n != 1:
                order *= p ** (e - f + 1)
                break
            exponent = exponent // p
    return order

def is_primitive_root(a, p):
    """
    Returns True if ``a`` is a primitive root of ``p``

    ``a`` is said to be the primitive root of ``p`` if gcd(a, p) == 1 and
    totient(p) is the smallest positive number s.t.

        a**totient(p) cong 1 mod(p)

    Examples
    ========

    >>> from sympy.ntheory import is_primitive_root, n_order, totient
    >>> is_primitive_root(3, 10)
    True
    >>> is_primitive_root(9, 10)
    False
    >>> n_order(3, 10) == totient(10)
    True
    >>> n_order(9, 10) == totient(10)
    False

    """
    a, p = int_tested(a, p)
    if igcd(a, p) != 1:
        raise ValueError("The two numbers should be relatively prime")
    if a > p:
        a = a % p
    if n_order(a, p) == totient(p):
        return True
    else:
        return False

def is_quad_residue(a, p):
    """
    Returns True if ``a`` (mod ``p``) is in the set of squares mod ``p``,
    i.e a % p in set([i**2 % p for i in range(p)]). If ``p`` is an odd
    prime, an iterative method is used to make the determination:

    >>> from sympy.ntheory import is_quad_residue
    >>> list(set([i**2 % 7 for i in range(7)]))
    [0, 1, 2, 4]
    >>> [j for j in range(7) if is_quad_residue(j, 7)]
    [0, 1, 2, 4]

    See Also
    ========

    legendre_symbol, jacobi_symbol
    """
    a, p = int_tested(a, p)
    if p < 1:
        raise ValueError('p must be > 0')
    if a >= p or a < 0:
        a = a % p
    if a < 2 or p < 3:
        return True
    if not isprime(p):
        if p % 2 and jacobi_symbol(a, p) == -1:
            return False
        for i in range(2, p//2 + 1):
            if i**2 % p == a:
                return True
        return False

    def square_and_multiply(a, n, p):
        if n == 1:
            return a
        elif n % 2 == 1:
            return ((square_and_multiply(a, n // 2, p) ** 2) * a) % p
        else:
            return (square_and_multiply(a, n // 2, p) ** 2) % p

    return (square_and_multiply(a, (p - 1) // 2, p) % p) == 1

def legendre_symbol(a, p):
    """
    Returns
    =======

    1. 0 if a is multiple of p
    2. 1 if a is a quadratic residue of p
    3. -1 otherwise

    p should be an odd prime by definition

    Examples
    ========

    >>> from sympy.ntheory import legendre_symbol
    >>> [legendre_symbol(i, 7) for i in range(7)]
    [0, 1, 1, -1, 1, -1, -1]
    >>> list(set([i**2 % 7 for i in range(7)]))
    [0, 1, 2, 4]

    See Also
    ========

    is_quad_residue, jacobi_symbol

    """
    a, p = int_tested(a, p)
    if not isprime(p) or p == 2:
        raise ValueError("p should be an odd prime")
    _, a = divmod(a, p)
    if not a:
        return 0
    if is_quad_residue(a, p):
        return 1
    else:
        return -1

def jacobi_symbol(m, n):
    """
    Returns the product of the legendre_symbol(m, p)
    for all the prime factors, p, of n.

    Returns
    =======

    1. 0 if m cong 0 mod(n)
    2. 1 if x**2 cong m mod(n) has a solution
    3. -1 otherwise

    Examples
    ========

    >>> from sympy.ntheory import jacobi_symbol, legendre_symbol
    >>> from sympy import Mul, S
    >>> jacobi_symbol(45, 77)
    -1
    >>> jacobi_symbol(60, 121)
    1

    The relationship between the jacobi_symbol and legendre_symbol can
    be demonstrated as follows:

    >>> L = legendre_symbol
    >>> S(45).factors()
    {3: 2, 5: 1}
    >>> jacobi_symbol(7, 45) == L(7, 3)**2 * L(7, 5)**1
    True

    See Also
    ========

    is_quad_residue, legendre_symbol
    """
    m, n = int_tested(m, n)
    if not n % 2:
        raise ValueError("n should be an odd integer")
    if m < 0 or m > n:
        m = m % n
    if not m:
        return int(n == 1)
    if n == 1 or m == 1:
        return 1
    if igcd(m, n) != 1:
        return 0

    j = 1
    s = trailing(m)
    m = m >> s
    if s % 2 and n % 8 in [3, 5]:
        j *= -1

    while m != 1:
        if m % 4 == 3 and n % 4 == 3:
            j *= -1
        m, n = n % m, m
        s = trailing(m)
        m = m >> s
        if s % 2 and n % 8 in [3, 5]:
            j *= -1
    return j

max_stars_repo_name: goodok/sympy

max_issues_repo_name: goodok/sympy

max_forks_repo_name: goodok/sympy

reason: from sympy


text:
import os
import io
import h5py
import glob
from tqdm import tqdm
import numpy as np
import cv2
import torch
import argparse

import detectron2
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
from detectron2.modeling.postprocessing import detector_postprocess
from detectron2.modeling.roi_heads.fast_rcnn import FastRCNNOutputLayers, FastRCNNOutputs, fast_rcnn_inference_single_image

parser = argparse.ArgumentParser()
parser.add_argument('--split', type=str, default='train2014', help='train2014, val2014')
parser.add_argument('--imgdir', type=str, help='path to coco image directory')
parser.add_argument('--outdir', type=str, help='path to save features')
parser.add_argument('--model', type=str, help='which model weights to use')
parser.add_argument('--verbose', dest='verbose', action='store_true')

args = parser.parse_args()

def run_detector(raw_image,predictor,num_objects=8000,verbose=True):
    with torch.no_grad():
        raw_height, raw_width = raw_image.shape[:2]
        if verbose: tqdm.write("Original image size: " + str((raw_height, raw_width)))

        # Preprocessing
        image = predictor.transform_gen.get_transform(raw_image).apply_image(raw_image)
        if verbose: tqdm.write("Transformed image size: "+str(image.shape[:2]))
        image = torch.as_tensor(image.astype("float32").transpose(2, 0, 1))
        inputs = [{"image": image, "height": raw_height, "width": raw_width}]
        images = predictor.model.preprocess_image(inputs)

        # Run Backbone Res1-Res4
        features = predictor.model.backbone(images.tensor)

        # Generate proposals with RPN
        proposals, _ = predictor.model.proposal_generator(images, features, None)
        proposal = proposals[0]
        if verbose: tqdm.write('Proposal Boxes size: ' + str(proposal.proposal_boxes.tensor.shape))

        # Run RoI head for each proposal (RoI Pooling + Res5)
        proposal_boxes = [x.proposal_boxes for x in proposals]
        features = [features[f] for f in predictor.model.roi_heads.in_features]
        box_features = predictor.model.roi_heads._shared_roi_transform(
            features, proposal_boxes
        )
        feature_pooled = box_features.mean(dim=[2, 3])  # pooled to 1x1
        if verbose: tqdm.write('Pooled features size: ' + str(feature_pooled.shape))

        # Predict classes and boxes for each proposal.
        pred_class_logits, pred_proposal_deltas = predictor.model.roi_heads.box_predictor(feature_pooled)
        outputs = FastRCNNOutputs(
            predictor.model.roi_heads.box2box_transform,
            pred_class_logits,
            pred_proposal_deltas,
            proposals,
            predictor.model.roi_heads.smooth_l1_beta,
        )
        probs = outputs.predict_probs()[0]
        boxes = outputs.predict_boxes()[0]

        # Note: BUTD uses raw RoI predictions,
        #       we use the predicted boxes instead.
        # boxes = proposal_boxes[0].tensor

        # NMS
        for nms_thresh in np.arange(0.5, 1.0, 0.1):
            instances, ids = fast_rcnn_inference_single_image(
                boxes, probs, image.shape[1:],
                score_thresh=0.0, nms_thresh=nms_thresh, topk_per_image=num_objects #0.01
            )
            if len(ids) == num_objects:
                break

        instances = detector_postprocess(instances, raw_height, raw_width)
        #roi_features = feature_pooled[ids].detach()
        if verbose: tqdm.write(str(instances))

        return instances#, roi_features

def main():
    cfg = get_cfg()
    cfg.merge_from_file("configs/COCO-Detection/faster_rcnn_R_50_C4_3x_coco_2014.yaml")
    cfg.MODEL.RPN.POST_NMS_TOPK_TEST = 100 #500
    cfg.MODEL.ROI_HEADS.NMS_THRESH_TEST = 0.6
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.01
    cfg.MODEL.RPN.NMS_THRESH = 0.7
    # select whichever model you want to cache features for
    if args.model=='gpv_trained':
        # faster rcnn trained on gpv-train set
        cfg.MODEL.WEIGHTS = "/home/tanmayg/Data/detectron_output/model_final.pth"
    elif args.model=='original_trained':
        #faster rcnn trained on original-train set
        cfg.MODEL.WEIGHTS = "https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_50_C4_3x/137849393/model_final_f97cb7.pkl"
    else:
        raise NotImplementedError

    predictor = DefaultPredictor(cfg)

    imgdir=os.path.join(args.imgdir,args.split) #'/home/tanmayg/Data/gpv/learning_phase_data/coco/images/train2014'
    outdir=args.outdir #'/home/tanmayg/Data/bua/'
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    feats_h5py = os.path.join(outdir,f'{args.split}.hdf5')
    f = h5py.File(feats_h5py,'w')
    paths = glob.glob(f'{imgdir}/*.jpg')
    for i,path in enumerate(tqdm(paths)):
        im = cv2.imread(path)
        instances = run_detector(im,predictor,verbose=args.verbose)
        # instances.pred_boxes: (36,4) x1,y1,x2,y2
        # instances.scores: (36,)
        # instances.classes: (36,)
        # roi_features: (36,2048)
        boxes = instances.pred_boxes.tensor.cpu().numpy()
        scores = instances.scores.cpu().numpy()
        classes = instances.pred_classes.cpu().numpy()
        #feats = roi_features.cpu().numpy()
        print(boxes.shape)
        #import pdb; pdb.set_trace()
        im_name = os.path.splitext(os.path.split(path)[1])[0]
        _,subset,im_id = im_name.split('_')
        im_id = int(im_id)
        grp = f.create_group(im_name)
        grp.create_dataset('boxes',data=boxes)
        grp.create_dataset('scores',data=scores)
        grp.create_dataset('classes',data=classes)
        #grp.create_dataset('feats',data=feats)

    f.close()

if __name__=='__main__':
    main()

max_stars_repo_name: allenai/py-bottom-up-attention

max_issues_repo_name: allenai/py-bottom-up-attention

max_forks_repo_name: allenai/py-bottom-up-attention


text:
# Libraries
import copy
import numpy as np
import pandas as pd

# Specific
from scipy import stats

# Specific
from pathlib import Path

# -----------------------------------------------------
#
# -----------------------------------------------------
def nanunique(x):
    if x.isnull().all():
        return []
    return x.dropna().unique().tolist()

def fbfill(x):
    """Computes forward and then backward fill."""
    return x.ffill().bfill()

def bffill(x):
    """Computes backward and then forward fill"""
    return x.bfill(x).ffill(x)

#def mode(series):
#    return series.mode()[0]

def mode(x):
    """Compute the mode and returns as a valid transform.

    .. note: The pd.Series mode function automatically
             removes the NaNs. Hence, if the series has
             all NaN values there wont be a mode and it
             will crash.

    .. note: An alternative use x.mode(dropna=True).
    .. note: stats.mode(x).mode[0

    """
    if x.isnull().all():
        return x
    return np.repeat(stats.mode(x).mode[0], len(x))

TRANSFORMATIONS = {
    'mode': mode,
    'fbfill': fbfill,
    'bffill': bffill
}

def str2func(d):
    """This method passes strings to functions

    Parameters
    ---------
    d: dict
        Dictionary where value is a function name."""
    # Create deep dictionary copy
    aux = copy.deepcopy(d)

    # Format some function names
    for k, v in aux.items():
        if v in TRANSFORMATIONS.keys():
            aux[k] = TRANSFORMATIONS[v]

    # Return
    return aux

# -----------------------------------------------------
#
# -----------------------------------------------------
def _save_dict_df_csv(d, filepath, filename, **kwargs):
    """

    Parameters
    ----------
    d
    filepath
    filename

    Returns
    -------

    """
    for k, df in d.items():
        # Create path
        path = "{0}/{1}_{2}.csv".format(filepath, filename, k)
        # Save DataFrame
        df.to_csv(path, **kwargs)

def _save_dict_df_xlsx(d, filepath, filename, **kwargs):
    """Save dictionary in xlsx file.

    Parameters
    ----------

    Returns
    -------
    """
    # Create full path
    fullpath = "{0}/{1}.xlsx".format(filepath, filename)

    # Creating Excel Writer Object from Pandas
    writer = pd.ExcelWriter(fullpath, engine='xlsxwriter')

    # Save each frame
    for sheet, frame in d.items():
        frame.to_excel(writer, sheet_name=sheet, **kwargs)

    # critical last step
    writer.save()

def save_df_dict(d, filepath, filename, extension='xlsx',
                 flat=False, **kwargs):
    """This method saves a dictionary of DataFrames

    Parameters
    ----------
    d: dict-like
       The dictionary where key is the name of the worksheet
       and the value is the pandas DataFrame.

    filepath: str-like
        The path to store the output files.

    filename: str-like
        The name to use for the output files without the extension.

    ext: str-like (xlsx or csv)
        The extension

    flat: boolean
        To concatenate all DataFrames to save them together.

    Returns
    -------
    """
    # Check is dictionary
    if not isinstance(d, dict):
        d = {'': d}

    # Concatenate DataFrames
    # ----------------------
    if flat:
        # Create full name
        fullpath = "{0}/{1}.{2}".format(filepath, filename, extension)
        # Flat the DataFrames
        flat = pd.concat(d.values(), ignore_index=True)
        # Save
        if extension == 'csv':
            return flat.to_csv(fullpath, **kwargs)
        if extension == 'xlsx':
            return flat.to_excel(fullpath, **kwargs)
        # Return
        return

    # Split worksheets
    # ----------------
    # Save all dfs in same xlsx file
    if extension == 'xlsx':
        return _save_dict_df_xlsx(d, filepath, filename)

    # Save each df in a different csv file
    if extension == 'csv':
        return _save_dict_df_csv(d, filepath, filename, **kwargs)

def save_xlsx(d, filepath, sheet_split=False, **kwargs):
    """This method saves a dictionary in .xlsx

    Parameters
    ----------
    d: dict-like
        The dictionary where key is the name of the worksheet
        and value is the pandas DataFrame.

    filepath: str-like

    sheet_split: boolean

    Returns
    -------
    """
    # Check is dictionary
    if not isinstance(d, dict):
        d = {'': d}

    # Save independent worksheets
    if sheet_split:
        # Create path
        path = Path(filepath)
        # Loop keys
        for k, df in d.items():
            df.to_csv(str(path).replace('.xlsx', '_{0}.csv'.format(k)))
        # Return
        return None

    # Creating Excel Writer Object from Pandas
    writer = pd.ExcelWriter(filepath, engine='xlsxwriter')

    # Save each frame
    for sheet, frame in d.items():
        frame.to_excel(writer, sheet_name=sheet, index=False)

    # critical last step
    writer.save()

max_stars_repo_name: bahp/datablend

max_stars_repo_licenses: FTL RSA-MD CECILL-B

max_issues_repo_name: bahp/datablend

max_issues_repo_licenses: FTL RSA-MD CECILL-B

max_forks_repo_name: bahp/datablend

max_forks_repo_licenses: FTL RSA-MD CECILL-B

reason: import numpy,from scipy


text:
import sys
import os
from pprint import pprint
from decimal import Decimal
import pickle
from SP2_particle_record_UTC import ParticleRecord
import matplotlib.pyplot as plt
import numpy as np
import math

BC_masses = [0.25,.4,4,6,8,10,12.5,41]
density = 1.8

sig = 16.700000000000045
print (0.18569 + 8.7668e-3 * sig + 8.8322e-7 * sig*sig)*0.7
print 0.01081*sig  - 0.32619
sys.exit()

for BC_mass in BC_masses:
	BC_VED = (((BC_mass/(10**15*density))*6/3.14159)**(1/3.0))*10**7 #VED in nm with 10^15fg/g and 10^7nm/cm
	BC_VED2 = (BC_mass**(1/3.0))*101.994391398 #VED in nm with 10^15fg/g and 10^7nm/cm
	print BC_mass
	print BC_VED
	#print BC_VED2

for BC_VED in range(70,230,10):
	mass = ((BC_VED/(10.**7))**3)*(math.pi/6.)*1.8*(10.**15)
	signal = (mass - 0.19238)/0.0031
	UL_mass = 0.00338*signal + 0.259713
	err = (UL_mass-mass)*100/mass
	print BC_VED,mass, signal, err


text:
import sys
import os
import numpy as np
from pprint import pprint
from datetime import datetime
from datetime import timedelta
import mysql.connector
import math
import calendar
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib import dates

SP2_ID = 58
saved_1_of_every = 10
start = datetime(2014,1,15)
end =   datetime(2014,3,1)
timestep = 1 #hours

#database connection
cnx = mysql.connector.connect(user='root', password='Suresh15', host='localhost', database='black_carbon')
cursor = cnx.cursor()

file_data = []

while start <= end:
	print start
	UNIX_start = calendar.timegm(start.utctimetuple())
	UNIX_end = UNIX_start + timestep*3600

	cursor.execute('''(SELECT
	mn.UNIX_UTC_ts_int_start,
	mn.UNIX_UTC_ts_int_end,
	mn.BB_incand_HG,
	mn.BB_incand_LG,
	hk.sample_flow
	FROM alert_mass_number_data_2014 mn
	JOIN alert_hk_data hk on mn.hk_id = hk.id
	WHERE
	mn.UNIX_UTC_ts_int_start >= %s
	AND mn.UNIX_UTC_ts_int_end < %s)''',
	(UNIX_start,UNIX_end))

	masses={
	'rBC_mass_fg_HG_low':[],
	'rBC_mass_fg_HG_high':[],
	'rBC_mass_fg_LG_low':[],
	'rBC_mass_fg_LG_high':[],
	}

	mass_uncertainties={
	'rBC_mass_fg_HG_low':[],
	'rBC_mass_fg_HG_high':[],
	'rBC_mass_fg_LG_low':[],
	'rBC_mass_fg_LG_high':[],
	}

	ind_data = cursor.fetchall()
	ind_mass_tot = 0
	ind_mass_uncer_tot = 0
	ind_number_tot = 0
	total_sample_vol = 0
	for row in ind_data:
		ind_start_time = row[0]
		ind_end_time = row[1]
		bbhg_incand_pk_amp = row[2]
		bblg_incand_pk_amp = row[3]
		sample_flow = row[4]  #in vccm
		if sample_flow == None:
			print 'no flow'
			continue
		sample_vol =  (sample_flow*(ind_end_time-ind_start_time)/60)    #/60 b/c sccm and time in secs  0.87 = STP corr?????
		total_sample_vol = total_sample_vol + sample_vol

		#calculate masses and uncertainties
		#HG
		bbhg_mass_uncorr = 0.29069 + 1.49267E-4*bbhg_incand_pk_amp + 5.02184E-10*bbhg_incand_pk_amp*bbhg_incand_pk_amp
		bbhg_mass_uncertainty_uncorr = 0.06083 + 7.67522E-6*bbhg_incand_pk_amp + 1.60111E-10*bbhg_incand_pk_amp*bbhg_incand_pk_amp

		bbhg_mass_corr = bbhg_mass_uncorr/0.7 #AD correction factor is 0.7 +- 0.05
		bbhg_mass_only_rel_err = bbhg_mass_uncertainty_uncorr/bbhg_mass_uncorr
		bbhg_ADcorr_rel_err = (0.05/0.7)
		bbhg_mass_abs_uncertainty_corr = (bbhg_ADcorr_rel_err + bbhg_mass_only_rel_err) * bbhg_mass_corr

		#LG
		bblg_mass_uncorr = -0.15884 + 0.00176*bblg_incand_pk_amp + 3.19118E-8*bblg_incand_pk_amp*bblg_incand_pk_amp
		bblg_mass_uncertainty_uncorr = 0.47976 + 1.93451E-4*bblg_incand_pk_amp + 1.53274E-8*bblg_incand_pk_amp*bblg_incand_pk_amp

		bblg_mass_corr = bblg_mass_uncorr/0.7 #AD correction factor is 0.7 +- 0.05
		bblg_mass_only_rel_err = bblg_mass_uncertainty_uncorr/bblg_mass_uncorr
		bblg_ADcorr_rel_err = (0.05/0.7)
		bblg_mass_abs_uncertainty_corr = (bblg_ADcorr_rel_err + bblg_mass_only_rel_err) * bblg_mass_corr

		if 0.33 <= bbhg_mass_corr < 1.8:
			masses['rBC_mass_fg_HG_low'].append(bbhg_mass_corr)
			mass_uncertainties['rBC_mass_fg_HG_low'].append(bbhg_mass_abs_uncertainty_corr)
		if 1.8 <= bbhg_mass_corr < 12.8:
			masses['rBC_mass_fg_HG_high'].append(bbhg_mass_corr)
			mass_uncertainties['rBC_mass_fg_HG_high'].append(bbhg_mass_abs_uncertainty_corr)
		if 1.8 <= bblg_mass_corr < 12.8:
			masses['rBC_mass_fg_LG_low'].append(bblg_mass_corr)
			mass_uncertainties['rBC_mass_fg_LG_low'].append(bblg_mass_abs_uncertainty_corr)
		if 12.8 <= bblg_mass_corr < 41:
			masses['rBC_mass_fg_LG_high'].append(bblg_mass_corr)
			mass_uncertainties['rBC_mass_fg_LG_high'].append(bblg_mass_abs_uncertainty_corr)

	tot_rBC_mass_fg_HG_low =  sum(masses['rBC_mass_fg_HG_low'])
	tot_rBC_mass_uncer_HG_low =  sum(mass_uncertainties['rBC_mass_fg_HG_low'])
	rBC_number_HG_low =  len(masses['rBC_mass_fg_HG_low'])

	tot_rBC_mass_fg_HG_high = sum(masses['rBC_mass_fg_HG_high'])
	tot_rBC_mass_uncer_HG_high = sum(mass_uncertainties['rBC_mass_fg_HG_high'])
	rBC_number_HG_high = len(masses['rBC_mass_fg_HG_high'])

	tot_rBC_mass_fg_LG_low =  sum(masses['rBC_mass_fg_LG_low'])
	tot_rBC_mass_fg_uncer_low =  sum(mass_uncertainties['rBC_mass_fg_LG_low'])
	rBC_number_LG_low = len(masses['rBC_mass_fg_LG_low'])

	tot_rBC_mass_fg_LG_high = sum(masses['rBC_mass_fg_LG_high'])
	tot_rBC_mass_fg_uncer_high = sum(mass_uncertainties['rBC_mass_fg_LG_high'])
	rBC_number_LG_high = len(masses['rBC_mass_fg_LG_high'])

	ind_mass_tot = ind_mass_tot + (tot_rBC_mass_fg_HG_low + (tot_rBC_mass_fg_HG_high+tot_rBC_mass_fg_LG_low)/2 + tot_rBC_mass_fg_LG_high)
	ind_mass_uncer_tot = ind_mass_uncer_tot + (tot_rBC_mass_uncer_HG_low + (tot_rBC_mass_uncer_HG_high+tot_rBC_mass_fg_uncer_low)/2 + tot_rBC_mass_fg_uncer_high)
	ind_number_tot = ind_number_tot + (rBC_number_HG_low + (rBC_number_HG_high+rBC_number_LG_low)/2 + rBC_number_LG_high)

	if total_sample_vol == 0:
		file_data.append([start,start + timedelta(hours = timestep), np.nan, np.nan, np.nan,total_sample_vol])
	else:
		file_data.append([start,start + timedelta(hours = timestep), ind_mass_tot*saved_1_of_every/total_sample_vol,ind_mass_uncer_tot*saved_1_of_every/total_sample_vol,ind_number_tot*saved_1_of_every/total_sample_vol,total_sample_vol])

	next_hour = start + timedelta(hours = timestep)

	#if this is the last hour of the day write to file
	if next_hour.day != start.day:
		if start.day < 10:
			file_name = str(start.year) + '0' +  str(start.month) + '0' + str(start.day) + ' - hourly mass and number concentration'
		else:
			file_name = str(start.year) + '0' +  str(start.month) + str(start.day) + ' - hourly mass and number concentration'
		file = open('C:/Users/Sarah Hanna/Documents/Data/Alert Data/Alert 1h mass and number concentrations/' + file_name +'.txt', 'w')
		file.write('mass and number concentration for SP2#58 at Alert \n')
		file.write('all concentrations have been corrected for sampling filter set at 1 of Every ' + str(saved_1_of_every) + ' particles saved \n')
		file.write('interval_start(UTC) \t interval_end(UTC) \t rBC_mass_concentration(ng/m3) \t rBC_mass_concentration_uncertainty(ng/m3) \t rBC_number_concentration(#/cm3) \t sampling_volume(cc) \n')
		for row in file_data:
			line = '\t'.join(str(x) for x in row)
			file.write(line + '\n')
		file.close()
		file_data = []

	start += timedelta(hours = timestep)

cnx.close()


text:
# -*- coding: utf-8 -*-
"""optum_1.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1daWPTXQrjXEcRTos6jjv_D2se5W6WaXK
"""

import xgboost

!gdown https://drive.google.com/uc?id=17Z4RZFpo6SIS6xKgem7U-5g4VwcOX7cg

!unzip data.zip

ls

import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import MinMaxScaler
from imblearn.over_sampling import SMOTE, ADASYN

data = pd.read_csv("f_data_cut_icutype_1.csv")
data=data.drop(columns=['RecordID', 'ICUType'])
from sklearn.model_selection import train_test_split
df_train, df_test = train_test_split(data,
                                     train_size = 0.7,
                                     test_size = 0.3,
                                     random_state = 100)

x_train=df_train.drop(columns=['In-hospital_death'])
y_train=df_train['In-hospital_death']

#x_resampled, y_resampled = SMOTE().fit_resample(x_train, y_train)

x_test=df_test.drop(columns=['In-hospital_death'])
y_test=df_test['In-hospital_death']

import numpy as np
import pandas as pd
import sklearn
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import metrics

import xgboost as xgb
from xgboost import XGBClassifier
from xgboost import plot_importance

tree = DecisionTreeClassifier(max_depth=2)

# adaboost with the tree as base estimator
adaboost_model_1 = AdaBoostClassifier(
    base_estimator=tree,
    n_estimators=600,
    learning_rate=1.5,
    algorithm="SAMME")

adaboost_model_1.fit(x_resampled, y_resampled)

predicted = model.predict(x_test)

# get the accuracy
print (accuracy_score(y_test, predicted))

from sklearn.metrics import confusion_matrix
#from sklearn.model_selection import GridSearchCV
print(confusion_matrix(y_test, predicted))

predictions = model.predict_proba(x_test)
predictions[:10]
metrics.roc_auc_score(y_test, predictions[:,1])

param_grid = {"base_estimator__max_depth" : [2, 5],
              "n_estimators": [200, 400, 600]
             }
tree = DecisionTreeClassifier()

# adaboost with the tree as base estimator
# learning rate is arbitrarily set to 0.6, we'll discuss learning_rate below
ABC = AdaBoostClassifier(
    base_estimator=tree,
    learning_rate=0.6,
    algorithm="SAMME")
folds = 3
grid_search_ABC = GridSearchCV(ABC,
                               cv = folds,
                               param_grid=param_grid,
                               scoring = 'roc_auc',
                               return_train_score=True,
                               verbose = 1)
grid_search_ABC.fit(x_resampled, y_resampled)

model = XGBClassifier()
model.fit(x_train, y_train)

y_pred = model.predict_proba(x_test)
y_pred[:10]

folds = 3

# specify range of hyperparameters
param_grid = {'learning_rate': [0.2, 0.6],
             'subsample': [0.3, 0.6, 0.9]}

# specify model
xgb_model = XGBClassifier(max_depth=2, n_estimators=200)

# set up GridSearchCV()
model_cv = GridSearchCV(estimator = xgb_model,
                        param_grid = param_grid,
                        scoring= 'roc_auc',
                        cv = folds,
                        verbose = 1,
                        return_train_score=True)

model_cv.fit(x_train, y_train)

cv_results = pd.DataFrame(model_cv.cv_results_)
cv_results

params = {'learning_rate': 0.2,
          'max_depth': 2,
          'n_estimators':200,
          'subsample':0.3,
         'objective':'binary:logistic'}

# fit model on training data
model = XGBClassifier(params = params)
model.fit(x_train, y_train)

predicted = model.predict(x_test)

# get the accuracy
print (accuracy_score(y_test, predicted))

from sklearn.metrics import confusion_matrix
#from sklearn.model_selection import GridSearchCV
print(confusion_matrix(y_test, predicted))

importance = dict(zip(x_train.columns, model.feature_importances_))
print(sorted(importance.items(), key =
             lambda kv:(kv[1], kv[0])))

from sklearn.externals import joblib
joblib.dump(model, 'ICU1.pkl')

ls


text:
import torch as torch
from torch.utils import data
import copy
import hexagdly as hg
import pandas as pd
import numpy as np

import math as math

from matplotlib import pyplot as plt
import torch.optim as optim
import time as time

from sklearn import metrics as metrics

import DataOption as DataOption

class MagicDataset(data.Dataset):
    """ Magic dataset used for loading MAGIC Data with the PyTorch DataLoader """

    def __init__(self, datf, telescope_data_position_map, data_option=DataOption.stereo_tot_sep,
                 regressor_columns=None, pixnum=1039):  # csv_file, root_dir, transform=None):
        """
         Magic dataset used for loading MAGIC Data with the PyTorch DataLoader

        :param datf pandas dataframe: Dataframe containing MAGIC Data according to ...
        :param data_option: DataOption from DataOption.py
        :param telescope_data_position_map: Map containing the position of data inside the Dataframe (given by DataHandler)
        :param regressor_columns: List of Regressor Column Names used for regressiond tasks, None if Classification
        """

        if regressor_columns is not None:
            self.regressor_columns = regressor_columns
        else:
            self.regressor_columns = []

        self.data_option = data_option
        #print(self.data_option)
        if self.data_option == DataOption.stereo_tot_sep:
            self.itemFunction = self.item_stereo_tot_sep

        if self.data_option == DataOption.it_channel_sep:
            self.itemFunction = self.item_it_channel_sep

        if self.data_option == DataOption.channel_stereo_comb_sep:
            self.itemFunction = self.item_channel_stereo_comb_sep

        if self.data_option == DataOption.channel_tot_comb:
            self.itemFunction = self.item_channel_tot_comb

        self.dataframe = datf

        #self.tIndex = "1_" + str(self.sizeX - 1) + '|' + str(self.sizeY - 1)
        #self.tIndex2 = "2_" + str(self.sizeX - 1) + '|' + str(self.sizeY - 1)

        #if telescope_data_position_map is None:
        #    print("Error, No position List found")
        #    self.telescope_data_position_list = [("1_0|0", '1_' + str(self.sizeX-1) + '|' + str(self.sizeY-1))]
        #    self.telescope_data_position_list.append(("2_0|0",'2_' + str(self.sizeX - 1) + '|' + str(self.sizeY - 1)))
        #if telescope_data_position_list is None:

        #else:
        self.telescope_data_position_map = telescope_data_position_map

        self.dtype = "double"

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

    def __getitem__(self, idx):
        return self.itemFunction(idx)

    def __get_data__(self, idx):
        row = self.dataframe.iloc[idx]

        # Read data from pandas series
        image1 = row.loc[self.telescope_data_position_map["i_M1"][0]: self.telescope_data_position_map["i_M1"][1]]
        image2 = row.loc[self.telescope_data_position_map["i_M2"][0]: self.telescope_data_position_map["i_M2"][1]]
        time_1 = row.loc[self.telescope_data_position_map["p_M1"][0]: self.telescope_data_position_map["p_M1"][1]]
        time_2 = row.loc[self.telescope_data_position_map["p_M2"][0]: self.telescope_data_position_map["p_M2"][1]]

        image1 = torch.tensor(image1.to_numpy(dtype=self.dtype))
        image2 = torch.tensor(image2.to_numpy(dtype=self.dtype))

        time_1 = torch.tensor(time_1.to_numpy(dtype=self.dtype))
        time_2 = torch.tensor(time_2.to_numpy(dtype=self.dtype))

        pID = row['particleID']
        minval = torch.tensor(row.iloc[-2])
        maxval = torch.tensor(row.iloc[-1])

        reg_col = []
        for regc in self.regressor_columns:

            reg_col.append(row.loc[regc])
        reg_col = torch.tensor(reg_col)

        return image1, image2, time_1, time_2, pID, maxval, minval, self.dataframe.index[idx], reg_col, row.loc['pointing_alt'], row.loc['pointing_az']

    def item_stereo_tot_sep(self, idx):

        image1, image2, time1, time2, pID, maxval, minval, pdRow, reg_col, palt, paz = self.__get_data__(idx)

        sample = {'image1': image1, 'image2': image2, 'time1': time1, 'time2': time2, 'particleID': pID,
                  'pdRow': pdRow, 'min_value': minval, 'max_value': maxval , 'regressor_columns':reg_col,
                  'pointing_alt': palt, 'pointing_az': paz}
        return sample

    def item_it_channel_sep(self, idx):
        image1, image2, time1, time2, pID, maxval, minval, pdRow, reg_col, palt, paz = self.__get_data__(idx)

        image1 = torch.cat([image1.unsqueeze(0), image2.unsqueeze(0)])#, dim=1)
        time1 = torch.cat([time1.unsqueeze(0), time2.unsqueeze(0)])#, dim=1)

        sample = {'image1': image1, 'time1': time1, 'particleID': pID,
                  'pdRow': pdRow, 'min_value': minval, 'max_value': maxval, 'regressor_columns': reg_col,
                  'pointing_alt': palt, 'pointing_az': paz}
        return sample

    def item_channel_stereo_comb_sep(self, idx):
        image1, image2, time1, time2, pID, maxval, minval, pdRow, reg_col, palt, paz = self.__get_data__(idx)

        image1 = torch.cat([time1.unsqueeze(0), image1.unsqueeze(0)])#, dim=1)
        image2 = torch.cat([time2.unsqueeze(0), image2.unsqueeze(0)])#, dim=1)

        sample = {'image1': image1, 'image2': image2, 'particleID': pID,
                  'pdRow': pdRow, 'min_value': minval, 'max_value': maxval , 'regressor_columns':reg_col,
                  'pointing_alt': palt, 'pointing_az': paz}
        return sample

    def item_channel_tot_comb(self, idx):
        image1, image2, time1, time2, pID, maxval, minval, pdRow, reg_col, palt, paz = self.__get_data__(idx)

        image1 = torch.cat([time1.unsqueeze(0), image1.unsqueeze(0), time2.unsqueeze(0), image2.unsqueeze(0)])#, dim=1)

        sample = {'image1': image1, 'particleID': pID,
                  'pdRow': pdRow, 'min_value': minval, 'max_value': maxval , 'regressor_columns':reg_col,
                  'pointing_alt': palt, 'pointing_az': paz}
        return sample

max_stars_repo_name: SKellerML/MAGICML

max_issues_repo_name: SKellerML/MAGICML

max_forks_repo_name: SKellerML/MAGICML


text:
# Copyright 2021-present, Nike, Inc.
# All rights reserved.
#
# This source code is licensed under the Apache-2.0 license found in
# the LICENSE file in the root directory of this source tree.

import logging
import re
import networkx as nx
from networkx.algorithms import topological_sort
from networkx.algorithms import is_directed_acyclic_graph

from knockoff.factory.sink import KnockoffSink
from knockoff.factory.source import KnockoffSource
from knockoff.factory.node import Table, FactoryPart, FactoryComponent, FactoryPrototype
from knockoff.exceptions import DependencyNotFoundError

logger = logging.getLogger(__name__)

DEPENDENCY_REGEX = (r'^(part|component|prototype)\:'
                    '([A-Za-z0-9-_]+)[.]?([A-Za-z0-9-_]*)$')

DEPENDENCY_PATTERN = re.compile(DEPENDENCY_REGEX)

class BlueprintBuilder:
    def __init__(self):
        self.dag = nx.DiGraph()
        self.nodes = []
        self.index = {}
        self.dependency_queue = []

    def _add_node_to_index(self, node_type, name):
        i = len(self.index)
        if (node_type, name) in self.index:
            raise Exception("Node of type={} with name={} already exists."
                            .format(node_type, name))
        self.index[(node_type, name)] = i
        return i

    def add_part(self, node):
        node_type = node['type']
        name = node['name']
        source = KnockoffSource(node_type, node['source'])

        i = self._add_node_to_index(node_type, name)

        node = FactoryPart(name, i, source)

        self.nodes.append(node)
        self.dag.add_node(i)

        dependencies = source.config.get('dependencies') or []
        for dep in dependencies:
            other_node_type, other_name, _ = Assembler.parse_dependency(dep)
            self._add_dependency(i, other_node_type, other_name)

    def _add_dependency(self, i, other_type, other_name, queue_on_error=True):
        try:
            other_i = self.index[other_type, other_name]
            self.dag.add_edge(other_i, i)
        except KeyError:
            if queue_on_error:
                self.dependency_queue.append((i, (other_type, other_name)))
            else:
                raise DependencyNotFoundError("Dependency not found: "
                                              "node={}, name={}"
                                              .format(other_type,
                                                      other_name))

    def add_table(self, node):
        node_type = node['type']
        name = node['name']
        source = KnockoffSource(node_type, node['source'])
        sink = KnockoffSink(node.get('sink', {'strategy': 'noop'}))

        i = self._add_node_to_index(node_type, name)

        node = Table(name, i, source, sink, table=node.get('table'))

        self.nodes.append(node)
        self.dag.add_node(i)

        if source.config['strategy'] == "knockoff":
            other_name = source.config.get('kwargs', {}).get('prototype')
            self._add_dependency(i, 'prototype', other_name)

    def add_prototype(self, node):
        node_type = node['type']
        name = node['name']

        i = self._add_node_to_index(node_type, name)
        source = KnockoffSource(node_type, node['source'])
        prototype = FactoryPrototype(name, i, source, unique=node.get('unique'))
        self.nodes.append(prototype)
        self.dag.add_node(i)

        if source.config['strategy'] == 'components':
            components = []
            for component in source.config['components']:
                component_index_name = "{}.{}".format(name,
                                                      component['name'])
                other_i = self._add_node_to_index('component',
                                                  component_index_name)
                component_source = KnockoffSource("component",
                                                  component['source'])
                component = FactoryComponent(component['name'],
                                             other_i,
                                             component_source)
                components.append(component)
                self.nodes.append(component)
                self._add_dependency(i, 'component',
                                     component_index_name)
                for dep in component.source.config.get('dependencies', []):
                    comp_dep_type, comp_dep_name, _ = Assembler.parse_dependency(dep)
                    self._add_dependency(other_i, comp_dep_type, comp_dep_name)

            prototype.source.components = components
        elif source.config['strategy'] == 'concat':
            for dep in source.config['dependencies']:
                other_type, other_name, _ = Assembler.parse_dependency(dep)
                self._add_dependency(i, other_type, other_name)
        elif source.config['strategy'] == 'io':
            # TODO: anything to do?
            pass
        else:
            raise ValueError("Unrecognized strategy: {} for prototype".format(source.config['strategy']))

    def build(self):
        assert len(self.index) == len(self.nodes), 'Mismatch in index and nodes'
        assert len(self.dag.nodes) == len(self.nodes), 'Mismatch in dag and nodes'
        for i, (node_type, name) in self.dependency_queue:
            self._add_dependency(i, node_type, name, queue_on_error=False)
        assert is_directed_acyclic_graph(self.dag)
        return Blueprint(self.dag, self.nodes, self.index)

class Blueprint:
    def __init__(self, dag, nodes, index):
        self.dag = dag
        self.nodes = nodes
        self.index = index

    def get_node(self, node_type, name):
        return self.nodes[self.index[node_type, name]]

    def iter_topologically(self):
        return topological_sort(self.dag)

    @staticmethod
    def from_config(config):
        # TODO: add validation for config

        builder = BlueprintBuilder()

        for node in config['dag']:

            if node['type'] == FactoryPart.node_type:
                builder.add_part(node)
            elif node['type'] == Table.node_type:
                builder.add_table(node)
            elif node['type'] == FactoryPrototype.node_type:
                builder.add_prototype(node)
            elif node['type'] == FactoryComponent.node_type:
                # TODO: can we get here? should components be restricted to prototypes
                raise Exception('component must be part of prototype')
            else:
                raise Exception('node type not recognized: {}'
                                .format(node['type']))
        blueprint = builder.build()

        return blueprint

class Assembler:
    def __init__(self, blueprint):
        self.blueprint = blueprint

    def accept(self, visitor):
        visitor.visit(self)

    def start(self):
        # TODO: we can parallelize the execution of the dag
        for i in self.blueprint.iter_topologically():
            node = self.blueprint.nodes[i]
            logger.debug(("Assembler accepting visit from node:"
                          " name={}, type={}, ix={}")
                         .format(node.name,
                                 # TODO: replace with enum / classmap?
                                 node.source.node_type,
                                 node.ix))
            self.accept(node)

    def assemble_table(self, node):
        node.load(self)
        node.sink.dump(node.table, node.data)

    def assemble_part(self, node):
        node.load(self)

    def prepare_component(self, node):
        node.prepare(self)

    def assemble_prototype(self, node):
        node.load(self)

    @staticmethod
    def parse_dependency(string, pattern=DEPENDENCY_PATTERN):
        match = pattern.match(string)
        if match:
            return match.groups()
        raise Exception("Dependency pattern not recognized: {}"
                        .format(string))

max_stars_repo_name: Nike-Inc/knockoff-factory

max_issues_repo_name: Nike-Inc/knockoff-factory

max_forks_repo_name: Nike-Inc/knockoff-factory

reason: import networkx,from networkx