text:
#!/usr/bin/env python

import os
from uuid import uuid4
import pandas as pd
from numpy import nan

from numpy import linspace
from random import choice
from matplotlib import pyplot as plt
from scipy.stats import beta as b
from textwrap import wrap
import time
beta = b.pdf

# Miremos cuantos casos tiene cada categoria,
# calculado sobre un subset del 5% de SISA
#
#   COLUMNA: nombre        CANTIDAD DE CASOS: numero
#
# COL:  sexo OPTIONS:  3
# COL:  edad OPTIONS:  105
# COL:  edad_años_meses OPTIONS:  2
# COL:  residencia_pais_nombre OPTIONS:  4
# COL:  residencia_provincia_nombre OPTIONS:  25
# COL:  residencia_departamento_nombre OPTIONS:  345
# COL:  carga_provincia_nombre OPTIONS:  24
# COL:  fecha_inicio_sintomas OPTIONS:  131
# COL:  fecha_apertura OPTIONS:  5
# COL:  sepi_apertura OPTIONS:  1
# COL:  fecha_internacion OPTIONS:  80
# COL:  cuidado_intensivo OPTIONS:  2
# COL:  fecha_cui_intensivo OPTIONS:  45
# COL:  fallecido OPTIONS:  2
# COL:  fecha_fallecimiento OPTIONS:  61
# COL:  asistencia_respiratoria_mecanica OPTIONS:  2
# COL:  carga_provincia_id OPTIONS:  24
# COL:  origen_financiamiento OPTIONS:  2
# COL:  clasificacion OPTIONS:  11
# COL:  clasificacion_resumen OPTIONS:  3
# COL:  residencia_provincia_id OPTIONS:  25
# COL:  fecha_diagnostico OPTIONS:  105
# COL:  residencia_departamento_id OPTIONS:  141

# Definimos las comparaciones tipo uno
# como las hechas intra-categoria
# e.g. 'proba en venado tuerto' VS
# 'proba en CABA' (LUGAR vs LUGAR)
#
def first_type(d,cat):
  local_info = {}
  vals = d[cat].unique().tolist()
  if len(vals)<2:
    print('vals are ',vals)
    raise Exception
  A = choice(vals)
  B = updater(A,vals)
  local_info['key1'] = (
                  [A],
                  *fair_diagnostic(d[d[cat]==A]),
                                                 )
  local_info['key2'] = (
                  [B],
                  *fair_diagnostic(d[d[cat]==B]),
                                                 )
  return local_info

# Definimos las comparaciones de segundo tipo
# como las hechas internamente en una categoria
# condicionados a pertenecer a una segunda
# e.g. proba de CABA vs Mendoza para >20
#
def second_type(d,cat1,cat2):
  local_info = {}
  Z = choice(d[cat1].unique().tolist())
  vals = d[d[cat1]==Z][cat2].unique().tolist()
  if len(vals)<2: raise Exception
  A = choice(vals)
  B = updater(A,vals)
  local_info['key1'] = (
                  [Z,A],
                  *fair_diagnostic(d[d[cat2]==A]),
                                                 )
  local_info['key2'] = (
                  [Z,B],
                  *fair_diagnostic(d[d[cat2]==B]),
                                                 )
  return  local_info

def third_type(d,cat1,cat2,cat3):
  local_info = {}
  Z_1 = choice(d[cat1].unique().tolist())
  Z_2 = choice(d[cat2].unique().tolist())
  vals = d[((d[cat1]==Z_1) & (d[cat2]==Z_2))][cat3].unique().tolist()
  if len(vals)<2:
    print('vals are ',vals)
    raise Exception
  A = choice(vals)
  B = updater(A,vals)
  local_info['key1'] = (
                  [Z_1,Z_2,A],
                  *fair_diagnostic(d[d[cat2]==A]),
                                                 )
  local_info['key2'] = (
                  [Z_1,Z_2,B],
                  *fair_diagnostic(d[d[cat2]==B]),
                                                 )
  return local_info

# Definimos un `diagonistoc justo`
# como confirmado o descartado
def fair_diagnostic(d):
  aux_2 = len(d[d['clasificacion_resumen'] == 'Descartado'])
  aux_1 = len(d[d['clasificacion_resumen'] == 'Confirmado'])
  return (aux_1, aux_2)

# Definimos un selector de columnas
# capaz de ignorar las que no esten
# comentadas
def colchus(d):
  FORBIDDEN = [
    'id_evento_caso',
    #'sexo',
    #'edad',
   'edad_años_meses',
    'residencia_pais_nombre',   # este es nuevito lo sacamos jeje
    #'residencia_provincia_nombre',
    #'residencia_departamento_nombre',
    #'carga_provincia_nombre',
     #'fecha_inicio_sintomas',
     'fecha_apertura',
     'sepi_apertura',
      #'fecha_internacion',
       'cuidado_intensivo',
       'fecha_cui_intensivo',
        #'fallecido',
        'fecha_fallecimiento',
         'asistencia_respiratoria_mecanica',
         'carga_provincia_id',
          #'origen_financiamiento',
         'clasificacion',            #CONSIDERAR VOLVER A PONERLO!
         'clasificacion_resumen',
          'residencia_provincia_id',
           'fecha_diagnostico',
           'residencia_departamento_id',
            'ultima_actualizacion']
  answ = choice(d.columns.tolist())
  if answ not in FORBIDDEN: return answ
  else: return colchus(d)

def edad_to_name(s):
  try:
    s=int(float(s))
  except: pass
  return str(s)+' años'

def comuna_to_name(s):
  if s in [f"COMUNA {x:02d}" for x in range(1,16)]:
    return 'la '+s.lower()+' de CABA'
  return s

def date_to_name(s):
  months={'01':'Enero' ,
           '02':'Febrero' ,
           '03':'Marzo' ,
           '04':'Abril' ,
           '05':'Mayo' ,
           '06':'Junio' ,
           '07':'Julio' ,
           '08':'Agosto' ,
           '09':'Septiembre' ,
           '10':'Octubre' ,
           '11':'Noviembre' ,
           '12':'Diciembre' ,
          }
  y,m,d = s.split('-')
  m = months[m]
  return f'{int(d)} de {m}'

def fancy_version(category, value):
  category_mapping = {
  'carga_provincia_nombre':'la provincia en la que que se hizo el test es', #"hizo el test" reemplaza "cargó"
  'fecha_inicio_sintomas':'la fecha en la que comenzaron los síntomas es el',
  'fecha_internacion':'la fecha en la que la persona ha sido internada es el',
  'residencia_departamento_nombre':'el departamento de residencia es',
  'residencia_provincia_nombre':'la provincia de residencia es',
  'origen_financiamiento':'el tipo de lugar en donde se hizo el test es',
  'sexo':'el sexo biológico es',
  'fallecido':'la persona',
  'edad':'la persona tiene',
               }
  fancy_category = category_mapping.get(category,category)

  value_mapping = {
  'carga_provincia_nombre':{
    'CABA':'Capital Federal',
                            },
  'fecha_inicio_sintomas': date_to_name,
  'fecha_internacion': date_to_name,
  'residencia_departamento_nombre':comuna_to_name,
  'origen_financiamiento':{
    'Privado':'una institución privada',
    'Público':'una institución pública',
         },
  'sexo':{
    'F':'femenino',
    'M':'masculino',
         },
  'fallecido':{
    'SI':'ha fallecido',
    'NO':'no ha fallecido',
         },
  'edad':edad_to_name,
  '':{
    '':'',
    '':'',
         },
                 }
  try:
    print('trying to tidy up the value ',value, 'for category ',category)
    fancy_value = value_mapping[category][value]
  except:
    try:
      print('trying one')
      fancy_value=value_mapping[category](value)
    except Exception as ins:
      print(ins.args)
      print('surrendering')
      fancy_value=value

  return fancy_category, fancy_value

def filter_version(category, value):
  print('checking filter for ',category,value)
  filterer = {
  'residencia_provincia_nombre':{
    'SIN ESPECIFICAR':False,
                            },
  'sexo':{
    'NR':False,
         },
  'edad':{
    nan:False,
    **{f'{x_}.0':False for x_ in range(90,110)},
         },
  'origen_financiamiento':{
    '*sin dato*':False,
         },
  'fecha_internacion': {nan:False
         },
  'fecha_internacion': {nan:False
         },
                 }
  if category in filterer:
    veredict = filterer[category].get(value,True)
  else: veredict=True
  return veredict

# protected while
def updater(C,D,flag=False):
  protector = 0
  if isinstance(C,int):
    new=C
    C = (C,)
  else:
    new=C[0]
  while new in C:
    if flag: new = colchus(D)
    else: new = choice(D)
    protector ++ 1
    if protector>8:
      print('ERROR: EXITED LOOP')
      sys.exit(1)
  print('new its different from C: ',new,C)
  return new

# definimos el inicializador
# del .CSV con la data
# Rdo: si no esta en el directorio
# lo podes bajar con
#os.system(
# 'wget https://sisa.msal.gov.ar/datos/descargas/covid-19/files/Covid19Casos.csv');
def initialize(opt=False):
  try:
    op = pd.read_csv('data/smaller.csv')
  except FileNotFoundError:
    if not opt:
      print('Apparently this is the first run...')
      print('creating "smaller.csv" database...')
    try:
      op.to_csv('data/smaller.csv',index=False)
      op = pd.read_csv('data/Covid19Casos.csv',
          sep=',',encoding='utf-8').sample(frac=1).iloc[:50000,:]
    except FileNotFoundError:
      print('No tenes la database en el directorio!'\
            '\nla vamos a descargar...')
      os.system('wget https://sisa.msal.gov.ar/datos/descargas'\
                            '/covid-19/files/Covid19Casos.csv')
      initialize(True)
    print('DONE!')
  return op

