text:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 tvm
from tvm import tir, script
from tvm.ir import Range
from tvm.script import ty

# fmt: off
@tvm.script.tir
def primfunc_global_allocates(placeholder_144: ty.handle, placeholder_145: ty.handle, placeholder_146: ty.handle, T_cast_48: ty.handle) -> None:
    # function attr dict
    tir.func_attr({"global_symbol": "fused_nn_conv2d_add_cast_fixed_point_multiply_clip_cast_cast_13", "tir.noalias": True})
    placeholder_147 = tir.match_buffer(placeholder_144, [1, 14, 14, 512], dtype="int16", elem_offset=0, align=128, offset_factor=1)
    placeholder_148 = tir.match_buffer(placeholder_145, [3, 3, 512, 1], dtype="int16", elem_offset=0, align=128, offset_factor=1)
    placeholder_149 = tir.match_buffer(placeholder_146, [1, 1, 1, 512], dtype="int32", elem_offset=0, align=128, offset_factor=1)
    T_cast_49 = tir.match_buffer(T_cast_48, [1, 14, 14, 512], dtype="int16", elem_offset=0, align=128, offset_factor=1)
    # body
    PaddedInput_22 = tir.allocate([131072], "int16", "global")
    DepthwiseConv2d_9 = tir.allocate([100352], "int32", "global")
    for i1_29, i2_39, i3_40 in tir.grid(16, 16, 512):
        PaddedInput_22[(((i1_29*8192) + (i2_39*512)) + i3_40)] = tir.if_then_else(((((1 <= i1_29) and (i1_29 < 15)) and (1 <= i2_39)) and (i2_39 < 15)), tir.load("int16", placeholder_147.data, ((((i1_29*7168) + (i2_39*512)) + i3_40) - 7680)), tir.int16(0), dtype="int16")
    for i_9, j_9, c_9 in tir.grid(14, 14, 512):
        DepthwiseConv2d_9[(((i_9*7168) + (j_9*512)) + c_9)] = 0
        for di_9, dj_9 in tir.grid(3, 3):
            DepthwiseConv2d_9[(((i_9*7168) + (j_9*512)) + c_9)] = (tir.load("int32", DepthwiseConv2d_9, (((i_9*7168) + (j_9*512)) + c_9)) + (tir.load("int16", PaddedInput_22, (((((i_9*8192) + (di_9*8192)) + (j_9*512)) + (dj_9*512)) + c_9)).astype("int32")*tir.load("int16", placeholder_148.data, (((di_9*1536) + (dj_9*512)) + c_9)).astype("int32")))
    for ax1_27, ax2_28, ax3_30 in tir.grid(14, 14, 512):
        DepthwiseConv2d_9[(((ax1_27*7168) + (ax2_28*512)) + ax3_30)] = (tir.load("int32", DepthwiseConv2d_9, (((ax1_27*7168) + (ax2_28*512)) + ax3_30)) + tir.load("int32", placeholder_149.data, ax3_30))
    for i1_30, i2_40, i3_41 in tir.grid(14, 14, 512):
        DepthwiseConv2d_9[(((i1_30*7168) + (i2_40*512)) + i3_41)] = tir.q_multiply_shift(tir.load("int32", DepthwiseConv2d_9, (((i1_30*7168) + (i2_40*512)) + i3_41)), 1269068532, 31, -4, dtype="int32")
    for i1_31, i2_41, i3_42 in tir.grid(14, 14, 512):
        DepthwiseConv2d_9[(((i1_31*7168) + (i2_41*512)) + i3_42)] = tir.max(tir.max(tir.load("int32", DepthwiseConv2d_9, (((i1_31*7168) + (i2_41*512)) + i3_42)), 255), 0)
    for ax1_28, ax2_29, ax3_31 in tir.grid(14, 14, 512):
        PaddedInput_22[(((ax1_28*7168) + (ax2_29*512)) + ax3_31)] = tir.load("int32", DepthwiseConv2d_9, (((ax1_28*7168) + (ax2_29*512)) + ax3_31)).astype("uint8")
    for ax1_29, ax2_30, ax3_32 in tir.grid(14, 14, 512):
        T_cast_49.data[(((ax1_29*7168) + (ax2_30*512)) + ax3_32)] = tir.load("uint8", PaddedInput_22, (((ax1_29*7168) + (ax2_30*512)) + ax3_32)).astype("int16")
# fmt: on

# fmt: off
@tvm.script.tir
def primfunc_local_allocates(placeholder_162: ty.handle, placeholder_163: ty.handle, placeholder_164: ty.handle, T_cast_76: ty.handle) -> None:
    # function attr dict
    tir.func_attr({"global_symbol": "fused_nn_conv2d_add_cast_fixed_point_multiply_clip_cast_cast_9", "tir.noalias": True})
    placeholder_165 = tir.match_buffer(placeholder_162, [1, 14, 14, 512], dtype="int16", elem_offset=0, align=128, offset_factor=1)
    placeholder_166 = tir.match_buffer(placeholder_163, [3, 3, 512, 1], dtype="int16", elem_offset=0, align=128, offset_factor=1)
    placeholder_167 = tir.match_buffer(placeholder_164, [1, 1, 1, 512], dtype="int32", elem_offset=0, align=128, offset_factor=1)
    T_cast_77 = tir.match_buffer(T_cast_76, [1, 14, 14, 512], dtype="int16", elem_offset=0, align=128, offset_factor=1)
    # body
    PaddedInput_25 = tir.allocate([1, 16, 16, 512], "int16", "global")
    for i1_35, i2_46, i3_47 in tir.grid(16, 16, 512):
        PaddedInput_25[(((i1_35*8192) + (i2_46*512)) + i3_47)] = tir.if_then_else(((((1 <= i1_35) and (i1_35 < 15)) and (1 <= i2_46)) and (i2_46 < 15)), tir.load("int16", placeholder_165.data, ((((i1_35*7168) + (i2_46*512)) + i3_47) - 7680)), tir.int16(0), dtype="int16")
    T_add_11 = tir.allocate([1, 14, 14, 512], "int32", "global")
    with tir.allocate([1, 14, 14, 512], "int32", "global") as DepthwiseConv2d_11:
        for i_11, j_11, c_11 in tir.grid(14, 14, 512):
            DepthwiseConv2d_11[(((i_11*7168) + (j_11*512)) + c_11)] = 0
            for di_11, dj_11 in tir.grid(3, 3):
                DepthwiseConv2d_11[(((i_11*7168) + (j_11*512)) + c_11)] = (tir.load("int32", DepthwiseConv2d_11, (((i_11*7168) + (j_11*512)) + c_11)) + (tir.load("int16", PaddedInput_25, (((((i_11*8192) + (di_11*8192)) + (j_11*512)) + (dj_11*512)) + c_11)).astype("int32")*tir.load("int16", placeholder_166.data, (((di_11*1536) + (dj_11*512)) + c_11)).astype("int32")))
        for ax1_44, ax2_45, ax3_47 in tir.grid(14, 14, 512):
            T_add_11[(((ax1_44*7168) + (ax2_45*512)) + ax3_47)] = (tir.load("int32", DepthwiseConv2d_11, (((ax1_44*7168) + (ax2_45*512)) + ax3_47)) + tir.load("int32", placeholder_167.data, ax3_47))
    compute_22 = tir.allocate([1, 14, 14, 512], "int32", "global")
    with tir.allocate([1, 14, 14, 512], "int32", "global") as T_cast_78:
        for ax1_45, ax2_46, ax3_48 in tir.grid(14, 14, 512):
            T_cast_78[(((ax1_45*7168) + (ax2_46*512)) + ax3_48)] = tir.load("int32", T_add_11, (((ax1_45*7168) + (ax2_46*512)) + ax3_48))
        for i1_36, i2_47, i3_48 in tir.grid(14, 14, 512):
            compute_22[(((i1_36*7168) + (i2_47*512)) + i3_48)] = tir.q_multiply_shift(tir.load("int32", T_cast_78, (((i1_36*7168) + (i2_47*512)) + i3_48)), 1948805937, 31, -5, dtype="int32")
    T_cast_79 = tir.allocate([1, 14, 14, 512], "uint8", "global")
    with tir.allocate([1, 14, 14, 512], "int32", "global") as compute_23:
        for i1_37, i2_48, i3_49 in tir.grid(14, 14, 512):
            compute_23[(((i1_37*7168) + (i2_48*512)) + i3_49)] = tir.max(tir.max(tir.load("int32", compute_22, (((i1_37*7168) + (i2_48*512)) + i3_49)), 255), 0)
        for ax1_46, ax2_47, ax3_49 in tir.grid(14, 14, 512):
            T_cast_79[(((ax1_46*7168) + (ax2_47*512)) + ax3_49)] = tir.load("int32", compute_23, (((ax1_46*7168) + (ax2_47*512)) + ax3_49)).astype("uint8")
    for ax1_47, ax2_48, ax3_50 in tir.grid(14, 14, 512):
        T_cast_77.data[(((ax1_47*7168) + (ax2_48*512)) + ax3_50)] = tir.load("uint8", T_cast_79, (((ax1_47*7168) + (ax2_48*512)) + ax3_50)).astype("int16")
# fmt: on

def test_global_allocates():
    primfunc = primfunc_global_allocates
    assert tvm.tir.analysis.calculate_workspace_bytes(primfunc) == 663552

def test_local_allocates():
    primfunc = primfunc_local_allocates
    assert tvm.tir.analysis.calculate_workspace_bytes(primfunc) == 1566720

if __name__ == "__main__":
    test_global_allocates()
    test_local_allocates()

max_stars_repo_name: zerowfz/incubator-tvm

max_issues_repo_name: zerowfz/incubator-tvm

max_forks_repo_name: zerowfz/incubator-tvm


text:
#!/home/mraz/anaconda3/bin/python3
import os
#import Image
import time
import matplotlib
matplotlib.use('Agg')  #  DISPLAY=:0  not necessary BEFORE pyplot imported!!!
import matplotlib.pyplot as plt
####import matplotlib.image as mpimg
from scipy.misc import imread
import matplotlib.cbook as cbook

import numpy as np

def get_picture(dest='web'):
	rname='annotation_placeholder'
	if ( dest=='web'):
### GET last picture name
		a=os.popen('wget -q http://www.chmi.cz/files/portal/docs/meteo/rad/data_tr_png_1km/ -O - |  grep png | awk -F \\\"  \'{print $2 }\' | sort | tail -1')
		rname=a.readline()
		tmppng='/tmp/'+rname.rstrip()
		print('i.... remote name=',tmppng)
		os.system('wget -c http://portal.chmi.cz/files/portal/docs/meteo/rad/data_tr_png_1km/'+ rname.rstrip() +' -O '+tmppng)
		return tmppng
	if ( dest=='debug'):
		tmppng='i.png'
		os.system('wget -c http://portal.chmi.cz/files/portal/docs/meteo/rad/data_tr_png_1km/pacz23.z_max3d.20160917.1500.0.png -O '+tmppng)
		print('i.... remote name=',tmppng)
###os.system('cp i.png  '+tmppng)  ### jakoby wget
		return tmppng
	if ( dest=='test1'):
		tmppng='/tmp/test1.png'
		print('i.... remote name=',tmppng)
		return tmppng

