text:
# -*- coding: utf-8 -*-
import os
import pandas as pd
import datetime
import time
import numpy as np
import matplotlib.pyplot as plt
import csv
from luminol.anomaly_detector import AnomalyDetector
import luminol.utils

def timestamp_to_datetime(x):
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(x))

def listdir(path, list_name, file_name):  #传入存储的list
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
                  listdir(file_path, list_name)
        else:
            list_name.append(file_path)
            file_name.append(file)

# in_path = 'K://Algorithm_study_insuyan/data/quater_data_three/mpp/incoming_data/'
# result_path = 'K://Algorithm_study_insuyan/data/quater_data_three/mpp/incoming_result/'

# in_path = 'K://Algorithm_study_insuyan/data/quater_data_three/mpp/outgoing_data/'
# result_path = 'K://Algorithm_study_insuyan/data/quater_data_three/mpp/outgoing_result/'

# in_path = 'K://Algorithm_study_insuyan/data/quater_data_three/hadoop/incoming_data/'
# result_path = 'K://Algorithm_study_insuyan/data/quater_data_three/hadoop/incoming_result/'

in_path = 'K://Algorithm_study_insuyan/data/quater_data_three/hadoop/outgoing_data/'
result_path = 'K://Algorithm_study_insuyan/data/quater_data_three/hadoop/outgoing_result/'

FORMAT1 = '%Y-%m-%d %H:%M:%S'

filepath_list = []
filename_list = []
filename = []
ip_list = []
listdir(in_path, filepath_list, filename_list)
for i in filename_list:
    filename.append(os.path.splitext(i)[0])
    ip_list.append(i.split('_')[0])

# param = tuple(ip_list)
# print type(param)
#
# param_dict = {}
# for i in param:
#     param_dict[i] = 'data_series' + str(i.split('.')[3])
#
# print param_dict
#
# def test_dict_param(**args) :
#     for k, v in args.iteritems():
#         temp = v
#
# test_dict_param(**param_dict)

# print ip_list
summary_series = []
# for i in range(len(ip_list)):
tmp = {}
series = {}
sample_series = {}
sample_df = {}
for i in range(len(filepath_list)):
    # locals()[ip_list[i]] = pd.read_csv(filepath_list[i], delimiter='\t')
    # print locals()[ip_list[i]]

    tmp[ip_list[i]] = pd.read_csv(filepath_list[i], delimiter='\t')

    tmp[ip_list[i]].ix[:,0] = tmp[ip_list[i]].ix[:,0].apply(timestamp_to_datetime)
    series[ip_list[i]] = tmp[ip_list[i]].ix[:, 1]
    series[ip_list[i]].index = tmp[ip_list[i]].ix[:,0].apply(lambda x: datetime.datetime.strptime(x,FORMAT1))
    # print series[ip_list[i]]
    sample_series[ip_list[i]] = series[ip_list[i]].resample('5T', how='mean', closed='left')
    print sample_series[ip_list[i]]

    sample_df[ip_list[i]] = pd.DataFrame(list(sample_series[ip_list[i]].values))
    sample_df[ip_list[i]].index = sample_series[ip_list[i]].index
    sample_df[ip_list[i]].columns = [ip_list[i]]

    print ip_list[i]
    print '*'*10 + str(i)
    if len(summary_series) == 0:
        summary_series = sample_df[ip_list[i]]
    else:
        # summary_series = pd.concat([summary_series,five_minute_data2])
        summary_series = pd.concat([summary_series, sample_df[ip_list[i]]], axis=1)
print summary_series
#
# #compute mean and std for each time(5 min)
# mean_series = summary_series.mean(1)
# std_series = summary_series.std(1)
#
# #get upper and down for each time(5 min)
# upper_series = mean_series + 3 * std_series
# down_series = mean_series - 3 * std_series
# upper_series.to_csv(result_path + 'upper_series_with_3std.csv')
# down_series.to_csv(result_path + 'down_series_with_3std.csv')
#
# # len(filepath_list)
# upper_data = pd.read_csv(result_path + 'upper_series_with_3std.csv',names=['kpi_time','upper'],parse_dates=True)
# down_data = pd.read_csv(result_path + 'down_series_with_3std.csv',names=['kpi_time','down'],parse_dates=True)
# upper_df = pd.DataFrame(upper_data)
# down_df = pd.DataFrame(down_data)
#
#
# stand_data = down_df.merge(upper_df, on=['kpi_time'])
# stand_data['kpi_time'] = stand_data['kpi_time'].apply(lambda x: datetime.datetime.strptime(x, FORMAT1))
#
# for i in range(len(filepath_list)):
#     test_data = pd.read_csv(filepath_list[i], delimiter='\t')
#     test_df = pd.DataFrame(test_data)
#     test_df.columns = ['kpi_time','kpi_value']
#
#     test_df['kpi_time'] = test_df['kpi_time'].apply(timestamp_to_datetime)
#     # test_df['kpi_time'] = test_df['kpi_time'].apply(lambda x: datetime.datetime.strptime(x,FORMAT1))
#
#     test_series = test_df['kpi_value']
#     test_series.index = test_df['kpi_time'].apply(lambda x: datetime.datetime.strptime(x, FORMAT1))
#
#     five_minute_data = test_series.resample('5T', how='mean', closed='left')
#     five_minute_data2 = pd.DataFrame(list(zip(five_minute_data.index, five_minute_data.values)))
#     five_minute_data2.columns = ['kpi_time', 'kpi_value']
#
#     all_data = pd.merge(five_minute_data2, stand_data, on=['kpi_time'])
#     all_data = all_data.dropna(how='any')
#     all_data['lable'] = 0
#     all_data['lable'][all_data['kpi_value'] > all_data['upper']] = 1
#     all_data['lable'][all_data['kpi_value'] < all_data['down']] = -1
    # all_data.ix[:,[0,1,2,3,4]].to_csv(result_path + filename[i] + '_three_std.csv',index=False)
    # print '*' * 30 + str(ip_list[i]) + '*' * 30 + 'remain' + str(len(filepath_list) - i - 1) + '*' * 30


text:
"""Introduces the Function class, a class which represents a mathematical function,
which can be plotted as an animation, or visualised as a graph"""

import numpy as np
from matplotlib.collections import PolyCollection

plot_defaults = {
	"lw" : 3,
	"color" : "black",
}

def add_point_to_line(l, x, y):
	"""Add (x,y) point to pyplot line2d"""
	l.set_xdata(np.concatenate([l._x, [x]]))
	l.set_ydata(np.concatenate([l._y, [y]]))

def add_points_to_line(l, xs, ys):
	"""Add points to pyplot line2d"""
	l.set_xdata(np.concatenate([l._x, xs]))
	l.set_ydata(np.concatenate([l._y, ys]))

class Function:

	def __init__(self, func, xrange=None, animate=False, id=0, **plt_kw):

		self.id = id
		self.func = func

		if xrange is None: xrange = np.arange(0, 1, 0.05)

		self.x = xrange
		self.plot = None # Line2D object

		self.plt_kw = plot_defaults.copy()
		# replace default plot keywords with any given
		for kw, val in plt_kw.items():
			self.plt_kw[kw] = val

	def set_plot(self, plot):
		self.plot = plot

	def __call__(self, *args, **kwargs):
		return self.func(*args, **kwargs)

class XSampler():
	"""Used to sample a numpy xrange in order to effectively provide smooth animations"""
	def __init__(self, x0=0, x1=1, n_samples=10, sample="linear"):
		self.x = np.linspace(x0, x1, n_samples)

class AnimatedFunction(Function):
	"""Subclass of function, used to produce animations"""

	def __init__(self, x0=0, x1=1, x_samples=10, sampling="linear", sample_per_it = 1, **kwargs):
		"""
		:param sample_data: tuple of (x_start, x_end, n_samples)
		:param sampling:
		:param kwargs:
		"""
		super().__init__(**kwargs)
		self.x = XSampler(x0, x1, x_samples * sample_per_it, sampling).x

		self.sample_per_it = sample_per_it
		self.c = 0 # counter

	def set_plot(self, plot):
		"""Take Line2D object, save to function"""
		# For animating, mark every param needs to be stored separately to render through animation
		if plot._markevery is not None:
			self.markevery = np.array(plot._markevery)
			plot._markevery = []
		else:
			self.markevery = None

		return super().set_plot(plot)

	def __iter__(self): return self

	def __next__(self):
		xs, ys = [], []
		for n in range(self.sample_per_it):
			if self.c >= len(self):
				add_points_to_line(self.plot, xs, ys) # add points so far
				raise StopIteration

			if self.markevery is not None:
				self.plot._markevery = self.markevery[self.markevery < self.c] # update markevery

			x = self.x[self.c]
			y = self(x)

			xs.append(self.x[self.c])
			ys.append(y)

			self.c += 1

		add_points_to_line(self.plot, xs, ys)

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

class AnimatedFunctional(Function):
	"""A univariable function f(x), which changes with t. """

	def __init__(self, func,  t0=0, t1=1, t_samples=10, t_sampling="linear", x0=0, x1=1, x_samples = 100, xrange=None, *args, **kwargs):

		if xrange is None:
			xrange = XSampler(x0, x1, x_samples).x

		self.t = XSampler(t0, t1, t_samples, t_sampling).x
		self.functional = func

		self.c = 0
		super().__init__(lambda x: func(x, t=t0), xrange=xrange, *args, **kwargs)

	def __next__(self):
		if self.c >= len(self): raise StopIteration
		t = self.t[self.c]
		self.func = lambda x: self.functional(x, t=t)

		self.plot.set_xdata(self.x)
		self.plot.set_ydata(self.func(self.x))

		self.c += 1

	def __iter__(self): return self
	def __len__(self): return len(self.t)

class FillBetween(PolyCollection):
	"""Fills between two functions. Can be animated."""

	def __init__(self, ax, func1, func2, animate=False, obj_id=0, **fill_kwargs):
		self.ax = ax
		self.id = obj_id
		self.func1, self.func2 = func1, func2
		self.animate = animate
		if not np.array_equal(func1.x, func2.x):
			print("Func1.x and func2.x do not match for fill_between. Using func1.x")

		self.x = x = func1.x

		if not animate:
			ax.fill_between(x, func1(x), func2(x), **fill_kwargs)

		self.fill_kwargs = fill_kwargs
		self.c = 0 # counter

	def get_animated(self):
		return self.animate

	def __len__(self):
		return len(self.x) - 1 # only go to penultimate x val

	def __iter__(self): return self

	def __next__(self):

		if self.c >= len(self): raise StopIteration

		x0, x1 = self.x[self.c:self.c+2]
		x1+=(x1-x0)*0.5 # padding to prevent whitespace
		f1, f2 = self.func1, self.func2
		verts = np.array([[[x, f(x)] for x, f in [[x0, f1], [x0, f2], [x1, f2], [x1, f1] ]]])

		collection = PolyCollection(verts, **self.fill_kwargs)
		self.ax.add_collection(collection, autolim=False)

		self.c += 1

ext: py

max_stars_repo_name: OllieBoyne/vis-eng

max_issues_repo_name: OllieBoyne/vis-eng

max_forks_repo_name: OllieBoyne/vis-eng

reason: import numpy


text:
"""
Use to convert *.pyx in the Bottleneck sandbox to a C file and compile it.

This setup.py is NOT used to install the Bottleneck package. The Bottleneck
setup.py file is bottleneck/setup.py

To convert from cython to C:

$ cd bottleneck/sandbox
$ python setup.py build_ext --inplace
"""

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy

ext_modules = [Extension("nanmean", ["nanmean.pyx"],
               include_dirs=[numpy.get_include()])]