def yellow_calculator(k,infor):
  ONE = (k['key1'][-2]+1)/(k['key1'][-2]
                        +k['key1'][-1]+2)
  TWO = (k['key2'][-2]+1)/(k['key2'][-2]
                        +k['key2'][-1]+2)
  if ONE>TWO:
    ind=1
    extra = round(abs(ONE-TWO)/TWO*100,1)
  else:
    ind=2
    extra = round(abs(ONE-TWO)/ONE*100,1)
  # ONLY INTEGER PROBA!
  extra = int(extra)
  q=''
  if len(k['key1'][0])==2:
    if extra!=0:
      msg = f"Cuando {infor[0]} {k['key1'][0][0]}, {q}"\
      f"si {infor[1]} {k[f'key{ind}'][0][1]} {q}la probabilidad "\
        f"de tener coronavirus es {extra}% mayor que si "\
          f"{infor[1]} {k[f'key{ind%2+1}'][0][1]}"
    else:
      msg = f"Cuando {infor[0]} {k['key1'][0][0]}, {q}"\
      f"si {infor[1]} {k[f'key{ind}'][0][1]} {q}la probabilidad de tener "\
        f"coronavirus es igual que si {infor[1]} {k[f'key{ind%2+1}'][0][1]}"

  elif len(k['key1'][0])==1:
    if extra!=0:
      msg =  f"Si {infor[0]} {k[f'key{ind}'][0][0]}, {q}la "\
            f"probabilidad de tener coronavirus es {extra}% mayor que si "\
              f"{infor[0]} {k[f'key{ind%2+1}'][0][0]}"
    else:
      msg =  f"Si {infor[0]} {k[f'key{ind}'][0][0]}, la "\
            f"probabilidad de tener coronavirus es igual que si "\
              f"{infor[0]} {k[f'key{ind%2+1}'][0][0]}"

  elif len(k['key1'][0])==3:
    if extra!=0:
      msg = f"Cuando {infor[0]} {k['key1'][0][0]}, {q}"\
      f"si {infor[1]} {k[f'key{ind}'][0][1]} {q}la probabilidad "\
        f"de tener coronavirus es {extra}% mayor que si "\
          f"{infor[1]} {k[f'key{ind%2+1}'][0][1]}"
    else:
      msg = f"Cuando {infor[0]} {k['key1'][0][0]} y {infor[1]} {k['key1'][0][1]},{q}"\
      f"si {infor[2]} {k[f'key{ind}'][0][2]} {q}la probabilidad de tener "\
        f"coronavirus es igual que si {infor[2]} {k[f'key{ind%2+1}'][0][2]}"

  return msg,ind

def main(**kwargs):

  # Inicializar
  data = initialize()

  # Elegir "nivel de nesteo"
  #
  # (pend expandir: systematic que
  #  funcione para N niveles)

  TIPO = choice([1]*2+[2]*8)
  limit =10
  lap = 0
  if TIPO==1:
    c = colchus(data)
    print('chose column ',c)
    # main TYPE1
    veredict = 0
    while veredict != 2:
      result = first_type(data,c)
      veredict = (int(filter_version(c,result['key1'][0][0])) +
                 int(filter_version(c,result['key2'][0][0])) )
      if veredict!=2:
        print(f'rejected: it was category {c} and cases '\
             f"{result['key1'][0]} and {result['key2'][0]}")
      lap += 1
      if lap>limit: raise Exception
    forward = (c,)
    fancy_cat, fancy_val1_0 = fancy_version(c, result['key1'][0][0])
    result['key1'] = ([fancy_val1_0,],
                       result['key1'][-2],result['key1'][-1],)
    fancy_cat, fancy_val2_0 = fancy_version(c, result['key2'][0][0])
    result['key2'] = ([fancy_val2_0,],
                       result['key2'][-2],result['key2'][-1],)
    forward = (fancy_cat,)

  elif TIPO==2:
    c1 = colchus(data)
    c2 = c1
    c2 = updater(c1,data,True)
    # SE TILDO CON residencia_pais_nombre  and  clasificacion
    # SE TILDO CON carga_provincia_nombre  and  fallecido
    # SE TILDO CON residencia_departamento_nombre  and  fallecido
    print('chose columns ',c1,' and ',c2)
    # main TYPE2
    veredict = 0
    limit =10
    lap = 0
    while veredict != 4:
      result = second_type(data,c1,c2)
      veredict = (int(filter_version(c1,result['key1'][0][0])) +
                 int(filter_version(c1,result['key2'][0][0])) +
                 int(filter_version(c2,result['key1'][0][1])) +
                 int(filter_version(c2,result['key2'][0][1])))
      if veredict!=4:
        print(f'rejected: they were categories {c1} and {c2} cases '\
             f"\nfor the first:{result['key1'][0]} and {result['key2'][0]}"\
             f"\nfor the second:{result['key1'][1]} and {result['key2'][1]}")
      # FILTER: c1&c2 cant be "residencia_departamento_nombre" and "residencia_provincia_nombre"
      if c1 in ["residencia_departamento_nombre", "residencia_provincia_nombre"]:
        if c2 in ["residencia_departamento_nombre", "residencia_provincia_nombre"]:
          veredict=0
      lap += 1
      if lap>limit: raise Exception
    forward = (c1,c2)
    print('ENTERED WITH SHAPE,LEN', len(result['key2']))

    fancy_cat_1, fancy_val1_0 = fancy_version(c1, result['key1'][0][0])
    fancy_cat_2, fancy_val1_1 = fancy_version(c2, result['key1'][0][1])
    result['key1'] = ([fancy_val1_0, fancy_val1_1],
                       result['key1'][-2],result['key1'][-1],)
    fancy_cat_1, fancy_val2_0 = fancy_version(c1, result['key2'][0][0])
    fancy_cat_2, fancy_val2_1 = fancy_version(c2, result['key2'][0][1])
    result['key2'] = ([fancy_val2_0, fancy_val2_1],
                       result['key2'][-2],result['key2'][-1],)

    forward = (fancy_cat_1, fancy_cat_2)
    print('EXITTED WITH SHAPE,LEN', len(result['key2']))
    #fancy_version(category, value): return fancy_category, fancy_value

  elif TIPO==3:
    c1 = colchus(data)
    c2 = c1
    c2 = updater(c1,data,True)
    c3 = updater((c1,c2),data,True)
    print('chose columns ',c1,', ',c2,' and ',c3)
    # main TYPE2
    veredict = 0
    limit =10
    lap = 0
    while veredict != 6:
      result = third_type(data,c1,c2,c3)
      veredict = (int(filter_version(c1,result['key1'][0][0])) +
                 int(filter_version(c1,result['key2'][0][0])) +
                 int(filter_version(c2,result['key1'][0][1])) +
                 int(filter_version(c1,result['key2'][0][1])) +
                 int(filter_version(c2,result['key1'][0][2])) +
                 int(filter_version(c2,result['key2'][0][2])))
      if veredict!=6:
        print(f'rejected: they were categories {c1} and {c2} cases '\
             f"\nfor the first:{result['key1'][0]} and {result['key2'][0]}"\
             f"\nfor the second:{result['key1'][1]} and {result['key2'][1]}"\
             f"\nfor the third:{result['key1'][2]} and {result['key2'][2]}")
      if c1 in ["residencia_departamento_nombre", "residencia_provincia_nombre"]:
        if c2 in ["residencia_departamento_nombre", "residencia_provincia_nombre"]:
          veredict=0
        elif c3 in ["residencia_departamento_nombre", "residencia_provincia_nombre"]:
          veredict=0
      elif c2 in ["residencia_departamento_nombre", "residencia_provincia_nombre"]:
        if c3 in ["residencia_departamento_nombre", "residencia_provincia_nombre"]:
          veredict=0
      lap += 1
      if lap>limit: raise Exception
    forward = (c1,c2,c3)
    print('ENTERED WITH SHAPE,LEN', len(result['key2']))

    fancy_cat_1, fancy_val1_0 = fancy_version(c1, result['key1'][0][0])
    fancy_cat_2, fancy_val1_1 = fancy_version(c2, result['key1'][0][1])
    fancy_cat_3, fancy_val1_2 = fancy_version(c3, result['key1'][0][2])
    result['key1'] = ([fancy_val1_0, fancy_val1_1, fancy_val1_2],
                       result['key1'][-2],result['key1'][-1],)
    fancy_cat_1, fancy_val2_0 = fancy_version(c1, result['key2'][0][0])
    fancy_cat_2, fancy_val2_1 = fancy_version(c2, result['key2'][0][1])
    fancy_cat_3, fancy_val2_2 = fancy_version(c3, result['key2'][0][2])
    result['key2'] = ([fancy_val2_0, fancy_val2_1, fancy_val2_2],
                       result['key2'][-2],result['key2'][-1],)

    forward = (fancy_cat_1, fancy_cat_2, fancy_cat_3)
    print('EXITTED WITH SHAPE,LEN', len(result['key2']))
    #fancy_version(category, value): return fancy_category, fancy_value

  f,ax = plt.subplots(1,
                    #dpi=200,
                    figsize=(21,7))
  amarillista,bigger_ind = yellow_calculator(result,forward)
  title=plot_from_keys(ax,result,forward,bigger_ind)

  new_name = str(uuid4())
  if kwargs.get('clean',True):
    for x in os.listdir('static'):
      if x[-4:]=='.png' and x!=new_name+'.png':
        if x!='cuidados.png' and x!='cuidadosB.png':
          os.remove('static/'+x)

  if kwargs.get('view',False):
    #add figtext
    plt.figtext(0.5, 0.02, amarillista,
               ha="center", fontsize=12,
              bbox={"facecolor":"orange",
                    "alpha":0.5, "pad":5})
    plt.title(title,fontsize=8)

  # Mostrar!
  #plt.tight_layout()
  #plt.show()
  print('current dir isSS', os.getcwd())
  with open('static/message.txt','w') as f:
    f.write(amarillista)
                                                   #f5f5f5 <-- light  #363636<--dark
  plt.savefig(f'static/{new_name}.png', facecolor='#f5f5f5', transparent=True,bbox_inches='tight')
  with open('static/image.txt','w') as f:
    f.write(new_name)

  # Fin
  return print('ENDED!')