def make_circle( cil ):
	samplesize=cil[2]
	circle1=[ cil[0],cil[1], cil[0]+1, cil[1]+1  ]
	circle2=[ cil[0],cil[1], cil[0]+3, cil[1]+3  ]
	circle3=[ cil[0],cil[1], cil[0]+samplesize/2, cil[1]+samplesize/2  ]
	rect1=[ cil[0] -samplesize/2,cil[1]-samplesize/2, cil[0]+samplesize/2, cil[1]+samplesize/2  ]
	#fuch='circle '
	#print( fuch , ','.join( map(str,circle3) ) )
	#CMD1=' -fill fuchsia -draw \"circle '+','.join( map(str,circle3) ) +'\" '
	CMD1=' -fill fuchsia -draw \"rectangle '+str(rect1[0])+','+str(rect1[1])+' '+str(rect1[2])+','+str(rect1[3])+'\" '
	CMD2=' -fill white   -draw \"circle '+','.join( map(str,circle2) ) +'\" '
	CMD3=' -fill fuchsia -draw \"circle '+','.join( map(str,circle1) ) +'\" '
	#print('test1 ',CMD1+CMD2)
	CMD='convert '+tmppng+' '+CMD1+CMD2+CMD3+' '+tmpcirc
	print('CIRC...:',CMD)
	if (os.system( CMD )!=0):
		print('error with '+CMD)
		exit(1)
	#samplesize=120
	crop=str(samplesize)+'x'+str(samplesize)+'+'+str(cil[0]-round(samplesize/2))+'+'+str(cil[1]-round(samplesize/2))
	#print( 289- round(samplesize/2 ) )  #9x9+285+314
	CMD='convert '+tmppng+'  -crop '+crop+' '+tmpsample
	print('CROP99...',CMD)
	if (os.system( CMD )!=0):
		print('error with '+CMD)
		exit(1)
	rgb=[
	 [56,0,112],
	 [48,0,168],
	 [0,0,252],
	 [0,108,192],
	 [0,160,0],
	 [0,188,0],
	 [52,216,0],
	 [156,220,0],
	 [224,220,0],
	 [252,176,0],
	 [252,132,0],
	 [252,88,0],
	 [252,0,0],
	 [160,0,0],
	 [252,252,252]
	 ]
	factor=[0.056, 0.1, 0.178, 0.315, 0.56, 1.0, 1.78, 3.15, 5.6, 10., 17.8, 31.5, 56., 100., 178. ]
	intensity=[]
	for i in range( len(rgb)):
		RGB='\"rgb('+','.join( map(str,rgb[i]))+')\"'
		CMD='convert '+tmpsample+' -fill black +opaque '+RGB+' -fill white -opaque '+RGB+' -format "%[fx:mean*'+str(factor[i])+']\\n" info: '
	#	CMD='convert '+tmpsample+' -fill black +opaque '+RGB+' -fill white -opaque '+RGB+' -format "%[fx:mean*'+str(1.0)+']\\n" info: '
		#print('ANA1...',CMD)
		a=os.popen(CMD)
		inten=a.readline().rstrip()
		intensity.append( float(inten) )
		print(i,inten)
	print('SUM=',sum(intensity))
	return intensity,factor
####################################################################

#####################################################################
#####################################################################
#
#
#          MAIN
#
#####################################################################
#####################################################################

#tmppng='/tmp/radar.png'
#tmppng='i.png'
#tmppng=get_picture('test1')  #####################
tmppng=get_picture('web')  #####################
print('ahoj')
tmpcirc='/tmp/i2.png'
tmpcrop='/tmp/i3.png'
tmpcropa='/tmp/i3a.png'  # annotated
tmpsample='/tmp/i4.png'
###### DATE.  bash.
CURRENTTIME='date +%Y%m%d_%H%M%S'
a=os.popen( CURRENTTIME )
currenttime=a.readline().rstrip()
### CREATE             things to store in /motion
os.system(' mkdir -p  /motion/cam_radar/')
os.system(' mkdir -p  /motion/cam_radarana/')
motionradar='/motion/cam_radar/'+currenttime+'.jpg'
motionradarana='/motion/cam_radarana/'+currenttime+'.jpg'
#### camradar.gif  in  myweb
gifradar='/tmp/camradar.gif'

time.sleep(1)
##################### I HAVE ALL FILES #####################

samplesize=9
#mnisek=[289,318, samplesize  ]  #  coord.,  samplesize=9
mnisek=[288,313, samplesize  ]  #  coord.,  samplesize=9
revnice=[282,307,samplesize  ]  #  coord.,  samplesize=9
praha=[301,292,  samplesize  ]  #  coord.,  samplesize=9
snezka=[3394,221,  samplesize  ]  #  coord.,  samplesize=9
rez= [296,280, samplesize ]
rana= [252,261, samplesize ]
bratri= [294,349, samplesize ]

cr=[301,305,  180  ]

icr,factor = make_circle( cr )
icrev,factor = make_circle( revnice )

intensity,factor = make_circle( mnisek )  # LAST WILL HAVE PICTURE
#intensity,factor = make_circle( revnice )

##################################### 9x9  SAMPLE OK
#################################################  CIRCLE IS OK now

time.sleep(1)
######################### i have intensity,factor
x=np.arange( len(factor) )
npi=np.array( intensity )
npicr=np.array( icr )
npirev=np.array( icrev )
npf=np.array( factor )

CMD='convert  '+tmpcirc+' -crop 266x200+150+205 -resize 360x300 -background white -alpha remove '+tmpcrop
print('CROP...:',CMD)
if (os.system( CMD )!=0):
	print('error with '+CMD)
	time.sleep(1)

	exit(1)
time.sleep(1)

######################################### CROP OK

#CMD='convert '+tmpcrop+' -fill white -gravity center -font Arial -pointsize 24  -undercolor \'#00000080\'  -annotate -150+120 %t '+tmpcropa
CMD='convert '+tmpcrop+' -fill white -gravity center -font Arial -pointsize 14  -undercolor \'#00000080\'  -annotate -150+120 "'+tmppng+'" '+tmpcropa
print('FNAME...',CMD)
if (os.system( CMD )!=0):
	print('error with '+CMD)
	time.sleep(1)

	exit(1)
time.sleep(1)

######################################## CROP WITH FILENAME OK

CMD='convert  '+tmpcropa+' '+ motionradar
print('RM...',CMD)
if (os.system( CMD )!=0):
	print('! error with '+CMD)
	time.sleep(3)
	exit(1)

########################################  convert to jpg, nightly motion needs jpg

print('before fig')
time.sleep(5)

fig = plt.figure( figsize=(6, 5), dpi=100 )
#fig = plt.figure(  )
print('after fig')
time.sleep(5)

ax = fig.add_subplot(111)

#plt.bar(  x, npi )
#plt.bar(  x, npi/npf )
width = 0.2                      # the width of the bars

#### RULE:
##### the last p[icture MNISEK
##### the first BAR  CR
## the bars
#CR
print('before bar')
time.sleep(2)

rects1cr = ax.bar( x , npicr, 1.0,
                color='dimgray'   )
#NUMBERS NORMALIZED
rects2 = ax.bar(x, npi/npf, width,
                    color='white'  )

#mnisek
rects1 = ax.bar( x+1*width , npi, width,
                color='blue'   )
#revnice
rects1rev = ax.bar( x+2*width , npirev, width,
                color='cyan'   )

#chartreuse   http://matplotlib.org/examples/color/named_colors.html
#orange
#dimgray
print('before matplotlib')
time.sleep(2)

matplotlib.rcParams['examples.directory']='.'
datafile = cbook.get_sample_data( tmpsample , asfileobj=True )
img = imread(datafile)
######## fig.set_size_inches(18.5, 10.5)
matplotlib.rcParams['figure.figsize'] = 5, 10

#plt.imshow(img, zorder=0, extent=[0,16,0, max(npi) ]  )

plt.imshow(img, zorder=0, extent=[1,16,0, max(  max(npicr),max(npirev),max(npi),max(npi/npf) ) ]  , aspect='auto', interpolation='none')

# axes and labels
#plt.plot( npf, npi, 'ro')

		#img = mpimg.imread( tmpsample )
		#plt.imshow(img)   # i add implot=
#plt.show()
plt.savefig( motionradarana )
time.sleep(2)

RMCMD='rm '+tmppng+'; rm '+tmpcirc+';  rm '+tmpcrop+'; rm  '+tmpcropa+'; rm '+tmpsample
#if (os.system( RMCMD )!=0):
	#print('error with '+RMCMD)
#	exit(1)

###########
#
##  create GIF
#
gifie=[]
CMD='cd /motion/cam_radar; ls -1tr 20*.jpg | tail -4 '
a=os.popen(CMD)
time.sleep(2)

while 1:
	gifie1=( a.readline().rstrip() )
	print( '   ... files : gifie1 ')
	if not gifie1:
		break
	gifie.append( gifie1 )
time.sleep(1)

CMD='cd /motion/cam_radar;convert -resize 360x300 -delay 60 '+' '.join(gifie)+'  -loop 0  '+gifradar
print('GIF prepare:' ,CMD)
if ( os.system(CMD)!=0):
	print('... problem with ',CMD)
	time.sleep(1)
	exit(1)

######################
#
#   SQMYLITE
##
#  rain3   mnisek  CR   revnice
#  rain5   mnisek  CR   revnice  bratri  rez
CMD='sqmylite -i rain3.mysql '+str(sum(npi))+' '+str(sum(npicr))+' '+str(sum(npirev))
print( 'saving to sqmylite ... '+CMD )
if (os.system(CMD) !=0):
	print('error with '+CMD)
	time.sleep(1)

	exit(1)
#os.system('sqmylite -i rain3.mysql  ', sum(npi),sum(npicr),sum(npirev)  )

print(' echo sleeping 900 sec for the next frame ....')
time.sleep(900)

ext: py

reason: import numpy,from scipy


text:
#!/usr/bin/env python
import sys
import numpy as np
import glob
from optparse import OptionParser

class breakpoint:
    chr_l = ''
    start_l = 0
    end_l = 0
    strand_l = ''
    p_l = []

    chr_l = ''
    start_l = 0
    end_l = 0
    strand_r = ''
    p_l = []

    v_id = 0
    score = 0
    ids_str = ''
    strands_str = ''
    max_str = ''
    nine_five_str = ''
    sv_type = ''
    evidence_size = 0
    src_file = ''

    def __init__(self, line, src_file):
        self.src_file = src_file

        A = l.rstrip().split('\t')
        self.chr_l = A[0]
        self.start_l = int(A[1])
        self.end_l = int(A[2])

        self.chr_r = A[3]
        self.start_r = int(A[4])
        self.end_r = int(A[5])

        self.v_id = A[6]
        self.score = A[7]
        self.strand_l = A[8]
        self.strand_r = A[9]

        #self.sv_type = A[10][5:]
        self.sv_type = A[10]

        #for ec in A[11][4:].split(';'):
            #e,c = ec.split(',')
            #self.evidence_size += int(c)

        self.ids_str = A[11]
        self.strands_str = A[12]
        self.max_str = A[13]
        self.nine_five_str = A[14]

        #self.p_l = A[15][3:]
        #self.p_r = A[16][3:]
        self.p_l = A[15]
        self.p_r = A[16]

    def __str__(self):
        return '\t'.join([self.chr_l, \
                         str(self.start_l), \
                         str(self.end_l), \
                         self.chr_r, \
                         str(self.start_r), \
                         str(self.end_r), \
                         self.v_id,\
                         self.score,\
                         self.strand_l,\
                         self.strand_r,\
                         self.sv_type, \
                         self.ids_str, \
                         self.strands_str, \
                         self.max_str, \
                         self.nine_five_str, \
                         self.p_l,\
                         self.p_r ])
    def __cmp__(self,other):

        presidence = [
                       [self.sv_type,other.sv_type],\
                       [self.chr_l,other.chr_l], \
                       [self.chr_r,other.chr_r], \
                       [self.start_l,other.start_l], \
                       [self.end_l,other.end_l], \
                       [self.start_r,other.end_l],
                       ]

        for p in presidence:
            if p[0] != p[1]:
                return cmp(p[0],p[1])

        return 0

parser = OptionParser()

parser.add_option("-d",
    "--data_file_dir",
    dest="data_file_dir",
    help="Data file directory")

(options, args) = parser.parse_args()

if not options.data_file_dir:
    parser.error('Data file directory not given')

BP = []

for file_name in glob.glob(options.data_file_dir):
    f = open(file_name,'r')

    for l in f:
        BP.append(breakpoint(l,file_name))
    f.close()

BP.sort()

for bp in BP:
    print bp

max_stars_repo_name: monkollek/lumpy-sv

max_issues_repo_name: monkollek/lumpy-sv

max_forks_repo_name: monkollek/lumpy-sv

reason: import numpy


text:
import os
import sys
import numpy as np
import heapq
from scipy.stats import multivariate_normal
import pytest
import cv2

scriptdir = os.path.abspath(__file__).split('scripts')[0] + 'scripts/'
sys.path.append(scriptdir)