setup(
  name = 'nanmean',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

max_stars_repo_name: fhal/bottleneck

max_issues_repo_name: fhal/bottleneck

max_forks_repo_name: fhal/bottleneck


text:
from setuptools import setup, find_packages, Extension
from Cython.Build import cythonize, build_ext

import numpy as np

exts = [
    Extension(
        "parallel_sort",
        sources=["parallel_sort/parallel_sort.pyx"],
        define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")],
        extra_compile_args=["-fopenmp"],
        extra_link_args=["-lgomp", "-lpthread"],
        include_dirs=[np.get_include()],
        language="c++"
    )
]

with open("README.md", "r") as f:
    long_desc = f.read()

setup(
    name="parallel-sort",
    version="0.0.3",
    author="Calvin Sykes",
    author_email="sykescalvin09@gmail.com",
    url="https://github.com/calvin-sykes/cython_parallel_sort",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: POSIX :: Linux"
    ],
    cmdclass={"build_ext": build_ext},
    description="Cython interface to GNU Parallel sorting routines",
    ext_modules=cythonize(exts, language_level=3),
    install_requires=["numpy"],
    long_description=long_desc,
    long_description_content_type="text/markdown",
    package_dir={"": "parallel_sort"},
    packages=find_packages(where="parallel_sort"),
    project_urls={
        "Bug Tracker":
        "https://github.com/calvin-sykes/cython_parallel_sort/issues"
    },
    python_requires=">=3.6",
    setup_requires=["cython", "numpy"],
    tests_require=["pytest"],
    zip_safe=False
)


text:
#*******************************************************
#* Copyright (C) 2017 Mitchell Beard
#* This file is part of MicroOlli.
#************************************************
#*****************client the runs on olli wile server also runs to make Olli drive autonomously
import socket               # Import socket module
from gopigo import *	#Has the basic functions for controlling the GoPiGo Robot
import sys	#Used for closing the running program
import pygame #Gives access to KEYUP/KEYDOWN events
import picamera
import os, os.path
import numpy as np
import argparse
import os
import sys
pwm = Adafruit_PCA9685.PCA9685()
def set_servo_pulse(channel, pulse):
    pulse_length = 1000000    # 1,000,000 us per second
    pulse_length //= 60       # 60 Hz
    print('{0}us per period'.format(pulse_length))
    pulse_length //= 4096     # 12 bits of resolution
    print('{0}us per bit'.format(pulse_length))
    pulse *= 1000
    pulse //= pulse_length
    pwm.set_pwm(channel, 0, pulse)
# Import the PCA9685 module.
pwm.set_pwm_freq(60)
import Adafruit_PCA9685
camera=picamera.PiCamera()
charpre='p'
accelerate=0

if __name__ == '__main__':
    iteration=0
    while True:
        imagename='/home/pi/Desktop/capture.jpg'
        camera.capture(imagename)
        s = socket.socket()         # Create a socket object
        host = '192.168.1.15' # Get local machine name
        port = 12344                 # Reserve a port for your service.

        s.connect((host, port))
        f = open('capture.jpg','rb')
        print 'Sending...'
        l = f.read(1024)
        while (l):
            print 'Sending...'
            s.send(l)
            l = f.read(1024)
        f.close()
        print "Done Sending"
        s.shutdown(socket.SHUT_WR)

        char=s.recv(1024)
        print('character received is ',char)
        s.close()

        if char=='w':
                    pwm.set_pwm(0, 1, 450)
                    pwm.set_pwm(1, 1, 450) #Move forward
                    print('forward')
                    time.sleep(.5)
                    pwm.set_pwm(0, 1, 0)
                    pwm.set_pwm(1, 1, 0)
        elif char=='a':
                    pwm.set_pwm(1, 0, 500) # Move left
                    pwm.set_pwm(0, 0, 325)
                    print('left')

                    time.sleep(0.5)

                    pwm.set_pwm(0, 1, 450)
                    pwm.set_pwm(1, 1, 450)

                    time.sleep(0.5)

                    pwm.set_pwm(0, 1, 0)
                    pwm.set_pwm(1, 1, 0)

                    print('running in a')

        elif char=='d':
                    #right();# Turn Right
                    pwm.set_pwm(0, 0, 500) # Move left
                    pwm.set_pwm(1, 0, 325)
                    print('left')

                    time.sleep(0.5)

                    pwm.set_pwm(0, 1, 450)
                    pwm.set_pwm(1, 1, 450)

                    time.sleep(0.5)

                    pwm.set_pwm(0, 1, 0)
                    pwm.set_pwm(1, 1, 0)

            print('running in a')

        elif char=='s':
                    pwm.set_pwm(1, 0, 350) #Move backward
                    pwm.set_pwm(0, 0, 350)
                    print('backward')

        elif char=='t':
                    increase_speed();	# Increase speed
                    print('running in t')
        elif char=='g':
                    decrease_speed();	# Decrease speed
                    print('running in g')

        if (charpre==char) and (charpre=='a' or charpre=='d'):
               accelerate=accelerate+5
        else:
               accelerate=0
        charpre=char
        print('acceleration is ',accelerate)
        iteration=iteration+1


text:
"""Convert inputs to arrays with at least one dimension."""
from __future__ import annotations
from typing import List, Union

import numpy
import numpoly

from ..baseclass import ndpoly, PolyLike
from ..dispatch import implements

@implements(numpy.atleast_1d)
def atleast_1d(*arys: PolyLike) -> Union[ndpoly, List[ndpoly]]:
    """
    Convert inputs to arrays with at least one dimension.

    Scalar inputs are converted to 1-dimensional arrays, whilst
    higher-dimensional inputs are preserved.

    Args:
        arys:
            One or more input arrays.

    Returns:
        An array, or list of arrays, each with ``a.ndim >= 1``.
        Copies are made only if necessary.

    Examples:
        >>> numpoly.atleast_1d(numpoly.variable())
        polynomial([q0])
        >>> numpoly.atleast_1d(1, [2, 3])
        [polynomial([1]), polynomial([2, 3])]

    """
    if len(arys) == 1:
        poly = numpoly.aspolynomial(arys[0])
        array = numpy.atleast_1d(poly.values)
        return numpoly.aspolynomial(array, names=poly.indeterminants)
    return [atleast_1d(ary) for ary in arys]

max_stars_repo_name: jonathf/npoly

max_issues_repo_name: jonathf/npoly

max_forks_repo_name: jonathf/npoly


text:
from __future__ import (absolute_import, unicode_literals, division,
                        print_function)

import numpy as np
import pytest
import os

from ..gti import cross_gtis, append_gtis, load_gtis, get_btis, join_gtis
from ..gti import check_separate, create_gti_mask, check_gtis
from ..gti import create_gti_from_condition, gti_len, gti_border_bins
from ..gti import time_intervals_from_gtis, bin_intervals_from_gtis
from ..gti import create_gti_mask_complete, join_equal_gti_boundaries

curdir = os.path.abspath(os.path.dirname(__file__))
datadir = os.path.join(curdir, 'data')

class TestGTI(object):

    """Real unit tests."""

    def test_crossgti1(self):
        """Test the basic working of the intersection of GTIs."""
        gti1 = np.array([[1, 4]])
        gti2 = np.array([[2, 5]])
        newgti = cross_gtis([gti1, gti2])

        assert np.all(newgti == [[2, 4]]), 'GTIs do not coincide!'

    def test_crossgti2(self):
        """A more complicated example of intersection of GTIs."""
        gti1 = np.array([[1, 2], [4, 5], [7, 10], [11, 11.2], [12.2, 13.2]])
        gti2 = np.array([[2, 5], [6, 9], [11.4, 14]])
        newgti = cross_gtis([gti1, gti2])

        assert np.all(newgti == [[4.0, 5.0], [7.0, 9.0], [12.2, 13.2]]), \
            'GTIs do not coincide!'

    def test_crossgti3(self):
        """A more complicated example of intersection of GTIs."""
        gti1 = np.array([[1, 2], [4, 5], [7, 10]])
        newgti = cross_gtis([gti1])

        assert np.all(newgti == gti1), \
            'GTIs do not coincide!'

    def test_bti(self):
        """Test the inversion of GTIs."""
        gti = np.array([[1, 2], [4, 5], [7, 10], [11, 11.2], [12.2, 13.2]])
        bti = get_btis(gti)

        assert np.all(bti == [[2, 4], [5, 7], [10, 11], [11.2, 12.2]]), \
            'BTI is wrong!, %s' % repr(bti)

    def test_gti_mask(self):
        arr = np.array([0, 1, 2, 3, 4, 5, 6])
        gti = np.array([[0, 2.1], [3.9, 5]])
        mask, new_gtis = create_gti_mask(arr, gti, return_new_gtis=True)
        # NOTE: the time bin has to be fully inside the GTI. That is why the
        # bin at times 0, 2, 4 and 5 are not in.
        assert np.all(mask == np.array([0, 1, 0, 0, 0, 0, 0], dtype=bool))

    def test_gti_mask_fails_empty_time(self):
        arr = np.array([])
        gti = np.array([[0, 2.1], [3.9, 5]])
        with pytest.raises(ValueError) as excinfo:
            create_gti_mask(arr, gti, return_new_gtis=True)
        assert 'empty time array' in str(excinfo.value)

    def test_gti_mask_fails_empty_gti(self):
        arr = np.array([0, 1, 2, 3, 4, 5, 6])
        gti = np.array([])
        with pytest.raises(ValueError) as excinfo:
            create_gti_mask(arr, gti, return_new_gtis=True)
        assert 'empty GTI array' in str(excinfo.value)

    def test_gti_mask_complete(self):
        arr = np.array([0, 1, 2, 3, 4, 5, 6])
        gti = np.array([[0, 2.1], [3.9, 5]])
        mask, new_gtis = create_gti_mask_complete(arr, gti,
                                                  return_new_gtis=True)
        # NOTE: the time bin has to be fully inside the GTI. That is why the
        # bin at times 0, 2, 4 and 5 are not in.
        assert np.all(mask == np.array([0, 1, 0, 0, 0, 0, 0], dtype=bool))

    def test_gti_mask_compare(self):
        arr = np.array([ 0.5, 1.5, 2.5, 3.5])
        gti = np.array([[0, 4]])
        mask_c, new_gtis_c = \
            create_gti_mask_complete(arr, gti, return_new_gtis=True,
                                     safe_interval=1)
        mask, new_gtis = create_gti_mask(arr, gti, return_new_gtis=True,
                                         safe_interval=1)
        assert np.all(mask == mask_c)
        assert np.all(new_gtis == new_gtis_c)

    def test_gti_mask_compare2(self):
        arr = np.array([ 0.5, 1.5, 2.5, 3.5])
        gti = np.array([[0, 4]])
        mask_c, new_gtis_c = \
            create_gti_mask_complete(arr, gti, return_new_gtis=True,
                                     safe_interval=[1, 1])
        mask, new_gtis = create_gti_mask(arr, gti, return_new_gtis=True,
                                         safe_interval=[1, 1])
        assert np.all(mask == mask_c)
        assert np.all(new_gtis == new_gtis_c)

    def test_gti_from_condition1(self):
        t = np.array([0, 1, 2, 3, 4, 5, 6])
        condition = np.array([1, 1, 0, 0, 1, 0, 0], dtype=bool)
        gti = create_gti_from_condition(t, condition)
        assert np.all(gti == np.array([[-0.5, 1.5], [3.5, 4.5]]))

    def test_gti_from_condition2(self):
        t = np.array([0, 1, 2, 3, 4, 5, 6])
        condition = np.array([1, 1, 1, 1, 0, 1, 0], dtype=bool)
        gti = create_gti_from_condition(t, condition, safe_interval=1)
        assert np.all(gti == np.array([[0.5, 2.5]]))

    def test_load_gtis(self):
        """Test event file reading."""
        fname = os.path.join(datadir, 'monol_testA.evt')
        load_gtis(fname, gtistring="GTI")

    def test_check_separate_overlapping_case(self):
        """Test if intersection between two GTIs can be detected. """
        gti1 = np.array([[1, 2], [4, 5], [7, 10], [11, 11.2], [12.2, 13.2]])
        gti2 = np.array([[2, 5], [6, 9], [11.4, 14]])
        assert check_separate(gti1, gti2) == False

    def test_check_separate_nonoverlapping_case(self):
        """Test if two non-overlapping GTIs can be detected."""
        gti1 = np.array([[1, 2], [4, 5]])
        gti2 = np.array([[6, 7], [8, 9]])
        assert check_separate(gti1, gti2) == True

    def test_check_separate_empty_case(self):
        """Test if intersection between two GTIs can be detected. """
        gti1 = np.array([[1, 2], [4, 5], [7, 10], [11, 11.2], [12.2, 13.2]])
        gti2 = np.array([])
        assert check_separate(gti1, gti2) == True

    def test_append_gtis(self):
        """Test if two non-overlapping GTIs can be appended. """
        gti1 = np.array([[1, 2], [4, 5]])
        gti2 = np.array([[6, 7], [8, 9]])
        assert np.all(append_gtis(gti1, gti2) == [[1,2],[4,5],[6,7],[8,9]])

    def test_append_overlapping_gtis(self):
        """Test if exception is raised in event of overlapping gtis."""
        gti1 = np.array([[1, 2], [4, 5]])
        gti2 = np.array([[3, 4.5], [8, 9]])

        with pytest.raises(ValueError):
            append_gtis(gti1, gti2)

    def test_join_gtis_nonoverlapping(self):
        gti0 = [[0, 1], [2, 3]]
        gti1 = [[10, 11], [12, 13]]
        assert np.all(join_gtis(gti0, gti1) == np.array([[0, 1], [2, 3],
                                                         [10, 11], [12, 13]]))

    def test_join_gtis_overlapping(self):
        gti0 = [[0, 1], [2, 3], [4, 8]]
        gti1 = [[7, 8], [10, 11], [12, 13]]
        assert np.all(join_gtis(gti0, gti1) == np.array([[0, 1], [2, 3], [4, 8],
                                                         [10, 11], [12, 13]]))

    def test_time_intervals_from_gtis(self):
        """Test the division of start and end times to calculate spectra."""
        start_times, stop_times = \
            time_intervals_from_gtis([[0, 400], [1022, 1200],
                                      [1210, 1220]], 128)
        assert np.all(start_times == np.array([0, 128, 256, 1022]))
        assert np.all(stop_times == np.array([0, 128, 256, 1022]) + 128)

    def test_time_intervals_from_gtis_frac(self):
        """Test the division of start and end times to calculate spectra."""
        start_times, stop_times = \
            time_intervals_from_gtis([[0, 400], [1022, 1200],
                                      [1210, 1220]], 128, fraction_step=0.5)
        assert np.all(start_times == np.array([0, 64, 128, 192, 256, 1022]))
        assert np.all(stop_times == start_times + 128)

    def test_bin_intervals_from_gtis(self):
        """Test the division of start and end times to calculate spectra."""
        times = np.arange(0.5, 13.5)
        start_bins, stop_bins = \
            bin_intervals_from_gtis([[0, 5], [6, 8]], 2, times)

        assert np.all(start_bins == np.array([0, 2, 6]))
        assert np.all(stop_bins == np.array([2, 4, 8]))

    def test_bin_intervals_from_gtis_frac(self):
        """Test the division of start and end times to calculate spectra."""
        times = np.arange(0.5, 13.5)
        start_bins, stop_bins = \
            bin_intervals_from_gtis([[0, 5], [6, 8]], 2, times,
                                    fraction_step=0.5)

        assert np.all(start_bins == np.array([0, 1, 2, 3, 6]))
        assert np.all(stop_bins == np.array([2, 3, 4, 5, 8]))

    def test_gti_border_bins(self):
        times = np.arange(0.5, 2.5)

        start_bins, stop_bins = gti_border_bins([[0, 2]], times)
        assert start_bins == [0]
        assert stop_bins == [2]

    def test_gti_border_bins_many_bins(self):
        times = np.arange(0, 2, 0.0001) + 0.00005

        start_bins, stop_bins = gti_border_bins([[0, 2]], times)
        assert start_bins == [0]
        assert stop_bins == [len(times)]

    def test_decide_spectrum_lc_intervals_invalid(self):
        with pytest.raises(ValueError):
            a, b = bin_intervals_from_gtis([[0, 400]], 128, [500, 501])
        with pytest.raises(ValueError):
            a, b = bin_intervals_from_gtis([[1000, 1400]], 128, [500, 501])

    def test_gti_length(self):
        assert gti_len([[0, 5], [6, 7]]) == 6

    def test_check_gtis_shape(self):
        with pytest.raises(TypeError):
            check_gtis([0, 1])

        with pytest.raises(TypeError):
            check_gtis([[0, 1], [0]])

    def test_check_gtis_values(self):
        with pytest.raises(ValueError):
            check_gtis([[0, 2], [1, 3]])

        with pytest.raises(ValueError):
            check_gtis([[1, 0]])

    def test_check_gti_fails_empty(self):
        with pytest.raises(ValueError) as excinfo:
            check_gtis([])
        assert 'Empty' in str(excinfo.value)

    def test_join_boundaries(self):
        gti = np.array([[1.16703354e+08, 1.16703386e+08],
                        [1.16703386e+08, 1.16703418e+08],
                        [1.16703418e+08, 1.16703450e+08],
                        [1.16703450e+08, 1.16703482e+08],
                        [1.16703482e+08, 1.16703514e+08]])
        newg = join_equal_gti_boundaries(gti)
        assert np.allclose(newg, np.array([[1.16703354e+08, 1.16703514e+08]]))