def plot_from_keys(axy,k,cate, bigger_ind):
  # utils
  xs = linspace(0,1,500)

  TYPE = len(k['key1'][0])

  # title is forked 1 vs 2
  if TYPE==1:
    title = f'Funcion de Densidad de Probabilidad para la "probabilidad de contagio":\n'\
         f"SI ''{cate[0]} {k['key1'][0][0]}'' \nVS\n SI ''{cate[0]} {k['key2'][0][0]}''"\

  elif TYPE==2:
    title = f'Funcion de Densidad de Probabilidad para la "probabilidad de contagio":\n '\
         f"SI ''{cate[0]} {k['key1'][0][0]}'' para los casos especificos\n"\
         f"SI ''{cate[1]} {k['key1'][0][1]}'' \nVS\n SI ''{cate[1]} {k['key2'][0][1]}''"\
         f"SI ''{cate[1]} {k['key1'][0][1]}'' \nVS\n SI ''{cate[1]} {k['key2'][0][1]}''"
  elif TYPE==3:
    title = f'Funcion de Densidad de Probabilidad para la "probabilidad de contagio":\n '\
         f"SI ''{cate[0]} {k['key1'][0][0]}'' y también \n"\
         f"SI ''{cate[1]} {k['key1'][0][1]}'' para los casos especificos\n"\
         f"SI ''{cate[2]} {k['key1'][0][2]}'' \nVS\n SI ''{cate[1]} {k['key2'][0][2]}''"\
         f"SI ''{cate[2]} {k['key1'][0][2]}'' \nVS\n SI ''{cate[1]} {k['key2'][0][2]}''"

  # Effective plotting
  import matplotlib.patheffects as path_effects
  colorer={f'key{bigger_ind%2+1}':'#d0581c' ,f'key{bigger_ind}':'#98a3a5'}
  for x in k.keys():
    axy.plot(
      xs,
      beta(
          xs,
          a=k[x][-2]+1,
          b=k[x][-1]+1 ),
      #label=' '.join([str(y) for y in k[x][0]]),
      label=k[x][0][-1],
          lw=6,ls='-',c=colorer[x],
          path_effects=[path_effects.SimpleLineShadow(),
          path_effects.Normal()])

  #f5f5f5 <-- light  #363636<--dark
  #axy.grid(color='#f5f5f5', linestyle='-', linewidth=5)
  with open('static/title.txt','w') as f:
    f.write(title)
  axy.get_yaxis().set_visible(False)
  from matplotlib.spines import Spine
  for child in axy.get_children():
    if isinstance(child, Spine):
      child.set_color('#363636')
      child.set_linewidth(10)
  axy.tick_params(axis='x', colors='#363636', width=10)
  #axy.set_title(title,fontsize=8)

  # Visual specs
  if TYPE==1:                      #34
    axy.legend(loc=1,title='\n'.join(wrap(cate[0],33)).capitalize(),
                  title_fontsize=18,fontsize=17,prop={'size': 17})#background_color='#f5f5f5'
  elif TYPE==2:
    axy.legend(loc=1,title='\n'.join(wrap(cate[0]+' '+k['key1'][0][0]+' y '+cate[1],33)).capitalize(),fontsize=17,
                  title_fontsize=17,prop={'size': 17,})#background_color='#f5f5f5'
  elif TYPE==3:
    axy.legend(loc=1,
           title='\n'.join(wrap(cate[0]+' '+k['key1'][0][0]+
                                ', '+cate[1]+' '+k['key1'][0][1]+
                                 ' y '+cate[2],33)).capitalize(),
                                     fontsize=17,
                  title_fontsize=17,prop={'size': 17,})#background_color='#f5f5f5'
  axy.set_xticks([0,0.25,0.5,0.75,1])
  axy.set_xticklabels(['0','25%','50%','75%','100%'],fontsize=30)
  #axy.set_xlim(0,1)

  return title

if __name__=='__main__':
  counter = 0
  import sys
  if len(sys.argv)==2:
    main()
    print('done!')
    sys.exit(1)
  while True:
    print(f'VUELTA NRO {counter}\nsi tarda mas de 2 segs'\
      ' se ha colgado\ndale reset...\n')
    main(clean=True, view=False)
    plt.show()
    time.sleep(3)
    counter += 1

max_stars_repo_name: GastonMazzei/covidarg-fake-news

max_issues_repo_name: GastonMazzei/covidarg-fake-news

max_forks_repo_name: GastonMazzei/covidarg-fake-news

reason: from numpy,from scipy


text:
import warnings
from functools import reduce
import math
import numpy
import operator
import time

from Bio.PDB.PDBExceptions import PDBConstructionWarning
from pandas import read_csv
from Bio.PDB import Atom, PDBParser, Superimposer

# class Atom:
#     def __init__(self, name: str, x: float, y: float, z: float):
#         self.name = name
#         self.coords = [x, y, z]
#         self.element = name[0]
#
#     def __eq__(self, other):
#         return self.name == other.name and self.coords == other.coords
#
#     def __lt__(self, other):
#         return self.element < other.element or self.name < other.name or \
#                self.coords[0] < other.coords[0] or self.coords[1] < other.coords[1] or \
#                self.coords[2] < other.coords[2]
#
#     def __hash__(self):
#         return self.element.__hash__() + self.name.__hash__() + \
#                self.coords[0].__hash__() + self.coords[1].__hash__() + self.coords[2].__hash__()
#
#     def distance_to(self, other_atom) -> float:
#         squared_distance = (self.coords[0] - other_atom.coords[0]) ** 2 + \
#                            (self.coords[1] - other_atom.coords[1]) ** 2 + \
#                            (self.coords[2] - other_atom.coords[2]) ** 2
#         return math.sqrt(squared_distance)
#
#     def __repr__(self):
#         return f'{self.name}:[{self.coords[0]: 0.3f} {self.coords[1]: 0.3f} {self.coords[2]: 0.3f}]'

class Pattern:
    def __init__(self, pdb: str, name: str, sequence: str, atoms: list):
        self.pdb = pdb
        self.name = name
        self.sequence = sequence
        # ugly code to remove duplicates from atoms
        self.atoms = []
        for new_atom in atoms:
            already_added = False
            for atom in self.atoms:
                if all(atom.coord == new_atom.coord):
                    already_added = True
                    break
            if not already_added:
                self.atoms.append(new_atom)

    def __repr__(self):
        return 'Pattern { ' + \
               f'pdb: {self.pdb}, ' + \
               f'name: {self.name}, ' + \
               f'sequence: {self.sequence}, ' + \
               f'atoms: {self.atoms} ' + \
               '}'

    def sort_atoms(self, element_process_priority: list):
        self.atoms = sorted(self.atoms, key=lambda a: element_process_priority.index(a.get_id()[0]))

class Match:
    def __init__(self, matched_pattern_atoms: list, matched_inspecting_atoms: list):
        self.matched_pattern_atoms = matched_pattern_atoms
        self.matched_inspecting_atoms = matched_inspecting_atoms
        self.calculate_average_distance_error()

    def generate_plus_atom_matches(self, next_pattern_atom: Atom, inspecting_atoms: list,
                                   distance_error_coefficient: float) -> list:
        pattern_atoms = self.matched_pattern_atoms + [next_pattern_atom]
        pattern_atoms_distances = [numpy.linalg.norm(atom.coord - next_pattern_atom.coord) for atom in self.matched_pattern_atoms]

        matches = list()
        for atom in inspecting_atoms:
            # skip atom that is already presented in matched atoms
            if atom in self.matched_inspecting_atoms:
                continue

            # skip non-compatible atoms
            if atom.get_id()[0] != next_pattern_atom.get_id()[0]:
                continue

            # skip atoms if they do not fit by distance
            all_distances_are_acceptable = True
            for atom_index in range(len(pattern_atoms_distances)):
                acceptable_distance = pattern_atoms_distances[atom_index]
                if not distance_is_acceptable(self.matched_inspecting_atoms[atom_index], atom, acceptable_distance, distance_error_coefficient):
                    all_distances_are_acceptable = False
                    break

            if all_distances_are_acceptable:
                matches.append(Match(pattern_atoms, self.matched_inspecting_atoms + [atom]))
        return matches

    def calculate_average_distance_error(self):
        # superimposer = Superimposer()
        # superimposer.set_atoms(self.matched_pattern_atoms, self.matched_inspecting_atoms)
        # superimposer.rms
        distance_errors = list()
        for first_atom_index in range(len(self.matched_pattern_atoms)):
            for second_atom_index in range(first_atom_index+1, len(self.matched_pattern_atoms)):
                pattern_distance = numpy.linalg.norm(self.matched_pattern_atoms[first_atom_index] - self.matched_pattern_atoms[second_atom_index])
                inspecting_distance = numpy.linalg.norm(self.matched_inspecting_atoms[first_atom_index] - self.matched_inspecting_atoms[second_atom_index])
                distance_errors.append(math.fabs(inspecting_distance / pattern_distance - 1.0))
        self.average_distance_error = reduce(operator.add, distance_errors, 0) / len(self.matched_pattern_atoms)

    def __repr__(self):
        return 'Match { ' + \
            f'matched_pattern_atoms: {[f"{a.get_id()}:{a.coord}" for a in self.matched_pattern_atoms]}, ' + \
            f'matched_inspecting_atoms: {[f"{a.get_id()}[{a.get_parent().get_id()[1]}]:{a.coord}" for a in self.matched_inspecting_atoms]}, ' + \
            f'average_distance_error: {self.average_distance_error} ' + \
            '}'

def distance_is_acceptable(atom_a: Atom, atom_b: Atom, acceptable_distance: float,
                           distance_error_coefficient: float) -> bool:
    distance = numpy.linalg.norm(atom_a.coord - atom_b.coord)
    distance_fraction = distance / acceptable_distance

    return 1 - distance_error_coefficient <= distance_fraction <= 1 + distance_error_coefficient

# returns list of Bio.PDB.Atom objects
def get_inspecting_atoms(path: str, element_process_priority: list) -> list:
    structure = PDBParser(QUIET=True).get_structure('structure', 'Data/CHO_surface.pdb')

    # get Bio.PDB.Atom objects
    atoms = [a for a in structure.get_atoms()]
    # remove unknown atoms
    atoms = [a for a in atoms if a.get_id()[0] in element_process_priority]
    # sort atoms by element process priority
    atoms = sorted(atoms, key=lambda a: element_process_priority.index(a.get_id()[0]))

    return atoms