from evaluation.gmm_prediction import GMMPrediction

'''
Test Fixture Parametrized by Dataset.
'''
def get_straight_traj(vel, num_timesteps, dt):
	xs = vel * dt * np.arange(1, 1+num_timesteps)
	ys = np.zeros(len(xs))
	return np.column_stack((xs, ys))

def get_const_curv_traj(vel, ang_vel, num_timesteps, dt):
	assert np.abs(ang_vel) > 0.
	xs = vel / ang_vel * np.sin(ang_vel * dt * np.arange(1, 1 + num_timesteps))
	ys = vel / ang_vel * (1 - np.cos(ang_vel * dt * np.arange(1, 1 + num_timesteps)))
	return np.column_stack((xs, ys))

def generate_perturbation_traj(n_steps, max_noise_radius):
	# Returns a trajectory with radius-bounded noise about the origin.
	noise_angles = np.random.rand(n_steps) * ( 2 * np.pi )
	noise_radii  = np.random.rand(n_steps) * max_noise_radius
	noise_vectors = np.array([rad * np.array([np.cos(ang), np.sin(ang)])
		                      for ang, rad in zip(noise_angles, noise_radii)])
	return noise_vectors

def make_test_predictions(N, dt):
	# 3 mode GMM with left, straight, and right turns.
	vel     = 10 # m/s
	ang_vel = 0.2 # rad/s

	mode_probs = np.array([0.25, 0.5, 0.25])
	traj_l = get_const_curv_traj(vel, ang_vel, N, dt)
	traj_c = get_straight_traj(vel, N, dt)
	traj_r = get_const_curv_traj(vel, -ang_vel, N, dt)
	num_modes = len(mode_probs)

	mus    = np.array([traj_l, traj_c, traj_r])
	sigmas = np.tile( np.diag([2., 5.]), (num_modes, N, 1, 1) )

	return GMMPrediction(num_modes, N, mode_probs, mus, sigmas)

def get_area_upper_bound(sigmas, beta):
	# Integrates the ellipsoid area over all sigmas.
	# This is an upper bound because it doesn't consider
	# area overlaps and will overcount such regions.
	area = 0.
	for mode in range(sigmas.shape[0]):
		for covar in sigmas[mode]:
			evals, evecs = np.linalg.eigh( covar ) # rotation/unitary matrices and evecs = diag(sigma_1**2, sigma_2**2)

			length_ax1 = np.sqrt(beta * evals[0]) # semi-major axis length, sigma_1 * sqrt(beta)
			length_ax2 = np.sqrt(beta * evals[1]) # semi-minor axis length, sigma_2 * sqrt(beta)

			area += (np.pi * length_ax1 * length_ax2)

	return area

@pytest.fixture(scope="module", params=['nuscenes', 'l5kit'])
def gmm_preds(request):
	# Identify prediction horizon (N) and discretization time (dt).
	# Build the GMM example given in make_test_predictions.
	if request.param == 'nuscenes':
		N = 12; dt = 0.5
	elif request.param == 'l5kit':
		N = 25; dt = 0.2
	else:
		raise NotImplementedError

	return make_test_predictions(N, dt)

'''
Test Suite
'''
def test_get_top_k_GMM(gmm_preds):
	# Check that the correct modes get selected for the truncated GMM.
	mode_probs = gmm_preds.mode_probabilities
	for k in range( 1, max(1, gmm_preds.n_modes) ):
		truncated_gmm = gmm_preds.get_top_k_GMM(k)

		# Check truncated probablity selection with heapq + renormalization match.
		# We need to reverse heapq to maintain consistent ordering with argsort.
		expected_mode_probs = np.array( heapq.nlargest(k, mode_probs)[::-1] )
		expected_mode_probs = expected_mode_probs / np.sum(expected_mode_probs)
		assert np.allclose(expected_mode_probs, truncated_gmm.mode_probabilities)

		# Check that the mean/covariance are the ones we expected.
		inds_k_largest = np.argsort(mode_probs)[-k:]
		for trunc_inc, ind in enumerate(inds_k_largest):
			mu_expected    = gmm_preds.mus[ind, :]
			sigma_expected = gmm_preds.sigmas[ind, :]

			assert np.allclose(mu_expected, truncated_gmm.mus[trunc_inc])
			assert np.allclose(sigma_expected, truncated_gmm.sigmas[trunc_inc])

def test_compute_trajectory_log_likelihood(gmm_preds):
	# Check if naive calculation matches logsumexp (for numerically simple case).
	for test_traj_idx in range(gmm_preds.n_modes):
		traj = np.copy(gmm_preds.mus[test_traj_idx])
		traj += generate_perturbation_traj(traj.shape[0], 5.)

		naive_prob_calc = 0.
		for mode_idx in range(gmm_preds.n_modes):
			gaussian_prod = 1.
			for t in range(gmm_preds.n_timesteps):
				prob = multivariate_normal.pdf( traj[t],
					                            mean=gmm_preds.mus[mode_idx][t],
					                            cov=gmm_preds.sigmas[mode_idx][t])
				gaussian_prod *= prob
			naive_prob_calc += gmm_preds.mode_probabilities[mode_idx] * gaussian_prod
		naive_ll = np.log( naive_prob_calc )

		lse_ll   = gmm_preds.compute_trajectory_log_likelihood(traj)

		assert np.isclose(naive_ll, lse_ll)

def test_get_class_top_k_scores(gmm_preds):
	# Test 1: Small perturbations about the most likely trajectory should result
	#         in 100% accuracy for all values of k.
	#         Also growing k should not reduce the accuracy.
	test_traj = np.copy(gmm_preds.mus[np.argmax(gmm_preds.mode_probabilities)])
	test_traj += generate_perturbation_traj(test_traj.shape[0], 0.1)

	top_k_scores = gmm_preds.get_class_top_k_scores(test_traj, ks=[1,3,5])

	assert np.all(np.array(top_k_scores) == 1)
	assert np.all( np.arange(len(top_k_scores)) == np.argsort(top_k_scores) )

	# Test 2: Small perturbations about the least likely trajectory should result
	#         in 100% accuracy when k >= num modes of original GMM.
	#         Also growing k should not reduce the accuracy.
	test_traj = np.copy(gmm_preds.mus[np.argmin(gmm_preds.mode_probabilities)])
	test_traj += generate_perturbation_traj(test_traj.shape[0], 0.1)

	top_k_scores = gmm_preds.get_class_top_k_scores(test_traj, ks=[1,3,5])

	assert top_k_scores[0] == 0
	assert np.all(np.array(top_k_scores[1:]) == 1)
	assert np.all( np.arange(len(top_k_scores)) == np.argsort(top_k_scores) )

def test_compute_min_ADE(gmm_preds):
	# Add noise within a norm ball of a true trajectory.
	# The minADE should be upper-bounded by the max radius.
	for max_noise_radius in np.arange(0., 21., 5.):
		for selected_traj in range(gmm_preds.n_modes):
			test_traj = np.copy(gmm_preds.mus[selected_traj])
			test_traj += generate_perturbation_traj(test_traj.shape[0],
				                                    max_noise_radius)

			ade = gmm_preds.compute_min_ADE(test_traj)

			assert ade >= 0
			assert ade <= max_noise_radius

def test_compute_min_FDE(gmm_preds):
	# Similar to ADE, the max radius should upper bound the FDE here.
	for max_noise_radius in np.arange(0., 21., 5.):
		for selected_traj in range(gmm_preds.n_modes):
			test_traj  = np.copy(gmm_preds.mus[selected_traj])
			test_traj += generate_perturbation_traj(test_traj.shape[0],
				                                    max_noise_radius)
			fde = gmm_preds.compute_min_FDE(test_traj)
			assert fde >= 0
			assert fde <= max_noise_radius

	# FDE should be zero if the last state is left unchanged.
	for selected_traj in range(gmm_preds.n_modes):
		test_traj = np.copy(gmm_preds.mus[selected_traj])
		test_traj[:-1] += generate_perturbation_traj(test_traj.shape[0] - 1,
			                                         5.)
		assert np.isclose(gmm_preds.compute_min_FDE(test_traj), 0.)

	# FDE should be impacted even if all prior states are noise-free.
	for selected_traj in range(gmm_preds.n_modes):
		test_traj = np.copy(gmm_preds.mus[selected_traj])
		test_traj[-1] += np.ravel(generate_perturbation_traj(1, 5.))
		fde = gmm_preds.compute_min_FDE(test_traj)
		assert fde >= 0
		assert fde <= 5.

def test_compute_set_accuracy(gmm_preds):
	# This test checks if a perturbed trajectory lies within
	# a given Mahalanobis distance of the mean trajectory.

	debug = False # for plotting the ellipse sets and trajectories

	for max_noise_radius in np.array([1., 10., 50.]):
		mdist_sq_upper_bound = max_noise_radius**2
		beta_inc = mdist_sq_upper_bound / 5
		betas = np.arange(1., mdist_sq_upper_bound + beta_inc, beta_inc)

		for selected_traj in range(gmm_preds.n_modes):
			test_traj  = np.copy(gmm_preds.mus[selected_traj])
			test_traj += generate_perturbation_traj(test_traj.shape[0],
				                                    max_noise_radius)

			accs = [gmm_preds.compute_set_accuracy(test_traj, beta, debug=debug) for beta in betas]

			# accs should be 0 or 1 and should be in ascending order
			# i.e. enlarging the set can only improve accuracy
			assert np.all( np.arange(len(betas)) == np.argsort(accs) )
			assert np.min(accs) >= 0
			assert np.max(accs) <= 1

def test_compute_set_area(gmm_preds):
	# This test checks that set area results are reasonable:
	# (1) Area should be positive.
	# (2) Increasing beta (conf. threshold) should increase area.
	# (3) It should be upper bounded by the areas of all ellipsoids added together
	#     up to some discretization error of OpenCV.
	betas     = np.linspace(1., 50., 4)
	areas     = [] # area computed via OpenCV numerical integration
	areas_ub  = [] # upper bound area computed as a sum of ellipse areas
	occ_grids = [] # the "occupancy grids" made with OpenCV to find areas above

	debug = False # for plotting the occupancy grids

	for beta in betas:
		area, occ_grid = gmm_preds.compute_set_area(beta)
		areas.append(area)
		occ_grids.append(occ_grid)

		area_ub = get_area_upper_bound(gmm_preds.sigmas, beta)
		areas_ub.append(area_ub)

	areas       = np.array(areas)
	areas_ub    = np.array(areas_ub)
	areas_ratio = areas / areas_ub

	if debug:
		n_rows, n_cols = occ_grids[0].shape
		n_rows_ds = int(n_rows / 10)
		n_cols_ds = int(n_cols / 10)

		combined_img = np.zeros( (n_rows_ds * len(occ_grids), n_cols_ds), dtype=np.uint8)

		for ind, grid in enumerate(occ_grids):
			combined_img[ind*n_rows_ds:(ind+1)*n_rows_ds, :] = cv2.resize(grid, (n_cols_ds, n_rows_ds))

		del occ_grids

		print(f"Betas: {betas}")
		print(f"Areas: {areas}")
		cv2.imshow("occ_grid", combined_img); cv2.waitKey(0)

	assert np.all(areas > 0)
	assert np.all( np.arange(len(areas)) == np.argsort(areas) )
	assert np.max(areas_ratio) < 1.05


text:
import numpy as np
import sklearn.svm
from semisup_learn.frameworks.SelfLearning import SelfLearningModel
from six import print_ as print
from sklearn.datasets.mldata import fetch_mldata
from sklearn.linear_model.stochastic_gradient import SGDClassifier

from semisup_learn.frameworks.CPLELearning import CPLELearningModel
from semisup_learn.methods.scikitWQDA import WQDA

# load data
cancer = fetch_mldata("Lung cancer (Ontario)")
X = cancer.target.T
ytrue = np.copy(cancer.data).flatten()
ytrue[ytrue > 0] = 1