max_issues_repo_name: fjhzwl/stingray

max_forks_repo_name: fjhzwl/stingray


text:
import numpy as np
import pandas as pd
import matplotlib_venn
import matplotlib.pyplot as plt
from matplotlib import gridspec
import itertools

import seaborn as sns
sns.set(style='ticks', context='talk')

import matplotlib as mpl
# Set the svg output text as actual text and not outlines of paths
# (easier to edit)
mpl.rcParams['svg.fonttype'] = 'none'

import collections
import itertools

# Nice colors
import brewer2mpl

set1 = brewer2mpl.get_map('Set1', 'qualitative', 9).mpl_colors
red = set1[0]
blue = set1[1]
green = set1[2]
purple = set1[3]
orange = set1[4]
yellow = set1[5]
brown = set1[6]
pink = set1[7]
grey = set1[8]

# To put the legend outside the plot, on the right side, add these keyword
# arguments ("kwargs") to your legend making, e.g.:
# legend = ax.legend(**OUTSIDE_LEGEND_KWARGS)
OUTSIDE_LEGEND_KWS = dict(bbox_to_anchor=(1, 0.5), loc='center left')

# As a placeholder. Should be the output from
# "ax.legend(**OUTSIDE_LEGEND_KWARGS)"
legend = None

# Also need to save the figure in a special way so that the legend doesn't
# get cut off. Make sure to save the "legend" object that "ax.legend" returns
# and then save that as "legend", then add these keyword arguments to your
# savefig call, e.g.:
# fig.savefig('figure.svg', **OUTSIDE_LEGEND_SAVEFIG_KWS)
OUTSIDE_LEGEND_SAVEFIG_KWS = dict(bbox_extra_artists=(legend,),
                                  bbox_inches='tight')

max_stars_repo_name: YeoLab/gscripts

max_issues_repo_name: YeoLab/gscripts

max_forks_repo_name: YeoLab/gscripts


text:
# Copyright 2019 Alessandro Davide Ialongo (@ialong)
#
# 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
#
# http://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 numpy as np
from numpy.testing import assert_allclose
from numpy.random import randn as mvn
from numpy.linalg import cholesky
import tensorflow as tf
import gpflow as gp
from gpflow.test_util import GPflowTestCase
from gpflow.kullback_leiblers import gauss_kl
from GPt.KL import KL, KL_samples

FLOAT_TYPE = gp.settings.float_type

def choleskify(mats):
    ret = []
    for m in mats:
        if m.ndim == 1:
            m = np.abs(m)
        else:
            m = cholesky(m @ (m.T if m.ndim == 2 else np.transpose(m, [0, 2, 1])))
        ret.append(m)
    return ret

def compare_KLs(sess, feed_dict, mu, Q_chol, P_chols):
    mu_gpflow = tf.transpose(mu) if mu.shape.ndims == 2 else mu[:, None]
    Q_chol_gpflow = Q_chol if Q_chol.shape.ndims == 3 else Q_chol[None, ...]

    KL_gpflow = sess.run(gauss_kl(q_mu=mu_gpflow, q_sqrt=Q_chol_gpflow, K=None), feed_dict=feed_dict)
    KL_gpt = sess.run(KL(mu_diff=mu, Q_chol=Q_chol, P_chol=None, P=None), feed_dict=feed_dict)
    assert_allclose(KL_gpflow, KL_gpt)

    for P_chol in P_chols:
        P_ndims = P_chol.shape.ndims
        P = tf.square(P_chol) if P_ndims == 1 else tf.matmul(P_chol, P_chol, transpose_b=True)

        KL_gpflow = sess.run(gauss_kl(q_mu=mu_gpflow, q_sqrt=Q_chol_gpflow,
                                      K=tf.diag(P) if P_ndims == 1 else P), feed_dict=feed_dict)

        KL_gpt = sess.run(KL(mu_diff=mu, Q_chol=Q_chol, P_chol=P_chol, P=None), feed_dict=feed_dict)
        assert_allclose(KL_gpflow, KL_gpt)
        KL_gpt = sess.run(KL(mu_diff=mu, Q_chol=Q_chol, P_chol=None, P=P), feed_dict=feed_dict)
        assert_allclose(KL_gpflow, KL_gpt)
        KL_gpt = sess.run(KL(mu_diff=mu, Q_chol=Q_chol, P_chol=P_chol, P=P), feed_dict=feed_dict)
        assert_allclose(KL_gpflow, KL_gpt)