# returns list of Atom objects
def get_pattern_atoms(atoms_string: str, element_process_priority: list) -> list:
    if atoms_string == 'FILE_NOT_FOUND':
        return list()

    atoms = list()

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always", PDBConstructionWarning)
        for atom_string in atoms_string.split(', '):
            components = atom_string.split(':')
            name = components[0]
            coords_string = components[1][1:-1]
            coords = [float(x) for x in coords_string.split(' ') if x != '']
            atoms.append(Atom.Atom(name, numpy.asarray(coords), None, None, name, name, None))
    atoms = sorted(atoms, key=lambda a: element_process_priority.index(a.get_id()[0]))

    return atoms

def create_pattern_from_row(row, element_process_priority: list) -> Pattern:
    pdb = row['PDB_code']
    name = row['Pattern_name']
    sequence = row['Pattern_sequence']
    atoms = get_pattern_atoms(row['Contacts'], element_process_priority)

    return Pattern(pdb=pdb, name=name, sequence=sequence, atoms=atoms)

# returns list of Match objects
def find_pattern_matches(pattern: Pattern, inspecting_atoms: list, element_process_priority: list,
                         distance_error_coefficient: float = 0.1) -> list:
    matches = list()

    pattern_atom_count = len(pattern.atoms)
    print(f'atoms in pattern: {pattern_atom_count}')

    if pattern_atom_count >= 1:
        first_pattern_atom = pattern.atoms[0]
        matches = [Match([first_pattern_atom], [a]) for a in inspecting_atoms if
                            a.get_id()[0] == pattern.atoms[0].get_id()[0]]
        print(f'1-atom matches: {len(matches)}')

        for atom_index in range(1, len(pattern.atoms)):
            next_pattern_atom = pattern.atoms[atom_index]
            next_matches = reduce(operator.add, [
                match.generate_plus_atom_matches(next_pattern_atom, inspecting_atoms, distance_error_coefficient)
                for match in matches
            ], [])
            print(f'{atom_index+1}-atoms matches: {len(next_matches)}')
            matches = next_matches

    return matches

if __name__ == '__main__':
    start_time = time.time()

    element_process_priority = ['S', 'N', 'O', 'C', 'H', 'P', 'F', 'B']
    patterns = read_csv('pdb-hits.extended.5.csv')

    inspecting_atoms = get_inspecting_atoms('Data/CHO_surface.pdb', element_process_priority)
    total_matches = []

    for row_index, pattern_row in patterns.iterrows():
        pattern = create_pattern_from_row(pattern_row, element_process_priority)
        if len(pattern.atoms) == 0:
            total_matches.append([])
            continue
        print(f'processing pattern {pattern.name} from {pattern.pdb}')
        matches = find_pattern_matches(pattern, inspecting_atoms, element_process_priority)
        total_matches.append(matches)

        print(f'{row_index+1} / {patterns.shape[0]}')

    patterns['Matches'] = total_matches
    patterns.to_csv('matches.csv')

    print(f'done in {time.time() - start_time:0.3} seconds')

ext: py

max_stars_repo_name: zedrian/smat

max_issues_repo_name: zedrian/smat

max_forks_repo_name: zedrian/smat

reason: import numpy


text:
from PIL import Image, ImageDraw, ImageFilter, ImageOps
import imagehash
import struct
import json
import hashlib
import numpy as np
from sqlalchemy import text

def ensure_pil(im):
  """Ensure image is Pillow format"""
  try:
    im.verify()
    return im
  except:
    return Image.fromarray(im.astype('uint8'), 'RGB')

def compute_ahash(im):
  """Compute average hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.average_hash(ensure_pil(im_pil))

def compute_phash(im):
  """Compute perceptual hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.phash(ensure_pil(im))

def phash2int(phash):
  """Compute perceptual hash using ImageHash library and convert to binary
    :param phash: Imagehash.ImageHash
    :returns: binary-encoded bigint
  """
  phash.hash[-1] = False
  phash_as_bigint = struct.unpack('Q', np.packbits(phash.hash))[0]
  return phash_as_bigint

def compute_phash_int(im):
  """Compute perceptual hash using ImageHash library and convert to binary
    :param im: Numpy.ndarray
    :returns: binary-encoded bigint
  """
  return phash2int(compute_phash(im))

def compute_dhash(im):
  """Compute difference hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.dhash(ensure_pil(im))

def compute_whash(im):
  """Compute wavelet hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.whash(ensure_pil(im))