# label a few points
labeled_N = 4
ys = np.array([-1] * len(ytrue))  # -1 denotes unlabeled point
random_labeled_points = np.random.choice(np.where(ytrue == 0)[0], labeled_N // 2, replace=False) + \
                        np.random.choice(np.where(ytrue == 1)[0], labeled_N // 2, replace=False)
ys[random_labeled_points] = ytrue[random_labeled_points]

# supervised score
# basemodel = WQDA() # weighted Quadratic Discriminant Analysis
basemodel = SGDClassifier(loss='log', penalty='l1')  # scikit logistic regression
basemodel.fit(X[random_labeled_points, :], ys[random_labeled_points])
print(("supervised log.reg. score", basemodel.score(X, ytrue)))

# fast (but naive, unsafe) self learning framework
ssmodel = SelfLearningModel(basemodel)
ssmodel.fit(X, ys)
print(("self-learning log.reg. score", ssmodel.score(X, ytrue)))

# semi-supervised score (base model has to be able to take weighted samples)
ssmodel = CPLELearningModel(basemodel)
ssmodel.fit(X, ys)
print(("CPLE semi-supervised log.reg. score", ssmodel.score(X, ytrue)))

# semi-supervised score, WQDA model
ssmodel = CPLELearningModel(WQDA(), predict_from_probabilities=True)  # weighted Quadratic Discriminant Analysis
ssmodel.fit(X, ys)
print(("CPLE semi-supervised WQDA score", ssmodel.score(X, ytrue)))

# semi-supervised score, RBF SVM model
ssmodel = CPLELearningModel(sklearn.svm.SVC(kernel="rbf", probability=True), predict_from_probabilities=True)  # RBF SVM
ssmodel.fit(X, ys)
print(("CPLE semi-supervised RBF SVM score", ssmodel.score(X, ytrue)))

max_stars_repo_name: oroszgy/semisup-learn

max_issues_repo_name: oroszgy/semisup-learn

max_forks_repo_name: oroszgy/semisup-learn


text:
import logging
import sys
import re
import codecs
import copy
import numpy as np

"""
~~~~~~~~~~~~~~~~~~~
Conversion factors
~~~~~~~~~~~~~~~~~~~
"""
BOHR_2_ANGSTROM = 0.529177210
ANGSROM_2_BOHR = 1. / BOHR_2_ANGSTROM
HARTREE_2_KJMOL = 2625.50
KJMOL_2_HARTREE = 1. / HARTREE_2_KJMOL

def logger(name='fragmenter', pattern='%(asctime)s %(levelname)s %(name)s: %(message)s',
           date_format='%H:%M:%S', handler=logging.StreamHandler(sys.stdout)):
    """
    Retrieves the logger instance associated to the given name
    :param name: The name of the logger instance
    :param pattern: The associated pattern
    :param date_format: The date format to be used in the pattern
    :param handler: The logging handler
    :return: The logger
    """
    _logger = logging.getLogger(name)
    _logger.setLevel(log_level(verbose))

    if not _logger.handlers:
        formatter = logging.Formatter(pattern, date_format)
        handler.setFormatter(formatter)
        handler.setLevel(log_level(verbose))
        _logger.addHandler(handler)
        _logger.propagate = False
    return _logger

verbose = False

def log_level(verbose=verbose):
    if verbose:
        return logging.DEBUG
    else:
        return logging.INFO

def sort_nicely(l):
    """

    Parameters
    ----------
    l

    Returns
    -------

    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
    l.sort(key=alphanum_key)

def flatten(l, ltypes=(list, tuple)):
    """
    Flatten list of lists
    Parameters
    ----------
    l: list to flatten
    ltypes: tuple of types

    Returns
    -------
    flattened list
    """
    ltype = type(l)
    l = list(l)
    i = 0
    while i < len(l):
        while isinstance(l[i], ltypes):
            if not l[i]:
                l.pop(i)
                i -=1
                break
            else:
                l[i:i + 1] = l[i]
        i += 1
    return ltype(l)

def make_python_identifier(string, namespace=None, reserved_words=None,
                           convert='hex', handle='force'):
    """
    Taken from https://gist.github.com/JamesPHoughton
    Takes an arbitrary string and creates a valid Python identifier.
    If the python identifier created is already in the namespace,
    or if the identifier is a reserved word in the reserved_words
    list, or is a python default reserved word,
    adds _1, or if _1 is in the namespace, _2, etc.
    Parameters
    ----------
    string : <basestring>
        The text to be converted into a valid python identifier
    namespace : <dictionary>
        Map of existing translations into python safe identifiers.
        This is to ensure that two strings are not translated into
        the same python identifier
    reserved_words : <list of strings>
        List of words that are reserved (because they have other meanings
        in this particular program, such as also being the names of
        libraries, etc.
    convert : <string>
        Tells the function what to do with characters that are not
        valid in python identifiers
        - 'hex' implies that they will be converted to their hexidecimal
                representation. This is handy if you have variables that
                have a lot of reserved characters
        - 'drop' implies that they will just be dropped altogether
    handle : <string>
        Tells the function how to deal with namespace conflicts
        - 'force' will create a representation which is not in conflict
                  by appending _n to the resulting variable where n is
                  the lowest number necessary to avoid a conflict
        - 'throw' will raise an exception
    Returns
    -------
    identifier : <string>
        A vaild python identifier based on the input string
    namespace : <dictionary>
        An updated map of the translations of words to python identifiers,
        including the passed in 'string'.
    Examples
    --------
    >>> make_python_identifier('Capital')
    ('capital', {'Capital': 'capital'})
    >>> make_python_identifier('multiple words')
    ('multiple_words', {'multiple words': 'multiple_words'})
    >>> make_python_identifier('multiple     spaces')
    ('multiple_spaces', {'multiple     spaces': 'multiple_spaces'})
    When the name is a python keyword, add '_1' to differentiate it
    >>> make_python_identifier('for')
    ('for_1', {'for': 'for_1'})
    Remove leading and trailing whitespace
    >>> make_python_identifier('  whitespace  ')
    ('whitespace', {'  whitespace  ': 'whitespace'})
    Remove most special characters outright:
    >>> make_python_identifier('H@t tr!ck')
    ('ht_trck', {'H@t tr!ck': 'ht_trck'})
    Replace special characters with their hex representations
    >>> make_python_identifier('H@t tr!ck', convert='hex')
    ('h40t_tr21ck', {'H@t tr!ck': 'h40t_tr21ck'})
    remove leading digits
    >>> make_python_identifier('123abc')
    ('abc', {'123abc': 'abc'})
    namespace conflicts
    >>> make_python_identifier('Variable$', namespace={'Variable@':'variable'})
    ('variable_1', {'Variable@': 'variable', 'Variable$': 'variable_1'})
    >>> make_python_identifier('Variable$', namespace={'Variable@':'variable', 'Variable%':'variable_1'})
    ('variable_2', {'Variable@': 'variable', 'Variable%': 'variable_1', 'Variable$': 'variable_2'})
    throw exception instead
    >>> make_python_identifier('Variable$', namespace={'Variable@':'variable'}, handle='throw')
    Traceback (most recent call last):
     ...
    NameError: variable already exists in namespace or is a reserved word
    References
    ----------
    Identifiers must follow the convention outlined here:
        https://docs.python.org/2/reference/lexical_analysis.html#identifiers
    """
    if namespace is None:
        namespace = {}

    if reserved_words is None:
        reserved_words = []

    s = copy.deepcopy(string)

    # remove leading and trailing whitespace
    s = s.strip()

    # Make spaces into underscores
    s = re.sub('[\\s\\t\\n]+', '_', s)

    if convert == 'hex':
        # Convert invalid characters to hex
        hexlify = codecs.getencoder('hex')
        s = ''.join(['_' + (hexlify(c.encode('utf-8'))[0]).decode('utf-8') + '_'
                     if re.findall('[^0-9a-zA-Z_]', c) else c for c in s])

    elif convert == 'drop':
        # Remove invalid characters
        s = re.sub('[^0-9a-zA-Z_]', '', s)

    # Remove leading characters until we find a letter or underscore
    s = re.sub('^[^a-zA-Z_]+', '', s)

    # Check that the string is not a python identifier
    while (#s in keyword.kwlist or
           s in namespace.values() or
           s in reserved_words):
        if handle == 'throw':
            raise NameError(s + ' already exists in namespace or is a reserved word')
        if handle == 'force':
            if re.match(".*?_\d+$", s):
                i = re.match(".*?_(\d+)$", s).groups()[0]
                s = s.strip('_'+i) + '_'+str(int(i)+1)
            else:
                s += '_1'

    namespace[string] = s

    return s, namespace

"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Movie making functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""

def merge_images(file1, file2, filename):
    """Merge two images into one, displayed side by side
    :param file1: path to first image file
    :param file2: path to second image file
    :return: the merged Image object
    """
    try:
        from PIL import Image
    except ImportError:
        raise ImportError("Must have PIL installed to use this function")

    image1 = Image.open(file1)
    image2 = Image.open(file2)

    (width1, height1) = image1.size
    (width2, height2) = image2.size

    result_width = width1 + width2 - 500 # play with the number here
    result_height = max(height1, height2)

    result = Image.new('RGB', (result_width, result_height), color='white')
    result.paste(im=image2, box=(width1-250, 0)) # play with the number here
    result.paste(im=image1, box=(0, int(result_height/2) - int(height1/2)))
    result.save(filename)

def make_movie(files, fps=15, filename='movie'):
    """
    Make movie from list of files

    Parameters
    ----------
    files: list of paths to files
    fps: files per second, int, optional
        defualt 15
    filename: str, optional
        default 'movie

    """
    try:
        import moviepy.editor as mpy
    except ImportError:
        raise ImportError("You need to install moviepy to use this function")

    im = mpy.ImageSequenceClip(files, fps=fps)
    im.write_videofile('{}.mp4'.format(filename), fps=fps)

max_stars_repo_name: dgasmith/fragmenter

max_issues_repo_name: dgasmith/fragmenter

max_forks_repo_name: dgasmith/fragmenter


text:
from flask import Flask, request, jsonify, session, send_file, redirect
from flask_cors import CORS
from flask_session import Session

import os
import urllib.request
import pandas as pd
import numpy as np
import pickle
import qnorm

import traceback
import sys

app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

print("server started")

base_name = os.environ.get('BASE_NAME', 'rookpy')
data_url = os.environ.get('DATA_URL', 'https://mssm-seq-matrix.s3.amazonaws.com/rooky_data.pkl')
token = os.environ.get('TOKEN', 'NA')

urllib.request.urlretrieve(data_url, 'data.pkl')
print("data loaded")

data = 0
with open('data.pkl', 'rb') as f:
    data = pickle.load(f)

@app.route('/'+base_name+'/signature', methods=["POST", "GET"])
def signature_search():
    print("search")
    query_data = request.get_json()
    print(query_data)

    signature_name = query_data.get("signatureName", "signature")
    upgenes = [x.upper() for x in query_data.get("upgenes", [])]
    downgenes = [x.upper() for x in query_data.get("downgenes",[])]
    #direction = query_data["direction"]
    species = query_data.get("species",[])
    signature_type = query_data.get("type", "")
    signature_values = query_data.get("signature", "")
    signature_genes = query_data.get("siggenes", "")

    if signature_type == "full_signature":
        genes = [x.upper() for x in list(data[species]["normalization"].index)]
        input_sig = pd.DataFrame(signature_values, index=signature_genes)
        signature = pd.DataFrame([0] * len(genes), index=genes)
        intergene = list(set(signature_genes).intersection(set(genes)))
        signature.loc[intergene, :] = input_sig.loc[intergene,:]

        qexp = pd.DataFrame(qnorm.quantile_normalize(np.log2(1+signature), target=data[species]["normalization"].loc[:,"target_quantiles"]))
        qexp = qexp.sub(np.array(data[species]["normalization"].loc[:,"mean"]), axis=0).div(np.array(data[species]["normalization"].loc[:,"std"]), axis=0)
        qexp = pd.DataFrame(np.dot(data[species]["transform"], qexp), dtype=np.float16)

        sigs = data[species]["signatures"].astype(np.float32)
        bs = pd.DataFrame(np.sqrt((sigs*sigs).sum(axis=0)))
        qs = np.sqrt((signature*signature).sum())
        ok = data[species]["signatures"].T.dot(signature)
        cosine_dist = (ok/bs/qs).sort_values(0, ascending=True)
        cosine_dist = (cosine_dist-cosine_dist.mean())/np.std(cosine_dist)
        significant = cosine_dist[cosine_dist[0] > 3]
        samples = [int(x.replace("GSM", "")) for x in significant.index]

        response = { 'name': signature_name}
        response["samples"] = samples
        response["similarity"] = list(significant[0])

    elif signature_type == "geneset":
        genes = [x.upper() for x in list(data[species]["normalization"].index)]
        signature = pd.DataFrame([0] * len(genes), index=genes)
        inter = list(set(genes).intersection(set(upgenes)))

        signature.loc[inter,:] = 1
        inter = list(set(genes).intersection(set(downgenes)))
        signature.loc[inter,:] = -1
        signature = pd.DataFrame(np.dot(data[species]["transform"], signature), dtype=np.float32)

        sigs = data[species]["signatures"].astype(np.float32)
        bs = pd.DataFrame(np.sqrt((sigs*sigs).sum(axis=0)))
        qs = np.sqrt((signature*signature).sum())
        ok = sigs.T.dot(signature)

        cosine_dist = (ok/bs/qs).sort_values(0, ascending=True)
        cosine_dist = (cosine_dist-cosine_dist.mean())/np.std(cosine_dist)
        significant = cosine_dist[cosine_dist[0] > 2.5]
        samples = [int(x.replace("GSM", "")) for x in significant.index]

        response = { 'name': signature_name}
        response["samples"] = samples

    return jsonify(response)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

max_stars_repo_name: MaayanLab/rookpy

max_issues_repo_name: MaayanLab/rookpy

max_forks_repo_name: MaayanLab/rookpy


text:
import pytest
import os
import os.path as osp
import sys
import numpy as np
import torch

sys.path.append('../')

def test_dataset():
    '''Test loading samples from a dataset.
    Checks dataset creation and index parsing.
    '''
    import scanpy as sc
    from scnym.dataprep import SingleCellDS
    torch.manual_seed(1)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )
    # add fake domain labels
    adata.obs['domain'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    ds = SingleCellDS(
        X = adata.X.toarray(),
        y = np.array(adata.obs['class']),
        domain = np.array(adata.obs['domain']),
    )

    # draw a sample
    idx = 10
    sample = ds[idx]
    sample['input'] = sample['input'].unsqueeze(0)
    # ensure the sample is the correct index
    assert np.all(
        sample['input'].numpy() == adata.X[idx, :].toarray().flatten()
    )
    # ensure the class label is correct
    y_gt = np.array(adata.obs['class'])[idx]
    y_onehot = sample['output'].view(1, -1)
    _, y_ds = torch.max(y_onehot, dim=1)
    assert np.all(y_ds.item() == y_gt)
    # ensure the domain label is correct
    d_gt = np.array(adata.obs['domain'])[idx]
    d_onehot = sample['domain'].view(1, -1)
    _, d_ds = torch.max(d_onehot, dim=1)
    assert np.all(d_ds.item() == d_gt)

    # throw an error by passing an invalid idx

    with pytest.raises(TypeError):
        sample = ds['bad_index_type']

    return

def test_dataset_sparse():
    '''Test loading samples from a dataset using sparse matrices.
    Checks dataset creation and index parsing.
    '''
    import scanpy as sc
    from scnym.dataprep import SingleCellDS
    from scipy import sparse
    torch.manual_seed(1)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    X = sparse.csr_matrix(adata.X)

    ds = SingleCellDS(
        X = X,
        y = np.array(adata.obs['class']),
    )

    # draw a sample
    idx = 10
    sample = ds[idx]
    sample['input'] = sample['input'].unsqueeze(0)
    # ensure the sample is the correct index
    assert np.all(
        sample['input'].numpy() == adata.X[idx, :].toarray().flatten()
    )

    # throw an error by passing an invalid idx

    with pytest.raises(TypeError):
        sample = ds['bad_index_type']

    return

def test_normalization():
    '''Test log transformations and library size normalization'''
    import scanpy as sc
    from scnym.dataprep import SingleCellDS, LibrarySizeNormalize, ExpMinusOne
    torch.manual_seed(1)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    ds = SingleCellDS(
        X = adata.X.toarray(),
        y = np.array(adata.obs['class']),
    )

    # draw a sample
    idx = 10
    sample = ds[idx]
    sample['input'] = sample['input'].unsqueeze(0)

    # normalize the vector and log1p transform
    norm = LibrarySizeNormalize(
        log1p=True,
    )
    sample_norm = norm(sample)

    # exponentiate the vector
    eom = ExpMinusOne()
    sample_expm1 = eom(sample)

    # renormalize the vector and check validity
    sample_renorm = norm(sample_expm1)

    assert torch.all(
        sample_renorm['input']==sample_norm['input']
    )

    return

def test_multinomial_sampling():
    '''Test that multinomial sampling works using a large
    sample depth and a permutation test
    '''
    import scanpy as sc
    from scnym.dataprep import SingleCellDS, LibrarySizeNormalize, MultinomialSample
    torch.manual_seed(1)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    ds = SingleCellDS(
        X = adata.X.toarray(),
        y = np.array(adata.obs['class']),
    )

    # draw a sample
    idx = 10
    sample = ds[idx]
    sample['input'] = sample['input'].unsqueeze(0)
    # keep an unmodified copy of the sample
    sample_orig = sample.copy()

    # normalize the sample but do not log transform
    norm = LibrarySizeNormalize(
        log1p=False,
    )
    sample_norm = norm(sample)

    # get a multinomial vector with random depth
    mn = MultinomialSample(
        depth = (10000, 20000),
    )
    sample_mn = mn(sample_norm)
    # check that the depth is in the expected range
    d = torch.sum(sample_mn['input'], 1)
    lt_bidx = d < 10000
    gt_bidx = d > 20000
    assert torch.sum(lt_bidx) == 0.
    assert torch.sum(gt_bidx) == 0.

    # sample deeply from a multinomial
    mn = MultinomialSample(
        depth = (1000000, 1000001),
    )
    sample_mn = mn(sample_norm)
    # check that the depth is in the expected range
    d = torch.sum(sample_mn['input'], 1)
    lt_bidx = d < 1000000
    gt_bidx = d > 1000001
    assert torch.sum(lt_bidx) == 0.
    assert torch.sum(gt_bidx) == 0.

    # sample using a depth ratio
    mn = MultinomialSample(
        depth_ratio = (0.25, 2),
    )
    sample_mn = mn(sample_norm)
    # check that the depth is in the expected range
    d = torch.sum(sample_mn['input'], 1)
    in_sizes = torch.sum(sample_norm['input'], 1)
    l_sz = in_sizes * 0.25
    h_sz = in_sizes * 2
    lt_bidx = d < l_sz
    gt_bidx = d > h_sz
    assert torch.sum(lt_bidx) == 0.
    assert torch.sum(gt_bidx) == 0.

    # normalize the sample abundance profile
    sample_mn_norm = norm(sample_mn)

    # get the l2 norm between the original sample and the
    # counts drawn from a multinomial
    d = torch.log1p(sample_mn_norm['input']) - torch.log1p(sample_orig['input'])
    test_norm = torch.norm(
        d, 2,
    )

    # permute genes to generate a null distribution of norms
    n_tests = 1000
    rand_norms = torch.zeros(n_tests,)
    for i in range(n_tests):
        ridx = torch.randperm(sample_orig['input'].size(0))
        d = torch.log1p(sample_mn_norm['input']) - torch.log1p(sample_orig['input'][ridx])
        rand_norms[i] = torch.norm(
            d,
            2,
        )

    p = torch.sum(test_norm > rand_norms) // n_tests

    msg = f'p = {p} suggests something is rotten with multinomial sampling.'
    assert p < 0.05, msg

    return

def test_poisson_sampling():
    '''Test the Poisson sampling method.'''
    import scanpy as sc
    from scnym.dataprep import SingleCellDS, LibrarySizeNormalize, PoissonSample
    from torchvision.transforms import Compose
    torch.manual_seed(17)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    ds = SingleCellDS(
        X = adata.X.toarray(),
        y = np.array(adata.obs['class']),
    )

    # draw a sample
    idx = 10
    sample = ds[idx]
    sample['input'] = sample['input'].unsqueeze(0)
    # keep an unmodified copy of the sample
    sample_orig = sample.copy()

    # normalize the sample but do not log transform
    norm = LibrarySizeNormalize(
        log1p=False,
    )
    sample_norm = norm(sample)
    sample_norm_orig = sample_norm.copy()

    # sample using a depth ratio
    poisson = PoissonSample(depth_range=(0.5, 2.))
    sample_ps = poisson(sample_norm.copy())
    print('initial:')
    print(sample_norm_orig['input'][sample_norm_orig['input']>0][:20])
    print('sampled 00:')
    print(sample_ps['input'][sample_ps['input']>0][:20])

    # check that sample depths are in an expected range
    library_sizes = sample_ps['input'].sum(1)
    too_small_bidx = torch.sum(library_sizes < 4.5e5)
    too_large_bidx = torch.sum(library_sizes > 2.1e6)
    if not too_small_bidx == 0:
        raise RuntimeError(f'{too_small_bidx} cells had small libraries.')
    if not too_large_bidx == 0:
        raise RuntimeError(f'{too_large_bidx} cells had large libraries.')

    # check that a second sample is different
    sample_ps2 = poisson(sample_norm.copy())
    print('sampled 01:')
    print(sample_ps2['input'][sample_ps2['input']>0][:20])

    all_eq = torch.all(
        sample_ps['input'] == sample_ps2['input']
    )
    if all_eq:
        print('sampled 00:')
        print(sample_ps['input'][sample_ps['input']>0][:20])
        print('sampled 01:')
        print(sample_ps2['input'][sample_ps2['input']>0][:20])
        msg = f'The two Poisson samples were identitcal: {all_eq}'
        raise RuntimeError(msg)

    # test passing a minibatch for sampling
    dl = torch.utils.data.DataLoader(ds, batch_size=16)
    batch = next(iter(dl))

    tr = Compose(
        [
            LibrarySizeNormalize(log1p=False),
            PoissonSample(),
            LibrarySizeNormalize(log1p=True),
        ]
    )

    batch_sampled = tr(batch)

    return

def test_gene_masking():
    '''Test that gene masking augmentations zero out a
    random subset of genes'''
    import scanpy as sc
    from scnym.dataprep import SingleCellDS, GeneMasking
    torch.manual_seed(1)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    ds = SingleCellDS(
        X = adata.X.toarray(),
        y = np.array(adata.obs['class']),
    )

    # draw a sample
    idx = 10
    sample = ds[idx]
    sample['input'] = sample['input'].unsqueeze(0)
    sample_orig = sample.copy()

    # initiate a gene masker
    gm = GeneMasking(p_drop = 0.8, p_apply=1.0)

    sample_masked = gm(sample.copy())

    # check that we masked some genes
    n_genes_original = torch.sum(sample_orig['input'] > 0)
    n_genes_masked = torch.sum(sample_masked['input'] > 0)
    assert n_genes_masked < n_genes_original

    # check the masking is randomized
    sample_masked0 = gm(sample.copy())
    sample_masked1 = gm(sample.copy())

    bidx0 = sample_masked0['input']==0
    bidx1 = sample_masked1['input']==0
    assert ~torch.all(bidx0 & bidx1)

    return

def test_mixup():
    import scanpy as sc
    from scnym.dataprep import SingleCellDS, SampleMixUp
    torch.manual_seed(1)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    # draw a batch from the dataloader
    ds = SingleCellDS(
        X = adata.X.toarray(),
        y = np.array(adata.obs['class']),
    )
    dl = torch.utils.data.DataLoader(ds, batch_size=16)

    batch = next(iter(dl))

    # mixup the batch
    mixup_op = SampleMixUp(
        alpha = 0.3,
        keep_dominant_obs = True,
    )

    mixed_batch = mixup_op(batch)

    # test that mixup can be turned off with `alpha = 0.0`
    batch = next(iter(dl))
    mixup_op = SampleMixUp(
        alpha = 0.0,
        keep_dominant_obs = True,
    )

    mixed_batch = mixup_op(batch)

    assert torch.all(
        mixed_batch['input'] == batch['input']
    )

    return

def test_augmentation_schemes():
    '''Test each of the predefined augmentation schemes'''
    import scanpy as sc
    from scnym.dataprep import SingleCellDS, SampleMixUp, AUGMENTATION_SCHEMES
    torch.manual_seed(1)
    np.random.seed(1)

    # load 10x human PBMC data as a sample
    adata = sc.datasets.pbmc3k()
    # generate fake class labels
    adata.obs['class'] = np.random.randint(
        0,
        3,
        adata.shape[0],
    )

    # draw a batch from the dataloader
    ds = SingleCellDS(
        X = adata.X.toarray(),
        y = np.array(adata.obs['class']),
    )
    dl = torch.utils.data.DataLoader(ds, batch_size=16)

    for k in AUGMENTATION_SCHEMES.keys():
        aug = AUGMENTATION_SCHEMES[k]
        batch = next(iter(dl))
        try:
            augmented = aug(batch)
        except RuntimeError:
            print(f'Augmentation scheme {k} failed.')
    return

def main():
    test_mixup()
    return

if __name__ == '__main__':
    main()

max_stars_repo_name: jdasam/scnym

max_issues_repo_name: jdasam/scnym

max_forks_repo_name: jdasam/scnym


text:
"""
Python part for
Molecular Dynamics Simulation on a Sphere

Please see README.md for details.

MIT license

Copyright(c) 2015 - 2021 Tim Scheffler

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""

import os
import sys
import time

# https://stackoverflow.com/questions/63475461/unable-to-import-opengl-gl-in-python-on-macos
try:
    import OpenGL as ogl
    try:
        import OpenGL.GL   # this fails in <=2020 versions of Python on OS X 11.x
    except ImportError:
        print('Drat, patching for Big Sur')
        from ctypes import util
        orig_util_find_library = util.find_library
        def new_util_find_library( name ):
            res = orig_util_find_library( name )
            if res: return res
            return '/System/Library/Frameworks/'+name+'.framework/'+name
        util.find_library = new_util_find_library
except ImportError:
    pass

import numpy as np
import OpenGL.GL as GL
import OpenGL.GLUT as GLUT
import scipy.spatial as spatial

import _thread
import simulation

COLORING_INTERVAL = 1.0      # in seconds
POINT_SIZE = 3

GREEN = np.array([0, 1, 0], dtype='float32')
RED = np.array([1, 0, 0], dtype='float32')
BLUE = np.array([0, 0, 1], dtype='float32')
YELLOW = np.array([1, 1, 0], dtype='float32')

class Simulation(simulation.Simulation):

    def __init__(self, world):
        super(Simulation, self).__init__()
        self.world = world
        self.last_coloring = 0
        self.is_coloring = False
        self.point_size = POINT_SIZE
        self.simulation_running = True
        self.position_data = None

    def timestep(self):
        self.integrate()

    def integrate(self):
        self.world.timestep()

    def retrieve_visual_data(self):
        """Retrieve data from simulation kernel for color_data and position_data"""
        self.position_data = self.world.get_3dPositions()
        if (not self.is_coloring and time.time() - self.last_coloring > COLORING_INTERVAL):
            self.is_coloring = True
            _thread.start_new_thread(self.do_NN_coloring, ())

    def render(self):
        GL.glEnable(GL.GL_LIGHTING)
        GL.glEnable(GL.GL_LIGHT0)
        GLUT.glutSolidSphere(.99,128,128)
        GL.glDisable(GL.GL_LIGHTING)
        GL.glColor3f(0., 1.0, 0.2)
        super(Simulation, self).render()

        if False:
            if not self.position_data is None:
                i0 = int(len(self.position_data) / 2)
                x, y, z = self.position_data[i0]
                GL.glPushMatrix()
                GL.glEnable(GL.GL_BLEND)
                GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
                GL.glTranslated(x, y, z)
                GL.glColor4f(1, 1, 0, 0.5)
                GLUT.glutSolidSphere(self.world.cutoff, 16, 16)
                GL.glPopMatrix()

    def do_NN_coloring(self):
        """Find the color for a particle based on the number of nearest neighbours."""
        if self.position_data is None:
            return

        hull = spatial.ConvexHull(self.position_data)
        a = hull.simplices.reshape(-1)
        counts = np.bincount(a)

        data = ( (counts == 6)[:, np.newaxis] * GREEN
               + (counts < 6)[:, np.newaxis] * BLUE
               + (counts > 6)[:, np.newaxis] * RED )

        i0 = int(len(data) / 2)
        data[i0] = YELLOW

        self.count6 = np.sum(counts == 6)
        self.countless = np.sum(counts < 6)
        self.countmore = np.sum(counts > 6)

        self.color_data = data
        self.has_color_data = True
        self.is_coloring = False
        self.last_coloring = time.time()

max_stars_repo_name: tscheff/Davis

max_issues_repo_name: tscheff/Davis

max_forks_repo_name: tscheff/Davis

reason: import numpy,import scipy


text:
# Keras
import keras
from keras import regularizers
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential, Model, model_from_json
from keras.layers import Dense, Embedding, LSTM
from keras.layers import Input, Flatten, Dropout, Activation, BatchNormalization
from keras.layers import Conv1D, MaxPooling1D, AveragePooling1D
from keras.utils import np_utils, to_categorical
from keras.callbacks import (EarlyStopping, LearningRateScheduler,
                             ModelCheckpoint, TensorBoard, ReduceLROnPlateau)
from keras import losses, models, optimizers
from keras.activations import relu, softmax
from keras.layers import (Convolution2D, GlobalAveragePooling2D, BatchNormalization, Flatten, Dropout,
                          GlobalMaxPool2D, MaxPool2D, concatenate, Activation, Input, Dense)

# sklearn
from sklearn.metrics import confusion_matrix, accuracy_score

# Other
from tqdm import tqdm
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from matplotlib.pyplot import specgram
import pandas as pd
import seaborn as sns
import sys
import IPython.display as ipd  # To play sound in the notebook
import warnings
# ignore warnings
if not sys.warnoptions:
    warnings.simplefilter("ignore")

from sklearn.preprocessing import LabelEncoder
lb = LabelEncoder()

'''
1. Data Augmentation method
'''
def speedNpitch(data):
    """
    Speed and Pitch Tuning.
    """
    # you can change low and high here
    length_change = np.random.uniform(low=0.8, high = 1)
    speed_fac = 1.2  / length_change # try changing 1.0 to 2.0 ... =D
    tmp = np.interp(np.arange(0,len(data),speed_fac),np.arange(0,len(data)),data)
    minlen = min(data.shape[0], tmp.shape[0])
    data *= 0
    data[0:minlen] = tmp[0:minlen]
    return data

'''
2. Extracting the MFCC feature as an image (Matrix format).
'''
def prepare_data(df, n, aug, mfcc):
    X = np.empty(shape=(df.shape[0], n, 216, 1))
    input_length = sampling_rate * audio_duration

    cnt = 0
    for fname in tqdm(df.path):
        file_path = fname
        data, _ = librosa.load(file_path, sr=sampling_rate
                               ,res_type="kaiser_fast"
                               ,duration=2.5
                               ,offset=0.5
                              )

        # Random offset / Padding
        if len(data) > input_length:
            max_offset = len(data) - input_length
            offset = np.random.randint(max_offset)
            data = data[offset:(input_length+offset)]
        else:
            if input_length > len(data):
                max_offset = input_length - len(data)
                offset = np.random.randint(max_offset)
            else:
                offset = 0
            data = np.pad(data, (offset, int(input_length) - len(data) - offset), "constant")

        # Augmentation?
        if aug == 1:
            data = speedNpitch(data)

        # which feature?
        if mfcc == 1:
            # MFCC extraction
            MFCC = librosa.feature.mfcc(data, sr=sampling_rate, n_mfcc=n_mfcc)
            MFCC = np.expand_dims(MFCC, axis=-1)
            X[cnt,] = MFCC

        else:
            # Log-melspectogram
            melspec = librosa.feature.melspectrogram(data, n_mels = n_melspec)
            logspec = librosa.amplitude_to_db(melspec)
            logspec = np.expand_dims(logspec, axis=-1)
            X[cnt,] = logspec

        cnt += 1

    return X

'''
3. Confusion matrix plot
'''
def print_confusion_matrix(confusion_matrix, class_names, figsize = (10,7), fontsize=14):
    '''Prints a confusion matrix, as returned by sklearn.metrics.confusion_matrix, as a heatmap.

    Arguments
    ---------
    confusion_matrix: numpy.ndarray
        The numpy.ndarray object returned from a call to sklearn.metrics.confusion_matrix.
        Similarly constructed ndarrays can also be used.
    class_names: list
        An ordered list of class names, in the order they index the given confusion matrix.
    figsize: tuple
        A 2-long tuple, the first value determining the horizontal size of the ouputted figure,
        the second determining the vertical size. Defaults to (10,7).
    fontsize: int
        Font size for axes labels. Defaults to 14.

    Returns
    -------
    matplotlib.figure.Figure
        The resulting confusion matrix figure
    '''
    df_cm = pd.DataFrame(
        confusion_matrix, index=class_names, columns=class_names,
    )
    fig = plt.figure(figsize=figsize)
    try:
        heatmap = sns.heatmap(df_cm, annot=True, fmt="d")
    except ValueError:
        raise ValueError("Confusion matrix values must be integers.")

    heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=fontsize)
    heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=fontsize)
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

'''
# 4. Create the 2D CNN model
'''
def get_2d_conv_model(n):
    ''' Create a standard deep 2D convolutional neural network'''
    nclass = 14
    inp = Input(shape=(n,216,1))  #2D matrix of 30 MFCC bands by 216 audio length.
    x = Convolution2D(32, (4,10), padding="same")(inp)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPool2D()(x)
    x = Dropout(rate=0.2)(x)

    x = Convolution2D(32, (4,10), padding="same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPool2D()(x)
    x = Dropout(rate=0.2)(x)

    x = Convolution2D(32, (4,10), padding="same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPool2D()(x)
    x = Dropout(rate=0.2)(x)

    x = Convolution2D(32, (4,10), padding="same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPool2D()(x)
    x = Dropout(rate=0.2)(x)

    x = Flatten()(x)
    x = Dense(64)(x)
    x = Dropout(rate=0.2)(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = Dropout(rate=0.2)(x)

    out = Dense(nclass, activation=softmax)(x)
    model = models.Model(inputs=inp, outputs=out)

    opt = optimizers.Adam(0.001)
    model.compile(optimizer=opt, loss=losses.categorical_crossentropy, metrics=['acc'])
    return model

'''
# 5. Other functions
'''
class get_results:
    '''
    We're going to create a class (blueprint template) for generating the results based on the various model approaches.
    So instead of repeating the functions each time, we assign the results into on object with its associated variables
    depending on each combination:
        1) MFCC with no augmentation
        2) MFCC with augmentation
        3) Logmelspec with no augmentation
        4) Logmelspec with augmentation
    '''

    def __init__(self, model_history, model ,X_test, y_test, labels):
        self.model_history = model_history
        self.model = model
        self.X_test = X_test
        self.y_test = y_test
        self.labels = labels

    def create_plot(self, model_history):
        '''Check the logloss of both train and validation, make sure they are close and have plateau'''
        plt.plot(model_history.history['loss'])
        plt.plot(model_history.history['val_loss'])
        plt.title('model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'test'], loc='upper left')
        plt.show()

    def create_results(self, model):
        '''predict on test set and get accuracy results'''
        opt = optimizers.Adam(0.001)
        model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
        score = model.evaluate(X_test, y_test, verbose=0)
        print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))

    def confusion_results(self, X_test, y_test, labels, model):
        '''plot confusion matrix results'''
        preds = model.predict(X_test,
                                 batch_size=16,
                                 verbose=2)
        preds=preds.argmax(axis=1)
        preds = preds.astype(int).flatten()
        preds = (lb.inverse_transform((preds)))

        actual = y_test.argmax(axis=1)
        actual = actual.astype(int).flatten()
        actual = (lb.inverse_transform((actual)))

        classes = labels
        classes.sort()

        c = confusion_matrix(actual, preds)
        print_confusion_matrix(c, class_names = classes)

    def accuracy_results_gender(self, X_test, y_test, labels, model):
        '''Print out the accuracy score and confusion matrix heat map of the Gender classification results'''

        preds = model.predict(X_test,
                         batch_size=16,
                         verbose=2)
        preds=preds.argmax(axis=1)
        preds = preds.astype(int).flatten()
        preds = (lb.inverse_transform((preds)))

        actual = y_test.argmax(axis=1)
        actual = actual.astype(int).flatten()
        actual = (lb.inverse_transform((actual)))

        # print(accuracy_score(actual, preds))

        actual = pd.DataFrame(actual).replace({'female_angry':'female'
                   , 'female_disgust':'female'
                   , 'female_fear':'female'
                   , 'female_happy':'female'
                   , 'female_sad':'female'
                   , 'female_surprise':'female'
                   , 'female_neutral':'female'
                   , 'male_angry':'male'
                   , 'male_fear':'male'
                   , 'male_happy':'male'
                   , 'male_sad':'male'
                   , 'male_surprise':'male'
                   , 'male_neutral':'male'
                   , 'male_disgust':'male'
                  })
        preds = pd.DataFrame(preds).replace({'female_angry':'female'
               , 'female_disgust':'female'
               , 'female_fear':'female'
               , 'female_happy':'female'
               , 'female_sad':'female'
               , 'female_surprise':'female'
               , 'female_neutral':'female'
               , 'male_angry':'male'
               , 'male_fear':'male'
               , 'male_happy':'male'
               , 'male_sad':'male'
               , 'male_surprise':'male'
               , 'male_neutral':'male'
               , 'male_disgust':'male'
              })

        classes = actual.loc[:,0].unique()
        classes.sort()

        c = confusion_matrix(actual, preds)
        print(accuracy_score(actual, preds))
        print_confusion_matrix(c, class_names = classes)


text:
"""
This module implements the training routine for the specified model.

Examples:
    $ python -m kg.ner.train \
        --config configs/baseline.yaml

Current supervised results (token level evaluation):
Macro average precision: 0.83
Macro average recall: 0.67
Macro F1: 0.74
"""
import argparse
import os
import time
import pickle
from types import SimpleNamespace

import numpy as np
from sklearn.metrics import accuracy_score, classification_report
import torch
import torch.optim as optim
import yaml

from kg.ner.model import LSTM, loss_fn, get_predictions, recover_labels, translate_predictions
from kg.ner.preprocess import Preprocessor

def evaluate(model: torch.nn.Module,
             dataloader: torch.utils.data.dataloader,
             split: str = 'Validation',
             print_report: bool = False) -> float:
    """Evaluate the model on a cut of the data, optionally printing a full classification report.

    Args:
        model (torch.nn.Module): Trained model.
        dataloader (torch.utils.data.dataloader): Dataloader containing data to evaluate.
        split (str, optional): String that will be printed to denote which set (e.g. Validation, Test, etc.) is being evaluated. Defaults to 'Validation'.
        print_report (bool, optional): If True, print a full classification report. Defaults to False.

    Returns:
        float: Average loss score.
    """
    # monitor validation loss
    model.eval()
    eval_loss_scores = []
    eval_preds = []
    eval_labels = []
    for sentences, labels in dataloader:
        output = model(sentences)
        loss = loss_fn(output, labels)
        eval_loss_scores.append(loss.item())
        eval_preds += get_predictions(output, sentences[1]).tolist()
        eval_labels += recover_labels(labels, sentences[1]).tolist()
    eval_loss = round(np.mean(eval_loss_scores), 4)
    eval_acc = round(accuracy_score(eval_labels, eval_preds) * 100, 2)
    print(f'{split} loss (average): {eval_loss}')
    print(f'{split} accuracy score: {eval_acc}%')
    if print_report:
        label_set = list(set(eval_preds).union(set(eval_labels)))
        print(
            classification_report(eval_labels,
                                  eval_preds,
                                  labels=label_set,
                                  target_names=list(label_dict.keys())))
    return eval_loss

def train_epoch(model: torch.nn.Module,
                dataloader: torch.utils.data.dataloader,
                optimizer: torch.optim) -> tuple:
    """Train the model for one epoch and update the weights.

    Args:
        model (torch.nn.Module): Model to train.
        dataloader (torch.utils.data.dataloader): Train dataloader.
        optimizer (torch.optim): Optimizer to apply gradient updates.

    Returns:
        tuple: model, optimizer
    """
    model.train()
    j = 0
    for sentences, labels in dataloader:
        output = model(sentences)
        loss = loss_fn(output, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if j % 100 == 0:
            batch_preds = get_predictions(output, sentences[1])
            batch_labels = recover_labels(labels, sentences[1])
            raw_acc = accuracy_score(batch_labels, batch_preds)
            print(
                f'Sample train batch - loss value: {round(loss.item(), 4)} \t accuracy score: {round(raw_acc*100, 2)}%'
            )
        j += 1
    return model, optimizer

def save_model(model: torch.nn.Module, optimizer: torch.optim, i: int,
               config: SimpleNamespace) -> str:
    """Save a checkpoint of the model.

    Args:
        model (torch.nn.Module): Model to be saved.
        optimizer (torch.optim): Optimizer to be saved.
        i (int): Number of epochs the model was trained for.
        config (SimpleNamespace): Configuration mapping containing the save directory.

    Returns:
        str: Fully specified file path where the model was saved.
    """
    # save the model
    state = {
        'model': model.state_dict(),
        'optimizer': optimizer.state_dict(),
        'epoch': i
    }
    save_file_path = os.path.join(config.run_dir, 'model.pt')
    torch.save(state, save_file_path)
    print('Saved model.')
    return save_file_path

def train(model: torch.nn.Module, dataloaders: tuple, optimizer: torch.optim,
          config: SimpleNamespace) -> torch.nn.Module:
    """Main training loop along with evaluation procedures.

    Args:
        model (torch.nn.Module): Model to be trained and evaluated.
        dataloaders (tuple): train_dataloader, val_dataloader, test_dataloader
        optimizer (torch.optim): Optimizer to apply gradient updates
        config (SimpleNamespace): Configuration mapping containing model hyperparameters.

    Returns:
        torch.nn.Module: Trained model.
    """
    train_dataloader, val_dataloader, test_dataloader = dataloaders
    running_patience = config.patience
    best_val_loss = float('inf')
    for i in range(1, config.epochs + 1):
        print(f'Epoch number: {i}')
        model, optimizer = train_epoch(model, train_dataloader, optimizer)
        val_loss = evaluate(model, val_dataloader, 'Validation')

        # stopping and saving criterion
        if val_loss < best_val_loss:
            running_patience = config.patience
            best_val_loss = val_loss

            # save the model
            save_file_path = save_model(model, optimizer, i, config)

        else:
            running_patience -= 1
            print(f'Patience at {running_patience}.')
            if running_patience == 0:
                print(
                    f'Model has not improved for {config.patience} epochs. Stopping training.'
                )
                break

        print()

    print('Loading best model from saved checkpoints.')
    state = torch.load(save_file_path)
    model.load_state_dict(state['model'])

    # run final validation eval
    print('Running final evaluations.')
    val_loss = evaluate(model, val_dataloader, 'Validation')

    # run test set eval
    test_loss = evaluate(model, test_dataloader, 'Test', print_report=True)
    return model

def main(args):
    preprocessor = Preprocessor(args.config)

    # TODO: clean this up
    global label_dict
    label_dict = preprocessor.label_dict

    dataloaders = preprocessor.get_train_dataloaders()

    config = preprocessor.config
    config.vocab_size = len(preprocessor.vocab)
    config.num_classes = len(preprocessor.label_dict)

    # create run directory
    runtime = time.strftime('%Y%m%d-%H%M%S')
    config.run_dir = os.path.join(config.model_dir, 'runs', runtime)
    os.makedirs(config.run_dir, exist_ok=True)

    model = LSTM(config)
    optimizer = optim.Adam(model.parameters())

    train(model, dataloaders, optimizer, config)

    # pickle/save the preprocessor
    preprocessor_file_path = os.path.join(config.run_dir,
                                          'preprocessor.pickle')
    with open(preprocessor_file_path, 'wb') as f:
        pickle.dump(preprocessor, f)

    # save the config file
    config_file_path = os.path.join(config.run_dir, 'config.yaml')
    with open(config_file_path, 'w') as f:
        yaml.dump(vars(config), f)

    # sample prediction with reloaded model
    model_file_path = os.path.join(config.run_dir, 'model.pt')
    state = torch.load(model_file_path)
    model = LSTM(config)
    model.load_state_dict(state['model'])

    sample_sentences = [
        'Todd Morrill lives in New York City.',
        'Emily is a math student at Columbia University.',
        'Bob works at the United Nations.'
    ]
    prepared_sentences = preprocessor.preprocess(sample_sentences)

    sample_output = model(prepared_sentences)
    sample_predictions = get_predictions(sample_output,
                                         lengths=prepared_sentences[1],
                                         concatenate=False)
    preds = translate_predictions(sample_predictions,
                                  preprocessor.idx_to_label)
    for idx, sent in enumerate(sample_sentences):
        print(f'Sample sentence:    {sent}')
        print(f'Sample predictions: {preds[idx]}')
        print()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--config',
        type=str,
        help='File path where the model configuration file is located.',
        required=True)

    args = parser.parse_args()
    main(args)

max_stars_repo_name: ToddMorrill/knowledge-graphs

max_issues_repo_name: ToddMorrill/knowledge-graphs

max_forks_repo_name: ToddMorrill/knowledge-graphs


text:
'''Creating the vocabulary'''

import pdb
import pickle
import gc
import copy
import random
from scipy import interp
import numpy as np
import base64
import zlib
import pandas as pd

import sklearn
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.svm import LinearSVC
from sklearn.grid_search import GridSearchCV
from sklearn import cross_validation
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix, precision_recall_fscore_support, accuracy_score, precision_score, recall_score, roc_auc_score, roc_curve, auc
from sklearn.cross_validation import StratifiedKFold

# input: one pandas dataframe and one list(?)
# df1: data to create vocabulary from [ids, labels (int), titles, abstracts]
# df2: stopwords (in list?)
def azureml_main(dataframe1 = None, dataframe2 = None):

	# load in data - articles to create vocabulary from
	myData = np.array(dataframe1)

	# stopwords that we want to exclude
	stopwords = np.array(dataframe2)

	# the ids of the papers that we build the vocabulary from
	ids = myData[:,0]

	# the labels (i.e. Review Groups) that we build the vocabulary from
	labels = myData[:,1].astype(int)

	# list to store all vocabularies
	all_features = []

	# loop over all papers
	for index in range(len(myData)):

		# extract title of paper
		title_tokens = myData[:,2][index]

		# extract abstract of paper
	   	abstract_tokens = myData[:,3][index]

		# save title as ["TI_This", "TI_is", "TI_a", "TI_title"] with stopwords excluded
		features = ["TI_%s" % t for t in title_tokens.split(" ") if not t in stopwords]

		# save abstract as ["AB_This", "AB_is", "AB_an", "AB_abstract"]
	   	features2 = ["AB_%s" % t for t in abstract_tokens.split(" ") if not t in stopwords]

		# combine into one list (and then join as one string)
	   	features.extend(features2)

		all_features.append(" ".join(features))

	# vectorizer object
	# ngram range: consider all 1-to-3-grams
	# only consider 75000 most frequently used words in the vocabulary
	# min_df: a word has to occur in minimum 3 documents
	# strip_accents: remove accents and 'other character normalization'
	vec = TfidfVectorizer(ngram_range=(1,3), max_features=75000, min_df=3, strip_accents='unicode')

	# X is a matrix of shape [len(all_features), len(vocab)] - i.e. [num_of_documents, all_words_in_vocab] ie the term-document matrix
	# each value is the TF-IDF score of each word in the document
   	X = vec.fit_transform(all_features)

	# gets the vocabulary
   	vocab = vec.get_feature_names()

	print(vocab)
   	#vocab_pickled = pickle.dumps(vocab)

	# pickle the vocabulary
   	vocab_pickled = base64.b64encode(zlib.compress(pickle.dumps(vocab)))

	# save the pickled vocabulary in a dataframe
   	output = pd.DataFrame({"vocab_pickled":[vocab_pickled]})

   	return output,

df1 = pd.read_csv('data/dummy_articles_with_classes.csv')
df2 = ['a', 'the']
azureml_main(df1, df2)


text:
import argparse
import pickle
from itertools import product
import numpy as np
import time

from dataStructures.VPForest import VPForest
from dataStructures.VPTreeElement import VPTreeElement
from dataStructures.VPTree import VPTree, VPTreeNode
from dataStructures.VLMCElement import VPTreeVLMC

from util.NN_data import NNData
from util.generateVLMCElements import generate_vlmc_elements, add_generate_vlmc_args

from sklearn.model_selection import RepeatedKFold

def hyper_parameter_test(elements, args):
    """ Perform a test on how the greedy factor effects different metrics

        Input
        -----
        elements - VPTreeVLMC objects that are to be used in the tree, or to be searched for.

        output
        ------
        writes an NNData object to the file location of args.o
    """

    greedy_factors = np.linspace(
        args.greedy_start, args.greedy_end, args.greedy_num_samples
    )
    k_values = np.arange(args.k_start - 1, args.k_end, args.k_step) + 1
    k_values = [int(k) for k in k_values]
    if args.gc_prune_test:
        gc_prune = [True, False]
    else:
        gc_prune = [True]
    if args.forest:
        forest = [True, False]
    else:
        forest = [False]

    all_runs = {}
    factors = [p for p in product(forest, greedy_factors, k_values, gc_prune)]
    for factor in factors:
        all_runs[factor] = []
    all_signatures_used = []
    splitter = RepeatedKFold(args.n_split, args.n_repeat, random_state = args.random_seed)
    i = 0
    for tree_indexes, search_indexes in splitter.split(elements):
        print("current run number:", i)
        i+=1
        tree_elems = elements[tree_indexes]
        search_elems = elements[search_indexes]

        if args.forest:
            forest = VPForest(
                tree_elems, random=args.random_vp, max_leaf_size=args.leaf_size
            )
        tree = VPTree(tree_elems, random=args.random_vp, max_leaf_size=args.leaf_size)
        tree_elem_names = [elem.identifier for elem in tree_elems]
        search_elem_names = [elem.identifier for elem in search_elems]
        all_signatures_used.append((tree_elem_names, search_elem_names))
        start = time.time()
        for factor in factors:
            if factor[0]:
                run_NNS = one_nn_search_run(forest, search_elems, factor, args.parallel)
            else:
                run_NNS = one_nn_search_run(tree, search_elems, factor, args.parallel)
            all_runs[factor].append(run_NNS)

        print("search time:", time.time()-start)
    data = NNData(all_runs, all_signatures_used, factors)
    with open(args.o, "wb") as f:
        pickle.dump(data, f)

def one_nn_search_run(tree, search_elems, factors, parallel):
    if parallel:
        run_NNS = tree.many_nearest_neighbor(
            search_elems, factors[2], factors[1], factors[3], args.pool_size
        )
    else:
        run_NNS = [
            tree.nearest_neighbor(elem, factors[2], factors[1], factors[3])
            for elem in search_elems
        ]
    return run_NNS

parser = argparse.ArgumentParser(description="test args")

parser.add_argument(
    "-o",
    default="hyper_parameter_test.pickle",
    help="output file name of greedy test results",
)
parser.add_argument(
    "--gc_prune_test",
    action="store_true",
    help="should gc distance be used to prune the search results",
)

parser.add_argument("--k_start", type=int, default=1, help="initial value of k")
parser.add_argument("--k_end", default=1, type=int, help="maximum value of k")
parser.add_argument("--k_step", default=1, type=int, help="step size of k")

parser.add_argument(
    "--pool_size",
    type=int,
    default=3,
    help="size of worker pool for parallel execution"
)

parser.add_argument(
    "--greedy_start",
    type=float,
    default=1,
    help="start value of greedy_factor when running greedy__test",
)
parser.add_argument(
    "--random_seed",
    type=int,
    default=None,
    help="the random seed used to split the data",
)
parser.add_argument(
    "--greedy_end",
    type=float,
    default=1,
    help="end value of greedy_factor when running greedy_test",
)
parser.add_argument(
    "--greedy_num_samples",
    type=int,
    default=1,
    help="number of samples to take between greedy_start and greedy_end when running greedy_test",
)

parser.add_argument(
    "--n_repeat",
    type=int,
    default=1,
    help="How many times should the entire dataset be used. Used for sklearn's RepeatedKFold",
)

parser.add_argument(
    "--n_split",
    type=int,
    default=5,
    help="how many portions should the dataset be split into. Used for sklearn's RepeatedKFold"
)

parser.add_argument(
    "--random_vp",
    action="store_true",
    help="Should the vantage point be choosen at random?",
)

parser.add_argument(
    "--leaf_size",
    type=int,
    default=1,
    help="The largest size a leaf may take in the tree",
)
parser.add_argument(
    "--no_randomize_elements",
    action="store_true",
    help="should the elements to be stored/quiered not be randomized",
)
parser.add_argument(
    "--parallel", action="store_true", help="Should the nn be searched for in parallel?"
)

parser.add_argument(
    "--forest", action="store_true", help="Should a VPForest be used instead of VPTree"
)
add_generate_vlmc_args(parser)
args = parser.parse_args()
elements = np.array(generate_vlmc_elements(args))
print("number of VLMC: " + str(len(elements)))
start_time = time.time()
hyper_parameter_test(elements, args)
print("time:", time.time() - start_time)


text:
import numpy as np
import tflite_runtime.interpreter as tflite
from flask import Flask, jsonify, request

MODEL_PATH='covid_19.tflite'

app = Flask(__name__)

@app.route("/covid19", methods=["POST"])
def calculateDeaths():
    print(request)
    cases = request.json['cases']

    interpreter = tflite.Interpreter(model_path=MODEL_PATH)
    interpreter.allocate_tensors()

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    input_data = np.array([[(cases /100000)**3]], dtype=np.float32)
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()

    output_data = interpreter.get_tensor(output_details[0]['index'])
    data = {'cases' : cases, 'deaths': output_data[0][0]}

    return jsonify(str(data))

if __name__ == '__main__':
    app.run(host= '0.0.0.0', port=8080, debug=False)

max_stars_repo_name: Magenic/MMLTensorflow

max_issues_repo_name: Magenic/MMLTensorflow

max_forks_repo_name: Magenic/MMLTensorflow


text:
"""rio_tiler.io.stac: STAC reader."""

from typing import Any, Dict, Sequence, Tuple

import numpy

from rio_tiler import reader
from rio_tiler.errors import InvalidBandName, TileOutsideBounds
from rio_tiler.utils import tile_exists

def _get_href(stac: Dict, assets: Sequence[str]) -> Sequence[str]:
    """Validate asset names and return asset's url."""
    _assets = list(stac["assets"].keys())
    for asset in assets:
        if asset not in _assets:
            raise InvalidBandName(f"{asset} is not a valid asset name.")

    return [stac["assets"][asset]["href"] for asset in assets]

def spatial_info(stac: Dict) -> Dict:
    """
    Return STAC spatial info.

    Attributes
    ----------
        stac : dict
            STAC item.

    Returns
    -------
        out : dict.

    """
    raise Exception("Not implemented")

def bounds(stac: Dict) -> Dict:
    """
    Return STAC bounds.

    Attributes
    ----------
        stac : dict
            STAC item.

    Returns
    -------
        out : dict
            dictionary with image bounds.

    """
    return dict(id=stac["id"], bounds=stac["bbox"])

def metadata(
    stac: Dict,
    assets: Sequence[str],
    pmin: float = 2.0,
    pmax: float = 98.0,
    hist_options: Dict = {},
    **kwargs: Any,
) -> Dict:
    """
    Return STAC assets statistics.

    Attributes
    ----------
        stac : dict
            STAC item.
        assets : list
            Asset names.
        pmin : int, optional, (default: 2)
            Histogram minimum cut.
        pmax : int, optional, (default: 98)
            Histogram maximum cut.
        hist_options : dict, optional
            Options to forward to numpy.histogram function.
            e.g: {bins=20, range=(0, 1000)}
        kwargs : optional
            These are passed to 'rio_tiler.reader.preview'

    Returns
    -------
        out : dict
            Dictionary with image bounds and bands statistics.

    """
    if isinstance(assets, str):
        assets = (assets,)

    assets_url = _get_href(stac, assets)
    responses = reader.multi_metadata(
        assets_url, percentiles=(pmin, pmax), hist_options=hist_options, **kwargs
    )

    info: Dict[str, Any] = dict(id=stac["id"])
    info["band_descriptions"] = [(ix + 1, b) for ix, b in enumerate(assets)]
    info["bounds"] = stac["bbox"]
    info["statistics"] = {b: d["statistics"][1] for b, d in zip(assets, responses)}
    info["dtypes"] = {b: d["dtype"] for b, d in zip(assets, responses)}
    info["nodata_types"] = {b: d["nodata_type"] for b, d in zip(assets, responses)}
    return info

def tile(
    stac: Dict,
    assets: Sequence[str],
    tile_x: int,
    tile_y: int,
    tile_z: int,
    tilesize: int = 256,
    **kwargs: Any,
) -> Tuple[numpy.ndarray, numpy.ndarray]:
    """
    Create mercator tile from any images.

    Attributes
    ----------
        stac : dict
            STAC item.
        assets : list
            Asset names.
        tile_x : int
            Mercator tile X index.
        tile_y : int
            Mercator tile Y index.
        tile_z : int
            Mercator tile ZOOM level.
        tilesize : int, optional (default: 256)
            Output image size.
        kwargs: dict, optional
            These will be passed to the 'rio_tiler.reader.tile' function.

    Returns
    -------
        data : numpy ndarray
        mask: numpy array

    """
    if isinstance(assets, str):
        assets = (assets,)

    if not tile_exists(stac["bbox"], tile_z, tile_x, tile_y):
        raise TileOutsideBounds(
            f"Tile {tile_z}/{tile_x}/{tile_y} is outside item bounds"
        )

    assets_url = _get_href(stac, assets)
    return reader.multi_tile(assets_url, tile_x, tile_y, tile_z, **kwargs)

max_stars_repo_name: mvmendes/rio-tiler

max_issues_repo_name: mvmendes/rio-tiler

max_forks_repo_name: mvmendes/rio-tiler


text:
from numpy import *

class Control:
    """
    Saves the actual trajectory and not just the calculated polynomial coefficients.

    Attributes:
        all paths: path in corresponding dimension
    """
    def __init__(self, waypoints, trajectory):
        self.x_path = []
        self.y_path = []
        self.z_path = []
        self.yaw_path = []

        for i in range(len(trajectory)):
            t = linspace(waypoints[i].time, waypoints[i + 1].time, int((waypoints[i + 1].time - waypoints[i].time) * 20))
            self.x_path = hstack((self.x_path, trajectory[i][0] * t ** 4 + trajectory[i][1] * t ** 3 + trajectory[i][2] * t ** 2 + trajectory[i][3] * t + trajectory[i][4]))
            self.y_path = hstack((self.y_path, trajectory[i][5] * t ** 4 + trajectory[i][6] * t ** 3 + trajectory[i][7] * t ** 2 + trajectory[i][8] * t + trajectory[i][9]))
            self.z_path = hstack((self.z_path, trajectory[i][10] * t ** 4 + trajectory[i][11] * t ** 3 + trajectory[i][12] * t ** 2 + trajectory[i][13] * t + trajectory[i][14]))
            self.yaw_path = hstack((self.yaw_path, trajectory[i][15] * t ** 2 + trajectory[i][16] * t + trajectory[i][17]))

    def nextUpPD(self, step):
        """
        Retrieve the next position to go to.
        """
        return self.x_path[step], self.y_path[step], self.z_path[step], self.yaw_path[step]

max_stars_repo_name: HJoonKwon/MAVeric

max_issues_repo_name: HJoonKwon/MAVeric

max_forks_repo_name: HJoonKwon/MAVeric

reason: from numpy