class KLTest(GPflowTestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        np.random.seed(0)
        tf.set_random_seed(0)
        self.D, self.M, self.n_samples = 11, 53, 7

    def prepare(self):
        mus = [mvn(self.D), mvn(self.M, self.D), mvn(self.D, self.M), mvn(self.M, self.n_samples, self.D)]
        Q_chols = [mvn(self.D, self.D), mvn(self.D, self.M, self.M), mvn(self.M, self.D, self.D)]
        Q_chols = choleskify(Q_chols)
        Q_chols.append(np.abs(mvn(self.M, self.D)))
        P_chols = [mvn(self.D), mvn(self.D, self.D), mvn(self.M), mvn(self.M, self.M), mvn(self.D, self.M, self.M)]
        P_chols = choleskify(P_chols)
        P_chols.append(np.abs(mvn(self.D, self.M)))
        mus = {a.shape: a for a in mus}
        Q_chols = {a.shape: a for a in Q_chols}
        P_chols = {a.shape: a for a in P_chols}
        return mus, Q_chols, P_chols

    def get_feed_dict(self, mus_tf, Qs_tf, Ps_tf):
        shape = lambda ph: tuple(np.array(ph.shape, dtype=int))
        mus, Qs, Ps = self.prepare()
        feed_dict = dict()
        for mu_tf in mus_tf:
            feed_dict[mu_tf] = mus[shape(mu_tf)]
        for Q_tf in Qs_tf:
            feed_dict[Q_tf] = Qs[shape(Q_tf)]
        for P_tf in Ps_tf:
            feed_dict[P_tf] = Ps[shape(P_tf)]
        return feed_dict

    def test_KL_mu_D_Q_DxD(self):
        with self.test_context() as sess:
            mu = tf.placeholder(FLOAT_TYPE, shape=(self.D,))
            Q_chol = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.D))
            P_chol_diag = tf.placeholder(FLOAT_TYPE, shape=(self.D))
            P_chol = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.D))

            feed_dict = self.get_feed_dict([mu], [Q_chol], [P_chol_diag, P_chol])

            compare_KLs(sess, feed_dict, mu, Q_chol, [P_chol_diag, P_chol])

    def test_KL_mu_MxD_Q_DxMxM(self):
        with self.test_context() as sess:
            mu = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M))
            Q_chol = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M, self.M))
            P_chol_1D = tf.placeholder(FLOAT_TYPE, shape=(self.M))
            P_chol_2D = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.M))
            P_chol_3D = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M, self.M))

            feed_dict = self.get_feed_dict([mu], [Q_chol], [P_chol_1D, P_chol_2D, P_chol_3D])

            compare_KLs(sess, feed_dict, mu, Q_chol, [P_chol_1D, P_chol_2D, P_chol_3D])

    def test_KL_samples_mu_2D(self):
        with self.test_context() as sess:
            mu = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.D))
            Q_chol_2D = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.D))
            Q_chol_3D = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.D, self.D))
            P_chol_1D = tf.placeholder(FLOAT_TYPE, shape=(self.D))
            P_chol_2D = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.D))

            feed_dict = self.get_feed_dict([mu], [Q_chol_2D, Q_chol_3D], [P_chol_1D, P_chol_2D])

            KL_s_1 = sess.run(tf.reduce_sum(KL_samples(mu, Q_chol_2D, None)), feed_dict)
            KL_s_2 = sess.run(tf.reduce_sum(KL_samples(mu, Q_chol_2D, P_chol_1D)), feed_dict)
            KL_s_3 = sess.run(tf.reduce_sum(KL_samples(mu, Q_chol_2D, P_chol_2D)), feed_dict)
            KL_s_4 = sess.run(tf.reduce_sum(KL_samples(mu, Q_chol_3D, None)), feed_dict)
            KL_s_5 = sess.run(tf.reduce_sum(KL_samples(mu, Q_chol_3D, P_chol_1D)), feed_dict)
            KL_s_6 = sess.run(tf.reduce_sum(KL_samples(mu, Q_chol_3D, P_chol_2D)), feed_dict)

            KL_1 = sess.run(KL(mu, tf.matrix_diag(Q_chol_2D), P_chol=None), feed_dict)
            KL_2 = sess.run(KL(mu, tf.matrix_diag(Q_chol_2D), P_chol=tf.diag(P_chol_1D)), feed_dict)
            KL_3 = sess.run(KL(mu, tf.matrix_diag(Q_chol_2D), P_chol=P_chol_2D), feed_dict)
            KL_4 = sess.run(KL(mu, Q_chol_3D, P_chol=None), feed_dict)
            KL_5 = sess.run(KL(mu, Q_chol_3D, P_chol=tf.diag(P_chol_1D)), feed_dict)
            KL_6 = sess.run(KL(mu, Q_chol_3D, P_chol=P_chol_2D), feed_dict)

            assert_allclose(KL_s_1, KL_1)
            assert_allclose(KL_s_2, KL_2)
            assert_allclose(KL_s_3, KL_3)
            assert_allclose(KL_s_4, KL_4)
            assert_allclose(KL_s_5, KL_5)
            assert_allclose(KL_s_6, KL_6)

    def test_KL_samples_mu_3D(self):
        with self.test_context() as sess:
            mu_3D = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.n_samples, self.D))
            Q_chol_2D = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.D))
            Q_chol_3D = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.D, self.D))
            P_chol_1D = tf.placeholder(FLOAT_TYPE, shape=(self.D))
            P_chol_2D = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.D))

            feed_dict = self.get_feed_dict([mu_3D], [Q_chol_2D, Q_chol_3D], [P_chol_1D, P_chol_2D])

            KL_s_1 = sess.run(KL_samples(mu_3D, Q_chol_2D, None), feed_dict)
            KL_s_2 = sess.run(KL_samples(mu_3D, Q_chol_2D, P_chol_1D), feed_dict)
            KL_s_3 = sess.run(KL_samples(mu_3D, Q_chol_2D, P_chol_2D), feed_dict)
            KL_s_4 = sess.run(KL_samples(mu_3D, Q_chol_3D, None), feed_dict)
            KL_s_5 = sess.run(KL_samples(mu_3D, Q_chol_3D, P_chol_1D), feed_dict)
            KL_s_6 = sess.run(KL_samples(mu_3D, Q_chol_3D, P_chol_2D), feed_dict)

            KL_mu_only_arg = lambda Q_chol, P_chol: lambda mu: KL_samples(mu, Q_chol, P_chol=P_chol)
            map_schema = lambda Q_chol, P_chol: \
                tf.reduce_mean(tf.map_fn(KL_mu_only_arg(Q_chol, P_chol), tf.transpose(mu_3D, [1,0,2])), 0)

            KL_map_1 = sess.run(map_schema(Q_chol_2D, None), feed_dict)
            KL_map_2 = sess.run(map_schema(Q_chol_2D, P_chol_1D), feed_dict)
            KL_map_3 = sess.run(map_schema(Q_chol_2D, P_chol_2D), feed_dict)
            KL_map_4 = sess.run(map_schema(Q_chol_3D, None), feed_dict)
            KL_map_5 = sess.run(map_schema(Q_chol_3D, P_chol_1D), feed_dict)
            KL_map_6 = sess.run(map_schema(Q_chol_3D, P_chol_2D), feed_dict)

            assert_allclose(KL_s_1, KL_map_1)
            assert_allclose(KL_s_2, KL_map_2)
            assert_allclose(KL_s_3, KL_map_3)
            assert_allclose(KL_s_4, KL_map_4)
            assert_allclose(KL_s_5, KL_map_5)
            assert_allclose(KL_s_6, KL_map_6)

    def test_whitening(self):
        with self.test_context() as sess:
            mu = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M))
            Q_chol = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M, self.M))
            P_chol = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M, self.M))

            feed_dict = self.get_feed_dict([mu], [Q_chol], [P_chol])

            KL_black = sess.run(KL(mu, Q_chol, P_chol=P_chol), feed_dict)
            KL_white = sess.run(KL(tf.matrix_triangular_solve(P_chol, mu[:, :, None], lower=True)[..., 0],
                                   tf.matrix_triangular_solve(P_chol, Q_chol, lower=True)), feed_dict)

            assert_allclose(KL_black, KL_white)

    def test_KL_x1_multiseq(self):
        with self.test_context() as sess:
            mu = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M))
            Q_chol = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M, self.M))
            P_chol_1D = tf.placeholder(FLOAT_TYPE, shape=(self.M))
            P_chol_2D = tf.placeholder(FLOAT_TYPE, shape=(self.M, self.M))
            P_chol_3D_diag = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M))
            P_chol_3D = tf.placeholder(FLOAT_TYPE, shape=(self.D, self.M, self.M))

            feed_dict = self.get_feed_dict([mu], [Q_chol], [P_chol_1D, P_chol_2D, P_chol_3D_diag, P_chol_3D])

            KL_1 = sess.run(KL(mu, Q_chol, P_chol=None), feed_dict)
            KL_2 = sess.run(KL(mu, Q_chol, P_chol=P_chol_1D), feed_dict)
            KL_3 = sess.run(KL(mu, Q_chol, P_chol=P_chol_2D), feed_dict)
            KL_4 = sess.run(KL(mu, Q_chol, P_chol=tf.matrix_diag(P_chol_3D_diag)), feed_dict)
            KL_5 = sess.run(KL(mu, Q_chol, P_chol=P_chol_3D), feed_dict)

            KL_map_1 = sess.run(tf.map_fn(lambda a: KL(a[0], a[1], P_chol=None),
                                          (mu, Q_chol), (FLOAT_TYPE)), feed_dict)
            KL_map_2 = sess.run(tf.map_fn(lambda a: KL(a[0], a[1], P_chol=P_chol_1D),
                                          (mu, Q_chol), (FLOAT_TYPE)), feed_dict)
            KL_map_3 = sess.run(tf.map_fn(lambda a: KL(a[0], a[1], P_chol=P_chol_2D),
                                          (mu, Q_chol), (FLOAT_TYPE)), feed_dict)
            KL_map_4 = sess.run(tf.map_fn(lambda a: KL(a[0], a[1], P_chol=a[2]),
                                          (mu, Q_chol, P_chol_3D_diag), (FLOAT_TYPE)), feed_dict)
            KL_map_5 = sess.run(tf.map_fn(lambda a: KL(a[0], a[1], P_chol=a[2]),
                                          (mu, Q_chol, P_chol_3D), (FLOAT_TYPE)), feed_dict)

            assert_allclose(KL_1, KL_map_1.sum())
            assert_allclose(KL_2, KL_map_2.sum())
            assert_allclose(KL_3, KL_map_3.sum())
            assert_allclose(KL_4, KL_map_4.sum())
            assert_allclose(KL_5, KL_map_5.sum())

if __name__ == '__main__':
    tf.test.main()

max_stars_repo_name: cisprague/GPt

max_issues_repo_name: cisprague/GPt

max_forks_repo_name: cisprague/GPt

reason: import numpy,from numpy


text:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# a random walk
prices = (1 + 0.01*np.random.randn(30)).cumprod() * 10
fig, ax = plt.subplots(1)
ax.plot(prices)
ax.grid()

# format the y tick labels as dolllars
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('$%.2f'))

plt.show()

max_stars_repo_name: aravindhnivas/FELion-Spectrum-Analyser

max_issues_repo_name: aravindhnivas/FELion-Spectrum-Analyser

max_forks_repo_name: aravindhnivas/FELion-Spectrum-Analyser


text:
import random

import numpy as np

from ncc.data.constants import DEFAULT_MAX_TARGET_POSITIONS
from ncc.data.ncc_dataset import NccDataset
from ncc.data.tools import data_utils

def collate(samples, pad_idx, unk_idx, attrs=None):
    # no need for left padding
    if len(samples) == 0:
        return {}

    def merge(key):
        return data_utils.collate_tokens(
            [s[key] for s in samples],
            pad_idx,
        )

    src_tokens = merge('source')
    tgt_tokens = merge('target')

    attr_masks = {attr: [] for attr in attrs} if attrs is not None else None

    extends = []
    max_len = src_tokens.size(-1)
    for i, s in enumerate(samples):
        extends.append(s['extend'])
        if attr_masks is not None:
            for attr in attrs:
                attr_masks[attr].append(s['attr_masks'][attr] + max_len * i)
    if attrs:
        for attr in attrs:
            attr_masks[attr] = np.concatenate(attr_masks[attr], axis=0)

    ntokens = sum(sum(s['target'][s['extend']:] != pad_idx) for s in samples).item()

    batch = {
        'id': [s['id'] for s in samples],
        'net_input': {
            'src_tokens': src_tokens,
        },
        'target': tgt_tokens,
        'attr_masks': attr_masks,
        'extends': extends,
        'ntokens': ntokens,
    }
    return batch

class CompletionDataset(NccDataset):

    def __init__(
        self, tgt, tgt_sizes, tgt_dict, extends=None,
        attrs=None, attr_indices=None, attr_dict=None,
        attrs_mapping=None, reversed_attrs_mapping=None,
        left_pad_source=False, left_pad_target=False,
        max_target_positions=DEFAULT_MAX_TARGET_POSITIONS,
        shuffle=True,
    ):
        self.tgt = tgt
        self.tgt_sizes = np.array(tgt_sizes)
        self.tgt_dict = tgt_dict
        self.extends = extends
        self.attrs = attrs
        self.attr_indices = attr_indices
        self.attr_dict = attr_dict
        self.attrs_mapping = attrs_mapping
        self.reversed_attrs_mapping = reversed_attrs_mapping
        self.left_pad_source = left_pad_source
        self.left_pad_target = left_pad_target
        self.max_target_positions = max_target_positions

        self.shuffle = shuffle

        self.pad = self.tgt_dict.pad()
        self.unk = self.tgt_dict.unk()

    def __getitem__(self, index):
        # Append EOS to end of tgt sentence if it does not have an EOS
        # and remove EOS from end of src sentence if it exists.
        # This is useful when we use existing datasets for opposite directions
        #   i.e., when we want to use tgt_dataset as src_dataset and vice versa
        src_item = self.tgt[index][:-1]
        tgt_item = self.tgt[index][1:]

        extend = 0 if self.extends is None else self.extends[index].item()
        if self.attrs_mapping:
            # do not move attr_masks into cuda
            attr_masks = {attr: [] for attr in self.attrs}
            for idx, attr_idx in enumerate(self.attr_indices[index].tolist()[1:][extend:], start=extend):
                if attr_idx in self.reversed_attrs_mapping:
                    attr_masks[self.reversed_attrs_mapping[attr_idx]].append(idx)
            for attr in self.attrs:
                attr_masks[attr] = np.array(attr_masks[attr])
        else:
            attr_masks = None

        example = {
            'id': index,
            'source': src_item,
            'target': tgt_item,

            'attr_masks': attr_masks,
            'extend': extend,
        }
        return example

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

    def collater(self, samples):
        return collate(samples, pad_idx=self.pad, unk_idx=self.unk, attrs=self.attrs)

    def num_tokens(self, index):
        return self.tgt_sizes[index]

    def size(self, index):
        return self.tgt_sizes[index]

    def ordered_indices(self):
        indices = super().ordered_indices()
        indices = indices[self.tgt_sizes > 1]
        if self.shuffle:
            random.shuffle(indices)
        return indices

max_stars_repo_name: code-backdoor/code-backdoor

max_issues_repo_name: code-backdoor/code-backdoor

max_forks_repo_name: code-backdoor/code-backdoor


text:
import dash_core_components as dcc
import dash_html_components as html
from utils import helpers
from utils.ec_analytics import tracking_pixel_img
from numpy import nan

SLIDER_MAX = 130