def compute_whash_b64(im):
  """Compute wavelest hash base64 using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return lambda im: imagehash.whash(ensure_pil(im), mode='db4')

def search_by_phash(session, phash, threshold=6, limit=1, offset=0, filter={}):
  cmd = """
    SELECT * FROM (
      SELECT images.*, BIT_COUNT(phash # :phash)
      AS hamming_distance FROM images
    ) f
    WHERE hamming_distance < :threshold
    AND context @> (:filter)::jsonb
    ORDER BY hamming_distance ASC
    LIMIT :limit
    OFFSET :offset
  """
  matches = session.execute(text(cmd), {
    'phash': phash,
    'threshold': threshold,
    'limit': limit,
    'offset': offset,
    'filter': json.dumps(filter)
  }).fetchall()
  keys = ('id', 'sha256', 'phash', 'ext', 'url', 'context', 'score')
  results = [ dict(zip(keys, values)) for values in matches ]
  return results

def sha256_stream(stream, block_size=65536):
  """Generates SHA256 hash for a file stream (from Flask)
  :param fp_in: (FileStream) stream object
  :param block_size: (int) byte size of block
  :returns: (str) hash
  """
  sha256 = hashlib.sha256()
  for block in iter(lambda: stream.read(block_size), b''):
    sha256.update(block)
  return sha256.hexdigest()


text:
import cv2
import numpy as np
import dlib, os
from imutils import face_utils
from imutils.face_utils import FaceAligner
from keras.models import load_model
from preprocess_img import create_mask, get_bounding_rect
from blend import blend

CNN_MODEL = 'cnn_model_keras.h5'
SHAPE_PREDICTOR_68 = "shape_predictor_68_face_landmarks.dat"

cnn_model = load_model(CNN_MODEL)
shape_predictor_68 = dlib.shape_predictor(SHAPE_PREDICTOR_68)
detector = dlib.get_frontal_face_detector()

cam = cv2.VideoCapture(1)
if cam.read()[0]==False:
	cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
fa = FaceAligner(shape_predictor_68, desiredFaceWidth=250)

def get_emojis():
	emojis_folder = 'emojis/'
	emojis = []
	for emoji in range(len(os.listdir(emojis_folder))):
		print(emoji)
		emojis.append(cv2.imread(emojis_folder+str(emoji)+'.png', -1))
	return emojis

def get_image_size():
	img = cv2.imread('dataset/0/100.jpg', 0)
	return img.shape

image_x, image_y = get_image_size()

def keras_process_image(img):
	img = cv2.resize(img, (image_x, image_y))
	img = np.array(img, dtype=np.float32)
	img = np.reshape(img, (1, image_x, image_y, 1))
	return img

def keras_predict(model, image):
	processed = keras_process_image(image)
	pred = model.predict(processed)
	pred_probab = pred[0]
	pred_class = list(pred_probab).index(max(pred_probab))
	return max(pred_probab), pred_class

def fun_util():
	emojis = get_emojis()
	disp_probab, disp_class = 0, 0
	while True:
		img = cam.read()[1]
		img = cv2.flip(img, 1)
		gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
		faces = detector(gray)
		if len(faces) > 0:
			for i, face in enumerate(faces):
				shape_68 = shape_predictor_68(img, face)
				shape = face_utils.shape_to_np(shape_68)
				mask = create_mask(shape, img)
				masked = cv2.bitwise_and(gray, mask)
				maskAligned = fa.align(mask, gray, face)
				faceAligned = fa.align(masked, gray, face)
				(x0, y0, x1, y1) = get_bounding_rect(maskAligned)
				faceAligned = faceAligned[y0:y1, x0:x1]
				faceAligned = cv2.resize(faceAligned, (100, 100))
				(x, y, w, h) = face_utils.rect_to_bb(face)
				#cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 0), 2)
				cv2.imshow('faceAligned', faceAligned)
				cv2.imshow('face #{}'.format(i), img[y:y+h, x:x+w])
				pred_probab, pred_class = keras_predict(cnn_model, faceAligned)
				img = blend(img, emojis[pred_class], (x, y, w, h))
		cv2.imshow('img', img)
		if cv2.waitKey(1) == ord('q'):
			break

keras_predict(cnn_model, np.zeros((100, 100, 1), dtype=np.uint8))
fun_util()

max_stars_repo_name: pankajkumarkbn/emojify

max_issues_repo_name: pankajkumarkbn/emojify

max_forks_repo_name: pankajkumarkbn/emojify


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 19 16:33:55 2018

@author: mrich
"""

from LossEventClassv2 import *
from LossSimClass import LossSim
from LossCurvePlot import plotCurves

from numpy.random import uniform

riskyEvent = decomposedRisk("Risky event", lambda foo=1:uniform(.01,.05))
riskyEvent.attachEvent(costOfLaborValue("Executive labor", LogNormalValue(40, 1, 60), LogNormalValue(200, 100, 300)))
riskyEvent.attachEvent(costOfLaborValue("Line Worker", LogNormalValue(3, 1), ConstantValue(18.50)))
riskyEvent.attachEvent(LogNormalValue(250000, 10000)) # Cost of IT equipment replacement

sim = LossSim()
replot = sim.replot

sim.attachEvent(riskyEvent)

sim.run(presentation=True, iterations=10000, aggregate=False)

max_stars_repo_name: richmr/QuantitativeRiskSim

max_issues_repo_name: richmr/QuantitativeRiskSim

max_forks_repo_name: richmr/QuantitativeRiskSim

reason: from numpy


text:
from functools import lru_cache
from itertools import combinations, count
from typing import Any, Dict, List, Generator

from .reader import SpreadsheetReader, InvalidFileException

import numpy as np
import pyperclip

HELP = (
    "ARROWS / hjkl - Move",
    "<TAB> <SHIFT-TAB> / n <SHIFT n> - Switch sheet",
    "v - Visual/selection mode",
    "<ESC> - Exit visual/selection mode",
    "c / y - Copy selected cell(s)",
    "q - Exit",
    "? - Show this message",
)

def read_spreadsheet(
    filename: str, delimiter: str, fillna: Any, encoding: str
) -> Dict[str, np.ndarray]:
    return SpreadsheetReader(filename, delimiter, fillna, encoding).read()

@lru_cache()
def get_alphas(n: int) -> List[str]:
    alphaiter = get_alpha_iter()
    return [next(alphaiter) for _ in range(n)]

def copy(obj: Any):
    if isinstance(obj, np.ndarray):
        string = array_to_string(obj)
    else:
        string = str(obj)
    pyperclip.copy(string)

def get_alpha_iter() -> Generator[str, None, None]:
    alpha = range(65, 91)
    for i in count(1):
        chars = combinations(alpha, i)
        for ch in chars:
            yield "".join(map(chr, ch))

def array_to_string(mat: np.ndarray) -> str:
    return "\n".join(",".join(map(str, e)) for e in mat)


text:
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import hydra
from hydra.utils import instantiate
import logging
from overlap.train_net import train_net
from overlap.test_net import test_net
from overlap.test_corrupt_net import test_corrupt_net
import numpy as np
import torch
import pickle

log = logging.getLogger(__name__)

@hydra.main(config_path="conf/test_cifar10.yaml")
def train(cfg):

    np.random.seed(cfg.rng_seed)
    torch.manual_seed(cfg.rng_seed)

    log.info(cfg.pretty())
    model = instantiate(cfg.model).cuda()
    test_dataset = instantiate(cfg.test)

    checkpoint = torch.load(cfg.weights, map_location='cpu')
    model.load_state_dict(checkpoint['model_state'])

    err = test_net(model=model,
            test_dataset=test_dataset,
            batch_size=cfg.test.batch_size,
            loader_params=cfg.data_loader)

    test_corrupt_net(model=model,
            corrupt_cfg=cfg.corrupt,
            batch_size=cfg.corrupt.batch_size,
            loader_params=cfg.data_loader,
            aug_string=cfg.corrupt.aug_string,
            clean_err=err,
            mCE_denom=cfg.corrupt.mCE_baseline_file)

if __name__=="__main__":
    train()


text:
import pandas as pd
import utils as ut
from training import train_model
from pathlib import Path
import argparse
import numpy as np
from sklearn.model_selection import StratifiedShuffleSplit

def parse_arguments():
    """ Parses program's arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument('--demonstration', type=int, default=1)
    parser.add_argument('--model_path', type=Path, default=Path('./model'))
    parser.add_argument('--data_path', type=Path, default=Path('./data/tc_dataset_processed_annotated'))
    parser.add_argument('--epochs', type=int, default=100)
    parser = parser.parse_args()
    return parser

if __name__ == '__main__':
    args = parse_arguments()
    if args.demonstration:
        # For demonstration purposes we use dummy data and the same training/testing.
        TRAIN_DATA = [
            ('Who is Nishanth?', {
                'entities': [(7, 15, 'PERSON')]
            }),
            ('Who is Kamal Khumar?', {
                'entities': [(7, 19, 'PERSON')]
            }),
            ('I like London and Berlin.', {
                'entities': [(7, 13, 'LOC'), (18, 24, 'LOC')]
            })
        ]

        TRAIN_DICT = {'Who is Nishanth?': {
            'entities': [(7, 15, 'PERSON')]
        },
            'Who is Kamal Khumar?': {
                'entities': [(7, 19, 'PERSON')]
            },
            'I like London and Berlin.': {
                'entities': [(7, 13, 'LOC'), (18, 24, 'LOC')]
            }
        }
        nlp = train_model(TRAIN_DATA, 100)
        nlp.to_disk(args.model_path)
        result = ut.eval_model(args.model_path, TRAIN_DICT, TRAIN_DATA)
        print(result)
    else:
        # Read data
        data = pd.read_csv(args.data_path)
        # Split data on whether they had a product in the recall or not.
        X = [i for i in range(0, len(data))]
        Y = [1 if row['annotation'] != str((0, 0, '')) else 0 for index, row in data.iterrows()]

        X = np.asarray(X)
        Y = np.asarray(Y)

        sss = StratifiedShuffleSplit(n_splits=5, test_size=0.3, random_state=0)
        sss.get_n_splits(X, Y)

        for train_index, test_index in sss.split(X, Y):
            # Create data in the required format
            TRAIN_DATA = ut.set_data_list(data, train_index)
            TEST_DICT = ut.set_data_dict(data, test_index)
            TEST_DATA = ut.set_data_list(data, test_index)
            # Train Model
            nlp = train_model(TRAIN_DATA, 100)
            # Save Model
            nlp.to_disk(args.model_path)
            # Evaluate Model
            result = eval_model(TEST_DICT, TEST_DATA)
            print(result)


text:
import numpy as np
import matplotlib
from ..config import mycfg
from ..config import ConfigDescriptor as cfgdec
import interactive
import copy
import history
from .. import specwarnings
from astropy import log

interactive_help_message = """
(1) Left-click or press 1 (one) at two positions to select or add to the baseline fitting range - it will be
highlighted in green if the selection is successful.
    You can select regions to e/x/clude by pressing 'x' at two positions
(2) Middle or right click or press '2','m', '3', or 'd' to /d/isconnect and perform the fit.
    If you press '2','m', or middle-click, the baseline will be subtracted
    If you press '3','d', or right-click, the baseline will be plotted but not subtracted
"""

class Baseline(interactive.Interactive):
    """
    Class to measure and subtract baselines from spectra.

    While the term 'baseline' is generally used in the radio to refer to
    broad-band features in a spectrum not necessarily associated with a source,
    in this package it refers to general continuum fitting.  In principle,
    there's no reason to separate 'continuum' and 'spectral feature' fitting
    into different categories (both require some model, data, and optional
    weights when fitting).  In practice, however, 'continuum' is frequently
    something to be removed and ignored, while spectral features are the
    desired measurable quantity.  In order to accurately measure spectral
    features, it is necessary to allow baselines of varying complexity.

    The Baseline class has both interactive and command-based data selection
    features.  It can be used to fit both polynomial and power-law continua.
    Blackbody fitting is not yet implemented [12/21/2011].  Baseline fitting is
    a necessary prerequisite for Equivalent Width measurement.

    As you may observe in the comments on this code, this has been one of the
    buggiest and least adequately tested components of pyspeckit.  Bug reports
    are welcome.  (as of 1/15/2012, a major change has probably fixed most of
    the bugs, and the code base is much simpler)
    """
    def __init__(self, Spectrum):
        super(Baseline, self).__init__(Spectrum,
                interactive_help_message=interactive_help_message)
        self.baselinepars  = None
        self.order = None
        self.basespec = np.zeros(Spectrum.data.shape[0])
        #self.excludemask = np.zeros(Spectrum.data.shape[0],dtype='bool')
        self.OKmask = np.ones(Spectrum.data.shape[0],dtype='bool')
        #self.Spectrum = Spectrum
        self.Spectrum.plotter = Spectrum.plotter
        self.blleg = None
        self.click = 0
        #self.nclicks_b1 = 0
        #self.fitregion=[]
        #self.excludevelo = []
        #self.excludepix  = []
        self.subtracted = False
        #self.xmin = 0
        #self.xmax = Spectrum.data.shape[0] - 1
        #self.include = [self.xmin,self.xmax]
        #self.includevelo = [Spectrum.xarr[self.xmin],Spectrum.xarr[self.xmax]]
        self.powerlaw=False
        self.spline=False
        self._xfit_units = None
        self._plots = []

    @cfgdec
    def __call__(self, order=1, excludefit=False, save=True, exclude=None,
                 exclusionlevel=0.01, interactive=False, debug=False,
                 LoudDebug=False, fit_original=True,
                 baseline_fit_color='orange', clear_all_connections=True,
                 fit_plotted_area=True, highlight_fitregion=False,
                 reset_selection=False, subtract=True, selectregion=True,
                 **kwargs):
        """
        Fit and remove a polynomial from the spectrum.
        It will be saved in the variable "self.basespec"
        and the fit parameters will be saved in "self.order"

        Parameters
        ----------
        order: int
            Order of the polynomial to fit
        excludefit: bool
            If there is a spectroscopic line fit, you can automatically exclude
            the region with signal above some tolerance set by `exclusionlevel`
            (it works for absorption lines by using the absolute value of the
            signal)
        exclusionlevel: float
            The minimum value of the spectroscopic fit to exclude when fitting
            the baseline
        save: bool
            Write the baseline fit coefficients into the spectrum's header in the
            keywords BLCOEFnn
        interactive: bool
            Specify the include/exclude regions through the interactive plot
            window
        fit_original: bool
            Fit the original spectrum instead of the baseline-subtracted spectrum.
            If disabled, will overwrite the original data with the
            baseline-subtracted version.

            .. warning:: If this is set False, behavior of `unsubtract` may be unexpected
        fit_plotted_area: bool
            Will respect user-specified zoom (using the pan/zoom buttons)
            unless xmin/xmax have been set manually
        reset_selection: bool
            Reset the selected region to those specified by this command only
            (will override previous xmin/xmax settings)
        select_region: bool
            Run the region selection procedure?  If false, will leave
            'includemask' untouched

        baseline_fit_color: color name (string)
            [plot parameter]
            Color to plot the baseline
        clear_all_connections: bool
            [plot parameter]
            Disable any previous interactive sessions
        highlight_fitregion: bool
            [plot parameter]
            Highlight the selected region for baseline fitting (default green)

        """
        specfit = self.Spectrum.specfit
        self.order = order
        self.set_spectofit(fit_original=fit_original)
        self.OKmask = (self.spectofit==self.spectofit)
        if exclude == 'interactive' or interactive:
            if np.all(self.includemask):
                specwarnings.warn("Include mask was uniformly True.  \n"
                                  "This has the effect of making the 'baseline'"
                                  " command do nothing.\n"
                                  "If this was not your intent, try"
                                  " spectrum.baseline(interactive=True,"
                                  "reset_selection=True)")
            self.start_interactive(clear_all_connections=clear_all_connections,
                                   debug=debug,
                                   reset_selection=reset_selection, **kwargs)
        else:
            if reset_selection:
                # this could be accomplished by passing reset=True to
                # selectregion, but that may cause
                # clashes [unclear; note added 2/12/2014]
                self.includemask[:] = True
            elif selectregion:
                # must select region (i.e., exclude edges) AFTER setting 'positive'
                # include region
                # also, DON'T highlight here because it will be cleared
                self.selectregion(fit_plotted_area=fit_plotted_area,
                                  exclude=exclude, debug=debug, **kwargs)

            # have to do excludefit after selection, otherwise
            # fit_plotted_area=True overrides
            if excludefit and specfit.modelpars is not None:
                #vlo = self.Spectrum.plotter.specfit.modelpars[1] - 2*self.Spectrum.plotter.specfit.modelpars[2]
                #vhi = self.Spectrum.plotter.specfit.modelpars[1] + 2*self.Spectrum.plotter.specfit.modelpars[2]
                #exclude = [np.argmin(abs(self.Spectrum.xarr-vlo)),argmin(abs(self.Spectrum.xarr-vhi))]
                full_model = specfit.get_full_model(add_baseline=False)

                # only set additional FALSE
                self.includemask *= (abs(full_model) <
                                     exclusionlevel*np.abs(full_model).max())
                #import pdb; pdb.set_trace()

            self.button2action(fit_original=fit_original,
                               baseline_fit_color=baseline_fit_color,
                               debug=debug,
                               subtract=subtract,
                               LoudDebug=LoudDebug,
                               **kwargs)
            if highlight_fitregion: self.highlight_fitregion()
        if save: self.savefit()

    def set_spectofit(self, fit_original=True, fit_residuals=False):
        """
        Reset the spectrum-to-fit from the data
        """
        if fit_residuals:
            self.spectofit = self.Spectrum.specfit.residuals
        elif self.subtracted and fit_original: # add back in the old baseline
            self.spectofit = self.Spectrum.data+self.basespec
        else:
            self.spectofit = self.Spectrum.data.copy()

    def fit(self, powerlaw=None, order=None, includemask=None, spline=False,
            spline_sampling=10, spline_downsampler=np.median, **kwargs):
        """
        Run the fit and set `self.basespec`
        """

        self.powerlaw = powerlaw or self.powerlaw
        self.spline = spline
        self.order = order or self.order
        if includemask is not None:
            self.includemask = includemask

        spline_sampling = (1 if spline_sampling is None else spline_sampling)

        xarr = self.Spectrum.xarr
        if spline:
            self.basespec = _spline(self.spectofit,
                                    xarr=xarr,
                                    masktofit=self.includemask,
                                    sampling=spline_sampling,
                                    downsampler=spline_downsampler,
                                    order=self.order)
        else:
            # use a,b to keep line under 80 chars
            a,b = self._baseline(self.spectofit, xarr=xarr,
                                 err=self.Spectrum.error, order=self.order,
                                 masktoexclude=~self.includemask,
                                 powerlaw=self.powerlaw,
                                 xarr_fit_units=xarr.units, **kwargs)
            self.basespec, self.baselinepars = (a,b)

            self.set_basespec_frompars()

    def button2action(self, event=None, debug=False, subtract=True,
                      powerlaw=None, fit_original=False,
                      spline=False,
                      spline_sampling=None,
                      spline_downsampler=np.median,
                      baseline_fit_color='orange', **kwargs):
        """
        Do the baseline fitting and save and plot the results.

        """
        if debug: print "Button 2/3 Baseline.  Subtract=",subtract
        if self.subtracted:
            self.unsubtract()

        # interactive guesspeakwidth passes this on; it should be excised
        if 'nwidths' in kwargs:
            kwargs.pop('nwidths')

        if powerlaw is None:
            powerlaw = self.powerlaw

        self.clear_highlights()

        self._xfit_units = self.Spectrum.xarr.units

        log.debug("Fitting baseline: powerlaw={0} "
                  "spline={1}".format(self.powerlaw, spline))
        self.fit(powerlaw=powerlaw, includemask=self.includemask,
                 order=self.order, spline=spline,
                 spline_sampling=spline_sampling,
                 spline_downsampler=spline_downsampler)

        if subtract:
            if self.subtracted and fit_original:
                # use the spectrum with the old baseline added in (that's what we fit to)
                self.Spectrum.data = self.spectofit - self.basespec
            else:
                self.Spectrum.data -= self.basespec
            self.subtracted = True
        else:
            if self.subtracted:
                self.unsubtract()
            self.subtracted = False

        if self.Spectrum.plotter.axis is not None:
            if debug: print "Plotting baseline"
            if event is not None:
                # preserve frame if fitting interactively
                kwargs.update({'use_window_limits':True})
            self.plot_baseline(baseline_fit_color=baseline_fit_color, **kwargs)

        # disconnect interactive window (and more importantly, reconnect to
        # original interactive cmds)
        self.clear_all_connections()

        if hasattr(self.Spectrum,'header'):
            history.write_history(self.Spectrum.header,
                    "BASELINE order=%i pars=%s" % (self.order,
                        ",".join([str(s) for s in self.baselinepars])) +
                        "(powerlaw)" if self.powerlaw else "")

    def set_basespec_frompars(self, baselinepars=None):
        """
        Set the baseline spectrum based on the fitted parameters
        """

        self.basespec = self.get_model(xarr=self.Spectrum.xarr,
                                       baselinepars=baselinepars)

    def get_model(self, xarr=None, baselinepars=None):
        # create the full baseline spectrum...
        fit_units = self._xfit_units
        if xarr is None:
            xarr = self.Spectrum.xarr.as_unit(fit_units)
        if baselinepars is None:
            baselinepars = self.baselinepars
        if baselinepars is None: # still...
            return 0 # no baseline has been computed
        if self.powerlaw:
            return (baselinepars[0]*(xarr)**(-baselinepars[1])).squeeze()
        else:
            return np.poly1d(baselinepars)(xarr)

    def button3action(self, *args, **kwargs):
        """
        Wrapper - same as button2action, but with subtract=False
        """
        if 'subtract' in kwargs:
            kwargs.pop('subtract')

        return self.button2action(*args, subtract=False, **kwargs)

    def plot_baseline(self, annotate=True, baseline_fit_color=(1,0.65,0,0.75),
            use_window_limits=None, linewidth=1, alpha=0.75, plotkwargs={},
            **kwargs):
        """
        Overplot the baseline fit

        Parameters
        ----------
        annotate : bool
            Display the fit parameters for the best-fit baseline on the
            top-left of the plot
        baseline_fit_color : matplotlib color
            What color to use for overplotting the line
            (default is slightly transparent orange)
        use_window_limits : None or bool
            Keep the current window or expand the plot limits?  If left as None,
            will use `self.use_window_limits`

        Other Parameters
        ----------------
        linewidth : number
        alpha : float [0-1]
        plotkwargs : dict
            Are passed to matplotlib's plot function
        """

        # clear out the errorplot.  This should not be relevant...
        if self.Spectrum.plotter.errorplot is not None:
            for p in self.Spectrum.plotter.errorplot:
                if isinstance(p,matplotlib.collections.PolyCollection):
                    if p in self.Spectrum.plotter.axis.collections:
                        self.Spectrum.plotter.axis.collections.remove(p)
                if isinstance(p,matplotlib.lines.Line2D):
                    if p in self.Spectrum.plotter.axis.lines:
                        self.Spectrum.plotter.axis.lines.remove(p)

        # if we subtract the baseline, replot the now-subtracted data with rescaled Y axes
        if self.subtracted:
            if self.Spectrum.plotter.axis is not None:
                for p in self.Spectrum.plotter.axis.lines:
                    self.Spectrum.plotter.axis.lines.remove(p)
            plotmask = self.OKmask*False # include nothing...
            plotmask[self.xmin:self.xmax] = self.OKmask[self.xmin:self.xmax] # then include everything OK in range
            self.Spectrum.plotter.ymin = abs(self.Spectrum.data[plotmask].min())*1.1*np.sign(self.Spectrum.data[plotmask].min())
            self.Spectrum.plotter.ymax = abs(self.Spectrum.data[plotmask].max())*1.1*np.sign(self.Spectrum.data[plotmask].max())
            # don't change the zoom (by default)!
            uwl = use_window_limits if use_window_limits is not None else self.use_window_limits
            self.Spectrum.plotter.plot(use_window_limits=uwl)
        else: # otherwise just overplot the fit
            self.Spectrum.plotter.axis.set_autoscale_on(False)
            for p in self._plots:
                # remove the old baseline plots
                if p in self.Spectrum.plotter.axis.lines:
                    self.Spectrum.plotter.axis.lines.remove(p)
            self._plots += self.Spectrum.plotter.axis.plot(
                    self.Spectrum.xarr,
                    self.basespec,
                    color=baseline_fit_color,
                    linewidth=linewidth,
                    alpha=alpha,
                    **plotkwargs)

        if annotate: self.annotate() # refreshes automatically
        elif self.Spectrum.plotter.autorefresh: self.Spectrum.plotter.refresh()

    def unsubtract(self, replot=True, preserve_limits=True):
        """
        Restore the spectrum to "pristine" state (un-subtract the baseline)

        *replot* [ True ]
            Re-plot the spectrum?  (only happens if unsubtraction proceeds,
            i.e. if there was a baseline to unsubtract)

        *preserve_limits* [ True ]
            Preserve the current x,y limits
        """
        if self.subtracted:
            self.Spectrum.data += self.basespec
            self.subtracted = False
            if replot:
                kwargs = self.Spectrum.plotter.plotkwargs
                kwargs.update({'use_window_limits':preserve_limits})
                self.Spectrum.plotter(**kwargs)
        else:
            print "Baseline wasn't subtracted; not unsubtracting."

    def annotate(self,loc='upper left'):
        if self.powerlaw:
            bltext = "bl: $y=%6.3g\\times(x)^{-%6.3g}$" % (self.baselinepars[0],self.baselinepars[1])
        elif self.spline:
            bltext = "bl: spline"
        else:
            bltext = "bl: $y=$"+"".join(["$%+6.3gx^{%i}$" % (f,self.order-i)
                for i,f in enumerate(self.baselinepars)])
        #self.blleg = text(xloc,yloc     ,bltext,transform = self.Spectrum.plotter.axis.transAxes)
        self.clearlegend()
        pl = matplotlib.collections.CircleCollection([0],edgecolors=['k'])
        self.blleg = self.Spectrum.plotter.axis.legend(
                (pl,),
                (bltext,),loc=loc,markerscale=0.001,
                borderpad=0.1, handlelength=0.1, handletextpad=0.1, frameon = False
                )
        self.Spectrum.plotter.axis.add_artist(self.blleg)
        if self.Spectrum.plotter.autorefresh: self.Spectrum.plotter.refresh()

    def clearlegend(self):
        if self.blleg is not None:
            self.blleg.set_visible(False)
            if self.blleg in self.Spectrum.plotter.axis.artists:
                self.Spectrum.plotter.axis.artists.remove(self.blleg)
        if self.Spectrum.plotter.autorefresh: self.Spectrum.plotter.refresh()

    def savefit(self):
        if self.baselinepars is not None and hasattr(self.Spectrum,'header'):
            for ii,p in enumerate(self.baselinepars):
                self.Spectrum.header['BLCOEF%0.2i' % (ii)] = (p,"Baseline power-law best-fit coefficient x^%i" % (self.order-ii-1))

    def _baseline(self, spectrum, xarr=None, err=None,
                  order=1, quiet=True, masktoexclude=None, powerlaw=False,
                  xarr_fit_units='pixels', LoudDebug=False, renormalize='auto',
                  zeroerr_is_OK=True, spline=False, **kwargs):
        """
        Fit a baseline/continuum to a spectrum

        Parameters
        ----------
        masktoexclude : boolean array
            True: will not be fit
            False: will be fit
            *if ALL are True, ALL pixels will be fit - just with silly weights*
        """

        #if xmin == 'default':
        #    if order <= 1 and mask is None: xmin = np.floor( spectrum.shape[-1]*0.1 )
        #    else: xmin = 0
        #elif xmin is None:
        #    xmin = 0
        #if xmax == 'default':
        #    if order <= 1 and mask is None: xmax = np.ceil( spectrum.shape[-1]*0.9 )
        #    else: xmax = spectrum.shape[-1]
        #elif xmax is None:
        #    xmax = spectrum.shape[-1]

        if xarr is None:
            xarr = np.indices(spectrum.shape).squeeze()

        # A good alternate implementation of masking is to only pass mpfit the data
        # that is unmasked.  That would require some manipulation above...
        if err is None:
            err = np.ones(spectrum.shape)
        else:
            # don't overwrite error
            err = err.copy()
            # assume anything with 0 error is GOOD
            if zeroerr_is_OK:
                err[err == 0] = 1.
            else: # flag it out!
                err[err == 0] = 1e10

        #err[:xmin] = 1e10
        #err[xmax:] = 1e10
        if masktoexclude is not None:
            if masktoexclude.dtype.name != 'bool':
                masktoexclude = masktoexclude.astype('bool')
            err[masktoexclude] = 1e10
            if LoudDebug: print "In _baseline: %i points masked out" % masktoexclude.sum()
        if (spectrum!=spectrum).sum() > 0:
            print "There is an error in baseline: some values are NaN"
            import pdb; pdb.set_trace()

        #xarrconv = xarr[xmin:xmax].as_unit(xarr_fit_units)
        OK = True-masktoexclude
        xarrconv = xarr.as_unit(xarr_fit_units)
        if powerlaw:
            # for powerlaw fitting, only consider positive data
            OK *= spectrum > 0
            pguess = [np.median(spectrum[OK]),2.0]
            if LoudDebug: print "_baseline powerlaw Guesses: ",pguess

            def mpfitfun(data,err):
                #def f(p,fjac=None): return [0,np.ravel(((p[0] * (xarrconv[OK]-p[2])**(-p[1]))-data)/err)]
                # Logarithmic fitting:
                def f(p,fjac=None):
                    #return [0,
                    #        np.ravel( (np.log10(data) - np.log10(p[0]) + p[1]*np.log10(xarrconv[OK]/p[2])) / (err/data) )
                    #        ]
                    return [0, np.ravel( (data - self.get_model(xarr=xarrconv[OK],baselinepars=p)) / (err/data) )]
                return f
        else:
            pguess = [0]*(order+1)
            if LoudDebug: print "_baseline Guesses: ",pguess

            def mpfitfun(data,err):
                def f(p,fjac=None): return [0,np.ravel((np.poly1d(p)(xarrconv[OK])-data)/err)]
                return f
        #scalefactor = 1.0
        #if renormalize in ('auto',True):
        #    datarange = spectrum.max() - spectrum.min()
        #    if abs(datarange) < 1e-9 or abs(datarange) > 1e9:
        #        scalefactor = np.median(np.abs(self.spectrum))
        #        print "BASELINE: Renormalizing data by factor %e to improve fitting procedure" % scalefactor
        #        spectrum /= scalefactor
        #        err /= scalefactor

        import pyspeckit.mpfit as mpfit
        mp = mpfit.mpfit(mpfitfun(spectrum[OK],err[OK]),xall=pguess,quiet=quiet) # mpfit doesn't need to take kwargs, I think ,**kwargs)
        if np.isnan(mp.fnorm):
            raise ValueError("chi^2 is NAN in baseline fitting")
        fitp = mp.params
        if powerlaw:
            #bestfit = (fitp[0]*(xarrconv)**(-fitp[1])).squeeze()
            bestfit = (fitp[0]*(xarrconv)**(-fitp[1])).squeeze()
        else:
            bestfit = np.poly1d(fitp)(xarrconv).squeeze()

        return bestfit,fitp

    def crop(self,x1pix,x2pix):
        """
        When spectrum.crop is called, this must be too
        """
        self.basespec = self.basespec[x1pix:x2pix]
        self.includemask = self.includemask[x1pix:x2pix]
        self.OKmask = self.OKmask[x1pix:x2pix]

    def downsample(self,factor):
        self.basespec = self.basespec[::factor]
        self.includemask = self.includemask[::factor]
        self.OKmask = self.OKmask[::factor]

    def copy(self, parent=None):
        """
        Create a copy of the baseline fit

        [ parent ]
            A spectroscopic axis instance that is the parent of the specfit
            instance.  This needs to be specified at some point, but defaults
            to None to prevent overwriting a previous plot.
        """

        newbaseline = copy.copy(self)
        newbaseline.Spectrum = parent
        newbaseline.OKmask = copy.copy( self.OKmask )
        newbaseline.basespec = copy.copy( self.basespec )
        newbaseline.baselinepars = copy.copy( self.baselinepars )
        newbaseline.includemask = self.includemask.copy()

        if parent is not None:
            newbaseline.Spectrum.plotter = parent.plotter
        else:
            newbaseline.Spectrum.plotter = None

        return newbaseline

def _spline(data, xarr=None, masktofit=None, order=3, sampling=10,
            downsampler=np.median, append_endpoints=True):
    from scipy.interpolate import UnivariateSpline

    assert masktofit.shape == data.shape

    if masktofit is None:
        masktofit = np.isfinite(data)
        if not any(masktofit):
            log.warn("All data was infinite or NaN")

    if xarr is None:
        xarr = np.arange(data.size, dtype=data.dtype)

    if downsampler is not None:
        ngood = np.count_nonzero(masktofit)
        if ngood == 0:
            log.warn("Fitting all data with spline")
            masktofit[:] = True
            ngood = masktofit.size
        endpoint = ngood - (ngood % sampling)
        yfit = downsampler([data[masktofit][ii:endpoint:sampling]
                            for ii in range(sampling)], axis=0)
        xfit = xarr[masktofit][sampling/2:endpoint:sampling]
        if append_endpoints:
            xfit = np.array([xarr[0]-(xarr[1]-xarr[0])] +
                            xfit.tolist() +
                            [xarr[-1]+(xarr[1]-xarr[0])])
            yfit = np.array([yfit[0]] + yfit.tolist() + [yfit[-1]])
        inds = np.argsort(xfit)
        spl = UnivariateSpline(xfit[inds],
                               yfit[inds],
                               k=order,
                               s=0)
    else:
        xfit = xarr[masktofit]
        yfit = data[masktofit]
        inds = np.argsort(xfit)
        spl = UnivariateSpline(xfit[inds],
                               yfit[inds],
                               k=order,
                               s=sampling)

    baseline_fitted = spl(xarr)
    if np.any(np.isnan(baseline_fitted)):
        log.error("NaNs in baseline.")
        import ipdb; ipdb.set_trace()
    return baseline_fitted

max_stars_repo_name: migueldvb/pyspeckit

max_issues_repo_name: migueldvb/pyspeckit

max_forks_repo_name: migueldvb/pyspeckit

reason: import numpy,from scipy,from astropy


text:
import sys
import os
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.autograd import Function
from torch.nn.modules.module import Module
import matplotlib.pylab as plt
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
import seaborn as sea

sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))

from TorchProteinLibrary.RMSD import Coords2RMSD
from TorchProteinLibrary.FullAtomModel import Angles2Coords
from TorchProteinLibrary.FullAtomModel import Coords2TypedCoords
from TorchProteinLibrary.FullAtomModel import Coords2CenteredCoords
from TorchProteinLibrary.Volume import VolumeRMSD
from TorchProteinLibrary.FullAtomModel.CoordsTransform import CoordsRotate, getRandomRotation, CoordsTranslate, getBBox
from TorchProteinLibrary.FullAtomModel import Angles2Coords, Coords2TypedCoords

import _Volume

if __name__=='__main__':

	box_size = 80
	resolution = 1.25
	ix = 0
	iy = 159
	iz = 0

	sequence = ['GGAGRRRGGWG']
	angles = torch.zeros(len(sequence), 7, len(sequence[0]), dtype=torch.double)
	angles[0,0,:] = -1.047
	angles[0,1,:] = -0.698
	angles[0,2:,:] = 110.4*np.pi/180.0
	a2c = Angles2Coords()
	rotate = CoordsRotate()
	translate = CoordsTranslate()
	rmsd = Coords2RMSD()
	R1 = getRandomRotation( len(sequence) )
	R0 = torch.zeros(1,3,3, dtype=torch.double, device='cpu')
	R0[0,0,0]=1.0
	R0[0,1,1]=1.0
	R0[0,2,2]=1.0
	T0 = torch.zeros(1,3, dtype=torch.double, device='cpu')
	R1 = R0
	#Generating protein
	protein, res_names, atom_names, num_atoms = a2c(angles, sequence)

	rmsd_vol = VolumeRMSD(protein, num_atoms, R0, R1, T0, resolution, box_size*2)

	#Rotating and translating protein
	T1 = torch.zeros(1, 3, dtype=torch.double, device='cpu')
	T1[0,0] = ix * resolution
	T1[0,1] = iy * resolution
	T1[0,2] = iz * resolution
	if ix>=box_size:
		T1[0,0] = -(2*box_size - ix)*resolution
	if iy>=box_size:
		T1[0,1] = -(2*box_size - iy)*resolution
	if iz>=box_size:
		T1[0,2] = -(2*box_size - iz)*resolution

	rotated_coords = rotate(protein, R1, num_atoms)
	trans_rotated_coords = translate(rotated_coords, T1, num_atoms)

	rmsd2 = protein[0, :3*num_atoms[0].item()] - trans_rotated_coords[0,:3*num_atoms[0].item()]
	rmsd2 = torch.sum(rmsd2*rmsd2)/num_atoms[0].item()
	rmsd = torch.sqrt(rmsd2)

	print('VolumeRMSD: ', rmsd_vol[0, ix, iy, iz].item())
	print('Coords2RMSD: ', rmsd)

max_stars_repo_name: anushriya/TorchProteinLibrary

max_issues_repo_name: anushriya/TorchProteinLibrary

max_forks_repo_name: anushriya/TorchProteinLibrary


text:
# Copyright 2018 The Sonnet Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Tests for sonnet.v2.src.nets.vqvae."""

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

from absl.testing import parameterized

import numpy as np
from sonnet.src import test_utils
from sonnet.src.nets import vqvae
import tensorflow as tf

class VqvaeTest(parameterized.TestCase, test_utils.TestCase):

  @parameterized.parameters((vqvae.VectorQuantizer, {
      'embedding_dim': 4,
      'num_embeddings': 8,
      'commitment_cost': 0.25
  }), (vqvae.VectorQuantizerEMA, {
      'embedding_dim': 6,
      'num_embeddings': 13,
      'commitment_cost': 0.5,
      'decay': 0.1
  }))
  def testConstruct(self, constructor, kwargs):
    vqvae_module = constructor(**kwargs)
    # Batch of input vectors to quantize
    inputs_np = np.random.randn(100, kwargs['embedding_dim']).astype(np.float32)
    inputs = tf.constant(inputs_np)

    # Set is_training to False, otherwise for the EMA case just evaluating the
    # forward pass will change the embeddings, meaning that some of our computed
    # closest embeddings will be incorrect.
    vq_output = vqvae_module(inputs, is_training=False)

    # Output shape is correct
    self.assertEqual(vq_output['quantize'].shape, inputs.shape)

    vq_output_np = tf.nest.map_structure(lambda t: t.numpy(), vq_output)
    embeddings_np = vqvae_module.embeddings.numpy()

    self.assertEqual(embeddings_np.shape,
                     (kwargs['embedding_dim'], kwargs['num_embeddings']))

    # Check that each input was assigned to the embedding it is closest to.
    distances = ((inputs_np**2).sum(axis=1, keepdims=True) -
                 2 * np.dot(inputs_np, embeddings_np) +
                 (embeddings_np**2).sum(axis=0, keepdims=True))
    closest_index = np.argmax(-distances, axis=1)
    # On TPU, distances can be different by ~1% due to precision. This can cause
    # the distanc to the closest embedding to flip, leading to a difference
    # in the encoding indices tensor. First we check that the continuous
    # distances are reasonably close, and then we only allow N differences in
    # the encodings. For batch of 100, N == 3 seems okay (passed 1000x tests).
    self.assertAllClose(distances, vq_output_np['distances'], atol=4e-2)
    num_differences_in_encodings = (closest_index !=
                                    vq_output_np['encoding_indices']).sum()
    num_differences_allowed = 3
    self.assertLessEqual(num_differences_in_encodings, num_differences_allowed)

  @parameterized.parameters((vqvae.VectorQuantizer, {
      'embedding_dim': 4,
      'num_embeddings': 8,
      'commitment_cost': 0.25
  }), (vqvae.VectorQuantizerEMA, {
      'embedding_dim': 6,
      'num_embeddings': 13,
      'commitment_cost': 0.5,
      'decay': 0.1
  }))
  def testShapeChecking(self, constructor, kwargs):
    vqvae_module = constructor(**kwargs)
    wrong_shape_input = np.random.randn(100, kwargs['embedding_dim'] * 2)
    with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
                                 'but the requested shape has'):
      vqvae_module(
          tf.constant(wrong_shape_input.astype(np.float32)), is_training=False)

  @parameterized.parameters((vqvae.VectorQuantizer, {
      'embedding_dim': 4,
      'num_embeddings': 8,
      'commitment_cost': 0.25
  }), (vqvae.VectorQuantizerEMA, {
      'embedding_dim': 6,
      'num_embeddings': 13,
      'commitment_cost': 0.5,
      'decay': 0.1
  }))
  def testNoneBatch(self, constructor, kwargs):
    """Check that vqvae can be built on input with a None batch dimension."""
    vqvae_module = constructor(**kwargs)
    inputs = tf.zeros([0, 5, 5, kwargs['embedding_dim']])
    vqvae_module(inputs, is_training=False)

  @parameterized.parameters({'use_tf_function': True},
                            {'use_tf_function': False})
  def testEmaUpdating(self, use_tf_function):
    embedding_dim = 6
    vqvae_module = vqvae.VectorQuantizerEMA(
        embedding_dim=embedding_dim,
        num_embeddings=7,
        commitment_cost=0.5,
        decay=0.1)
    if use_tf_function:
      vqvae_module = tf.function(vqvae_module)

    batch_size = 16

    prev_embeddings = vqvae_module.embeddings.numpy()

    # Embeddings should change with every forwards pass if is_training == True.
    for _ in range(10):
      inputs = tf.random.normal([batch_size, embedding_dim])
      vqvae_module(inputs, is_training=True)
      current_embeddings = vqvae_module.embeddings.numpy()
      self.assertFalse((prev_embeddings == current_embeddings).all())
      prev_embeddings = current_embeddings

    # Forward passes with is_training == False don't change anything
    for _ in range(10):
      inputs = tf.random.normal([batch_size, embedding_dim])
      vqvae_module(inputs, is_training=False)
      current_embeddings = vqvae_module.embeddings.numpy()
      self.assertTrue((current_embeddings == prev_embeddings).all())

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