def main_controls(map_data, CONFIG):
    TRENDWINDOW = CONFIG["TRENDWINDOW"]
    MEASUREMENTS = CONFIG["measurements"]
    landkreis_options = [{'label': x, 'value': x} for x in sorted(map_data["landkreis_label"].unique())]
    bundesland_options = [{'label': x, 'value': x} for x in sorted(map_data["bundesland"].unique())]

    return html.Div(id="main_controls", children=[
        html.Img(id="title",
                 className="container",
                 src="assets/logo.png",
                 alt="EveryoneCounts - Das Social Distancing Dashboard"),
        html.Div(id="navigation_container",
                 className="container",
                 children=[
                     html.A(href="https://blog.everyonecounts.de", children="Blog"),
                     html.A(href="https://blog.everyonecounts.de/die-loesung/", children="Die Lösung")
                 ]),
        html.Div(id="info_container", className="container", children=[
            html.Button(id="btn-info", children="Informationen anzeigen ↓"),
            html.Div(id="infotext", style={"display": "none"}, children=[
                html.H3("Informationen über diese Karte"),
                dcc.Markdown(f"""
                Die Maßnahmen gegen COVID-19 wie Kontaktverbote und geschlossene Geschäfte haben große
                Änderungen in unserem Alltag mit sich gebracht. Wir sehen dies jeden Tag wenn wir vor die Haustür
                gehen. Aber wie ist die Lage im Rest des Landes? Wird Social Distancing überall gleich strikt
                befolgt? Sinkt die Zurückhaltung am Wochenende oder bei guten Wetter? Sind tatsächlich mehr/weniger
                Menschen im Park unterwegs? Diese Fragen sind sehr schwer direkt zu beantworten, aber wir können
                versuchen, **indirekt** Erkentnisse darüber zu gewinnen indem wir verschiedene Indikatoren
                zur **Aktivität im öffentlichen Raum** betrachten. Dazu setzen wir auf unterschiedliche Datenquellen,
                um ein möglichst umfassendes Bild zu zeichnen.

                Die Punkt auf der Karte stellen einzelne Messtationen dar. Die Farbe entspricht dem aktuellen
                **{TRENDWINDOW}-Tage-Trend**:"""),
                html.Div(id="legende", children=[
                    html.Div(id="legende-1",
                             style={"background": helpers.trend2color(-1.)},
                             children="fallend oder gleich (< +10%)"),
                    html.Div(id="legende-2",
                             style={"background": helpers.trend2color(0.3)},
                             children="leicht steigend (+10% bis +100%) "),
                    html.Div(id="legende-3",
                             style={"background": helpers.trend2color(2.0)},
                             children="stark steigend (> +100%)"),
                    html.Div(id="legende-4",
                             style={"background": helpers.trend2color(nan)},
                             children="zu wenig Daten für die Trendbestimmung"),
                ]),
                dcc.Markdown(f"""Du kannst auf jede Station klicken um mehr
                Informationen zu erhalten. Außerdem kannst Du den **Detailgrad** ändern um verschiedene Landkreise mit
                einander zu vergleichen. In der "Punkte"-Ansicht kannst Du den Trend über die
                Mess-Stationen in deiner Umgebung (hellblauer Bereich auf der Karte) mitteln lassen. Diesen Bereich
                kannst Du über eine Umkreis-Auswahl oder mit der Landkreis- oder Bundesland-Suche weiter unten im Menü
                festlegen.
                """),
                html.H3("Weitere Informationen"),
                html.Ul([
                    html.Li(html.A(children="Über das Projekt",
                                   href="https://blog.everyonecounts.de/das-projekt/",
                                   target="_blank")),
                    html.Li(html.A(children="Über das Team",
                                   href="https://blog.everyonecounts.de/das-team/",
                                   target="_blank")),
                    html.Li(html.A(children="Über die Daten",
                                   href="https://blog.everyonecounts.de/die-daten/",
                                   target="_blank")),
                    html.Li(html.A(children="Presseberichte",
                                   href="https://blog.everyonecounts.de/presseschau/",
                                   target="_blank")),
                    html.Li(html.A(children="Blog",
                                   href="https://blog.everyonecounts.de/",
                                   target="_blank")),
                ]),
                html.Div(id="supporter", children=[
                    html.A(href="https://bmbf.de",
                           className="supporter",
                           target="_blank",
                           children=[
                               html.Img(src="assets/support_bmbf.png")
                           ]),
                    html.A(href="https://projecttogether.org/wirvsvirus/",
                           className="supporter",
                           target="_blank",
                           children=[
                               html.Img(src="assets/support_solutionenabler.png")
                           ]),
                ]),
                html.Button(id="btn-info-close", children="Informationen ausblenden ↑"),
            ])
        ]),
        html.Div(id="btn-main-toolbar-container", className="container", children=[
            html.Button(id="btn-main-toolbar", children="Optionen anzeigen ↓"),
        ]),
        html.Div(id="detail_container", className="container", children=[
            html.H3("Detailgrad"),
            dcc.RadioItems(
                id="detail_radio",
                options=[
                    {'label': 'Punkte', 'value': 'stations'},
                    {'label': 'Landkreise', 'value': 'landkreis'}
                ],
                value='stations',
                labelStyle={'display': 'inline-block'}
            ),
            html.H3("Datenquellen"),
            dcc.Checklist(
                id="trace_visibility_checklist",
                options=[
                    {'label': helpers.measurementtitles[key], 'value': key}
                    for key in helpers.measurementtitles
                    if key in MEASUREMENTS
                ],
                value=MEASUREMENTS,
                labelStyle={'display': 'block'}
            ),
        ]),
        html.Div(id="trend_container", className="container", children=[
            html.Div(children=[
                html.H3(f"{TRENDWINDOW}-Tage-Trend im gewählten Bereich"),
                html.P(id="mean_trend_p", style={}, children=[
                    html.Span(id="mean_trend_span", children="")
                ]),
            ]),
            html.P(id="location_p", children=[
                html.Span(id="location_text", children="?"),
            ]),
            html.Button(id="btn-region-select", children="Region auswählen ↓"),
            html.Div(id="region_container",
                     style={"display": "none"},
                     children=[
                         dcc.Tabs(id='region_tabs', className="", value='tab-umkreis', children=[
                             dcc.Tab(label='Umkreis', value='tab-umkreis', children=[
                                 html.Div(id="radius_search_tab", children=[
                                     html.H3("Mittelpunkt bestimmen:"),
                                     html.Div(id="search-container", children=[
                                         dcc.Input(id="nominatim_lookup_edit", type="text", placeholder="",
                                                   debounce=False),
                                         html.Button(id='nominatim_lookup_button', n_clicks=0, children='Suchen'),
                                     ]),
                                     html.Button(id='geojs_lookup_button', n_clicks=0,
                                                 children='Automatisch bestimmen'),
                                     html.Button(id='mapposition_lookup_button', n_clicks=0,
                                                 children='Kartenmittelpunkt verwenden'),
                                     html.H3("Umkreis:"),
                                     dcc.Slider(
                                         id='radiusslider',
                                         min=5,
                                         max=SLIDER_MAX,
                                         step=5,
                                         value=60,
                                         tooltip=dict(
                                             always_visible=False,
                                             placement="top"
                                         ),
                                         marks={20 * x: str(20 * x) + 'km' for x in range(SLIDER_MAX // 20 + 1)}
                                     )
                                 ]),
                             ]),
                             dcc.Tab(label='Landkreis', value='tab-landkreis', children=[
                                 html.H3("Wähle einen Landkreis:"),
                                 dcc.Dropdown(
                                     id='landkreis_dropdown',
                                     options=landkreis_options,
                                     value=landkreis_options[0]["value"],
                                     clearable=False
                                 ),
                                 html.P("Hinweis: Nur Landkreise mit Datenpunkten können ausgewählt werden!"),
                             ]),
                             dcc.Tab(label='Bundesland', value='tab-bundesland', children=[
                                 html.H3("Wähle ein Bundesland:"),
                                 dcc.Dropdown(
                                     id='bundesland_dropdown',
                                     options=bundesland_options,
                                     value=bundesland_options[0]["value"],
                                     clearable=False
                                 ),
                             ]),
                         ])
                     ]),
        ]),
        html.Div(id="footer-container", children=[
            html.Div(id="footer", className="footer", children=[
                html.P([
                    html.Span("EveryoneCounts 2020"),
                    html.A(children="Impressum", href="https://blog.everyonecounts.de/impressum/", target="_blank"),
                    html.A(children="Kontakt", href="mailto:kontakt@everyonecounts.de", target="_blank"),
                    html.A(children="Twitter", href="https://twitter.com/_everyonecounts", target="_blank"),
                    html.A(children="Github", href="https://github.com/socialdistancingdashboard/", target="_blank"),
                ]),
            ]),
        ]),
        tracking_pixel_img()
    ])

def mainmap():
    default_lat = 50
    default_lon = 10
    config_plots = dict(
        locale="de-DE",
        displaylogo=False,
        modeBarButtonsToRemove=['lasso2d',
                                'toggleSpikelines',
                                'toImage',
                                'toggleHover',
                                'select2d',
                                'autoScale2d',
                                'resetScale2d',
                                'resetViewMapbox'],
        displayModeBar=True,
        responsive=True
    )

    #  Dash Map
    return dcc.Graph(
        id='map',
        config=config_plots,
        figure={
            'data': [],
            'layout': dict(
                autosize=True,
                hovermode='closest',
                showlegend=False,
                legend_title_text='Datenquelle',
                legend=dict(
                    x=0.5,
                    y=1,
                    traceorder="normal",
                    font=dict(
                        family="sans-serif",
                        size=14,
                        color="black"
                    ),
                    bgcolor="#fff",
                    bgopacity=0.3,
                    bordercolor="#eee",
                    borderwidth=1
                ),
                # height=400,
                margin=dict(l=0, r=0, t=0, b=0),
                mapbox=dict(
                    style="carto-positron",
                    # open-street-map,
                    # white-bg,
                    # carto-positron,
                    # carto-darkmatter,
                    # stamen-terrain,
                    # stamen-toner,
                    # stamen-watercolor
                    bearing=0,
                    center=dict(
                        lat=default_lat,
                        lon=default_lon
                    ),
                    pitch=0,
                    zoom=6,
                )
            ),
        },
    )

def storage():
    # dcc Storage
    return [
        dcc.Store(id='clientside_callback_storage', storage_type='memory'),
        dcc.Store(id='nominatim_storage', storage_type='memory'),
        dcc.Store(id='urlbar_storage', storage_type='memory'),
        dcc.Store(id='highlight_polygon', storage_type='memory'),
        dcc.Store(id='latlon_local_storage', storage_type='local', data=(50.144, 8.617, "Frankfurt am Main")),
    ]

def timeline_chart():
    return html.Div(id="chart-container", style={'display': 'none'}, children=[
        html.Button(id="chart-close", children=" × "),
        dcc.Loading(
            id="timeline-chart",
            type="circle",
            children=[
                dcc.Checklist(id="timeline-avg-check", value=[])
                # This checklist  needs to be in the layout because
                # a callback is bound to it. Otherwise, Dash 1.12 will throw errors
                # This is an issue even when using app.validation_layout or
                # suppress_callback_exceptions=True, as suggested in the docs
                # Don't trust the documentation in this case.
            ]
        ),
    ])

def feedback_window():
    return html.Div(id="feedback-container", style={'display': 'block'}, children=[
        html.Button(id="feedback-close", children=" × "),
        dcc.Markdown(
            """
            Hast Du **3 Minuten Zeit** für unsere kleine **Nutzerbefragung**? Wir möchten besser werden und
            EveryoneCounts weiterentwickeln, dafür ist Feedback von unseren Nutzern von unschätzbarem Wert. Hilf mit!
            """),
        html.A(children=">> Link zur Umfrage (Google Forms) <<",
               target="_blank",
               href="https://docs.google.com/forms/d/e/1FAIpQLSda91f1ewYx2y-Z7GOK8FqffThDIxUMe1OJ0bWaC0EuRiMxcA/viewform")
    ])

reason: from numpy


text:
"""
Need to change herCPRAND to find the graphs in report because it doesn't return exact error for now
Test of param_research
"""
import numpy as np
from src._paramResearch import param_research

def test_paramresearch(i):
    """
    Test of paramresearch
    Need to change plot label in pram_research to run the test.
    Parameters
    ----------
    i : int
        i=1, plot for beta
        i=2, plot for eta
        i=3, plot for gamma

    Returns
    -------
    None.

    """

    I=100
    J=100
    K=100
    r=10 # rank
    n_samples=int(10*r*np.log(r)+1) # nb of randomized samples
    nb_rand=10 # nb of random initialization

    # beta
    if i ==1 :
        param_research(I,J,K,r,nb_rand,n_samples,exact_err=True)
    # eta
    if i ==2 :
        param_research(I,J,K,r,nb_rand,n_samples,beta=False,eta=True,exact_err=True)
    # gamma
    if i ==3 :
        param_research(I,J,K,r,nb_rand,n_samples,beta=False,gamma=True,exact_err=True)

max_stars_repo_name: Kefan-pauline/HER-CPRAND

max_issues_repo_name: Kefan-pauline/HER-CPRAND

max_forks_repo_name: Kefan-pauline/HER-CPRAND


text:
import numpy as np
from nilearn.image.image import check_niimg
from nilearn.image.image import _crop_img_to as crop_img_to

def crop_img(img, rtol=1e-8, copy=True, return_slices=False):
    """Crops img as much as possible
    Will crop img, removing as many zero entries as possible
    without touching non-zero entries. Will leave one voxel of
    zero padding around the obtained non-zero area in order to
    avoid sampling issues later on.
    Parameters
    ----------
    img: Niimg-like object
        See http://nilearn.github.io/manipulating_images/input_output.html
        img to be cropped.
    rtol: float
        relative tolerance (with respect to maximal absolute
        value of the image), under which values are considered
        negligeable and thus croppable.
    copy: boolean
        Specifies whether cropped data is copied or not.
    return_slices: boolean
        If True, the slices that define the cropped image will be returned.
    Returns
    -------
    cropped_img: image
        Cropped version of the input image
    """

    #img = check_niimg(img)
    data = img
    infinity_norm = max(-data.min(), data.max())
    passes_threshold = np.logical_or(data < -rtol * infinity_norm,
                                     data > rtol * infinity_norm)

    if data.ndim == 4:
        passes_threshold = np.any(passes_threshold, axis=-1)
    coords = np.array(np.where(passes_threshold))
    start = coords.min(axis=1)
    end = coords.max(axis=1) + 1

    # pad with one voxel to avoid resampling problems
    start = np.maximum(start - 1, 0)
    end = np.minimum(end + 1, data.shape[:3])

    slices = [slice(s, e) for s, e in zip(start, end)]

    if return_slices:
        return slices

    return crop_img_to(img, slices, copy=copy)

max_stars_repo_name: AnaFOliveira/Segmentation-of-Lungs

max_issues_repo_name: AnaFOliveira/Segmentation-of-Lungs

max_forks_repo_name: AnaFOliveira/Segmentation-of-Lungs


text:
"""Simple test script for 2D Double Ball Balancer
"""
import matplotlib.pyplot as plt
import numpy as np
import time
import argparse

import context
from model_2d.dynamics_2 import StateIndex

from model_2d.param import getDefaultParam

N = 2

exec(f'from model_2d.dynamics_{N} import DynamicModel, StateIndex')
exec(f'from model_2d.controller_{N} import Controller')

parser = argparse.ArgumentParser(description="Test 2D double ball balancer")
parser.add_argument("-a", "--no-animation", help="disable animation", action="store_true")
parser.add_argument(
    "-c",
    "--contact-forces",
    help="enable visualization of contact forces in the animation",
    action="store_true")
parser.add_argument("-p", "--no-plot", help="disable plotting states", action="store_true")
parser.add_argument("-v", "--verbose", help="print simulation time", action="store_true")
args = parser.parse_args()

enable_plot = not args.no_plot
enable_animation = not args.no_animation
enable_contact_forces = args.contact_forces

# create parameter struct
model_param = getDefaultParam(N)

# initial state
x0 = np.zeros(StateIndex.NUM_STATES)

# instantiate model
model = DynamicModel(model_param, x0)

# instantiate controller
controller = Controller(model_param)

# simulation time step
dt = 0.05

# commands
beta_cmd = 16 * np.pi

# prepare simulation
max_sim_time = 30
sim_time = 0
sim_time_vec = [sim_time]
state_vec = [model.x]
input_vec = []
start_time = time.time()
u = 0
contact_forces = None

# simulate until system is irrecoverable or max_sim_time reached
while model.is_recoverable(
        contact_forces=contact_forces,
        omega_cmd=u) and sim_time < max_sim_time:
    # get control input
    u = controller.compute_ctrl_input(model.x, beta_cmd, mode=controller.ANGLE_MODE)

    # calculate contact forces
    contact_forces = model.compute_contact_forces(omega_cmd=u)

    if enable_animation:
        plt.figure(0)

        # get visualization
        vis = model.get_visualization(contact_forces=contact_forces)

        # plot
        plt.cla()
        for i in range(N + 1):
            plt.plot(*vis[str(i)])
            if enable_contact_forces:
                plt.arrow(*vis[f'F{i}'], head_width=0.1, color='red')
        plt.xlabel('x [m]')
        plt.ylabel('y [m]')
        plt.axis('equal')
        plt.show(block=False)
        time_passed = time.time() - start_time
        plt.pause(max(dt - time_passed, 0.001))

        start_time = time.time()

    # simulate one time step
    model.simulate_step(dt, u)
    sim_time += dt

    # save states as matrix, sim_time and inputs as lists
    state_vec = np.concatenate([state_vec, [model.x]])
    sim_time_vec.append(sim_time)
    input_vec.append(u)

    if args.verbose:
        print('sim_time: {0:.3f} s'.format(sim_time))

if enable_plot:
    plt.figure()
    for x in list(StateIndex)[:StateIndex.NUM_STATES // 2]:
        plt.plot(sim_time_vec, state_vec[:, x.value], label=x.name)
    plt.xlabel('time [s]')
    plt.ylabel('angles [rad]')
    plt.legend()
    plt.title('angles')

    plt.figure()
    for x in list(StateIndex)[StateIndex.NUM_STATES // 2: StateIndex.NUM_STATES]:
        plt.plot(sim_time_vec, state_vec[:, x.value], label=x.name)
    plt.plot(sim_time_vec[:-1], input_vec, label='motor_cmd')
    plt.xlabel('time [s]')
    plt.ylabel('omega [rad]')
    plt.legend()
    plt.title('omega')
    plt.show(block=True)

max_stars_repo_name: ChristofDubs/DoubleBallBalancer

max_issues_repo_name: ChristofDubs/DoubleBallBalancer

max_forks_repo_name: ChristofDubs/DoubleBallBalancer


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb  3 21:43:46 2022

@author: stephane
"""
import numpy as np
import pandas as pd

hbar = 6.625E-34/(2*np.pi)
m0 = 9.109E-31
eps0 = 8.85E-14
q = 1.602E-19

# define layer stack class

class stack:
    # The init method
    def __init__(self, wl):
        self.nair = 1.0*np.ones(wl.shape)
        self.nsubs = 1.5*np.ones(wl.shape)
        self.index = self.nsubs
        self.numlayers = int(0)
        self.wl=wl

    def addlayer(self, index, thickness):
        if self.numlayers == 0:
            self.thick = thickness
            self.index = np.vstack((index,self.index))
        else:
            self.thick = np.append(self.thick,thickness)
            self.index = np.insert(self.index,self.numlayers,index,axis=0)

        if index.shape != self.nair.shape:
            raise Exception("Sorry, index has the wrong dimensions")
        self.numlayers += 1

    def addDBR(self, index1, index2, centerwl, pairno, hilo):
        thick1=centerwl/(4*np.interp(centerwl,self.wl,index1))
        thick2=centerwl/(4*np.interp(centerwl,self.wl,index2))
        if thick2>thick1:
            high=index1
            low=index2
        else:
            high=index2
            low=index1
        if hilo == 'HL':
            for k in range(2*pairno):
                if k % 2 == 1:
                    self.addlayer(low, max(thick1,thick2))
                else:
                    self.addlayer(high, min(thick1,thick2))
        elif hilo == 'LH':
            for k in range(2*pairno):
                if k % 2 == 0:
                    self.addlayer(low, max(thick1,thick2))
                else:
                    self.addlayer(high, min(thick1,thick2))
        else: raise Exception("Please specify DBR pair order")

    def calc(self,angles):
        # start calculations

        R_TE=np.empty((angles.shape[0],self.wl.shape[0]),dtype=float)
        R_TM=np.empty((angles.shape[0],self.wl.shape[0]),dtype=float)
        T_TE=np.empty((angles.shape[0],self.wl.shape[0]),dtype=float)
        T_TM=np.empty((angles.shape[0],self.wl.shape[0]),dtype=float)
        S=np.empty((2,2,self.wl.shape[0]),dtype=complex)
        S=np.empty((2,2,self.wl.shape[0]),dtype=complex)
        S_TM=np.empty((2,2,self.wl.shape[0]),dtype=complex)
        Ijk=np.empty((2,2,self.wl.shape[0]),dtype=complex)
        IjkTM=np.empty((2,2,self.wl.shape[0]),dtype=complex)
        Lj=np.empty((2,2,self.wl.shape[0]),dtype=complex)
        bj=np.empty(self.wl.shape[0],dtype=complex)

        df=pd.DataFrame(columns=['wavelength','angle','R_TE','R_TM','T_TE','T_TM'])
        dfn=pd.DataFrame(columns=['wavelength','angle','R_TE','R_TM','T_TE','T_TM'])
        dfn['wavelength']=self.wl

        for idx,theta in enumerate(angles):

        # from air to first layer

            qj = self.nair*np.cos(theta)

            qk = np.sqrt(self.index[0]**2-(self.nair**2)*(np.sin(theta))**2)

            rjk = (qj-qk)/(qj+qk)
            tjk = 2*qj/(qj+qk)

            rjkTM = (-self.index[0]**2*qj+self.nair**2*qk)/(self.index[0]**2*qj+
                                                                self.nair**2*qk)
            tjkTM = 2*self.index[0]*self.nair*qj/(self.index[0]**2*qj+self.nair**2*qk)

            S[0, 0, :] = 1/tjk
            S[0, 1, :] = rjk/tjk
            S[1, 0, :] = S[0, 1, :]
            S[1, 1, :] = S[0, 0, :]

            S_TM[0,0,:]=1/tjkTM;
            S_TM[0,1,:]=rjkTM/tjkTM
            S_TM[1,0,:]=S_TM[0,1,:]
            S_TM[1,1,:]=S_TM[0,0,:]

        # all other layers

            for k in range(self.numlayers):
                qj=np.sqrt(self.index[k]**2-(self.nair**2)*(np.sin(theta))**2)
                qk=np.sqrt(self.index[k+1]**2-(self.nair**2)*(np.sin(theta))**2)

                bj = 2*np.pi*qj/self.wl

                Lj[0,0,:]=np.exp(-1j*bj*self.thick[k])
                Lj[0,1,:]=0
                Lj[1,0,:]=0
                Lj[1,1,:]=np.exp(1j*bj*self.thick[k])

                rjk=(qj-qk)/(qj+qk)
                tjk=2*qj/(qj+qk)

                rjkTM=(-self.index[k+1]**2*qj+self.index[k]**2*qk)/((self.index[k+1]**2)*qj+(self.index[k]**2)*qk)
                tjkTM=2*self.index[k+1]*self.index[k]*qj/(self.index[k+1]**2*qj+self.index[k]**2*qk)

                Ijk[0,0,:]=1/tjk;
                Ijk[0,1,:]=rjk/tjk;
                Ijk[1,0,:]=Ijk[0,1,:]
                Ijk[1,1,:]=Ijk[0,0,:]

                IjkTM[0,0,:]=1/tjkTM;
                IjkTM[0,1,:]=rjkTM/tjkTM
                IjkTM[1,0,:]=IjkTM[0,1,:]
                IjkTM[1,1,:]=IjkTM[0,0,:]

                for kk in range(self.wl.shape[0]):
                    S[:,:,kk]=S[:,:,kk] @ Lj[:,:,kk] @ Ijk[:,:,kk]
                    S_TM[:,:,kk]=S_TM[:,:,kk] @ Lj[:,:,kk] @ IjkTM[:,:,kk]

            q0=np.sqrt(self.nair**2-(self.nair**2)*(np.sin(theta))**2)/self.nair
            qsubs=np.sqrt(self.nsubs**2-(self.nair**2)*(np.sin(theta))**2)/self.nsubs

            #R_TM[idx,:]=np.abs(S_TM[1,0,:]/S_TM[0,0,:])**2
            #R_TE[idx,:]=np.abs(S[1,0,:]/S[0,0,:])**2
            #T_TM[idx,:]=(qsubs/q0)*(self.nsubs/self.nair)*(np.abs(1/S_TM[0,0,:])**2)
            #T_TE[idx,:]=(qsubs/q0)*(self.nsubs/self.nair)*(np.abs(1/S[0,0,:])**2)

            dfn['angle']=180*theta/np.pi

            dfn['R_TM'] =np.abs(S_TM[1,0,:]/S_TM[0,0,:])**2
            dfn['R_TE'] =np.abs(S[1,0,:]/S[0,0,:])**2
            dfn['T_TM'] =(qsubs/q0)*(self.nsubs/self.nair)*(np.abs(1/S_TM[0,0,:])**2)
            dfn['T_TE'] =(qsubs/q0)*(self.nsubs/self.nair)*(np.abs(1/S[0,0,:])**2)
            df=pd.concat([df,dfn])
        return df

# fig = go.Figure(go.Line(x=wl, y=R_TE.T))
# fig.update_layout(title_text='hello world')
# pio.write_html(fig, file='hello_world.html', auto_open=True)

# py.iplot(data, filename = 'basic-line')

#figure, ax = plt.subplots(1,2,figsize=(10,4))

#ax[0].set_title("TE Reflectivity")
#ax[0].set_xlabel("Wavelength [nm]")
#ax[0].set_ylabel("Reflectivity")
#ax[0].plot(wl,R_TE.T)

#ax[1].set_title("TM Reflectivity")
#ax[1].set_xlabel("Wavelength [nm]")
#ax[1].plot(wl,R_TM.T)
#plt.show()
#plt.savefig('reflectivity.pdf',dpi=600)

# figure (1)
# plot(1239.85./lambda,R_TE(:,:));
# figure (2)
# plot(1239.85./lambda,T_TE(:,:));
# figure (3)
# plot(1239.85./lambda,1-T_TE(:,:)-R_TE(:,:));

# % figure (1)
# % [X,Y]=meshgrid(angles,lambda);
# % mesh(X,Y,R_TE');
# % view(0,-90)
# % figure (2)
# % mesh(X,Y,R_TM');
# % view(0,-90)

# %legend('15','20','25','30','35','40','45','50','55','60','65','70','75');

max_stars_repo_name: skenac/tmatrix

max_issues_repo_name: skenac/tmatrix

max_forks_repo_name: skenac/tmatrix


text:
from __future__ import division, print_function
import numpy as np
import sys
import os
sys.path.append('/Users/kim/Science/Projects/Bose Hubbard Scattering/Numerics/')
import babusca.scattering as scattering

class QL(scattering.Setup):

    def __init__(self, N, omegas, Us, xs, gs=None, parasite=None):

        model = scattering.Model(
            Es=omegas,
            links=[],
            Us=Us)

        if gs is None:
            gs = normalize([1, 1])

        sites = np.arange(0, N)

        channels = []
        channels.append(scattering.Channel(sites=sites, strengths=gs, positions=xs))
        channels.append(scattering.Channel(sites=sites, strengths=gs, positions=-np.array(xs)))

        parasites = None
        if parasite is not None:
            parasites = [scattering.Channel(site=i, strength=parasite) for i in xrange(N)]

        scattering.Setup.__init__(self, model, channels, parasites)

def normalize(gs):
    gs = [g * 2 / (np.sqrt(np.pi) * (gs[0] + gs[1])) for g in gs]
    return gs

max_stars_repo_name: georglind/babusca

max_issues_repo_name: georglind/babusca

max_forks_repo_name: georglind/babusca


text:
#%%
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# 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
#
#     http://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 numpy as np
import random
import itertools
import scipy.misc
import matplotlib.pyplot as plt

class gameOb():
    def __init__(self,coordinates,size,intensity,channel,reward,name):
        self.x = coordinates[0]
        self.y = coordinates[1]
        self.size = size
        self.intensity = intensity
        self.channel = channel
        self.reward = reward
        self.name = name

class gameEnv():
    def __init__(self,size):
        self.sizeX = size
        self.sizeY = size
        self.actions = 4
        self.objects = []
        a = self.reset()
        plt.imshow(a,interpolation="nearest")

    def reset(self):
        self.objects = []
        hero = gameOb(self.newPosition(),1,1,2,None,'hero')
        self.objects.append(hero)
        goal = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(goal)
        hole = gameOb(self.newPosition(),1,1,0,-1,'fire')
        self.objects.append(hole)
        goal2 = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(goal2)
        hole2 = gameOb(self.newPosition(),1,1,0,-1,'fire')
        self.objects.append(hole2)
        goal3 = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(goal3)
        goal4 = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(goal4)
        state = self.renderEnv()
        self.state = state
        return state

    def moveChar(self,direction):
        # 0 - up, 1 - down, 2 - left, 3 - right
        hero = self.objects[0]
        heroX = hero.x
        heroY = hero.y
        if direction == 0 and hero.y >= 1:
            hero.y -= 1
        if direction == 1 and hero.y <= self.sizeY-2:
            hero.y += 1
        if direction == 2 and hero.x >= 1:
            hero.x -= 1
        if direction == 3 and hero.x <= self.sizeX-2:
            hero.x += 1
        self.objects[0] = hero

    def newPosition(self):
        iterables = [ range(self.sizeX), range(self.sizeY)]
        points = []
        for t in itertools.product(*iterables):
            points.append(t)
        currentPositions = []
        for objectA in self.objects:
            if (objectA.x,objectA.y) not in currentPositions:
                currentPositions.append((objectA.x,objectA.y))
        for pos in currentPositions:
            points.remove(pos)
        location = np.random.choice(range(len(points)),replace=False)
        return points[location]

    def checkGoal(self):
        others = []
        for obj in self.objects:
            if obj.name == 'hero':
                hero = obj
            else:
                others.append(obj)
        for other in others:
            if hero.x == other.x and hero.y == other.y:
                self.objects.remove(other)
                if other.reward == 1:
                    self.objects.append(gameOb(self.newPosition(),1,1,1,1,'goal'))
                else:
                    self.objects.append(gameOb(self.newPosition(),1,1,0,-1,'fire'))
                return other.reward,False
        return 0.0,False

    def renderEnv(self):
        #a = np.zeros([self.sizeY,self.sizeX,3])
        a = np.ones([self.sizeY+2,self.sizeX+2,3])
        a[1:-1,1:-1,:] = 0
        for item in self.objects:
            a[item.y+1:item.y+item.size+1,item.x+1:item.x+item.size+1,item.channel] = item.intensity
        b = scipy.misc.imresize(a[:,:,0],[84,84,1],interp='nearest')
        c = scipy.misc.imresize(a[:,:,1],[84,84,1],interp='nearest')
        d = scipy.misc.imresize(a[:,:,2],[84,84,1],interp='nearest')
        a = np.stack([b,c,d],axis=2)
        return a

    def step(self,action):
        self.moveChar(action)
        reward,done = self.checkGoal()
        state = self.renderEnv()
        return state,reward,done

reason: import numpy,import scipy


text:
from collections import OrderedDict
from typing import Tuple, Union
from fvcore.common.registry import Registry
from omegaconf.listconfig import ListConfig

import copy
import threading
import numpy as np
import torch
import torch.nn.functional as F
from torch import nn

from timm.models.vision_transformer import _cfg

from .. import ModifiedResNet, VisualTransformer, DistilledVisionTransformer

IMAGE_HEADS_REGISTRY = Registry("IMAGE_HEADS")
IMAGE_HEADS_REGISTRY.__doc__ = """
Registry for image encoders.
"""

def build_image_head(cfg, **kwargs):
    return IMAGE_HEADS_REGISTRY.get(cfg.name)(cfg, **kwargs)

@IMAGE_HEADS_REGISTRY.register()
class ImageHead(nn.Module):
    def __init__(self, cfg, **kwargs):
        super().__init__()
        if isinstance(cfg.layers, (tuple, list, ListConfig)):
            heads = cfg.width * 32 // 64
            self.encoder = ModifiedResNet(
                input_resolution=cfg.resolution,
                output_dim=cfg.embed_dim,
                layers=cfg.layers,
                width=cfg.width,
                heads=heads,
            )
        else:
            heads = cfg.width // 64
            self.encoder = VisualTransformer(
                input_resolution=cfg.resolution,
                output_dim=cfg.embed_dim,
                patch_size=cfg.patch_size,
                layers=cfg.layers,
                width=cfg.width,
                heads=heads,
            )

    def copy_state_dict(self, state_dict):
        self.encoder.load_state_dict(state_dict)

    def forward(self, images, *args, **kwargs):
        z = self.encoder(images)
        if kwargs.get("normalized", False):
            z = z / z.norm(dim=-1, keepdim=True)
            #print(f"{threading.current_thread().ident} image --{kwargs.get('normalized', False)}")
        return z

@IMAGE_HEADS_REGISTRY.register()
class DeiTImageHead(nn.Module):
    def __init__(self, cfg, **kwargs):
        super().__init__()
        heads = cfg.width // 64
        self.encoder = DistilledVisionTransformer(
            img_size=cfg.resolution,
            patch_size=cfg.patch_size,
            representation_size=cfg.embed_dim,
            embed_dim=cfg.width,
            depth=cfg.layers,
            num_heads=heads,
            mlp_ratio=4,
            qkv_bias=True,
            norm_layer=partial(nn.LayerNorm, eps=1e-6),
            **kwargs
        )

    def copy_state_dict(self, state_dict):
        self.encoder.load_state_dict(state_dict)

    def forward(self, images, *args, **kwargs):
        cls_z, distilled_z = self.encoder.forward_features(images)
        z = (cls_z + distilled_z) / 2
        if kwargs.get("normalized", False):
            z = z / z.norm(dim=-1, keepdim=True)
            #print(f"{threading.current_thread().ident} image --{kwargs.get('normalized', False)}")
        return z

max_stars_repo_name: zhaoyanpeng/lvamodel

max_issues_repo_name: zhaoyanpeng/vipant

max_forks_repo_name: zhaoyanpeng/vipant


text:
# -*- coding: utf-8 -*-
import sugartensor as tf
import numpy as np
import matplotlib.pyplot as plt

__author__ = 'njkim@jamonglab.com'

# set log level to debug
tf.sg_verbosity(10)

#
# hyper parameters
#

batch_size = 32   # batch size
num_category = 10  # category variable number
num_cont = 2   # continuous variable number
num_dim = 30   # total latent dimension ( category + continuous + noise )

#
# Data loading
#

# load data
x = np.genfromtxt('asset/data/sample.csv', delimiter=',', dtype=np.float32)
x = x[1:, 1:]

window = 384  # window size
max = 3000  # max value

# delete zero pad data
n = ((np.where(np.any(x, axis=1))[0][-1] + 1) // window) * window

# normalize data between 0 and 1
x = x[:n] / max

# make to matrix
X = np.asarray([x[i:i+window] for i in range(n-window)])
X = np.expand_dims(X, axis=2)

num_batch = X.shape[0] // batch_size

#
# inputs place holder
#

ph = tf.sg_input(shape=(window, 1, 2))

#
# create generator
#

# dummy place holder
z = tf.sg_input(num_dim)

# generator network
with tf.sg_context(name='generator', size=(4, 1), stride=(2, 1), act='relu', bn=True):
    gen = (z.sg_dense(dim=1024)
           .sg_dense(dim=48*1*128)
           .sg_reshape(shape=(-1, 48, 1, 128))
           .sg_upconv(dim=64)
           .sg_upconv(dim=32)
           .sg_upconv(dim=2, act='sigmoid', bn=False))

#
# create discriminator & recognizer
#

with tf.sg_context(name='discriminator', size=(4, 1), stride=(2, 1), act='leaky_relu'):
    # shared part
    shared = (ph.sg_conv(dim=32)
              .sg_conv(dim=64)
              .sg_conv(dim=128)
              .sg_flatten()
              .sg_dense(dim=1024))
    # shared recognizer part
    recog_shared = shared.sg_dense(dim=128)
    # discriminator end
    disc = shared.sg_dense(dim=1, act='linear').sg_squeeze()
    # categorical recognizer end
    recog_cat = recog_shared.sg_dense(dim=num_category, act='softmax')
    # continuous recognizer end
    recog_cont = recog_shared.sg_dense(dim=num_cont, act='sigmoid')

#
# run discrimination
#
with tf.Session() as sess:
    # init session
    tf.sg_init(sess)
    # restore parameters
    saver = tf.train.Saver()
    saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
    # run recoginizer
    cats, conts = [], []
    for i in range(0, len(X), batch_size):
        a, b = sess.run([recog_cat, recog_cont], {ph: X[i:i+batch_size]})
        cats.append(a)
        conts.append(b)
        tf.sg_debug('%d/%d processed.' % (i/batch_size + 1, num_batch))
    # to numpy array
    cats = np.argmax(np.concatenate(cats, axis=0), axis=1)   # we take max value.
    conts = np.concatenate(conts, axis=0)

#
# Plotting result
#
##
colors = [(0, 0, 0.5), (0, 0, 1), (0, 0.5, 0), (0, 0.5, 0.5), (0, 0.5, 1),
          (0, 1, 0), (0, 1, 0.5), (0, 1, 1), (0.5, 0, 0), (0.5, 0, 0.5)]

_, ax = plt.subplots(2, 1, sharex=True, sharey=True)

# plot original time series
ax[0].plot(x)
ax[0].set_title('original time series')
ax[0].set_xlabel('time')
ax[0].set_ylabel('normalized value')
ax[0].grid()

# coloring category division
i_prev = 0
for i in np.where(np.diff(cats))[0]:
    ax[0].axvspan(i_prev + window/2, i + window/2, facecolor=colors[cats[i]], alpha=0.2)
    i_prev = i + 1

# plot continous factors
ax[1].plot(np.arange(len(conts))+window/2, conts)
ax[1].set_title('decomposed continuous factors')
ax[1].set_xlabel('time')
ax[1].set_ylabel('normalized value')
ax[1].grid()

# save plot
plt.savefig('asset/train/classify.png', dpi=600)

# show plot
plt.show()


text:
import theano
import theano.tensor as T
import numpy as np

from .utils.theano_utils import shared_scalar

"""
# Losses: loss functions
In the training process, once we compute the output of a neurual network
  by input a training set (or mini-batch), in order to adjust the parameter,
  performance needs to be evaluated. classification accuracy is one measurement.
  However, accuracy is non-differentiable. Some better measurements have been
  commonly used, such as cross-entropy for classification problem and L2-distance
  for regression problem.
"""

#######################################
## Loss Function for Classification  ##
#######################################
def categorical_crossentropy(py_x, y_true):
    """
    calculate cross entropy loss
    Inputs:
        - y_probs: tensor.matrix, each row is a distribution(prob value)
        - y_true: tensor.matrix, one-hot presentation,
    Outputs:
        - tensor.scalar, data loss
    """
    return T.mean(T.nnet.categorical_crossentropy(py_x, y_true))

##################################
## Loss Function for Regression ##
##################################
def euclidean_loss(y_pred, y_true):
    """
    Compute euclidean loss on 1 dim
    Reference: http://mlg.eng.cam.ac.uk/yarin/blog_3d801aa532c1ce.html
    section: Dropout and Deep Models
    """
    #return T.mean(T.abs_(T.max(y_pred,axis=1) - y_true))/2.
    return T.mean(T.abs_(y_pred - y_true))/2.

def mean_squared_error(y_pred, y_true):
    #return T.mean(T.sqrt(T.max(y_pred,axis=1) - y_true))
    return T.mean(T.pow((y_pred - y_true),2))

def mean_absolute_error(y_pred, y_true):
    """
    Almost the same as euclidean loss
    """
    #return T.mean(T.abs_(T.max(y_pred,axis=1) - y_true))
    return T.mean(T.abs_(y_pred - y_true))

#########################
## Regularization Loss ##
#########################
def L1(params, regs):
    """
    compute regularization loss L1
    Inputs:
        - params: list of tensor vars, contains all the weights
        - regs: list of folat, contains reg strength of each weight
        Note: most times, the bias reg strength is set to 0.
    Outputs:
        - regloss: tensor scalar, the reg loss value
    """
    reg_loss = shared_scalar(0.)
    for (p, reg) in zip(params, regs):
        reg_loss += reg * T.sum(abs(p))
    return reg_loss

def L2(params, regs):
    """
    compute L2 regularization loss
    """
    reg_loss = shared_scalar(0.)
    for (p, reg) in zip(params, regs):
        reg_loss += reg * T.sum(p**2)
    return reg_loss

from .utils.generic_utils import get_from_module
def get(identifier):
    return get_from_module(identifier, globals(), 'losses')

######################################################
## Customized loss for angle difference measurement ##
#####################################################
def angular_mae(y_pred,y_true):
    return T.arccos(T.cos(T.mean(T.abs_(y_pred - y_true))))

max_stars_repo_name: shenxudeu/deuNN

max_issues_repo_name: shenxudeu/deuNet

max_forks_repo_name: shenxudeu/deuNet

reason: import numpy,import theano


text:
# Copyright 2019 Francesco Ceccon
#
# 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
#
#     http://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.

"""Re-import Pyomo expression types."""

from pyomo.core.expr.numeric_expr import (
    NegationExpression,
    PowExpression,
    ProductExpression,
    MonomialTermExpression,
    DivisionExpression,
    ReciprocalExpression,
    SumExpression,
    UnaryFunctionExpression,
    AbsExpression,
    LinearExpression,
    nonpyomo_leaf_types,
)
from pyomo.core.base.expression import _GeneralExpressionData, SimpleExpression, ScalarExpression
from pyomo.core.expr.numvalue import NumericConstant
from pyomo.core.base.var import Var

from suspect.dag.expressions import Constraint, Objective, Sense, Domain

max_stars_repo_name: cog-imperial/suspect

max_issues_repo_name: cog-imperial/suspect

max_forks_repo_name: cog-imperial/suspect

reason: from pyomo


text:
# -*- coding: utf-8 -*-
#!/usr/bin/env python

import sys
import time
import requests
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt

#Variables
#hostname = 'https://usa-gun-violence.appspot.com'
hostname = 'http://localhost:8080'
#endpoint = '/data'
endpoint = '/yearsummary'

#Functions
def processRow(df):
    df['year'] = df['date'].strftime('%Y')
    df['month'] = df['date'].strftime('%m')
    #payload = 'incidentID='+str(df.incident_id)+'&year='+df.year+'&month='+df.month+'&state='+str(df.state)+'&city_or_county='+str(df.city_or_county)+'&n_killed='+str(df.n_killed)+'&n_injured='+str(df.n_injured)+'&latitude='+str(df.latitude)+'&longitude='+str(df.longitude)
    print df['year'], df['month'], df.n_injured, df.n_killed

def putIncident(hostName,endpoint,df):
    df['year'] = df['date'].strftime('%Y')
    df['month'] = df['date'].strftime('%m')
    payload = 'incidentID='+str(df.incident_id)+'&year='+df.year+'&month='+df.month+'&state='+str(df.state)+'&city_or_county='+str(df.city_or_county)+'&n_killed='+str(df.n_killed)+'&n_injured='+str(df.n_injured)+'&latitude='+str(df.latitude)+'&longitude='+str(df.longitude)
    r = requests.put(hostName+endpoint, params=payload)
    print df.incident_id, r.text

def putYearSummary(hostName,endpoint,year):
    columns = ['year','month','n_injured','n_killed']
    indexes = []
    lst = []
    for i in range(13):
        if i == 0:
            continue
        month = str(i).zfill(2)
        lst.append([year,month,0,0])
    dfSUM = pd.DataFrame(data=lst,columns=columns)
    #dfSUM[['n_injured', 'n_killed']] = dfSUM[['n_injured', 'n_killed']].astype(int)
    dfSUM = dfSUM.infer_objects()
    dfData = pd.read_csv(str(year)+'.csv',skiprows=0, parse_dates=['date'])
    #dfData[['n_injured', 'n_killed']] = dfData[['n_injured', 'n_killed']].astype(int)
    #dfData = dfData.infer_objects()
    for index, row in dfData.iterrows():
        row['month'] = row['date'].strftime('%m')
        df = dfSUM.loc[dfSUM['month'] == row['month']]
        df['n_injured'] = df['n_injured'] + row['n_injured']
        df['n_killed'] = df['n_killed'] + row['n_killed']
        dfSUM.update(df)
    for index, row in dfSUM.iterrows():
        payload = 'year='+str(row.year).rstrip('.0')+'&month='+str(row.month)+'&n_killed='+str(row.n_killed).rstrip('.00')+'&n_injured='+str(row.n_injured).rstrip('.00')
        r = requests.put(hostName+endpoint, params=payload)
        print row.year, row.month, r.text

#Main Script
print "Start"
beginTime = time.time();

year = 2013
putYearSummary(hostname,endpoint,year)

year = 2014
putYearSummary(hostname,endpoint,year)

year = 2015
putYearSummary(hostname,endpoint,year)

year = 2016
putYearSummary(hostname,endpoint,year)

year = 2017
putYearSummary(hostname,endpoint,year)

year = 2018
putYearSummary(hostname,endpoint,year)

"""
for index, row in df.iterrows():
    dfSum['month'] = row['date'].strftime('%m')

print("2013:",df['month,n_injured'].sum(),df['monthn_killed'].sum())

df2014 = pd.read_csv('2014.csv',skiprows=0, parse_dates=['date'])
for index, row in df2014.iterrows():
    putYearSummary(hostname,endpoint,row)
print("2014:",df2014['n_injured'].sum(),df2014['n_killed'].sum())

df2015 = pd.read_csv('2015.csv',skiprows=0, parse_dates=['date'])
for index, row in df2015.iterrows():
    putYearSummary(hostname,endpoint,row)
print("2015:",df2015['n_injured'].sum(),df2015['n_killed'].sum())

df2016 = pd.read_csv('2016.csv',skiprows=0, parse_dates=['date'])
for index, row in df2016.iterrows():
    putYearSummary(hostname,endpoint,row)
print("2016:",df2016['n_injured'].sum(),df2016['n_killed'].sum())

df2017 = pd.read_csv('2017.csv',skiprows=0, parse_dates=['date'])
for index, row in df2017.iterrows():
    putYearSummary(hostname,endpoint,row)
print("2017:",df2017['n_injured'].sum(),df2017['n_killed'].sum())

df2018 = pd.read_csv('2018.csv',skiprows=0, parse_dates=['date'])
for index, row in df2018.iterrows():
    putYearSummary(hostname,endpoint,row)
print("2018:",df2018['n_injured'].sum(),df2018['n_killed'].sum())
"""
#df2013 = dfs[dfs['date'].dt.year == 2013]
#df2014 = dfs[dfs['date'].dt.year == 2014]
#df2015 = dfs[dfs['date'].dt.year == 2015]
#df2016 = dfs[dfs['date'].dt.year == 2016]
#df2017 = dfs[dfs['date'].dt.year == 2017]
#df2018 = dfs[dfs['date'].dt.year == 2018]
#print("2013:",dfs['n_injured'].sum(),dfs['n_killed'].sum())
#print("2013:",df2013['n_injured'].sum(),df2014['n_killed'].sum())
#print("2014:",df2014['n_injured'].sum(),df2014['n_killed'].sum())
#print("2015:",df2015['n_injured'].sum(),df2015['n_killed'].sum())
#print("2016:",df2016['n_injured'].sum(),df2016['n_killed'].sum())
#print("2017:",df2017['n_injured'].sum(),df2017['n_killed'].sum())
#print("2018:",df2018['n_injured'].sum(),df2018['n_killed'].sum())
#df2013.to_csv("2013.csv")
#df2014.to_csv("2014.csv")
#df2015.to_csv("2015.csv")
#df2016.to_csv("2016.csv")
#df2017.to_csv("2017.csv")
#df2018.to_csv("2018.csv")

#df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))

#df = df.cumsum()

#plt.figure(); df.plot();

endTime = time.time();
print "End"
print endTime - beginTime , "secs to put data into the cloud."

max_stars_repo_name: ctava/usa-gun-violence

max_issues_repo_name: ctava/usa-gun-violence

max_forks_repo_name: ctava/usa-gun-violence