max_stars_repo_name: chentingpc/sonnet

max_issues_repo_name: chentingpc/sonnet

max_forks_repo_name: chentingpc/sonnet


text:
# _*_coding:utf-8_*_
"""
@author: Zhaoqing Liu
@email: Zhaoqing.Liu-1@student.uts.edu.au
@date: 03/12/2020 4:42 pm
@desc:
TODO:
    1. Done - Do feature fuzzification in data_preprocessing_funcs.py
    2. Done - Add function implementing splitting criteria fuzzification in util_criterion_funcs.py
    3. Upgrade program from Python to Cython to speed up the algorithms.
"""
import os
import numpy as np
from fuzzytrees.settings import DirSave
from fuzzytrees.util_comm import get_today_str

if __name__ == '__main__':
    # df = pd.DataFrame({"id": [1, 2, 3, 4], "name": ["sam", "sam", "peter", "jack"], "number": [8, 6, 8, 2]})
    # df_after = df[["name", "number"]]
    # print(df_after.values)
    # unique_values = df["name"].unique()
    # for idx, val in enumerate(unique_values):
    #     print(idx, val)
    # print(np.size(unique_values))
    # print(np.shape(unique_values))
    # print(type(unique_values))
    # print(unique_values)
    # for unique_value in unique_values:
    #     print(unique_value)
    #     sub_df = df[df["name"] == unique_value]
    # print(type(sub_df))
    # print(type(sub_df.values))
    # print(type(sub_df["number"]))
    # print(sub_df["number"].values)
    # print(sub_df)
    # sdf = sub_df.sort_values(by="number")
    # print(sdf)

    # ds = df.groupby(["name"]).groups
    # print(ds)
    # for (name, data) in ds.items():
    #     print(name)
    #     print(type(data))
    #     print(type(data.values))
    #     print(type(df.iloc[data, :]))
    #     print(type(df.iloc[data.values, :]))

    # a = np.array([2, 8, 5])
    # print(a)
    # b = a - 1
    # print(b)

    # dir = DirSave.EVAL_DATA.value + get_today_str()
    # print(os.path.exists(dir))
    # if not os.path.exists(dir) or not os.path.isdir(dir):
    #     os.makedirs(dir)

    # =======================================================================================
    # # Test for mapping [0, infinity] to [0, 1]
    # a = np.arange(0, 1, 0.01)
    # print(a)
    # b = 1 - 1 / (1 + 100 * a)
    # print(b)
    #
    # b = np.log(a) - np.log(1 - a)
    # print(b)

    # =======================================================================================
    # Compare the speed of random.sample() and numpy.random.choice():

    X = [[1, 2, 3],
         [4, 5, 6]]
    X = np.array(X)
    print(X.shape[1:])
    print(np.prod(X.shape[1:]))

max_stars_repo_name: Anjin-Liu/FuzzyTrees

max_issues_repo_name: Anjin-Liu/FuzzyTrees

max_forks_repo_name: Anjin-Liu/FuzzyTrees


text:
import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
import numpy as np

train_dataset = paddle.vision.datasets.MNIST(mode = 'train')

max_stars_repo_name: jingyuangaosuxunyangjian/HelloWorld

max_issues_repo_name: jingyuangaosuxunyangjian/HelloWorld

max_forks_repo_name: jingyuangaosuxunyangjian/HelloWorld