text:
"""
This module contains functions related to orbit calculations
"""
# Standard library imports
from typing import Any,Dict,List,Tuple,Sequence
#https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html

# Third party imports
import pandas as pd
import numpy as np
from numpy import rad2deg, deg2rad
from numpy.linalg import norm
import toolz as tz
# Using Newton-Ramson method
from scipy.integrate import solve_ivp

from myastro import util as ut
from myastro import data_catalog as dc
from myastro import timeutil as tc
from myastro import coord as co
from myastro import orbit as ob

from myastro.orbit import EphemrisInput

from myastro.timeutil import  PI_HALF, PI, TWOPI
from myastro.keplerian import KeplerianOrbit
from myastro.lagrange_coeff import rv_from_r0v0
from myastro.timeutil import epochformat2jd, jd2mjd, T, mjd2jd, jd2str_date,MDJ_J2000, JD_J2000
from myastro.planets import g_xyz_equat_sun_j2000, g_rlb_eclip_sun_eqxdate
from myastro.util import mu_by_name, mu_Sun

from myastro.log import get_logger
logger = get_logger(__file__.split('/')[-1])

def my_dfdt(t, y):
    """
    Computes the time derivative of the unknown function. Integrating this function, we obtain the unknown
    function. We know the velocity and acceleration that is basically what this function returns so integrating we obtain
    the position and velocity.
    This method basically calculates the acceleration based on the position (r). This acceleration is
    composed of the one corresponding to the force exerted by the Sun (two bodys acceleration) plus
    the perturbed one due to the every planet that which includes two componemts:
        - The direct one: The force the planet exerts to the body
        - The indirect one: The force the planet exerts to the Sun that also impacts to the body.

    Args:
        t : point in time (normally used in modified julian days) at which we want to calculate the derivative
        y  : The vector with the variables to solve the differential equation system
                [0..3] r
                [3..6] v (not used in this case)

    Returns :
        A vector of 6 positions with v and acceleration
    """
    h_xyz = y[0:3]
    acc = -ob.accel(ut.GM, h_xyz) + ob.calc_perturbed_accelaration(t, h_xyz)
    return np.concatenate((y[3:6], acc))

def calc_eph_by_cowells (body, eph , type='body', include_osc=False):
    """
    Computes the ephemeris for a minor body using the Cowells method. This has more inexact than
    Enckes but quicker

    Args:
        body : The orbital elements of the body, it can be an body or a comet
        eph : Ephemeris data (EphemrisInput)
        type : Indicates if the body is a asteroid ('body') or a comet ('comet')

    Returns :
        A dataframe with the result
    """

    # This matrix just depends on the desired equinox to calculate the obliquity
    # to pass from ecliptic coordinate system to equatorial
    MTX_equatFeclip = co.mtx_equatFeclip(eph.T_eqx)

    T_J2000 = 0.0
    # This is to precess from J2000 to ephemeris equinox (normally this matrix will be identity matrix)
    MTX_J2000_Teqx = co.mtx_eclip_prec(T_J2000,eph.T_eqx)

    # The PQR mtx (from orbital plane to eclipt) is preccesed from equinox of body object to the desired equinox
    MTX_J2000_PQR = co.mtx_eclip_prec(body.T_eqx0, T_J2000).dot(body.mtx_PQR)

    # The initial conditions for doing the integration is calculated, i.e.,
    # the r,v of the body at its epoch (in the example of Ceres, the epoch of
    # book that epoch  is 1983/09/23.00)
    # The integration is done in the ecliptic plane and precessed in to J2000
    # so the solution will be also ecliptic and precessed.

    initial_mjd = body.epoch_mjd

    if type == 'body' :
        k_orbit = KeplerianOrbit.for_body(body)
    else :
        k_orbit = KeplerianOrbit.for_comet(body)

    r0, v0 = k_orbit.calc_rv(initial_mjd)

    y0 = np.concatenate((MTX_J2000_PQR.dot(r0), MTX_J2000_PQR.dot(v0)))

    # The integration is done in the ecliptic plane. First, we propagete the state vector
    # from the epoch of the body (when the data are fresh) up to the start of the ephemeris
    # in that interval we dont request the solution.
    # Second we integrate in the ephemeris interval and asking the solution every step
    # (e.g. 2 days) this is t.sol time samples
    # In case the epoch of the objet is at the future, the integration is done backward
    # in time (during the same interval but in reverse mode)

    if eph.from_mjd < initial_mjd < eph.to_mjd :
        # We need to do 2 integrations
        # First one backwards
        t_sol = list(reversed(list(ut.frange(eph.from_mjd, initial_mjd, eph.step))))
        sol_1 = ob.do_integration(my_dfdt, y0, initial_mjd, t_sol[-1], t_sol)
        # Second one forwards
        t_sol = list(ut.frange(initial_mjd, eph.to_mjd, eph.step))
        sol_2 = ob.do_integration(my_dfdt, y0, initial_mjd, t_sol[-1], t_sol)

        SOL_T = np.concatenate((sol_1.t, sol_2.t))
        SOL_Y = np.concatenate((sol_1.y, sol_2.y), axis=1)
    else :
        t_sol = list(ut.frange(eph.from_mjd, eph.to_mjd, eph.step))
        if eph.to_mjd < initial_mjd :
            # If the epoch is in the future, we need to integrate backwards, i.e.
            # propagatin the state vector from the future to the past.
            t_sol = list(reversed(t_sol))
        sol = ob.do_integration(my_dfdt, y0, initial_mjd, t_sol[-1], t_sol)
        SOL_T = sol.t
        SOL_Y = sol.y

    tpoints = dict()
    for idx, t in enumerate(SOL_T) :
        tpoints[t] = (SOL_Y[:,idx][:3], SOL_Y[:,idx][3:6])
    tpoints = {t:tpoints[t] for t in sorted(tpoints.keys())}

    return ob.process_solution(tpoints, MTX_J2000_Teqx, MTX_equatFeclip)

def test_all():
    eph = EphemrisInput(from_date="2020.05.25.0",
                        to_date = "2020.06.15.0",
                        step_dd_hh_hhh = "02 00.0",
                        equinox_name = "J2000")

    for name in dc.DF_BODYS['Name']:
        body = dc.read_body_elms_for(name,dc.DF_BODYS)
        print ("Calculating for ",name)
        print (calc_eph_by_cowells(body, eph))

def test_body():
    eph = EphemrisInput(from_date="2020.05.25.0",
                        to_date = "2020.06.15.0",
                        step_dd_hh_hhh = "02 00.0",
                        equinox_name = "J2000")

    eph = ob.EphemrisInput(from_date="2019.04.01.0",
                       to_date = "2020.07.01.0",
                       step_dd_hh_hhh = "10 00.0",
                       equinox_name = "J2000")

    """
    eph = ob.EphemrisInput(from_date="2020.04.01.0",
                       to_date = "2020.07.01.0",
                       step_dd_hh_hhh = "50 00.0",
                       equinox_name = "J2000")
    """

    B_2002_NN4 = dc.read_body_elms_for("2002 NN4",dc.DF_BODYS)

    CERES = dc.read_body_elms_for("Ceres",dc.DF_BODYS)

    df = calc_eph_by_cowells(B_2002_NN4, eph)

    print (df[df.columns[0:8]])

def test_comet():

    eph  = EphemrisInput(from_date="1985.11.15.0",
                        to_date = "1986.04.05.0",
                        step_dd_hh_hhh = "10 00.0",
                        equinox_name = "J2000")

    HALLEY_J2000 = dc.read_comet_elms_for("1P/Halley", dc.DF_COMETS)

    df = calc_eph_by_cowells(HALLEY_J2000, eph, 'comet')

    print (df[df.columns[0:8]])

def test_all_comets():
    """
    eph = EphemrisInput(from_date="2020.05.25.0",
                        to_date = "2020.06.15.0",
                        step_dd_hh_hhh = "02 00.0",
                        equinox_name = "J2000")
    """

    df_ = dc.DF_COMETS
    #df_ = df_.query("0.999 < e < 1.001")
    df_ = df_.query("Name=='C/1532 R1'")

    for idx, name in enumerate(df_['Name']):
        logger.warning(f"{idx+1} Calculating for {name} ")
        body = dc.read_comet_elms_for(name,dc.DF_COMETS)
        eph = EphemrisInput.from_mjds( body.epoch_mjd-25, body.epoch_mjd+25, "02 00.0", "J2000" )
        print (f"{idx+1} Calculating for {name} ")
        print (calc_eph_by_cowells(body, eph, 'comet'))

def test_several_comets():
    names = [#"C/1988 L1 (Shoemaker-Holt-Rodriquez)",   # Parabolic
             #"C/1980 E1 (Bowell)"]
             #"C/1848 P1 (Petersen)"
              "C/1672 E1"]

    for name in names :
        body = dc.read_comet_elms_for(name,dc.DF_COMETS)
        eph = EphemrisInput.from_mjds( body.epoch_mjd-50, body.epoch_mjd+50, "02 00.0", "J2000" )
        print (f"Calculating for {name} ")
        df = calc_eph_by_cowells(body, eph, 'comet')
        print (df[df.columns[0:8]])
        #print (calc_eph_by_enke(body, eph, 'comet'))

if __name__ == "__main__" :
    #test_all()
    #test_2()
    #test_all_comets()
    #test_comet()
    #test_several_comets()
    test_body()
    #test_comet()

max_stars_repo_name: benitocm/practical-astronomy

max_issues_repo_name: benitocm/practical-astronomy

max_forks_repo_name: benitocm/practical-astronomy

reason: import numpy,from numpy,from scipy


text:
import os
import json
import time
import numpy as np
import pandas as pd
from B3 import calc_b3

N = 1000

levels = ['u10', 'u25', 'u50', 'u100']

Number_of_tests = 5
number_of_batch = 50

csv_root = input("Enter address of csv (input) to process : \n")

if csv_root[-1] != '/':
  csv_root = csv_root + '/'

print(" ")

with open('../data/folder_to_id_dict_known.json', 'r') as f:
  folder_to_id_dict_known = json.load(f)

with open('../data/folder_to_id_dict_unknown_166.json', 'r') as f:
  folder_to_id_dict_unknown = json.load(f)

def label_finder(id):
  y = np.zeros(len(id))
  for (k,x) in enumerate(id):
    z = x.find('/n')
    if x.find('ILSVRC_2012')>0:
      folder_name = x[(z+1):(z+10)]
      y[k] = folder_to_id_dict_known[folder_name]
    elif x.find('ILSVRC_2010')>0:
      folder_name = x[(z+1):(z+10)]
      y[k] = folder_to_id_dict_unknown[folder_name]
    else:
      raise ValueError()
  return y

for level in levels:

  score_pre = np.zeros(Number_of_tests)
  score_post = np.zeros(Number_of_tests)

  for test_id in range(Number_of_tests):

    all_L_pre = np.array([])
    all_C_pre = np.array([])

    csv_files_path = csv_root + "characterization_" + level + "_"  + str(test_id) + "_"
    charcterization_csv_fils_pre  = [csv_files_path  + "pre_"  + str(k).zfill(2) + ".csv" for k in range(number_of_batch)]

    for index,csv_file in enumerate(charcterization_csv_fils_pre):
      df = pd.read_csv(csv_file, sep=',',header=None, index_col=False)
      id = df.iloc[:,0]
      c = df.values[:,1:]
      L = label_finder(id)
      C = np.argmax(c, axis =1)
      all_L_pre = np.append(all_L_pre, L)
      all_C_pre = np.append(all_C_pre, C)

    last_L_pre = all_L_pre[-N:]
    last_C_pre = all_C_pre[-N:]

    is_known_pre = (last_L_pre>0) * (last_L_pre<1001)
    is_unknown_pre = ~is_known_pre
    predicted_known_pre = (last_C_pre>0) * (last_C_pre<1001)
    predicted_unknown_pre = ~predicted_known_pre

    N_KK_pre =  np.sum(is_known_pre*predicted_known_pre)
    N_KU_pre =  np.sum(is_known_pre*predicted_unknown_pre)
    N_UK_pre =  np.sum(is_unknown_pre*predicted_known_pre)
    N_UU_pre =  np.sum(is_unknown_pre*predicted_unknown_pre)

    N_pre = N_KK_pre + N_KU_pre + N_UK_pre + N_UU_pre
    assert N_pre == N

    last_y_pre = last_L_pre[is_known_pre*predicted_known_pre]
    last_k_pre = last_C_pre[is_known_pre*predicted_known_pre]
    last_LUU_pre = last_L_pre[is_unknown_pre*predicted_unknown_pre]
    last_CUU_pre = last_C_pre[is_unknown_pre*predicted_unknown_pre]

    if N_KK_pre > 0:
      correct_pre = np.sum(last_y_pre==last_k_pre)
    else:
      correct_pre = 0
    if N_UU_pre > 0:
      b3_pre, _, _ = calc_b3(L = last_LUU_pre , K = last_CUU_pre)
    else:
      N_UU_pre = 0

    score_pre[test_id] = ( correct_pre +  ( b3_pre * N_UU_pre ) ) /  N_pre

    all_L_post = np.array([])
    all_C_post = np.array([])

    csv_files_path = csv_root + "characterization_" + level + "_"  + str(test_id) + "_"
    charcterization_csv_fils_post  = [csv_files_path  + "post_"  + str(k).zfill(2) + ".csv" for k in range(number_of_batch)]

    for index,csv_file in enumerate(charcterization_csv_fils_post):
      df = pd.read_csv(csv_file, sep=',',header=None, index_col=False)
      id = df.iloc[:,0]
      c = df.values[:,1:]
      L = label_finder(id)
      C = np.argmax(c, axis =1)
      all_L_post = np.append(all_L_post, L)
      all_C_post = np.append(all_C_post, C)

    last_L_post = all_L_post[-N:]
    last_C_post = all_C_post[-N:]

    is_known_post = (last_L_post>0) * (last_L_post<1001)
    is_unknown_post = ~is_known_post
    predicted_known_post = (last_C_post>0) * (last_C_post<1001)
    predicted_unknown_post = ~predicted_known_post

    N_KK_post =  np.sum(is_known_post*predicted_known_post)
    N_KU_post =  np.sum(is_known_post*predicted_unknown_post)
    N_UK_post =  np.sum(is_unknown_post*predicted_known_post)
    N_UU_post =  np.sum(is_unknown_post*predicted_unknown_post)

    N_post = N_KK_post + N_KU_post + N_UK_post + N_UU_post
    assert N_post == N

    last_y_post = last_L_post[is_known_post*predicted_known_post]
    last_k_post = last_C_post[is_known_post*predicted_known_post]
    last_LUU_post = last_L_post[is_unknown_post*predicted_unknown_post]
    last_CUU_post = last_C_post[is_unknown_post*predicted_unknown_post]

    if N_KK_post > 0:
      correct_post = np.sum(last_y_post==last_k_post)
    else:
      correct_post = 0
    if N_UU_post > 0:
      b3_post, _, _ = calc_b3(L = last_LUU_post , K = last_CUU_post)
    else:
      b3_post = 0

    score_post[test_id] = ( correct_post +  ( b3_post * N_UU_post ) ) /  N_post

  print(round( np.mean(score_pre) , 4) , round( np.mean(score_post) , 4) , sep=' & ' , end=' & ')

print("\n")

ext: py

reason: import numpy


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

df = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','>100'],
                     'Male':[7016987, 7624144, 8725413, 8558868, 8630229, 8460995, 7717658, 6766664, 6320568, 5692014, 4834995, 3902344, 3041035, 2224065, 1667372, 1090517, 668623, 310759, 114964, 31529, 7247],
                     'Female': [-6779171, -7345231, -8441348, -8432004, -8614963, -8643419, -8026854, -7121915, -6688796, -6141338, -5305407, -4373877,-3468085, -2616745, -2074264, -1472930, -998349, -508724, -211594, -66806, -16989]})

plt.figure(figsize=(25, 10))
Age2 = ["0-4","5-9","10-14","15-19","20-24","25-29","30-34","35-39","40-44",
             "45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85-89",
             "90-94","95-99", ">100"]
bar_plot = sns.barplot(x='Male', y='Age', data=df, order=Age2, color="royalblue")

bar_plot = sns.barplot(x='Female', y='Age', data=df, order=Age2,color = "red")

plt.xticks([0,-1000000,  -2000000,  -3000000,  -4000000, -5000000, -6000000, -7000000, -8000000, -9000000,
              1000000,  2000000,  3000000,  4000000, 5000000, 6000000, 7000000, 8000000, 9000000 ],
           ["0","1 mi", "2 mi", "3 mi", "4 mi", "5 mi", "6 mi", "7 mi", "8 mi", "9 mi",
             "1 mi", "2 mi", "3 mi", "4 mi", "5 mi", "6 mi", "7 mi", "8 mi", "9 mi"] )
plt.ylabel("Faixa etária", fontsize = 14)
plt.xlabel("População", fontsize = 14)

plt.title("Distribuição da população feminina e masculina no Brasil-(2010)")
plt.gca().invert_yaxis()

plt.show()


text:
"""Utility functions for AFQ-Insight."""
import numpy as np

__all__ = ["ecdf"]

CANONICAL_TRACT_NAMES = [
    "Left Arcuate",
    "Left SLF",
    "Left Uncinate",
    "Left ILF",
    "Left IFOF",
    "Left Cingulum Hippocampus",
    "Left Thalamic Radiation",
    "Left Corticospinal",
    "Left Cingulum Cingulate",
    "Callosum Forceps Minor",
    "Callosum Forceps Major",
    "Right Cingulum Cingulate",
    "Right Corticospinal",
    "Right Thalamic Radiation",
    "Right Cingulum Hippocampus",
    "Right IFOF",
    "Right ILF",
    "Right Uncinate",
    "Right SLF",
    "Right Arcuate",
]

def ecdf(data, reverse=False):
    """Compute ECDF for a one-dimensional array of measurements.

    Parameters
    ----------
    data : np.ndarray
        one-dimensional array of measurements

    reverse : bool, default=False
        If True, reverse the sorted data so that ecdf runs from top-left
        to bottom-right.

    Returns
    -------
    x : numpy.ndarray
        sorted data
    y : numpy.ndarray
        cumulative probability
    """
    # Number of data points: n
    n = len(data)

    # x-data for the ECDF: x
    x = np.sort(data)
    if reverse:
        x = np.flip(x)

    # y-data for the ECDF: y
    y = np.arange(1, n + 1) / n

    return x, y

max_stars_repo_name: mnarayan/AFQ-Insight

max_issues_repo_name: mnarayan/AFQ-Insight

max_forks_repo_name: mnarayan/AFQ-Insight


text:
from typing import Callable

import pandas as pd
import numpy as np

"""
From raw .xlsx ESG data construct a function of year that yields pd.DataFrame with companies as row, type of index as columns, and scores.
"""
def yearly_esg_maker(raw:pd.DataFrame) -> Callable[[str, str], pd.DataFrame]:

    data = raw.iloc[3:, :]
    data.columns = raw.iloc[2, :].apply(
        lambda e: str(int(e) if type(e) is np.float64 else str(e)))

    data = data.reset_index(drop = True)

    name_df = data["Name"].str.rsplit("-", n = 1, expand = True)
    data["Name"] = name_df[0]
    data["Data type"] = name_df[1]

    def getyear(year):
        if int(year) < 2016 or int(year) > 2021:
            raise ValueError(f"Year {year} not valid. Data only available for period 2016-2021.")

        year_df = pd.pivot_table(data, index = ["Name"], columns=["Data type"], values = [str(year)])[str(year)]

        year_df.index = [s.replace(" ", "") for s in year_df.index]
        year_df.columns = [s.strip() for s in year_df.columns]

        return year_df

    return getyear

max_stars_repo_name: NoFishLikeIan/nlp-financial-env

max_issues_repo_name: NoFishLikeIan/nlp-financial-env

max_forks_repo_name: NoFishLikeIan/nlp-financial-env


text:
'''
@author: Sergio Rojas
@contact: rr.sergio@gmail.com
--------------------------
Contenido bajo
 Atribución-NoComercial-CompartirIgual 3.0 Venezuela (CC BY-NC-SA 3.0 VE)
 http://creativecommons.org/licenses/by-nc-sa/3.0/ve/

Creado en abril 23, 2016
'''

#In [1]:
import numpy as np

#In [2]:
x = [1,2,3,4,5,6,7,8,9]

#In [3]:
nfilas = 3

#In [4]:
ncolums = 3

#In [5]:
print(x)

#In [6]:
X = np.array(x).reshape(nfilas , ncolums)

#In [7]:
print(X)

#In [8]:
print(type(x))

#In [9]:
print(type(X))

#In [10]:
u = np.array(x)

#In [11]:
print(u)

#In [12]:
print(type(u))

#In [13]:
U = u.reshape(nfilas , ncolums)

#In [14]:
print(U)

#In [15]:
print(U-X)

#In [16]:
print(U**2)

#In [17]:
print(x**2)

#In [18]:


text:
import torch

from alaska2.loss import ContrastiveCosineEmbeddingLoss, PairwiseRankingLoss, PairwiseRankingLossV2, RocAucLoss

def test_roc_auc_loss():
    c = RocAucLoss()

    x = torch.tensor([-10, 10]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([10, -10]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([0, 0]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([10, 10]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([-10, -9]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    import matplotlib.pyplot as plt
    import numpy as np

    x_coords = np.arange(-5, 5, 0.1)
    y_loss_neg = []
    y_loss_pos = []
    for x_val in x_coords:
        x = torch.tensor([x_val, 2], dtype=torch.float32)
        y = torch.tensor([0, 1])
        loss = c(x, y)
        y_loss_neg.append(loss.item())

        x = torch.tensor([-2, x_val], dtype=torch.float32)
        y = torch.tensor([0, 1])
        loss = c(x, y)
        y_loss_pos.append(loss.item())

    plt.figure()
    plt.plot(x_coords, y_loss_neg, label="neg")
    plt.plot(x_coords, y_loss_pos, label="pos")
    plt.legend()
    plt.show()

def test_pairwise_rank_loss():
    c = PairwiseRankingLoss()

    x = torch.tensor([-10, 10]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([10, -10]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([0, 0]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([10, 10]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

    x = torch.tensor([-10, -9]).float()
    y = torch.tensor([0, 1])
    loss = c(x, y)
    print(loss)

def test_pairwise_rank_loss_v2():
    c = PairwiseRankingLossV2()

    # x = torch.tensor([-10, 10, -10, 10]).float()
    y = torch.tensor([0, 1, 0, 1])
    # loss = c(x, y)
    # print(loss)
    #
    # x = torch.tensor([10, -10, -10, 10]).float()
    # loss = c(x, y)
    # print(loss)
    #
    # x = torch.tensor([0, 0, 0, 0]).float()
    # loss = c(x, y)
    # print(loss)
    #
    # x = torch.tensor([10, 10, -10, -10]).float()
    # loss = c(x, y)
    # print(loss)

    x = torch.tensor([-10, -1, 1, 10]).float()
    # x = torch.tensor([-10, 0, 0, 10]).float()
    loss = c(x, y)
    print(loss)

def test_ccos():
    # input = torch.ones((8, 32)).float()
    # target = torch.zeros(8).long()
    #
    # criterion = ContrastiveCosineEmbeddingLoss()
    # loss = criterion(input, target)
    # print(loss)
    # assert loss == 0
    #
    # input = torch.ones((8, 32)).float()
    # target = torch.ones(8).long()
    #
    # criterion = ContrastiveCosineEmbeddingLoss()
    # loss = criterion(input, target)
    # print(loss)
    # assert loss == 0

    input = torch.randn((160, 32)).float().cuda()
    target = torch.randint(4, (160,)).long().cuda()

    criterion = ContrastiveCosineEmbeddingLoss()
    loss = criterion(input, target)
    print(loss)


text:
from argparse import ArgumentParser

import numpy as np

from elasticai.creator.vhdl.precomputed_scalar_function import Tanh
from elasticai.creator.vhdl.vhdl_formatter.vhdl_formatter import format_vhdl

def main():
    arg_parser = ArgumentParser()
    arg_parser.add_argument(
        "--file",
        help="filepath of the generated vhd file",
        required=True,
    )
    args = arg_parser.parse_args()
    file_path = args.file

    tanh = Tanh(data_width=16, frac_width=8, x=np.linspace(-5, 5, 259))
    with open(file_path, "w") as writer:
        tanh_code = tanh()
        for line in tanh_code:
            writer.write(line + "\n")

    format_vhdl(file_path=file_path)

if __name__ == "__main__":
    main()


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

import phyre.creator as creator_lib

import numpy as np

@creator_lib.define_task_template(
    seed=range(1000),
    version="2",
    search_params=dict(
        max_search_tasks=1000,
        reject_ball_solvable=True,
        require_two_ball_solvable=True,
    ),
)
def build_task(C, seed):
    rng = np.random.RandomState(seed=seed)

    # Set properties of objects.
    scene_width = C.scene.width
    scene_height = C.scene.height

    # Create ball.
    center = rng.uniform(0.2, 0.8)
    ball = C.add('dynamic ball', scale=0.1) \
        .set_center(center * scene_width, 0.9 * scene_height)

    top = (ball.bottom - ball.height) / scene_height

    # Create a bunch of stars (y, x).
    stars = []
    #for _ in range(15):
    #    x = (rng.normal() / 6 + 0.5)
    #    y = rng.uniform(0.1, top)
    #    stars.append((x, y))

    angle = 0
    stars = [(center, 0.7)]
    line_length = 1
    n_valid = 0
    while n_valid < 15:
        if line_length >= 3 and rng.uniform() < 0.5:
            # x = rng.uniform()
            # y = rng.uniform(0.1, top)
            x = rng.normal()
            y = rng.normal()
            l = (x * x + y * y) ** .5
            x = x / l * 0.2 + 0.5
            y = y / l * 0.2 + 0.5
            line_length = 1
            angle = rng.uniform() * 2 * np.pi
        else:
            line_length += 1
            step = rng.uniform(0.05, 0.2)
            angle += rng.uniform() * 2 * np.pi / 8
            dx, dy = step * np.cos(angle), step * np.sin(angle)
            x, y = stars[-1]
            x += dx
            y += dy
        stars.append((x, y))
        if  0.0 < x < 1 and 0.0 < y < 1:
            n_valid += 1
    for i, (x, y) in enumerate(stars):
        C.add('static ball', scale=0.05) \
            .set_center(scene_width * x, scene_height * y)

    #x = abs(rng.normal() / 6) + 0.1
    #if ball.center_x < 0.5 * scene_width:
    #    x = 1. - x
    #y = rng.uniform(0.1, top)
    #selected_star = C.add('dynamic ball', scale=0.05) \
    #    .set_center(scene_width * x, scene_height * y)
    #C.add('static bar', scale=0.05) \
    #    .set_center(scene_width * x, scene_height * (y - 0.15))

    # Create task.
    C.update_task(body1=ball,
                  #body2=selected_star,
                  body2=C.bottom_wall,
                  relationships=[C.SpatialRelationship.TOUCHING])


text:
'''
A Python program for testing the regrid3d.CurvRect3DRegridder class "by hand"
and also serves as a coding example of using this class.

@author: Karl Smith
'''

from __future__ import print_function

import sys
import numpy
import ESMP
from esmpcontrol import ESMPControl
from regrid3d import CurvRect3DRegridder

GULF_MEX_LONS = (262.0, 278.0, 0.5)
GULF_MEX_LATS = (18.0, 31.0, 0.5)
# Gulf of Mexico Relief of the Surface of the Earth (meters) 98W:82W:0.5 (columns), 18N:31N:0.5 (rows)
GULF_MEX_ROSE = (
      ( 1627.1,  2130.9,  1317.9,   373.8,   125.7,    45.0,    25.0,    20.0,    25.0,    25.0,
          15.0,    15.0,    25.0,    25.0,    40.0,    74.3,   169.7,   179.7,   151.3,    23.0,
          -3.2, -1638.9, -3668.0, -4098.9, -3075.3, -3017.9, -4554.6, -4969.7, -4961.6, -5008.2,
       -4759.7, -4932.3, -4016.7, ),
      ( 1951.7,  1669.0,   685.7,    63.4,    50.7,   779.4,   400.0,   -70.7,   -49.0,   -16.2,
          -6.1,     2.0,    -9.6,     5.0,    69.7,    80.1,   194.7,   210.2,   110.6,     6.1,
          -0.3,  -192.5, -1587.3, -4357.8, -4401.9, -4397.7, -2980.1, -2035.6,  -463.5, -2265.7,
       -4933.4, -4959.4, -4490.4, ),
      ( 2220.5,  2654.2,  1232.2,    60.8,   -10.0,  -217.9, -1170.2,  -807.9,  -599.1,  -388.8,
        -101.1,   -40.1,   -14.0,    -8.1,    19.9,    65.0,   139.4,   175.8,   115.2,    40.3,
           0.0,  -220.1,  -919.6, -4284.3, -4502.4, -4474.9, -4418.9, -3648.0, -2258.9, -2581.0,
       -2998.4, -3578.1, -3493.9, ),
      ( 2593.1,  2368.9,  1097.8,    29.9,  -225.9, -1889.3, -2357.3, -1504.2, -1161.4,  -939.7,
        -702.9,  -141.0,   -47.9,   -29.0,   -13.1,    29.7,    94.8,   150.0,   110.0,    70.5,
           0.0,  -340.6, -1551.1, -4353.5, -4516.0, -4534.5, -4455.3, -4438.0, -4153.9, -3078.7,
       -3004.0, -3228.8, -3595.4, ),
      ( 1944.0,   923.6,   191.8,   -12.8, -1599.3, -2398.4, -2802.9, -1996.6, -1597.9, -1039.4,
       -1061.3, -1345.8,   -62.4,   -25.9,   -21.1,    -9.9,    79.4,   120.0,    65.3,    30.5,
          -1.0,  -278.2,  -994.7, -1794.1, -4315.4, -4438.8, -4437.1, -4443.0, -4378.4, -3605.0,
       -3977.6, -4239.2, -2402.8, ),
      (  459.9,   210.9,    -9.9,  -588.9, -1913.3, -2591.4, -3053.4, -2975.4, -2016.1, -1992.1,
       -1619.8, -1926.1,   -38.2,   -23.6,   -23.0,    -0.1,    30.0,    30.2,    24.8,    15.0,
          10.0,     5.0,  -262.5, -1178.6, -2949.8, -4621.6, -4421.9, -4450.7, -4401.8, -4361.9,
       -4401.3, -4437.3, -3876.6, ),
      (  108.9,    23.3,   -97.1, -1136.2, -2098.3, -2602.7, -3192.4, -3178.5, -2608.1, -2588.1,
       -2597.9,  -660.0,    -9.9,   -40.1,   -29.0,   -10.1,     5.0,    10.0,    10.0,    10.0,
          10.0,    10.0,     0.0,  -276.1, -1413.3, -4101.5, -3835.0, -4424.2, -4415.9, -4443.6,
       -4443.1, -4460.6, -4399.8, ),
      (   69.7,   -10.0,  -402.6, -1785.3, -2391.5, -2800.2, -3278.6, -3408.0, -3111.9, -3002.1,
       -3014.9,  -782.6,   -44.6,   -17.9,   -21.9,   -29.0,   -21.0,   -14.0,   -12.0,     0.0,
           0.0,   -10.0,   -10.0,  -103.2, -1775.1, -2001.9, -2965.9, -3711.9, -3032.2, -2801.0,
          -3.1,  -920.6, -1273.1, ),
      (   64.0,   -10.0, -1313.9, -2190.4, -2769.9, -2899.3, -3317.6, -3536.4, -3564.0, -3364.1,
       -3331.0, -1715.7,   -13.2,   -47.9,   -19.7,   -46.2,   -45.9,   -40.0,   -33.1,    -3.1,
         -24.9,    -4.2,   -10.0,  -202.8, -1409.1, -1982.8,  -298.8,     0.0,  -575.5,  -339.9,
          -3.9,   -10.0,   -10.0, ),
      (   -8.5,   -10.0, -1714.1, -2321.6, -2790.2, -3072.8, -3446.8, -3621.8, -3639.0, -3635.0,
       -3581.5, -3569.9, -3367.2, -1055.4,  -227.7,  -117.0,   -53.2,   -47.7,   -61.2,   -55.2,
         -53.0,   -50.0,  -133.0,  -507.9, -1065.2, -1840.9, -2201.4,  -907.3,   192.0,    32.1,
          -2.2,   -10.0,   -10.0, ),
      (    7.2,   -45.9, -1526.8, -2196.7, -2588.5, -3178.9, -3593.6, -3630.1, -3623.9, -3628.0,
       -3621.0, -3619.0, -3709.8, -3908.8, -3558.9, -1507.3,  -237.2,   -45.2,    -4.3,   -62.2,
         -48.1,   -32.1,  -394.2,  -807.6, -1193.3, -1139.0, -2075.7, -2395.8, -2128.4, -1034.6,
          42.3,   112.0,   125.4, ),
      (  134.6,   -39.1, -1078.0, -2179.4, -2595.1, -3002.4, -3565.4, -3618.1, -3666.8, -3679.0,
       -3648.2, -3598.0, -3597.9, -3602.0, -3695.7, -3695.5, -1106.2,  -725.6,   -12.7,   -10.0,
         -92.3,  -151.6,  -591.2,  -977.7, -2939.2, -1435.4, -3011.0, -2948.8, -2399.3, -2182.2,
       -1800.0, -1789.0, -1570.9, ),
      (   55.0,   -35.2,  -838.4, -1609.6, -2488.7, -3268.8, -3594.0, -3613.1, -3724.5, -3727.1,
       -3714.9, -3671.4, -3739.6, -3718.0, -3655.8, -3607.0, -3826.2, -3652.7, -3069.2,  -352.8,
        -181.9,  -396.3,  -994.3, -1254.2, -3228.1, -3401.8, -3468.6, -2829.9, -2184.4, -1017.2,
       -1006.6,  -808.0, -1059.1, ),
      (   -8.7,   -18.7,  -141.0, -1008.6, -2264.6, -3205.6, -3619.4, -3620.2, -3654.8, -3655.1,
       -3631.0, -3622.0, -3611.0, -3609.0, -3616.1, -3611.1, -3600.8, -3851.1, -3622.7, -2515.4,
       -1578.2, -1492.1, -1769.6, -3051.5, -3383.7, -3415.2, -3402.4, -3268.5, -1954.1,  -192.3,
         -40.4,   -13.9,   -34.0, ),
      (    2.1,   -10.0,   -65.5,  -487.2, -1596.1, -2703.7, -3579.7, -3627.0, -3628.0, -3612.1,
       -3609.9, -3578.6, -3542.4, -3511.9, -3587.7, -3583.2, -3545.1, -3585.9, -3544.5, -3302.6,
       -3388.8, -3189.2, -3241.8, -3287.6, -3321.9, -3363.9, -3249.2, -2189.4,  -127.6,   -33.0,
         -43.8,   -35.9,   -10.0, ),
      (   17.1,     2.0,   -25.7,  -129.9, -1088.0, -1432.3, -3386.1, -3450.0, -3524.9, -3534.9,
       -3478.5, -3322.9, -3172.3, -3173.0, -3485.8, -3474.6, -3377.6, -3338.1, -3346.9, -3323.2,
       -3332.8, -3348.0, -3326.2, -3248.5, -3205.8, -3292.8, -3125.2,  -576.5,   -10.0,   -95.3,
         -54.2,   -34.1,   -10.1, ),
      (   17.2,     5.1,   -26.6,   -16.6,  -984.4, -1684.9, -2579.5, -3005.0, -3172.7, -3123.2,
       -2726.5, -2209.1, -2458.3, -2407.9, -3268.3, -3300.9, -3207.9, -3135.3, -3060.5, -2991.1,
       -3029.2, -3116.5, -3162.7, -3184.2, -3205.8, -3252.9, -2952.0,  -322.8,   -16.8,  -100.2,
         -44.2,   -30.0,   -12.1, ),
      (   13.1,   -10.0,   -29.0,   -98.8, -1000.0, -1589.9, -1674.0, -1989.6, -1577.0, -1851.7,
       -1797.0, -1904.0, -1999.0, -2290.0, -2206.1, -2795.8, -2937.9, -2917.3, -2723.1, -2597.1,
       -2752.5, -2909.1, -2999.6, -3094.5, -3177.6, -3381.8, -2460.4,  -200.7,   -85.4,   -73.2,
         -36.1,   -16.1,     0.9, ),
      (   14.0,    -9.7,   -41.8,  -140.8,  -789.2, -1386.5, -1399.6, -1211.4, -1293.0, -1023.3,
       -1212.6, -1503.1, -1733.2, -1991.2, -1894.8, -1864.4, -2391.9, -2511.8, -2282.3, -2586.4,
       -2724.9, -2879.5, -2959.6, -3129.6, -3203.8, -3205.9,  -817.6,  -102.9,   -89.2,   -50.3,
         -33.1,    -9.1,     7.8, ),
      (   19.3,    -9.6,   -28.7,   -67.7,  -205.8,  -796.4,  -994.9,  -950.1,  -881.5,  -601.2,
        -796.9,  -869.5,  -814.3, -1010.9, -1083.3, -1199.0, -1203.9, -1873.3, -1800.9, -2018.7,
       -2598.2, -2898.5, -3047.9, -3258.1, -3224.9, -1994.5,  -406.1,  -116.8,   -65.2,   -39.1,
         -17.2,    13.8,    20.2, ),
      (   41.6,    14.1,    -5.8,   -28.8,   -43.9,   -61.6,   -58.8,   -89.9,   -80.0,  -100.0,
        -103.2,  -105.9,  -134.1,  -203.9,  -211.7,  -228.9,  -537.9, -1026.6, -1397.3, -2180.6,
       -2476.4, -2800.7, -2668.6, -2293.7, -1026.3,  -600.2,  -230.9,   -80.6,   -44.1,   -29.0,
          -9.1,     8.9,    38.4, ),
      (  105.0,    44.1,     7.2,     0.0,    -7.9,   -27.9,   -31.0,   -28.0,   -41.9,   -47.0,
         -47.0,   -49.0,   -49.0,   -39.0,   -29.0,   -37.9,  -196.6,  -395.4,  -885.8, -1677.7,
       -2276.7, -1953.6,  -873.2,  -570.8,  -303.7,  -200.0,  -100.1,   -50.1,   -33.1,   -23.1,
         -10.1,    36.6,    35.8, ),
      (   85.3,    81.1,    31.2,    15.0,    11.1,     2.1,    -7.0,   -17.9,   -19.0,   -20.0,
         -22.0,   -25.0,   -18.0,   -10.0,    -3.0,    -8.0,   -24.8,   -11.4,   -77.7,  -502.6,
       -1330.0, -1583.2,  -704.2,  -377.0,  -219.5,   -99.1,   -42.1,   -34.1,   -28.0,   -14.1,
          -4.1,    16.4,    25.0, ),
      (  189.7,    83.3,    79.0,    45.2,    29.9,    15.0,   -10.7,    -1.3,   -12.0,    -9.0,
         -12.0,    -9.0,    -1.0,    -1.0,     1.0,     1.0,    -0.3,    -9.0,   -13.9,   -42.7,
         -51.4,  -109.8,  -397.0,  -187.9,   -58.6,   -20.2,    -9.0,   -20.1,   -19.0,    -4.2,
          11.8,    24.0,    36.4, ),
      (  210.0,   148.8,   104.6,    76.4,    56.2,    26.2,    14.1,     8.1,     1.1,     2.9,
           5.0,     3.1,     6.0,     1.0,     3.0,     3.0,    -9.6,    -0.3,   -10.0,   -24.8,
         -28.1,   -28.0,   -95.3,   -58.0,   -31.1,     4.8,     8.9,     0.1,    -1.0,    16.0,
          12.0,    32.1,    24.7, ),
      (  259.9,   163.1,   111.3,    66.4,   105.2,    97.8,    20.5,    30.1,    20.2,    17.1,
          12.0,    14.0,     8.0,     7.0,     9.0,     6.1,    15.9,    20.1,     9.0,    14.8,
          -1.0,     4.2,    -0.8,     3.8,    17.9,    46.0,    53.6,    21.5,    39.1,    32.4,
          34.1,    31.9,    19.2, ),
      (  272.2,   149.6,   114.9,   130.2,    88.9,    55.0,    95.1,    92.3,    93.1,    58.1,
          52.1,    23.1,    11.9,    71.1,    66.6,    91.5,    70.5,    76.5,    59.7,    54.1,
          11.9,    68.2,    64.6,    76.9,    37.2,    37.3,    44.9,    63.2,    76.9,    67.6,
          51.2,    33.1,     9.2, ),
)

def createExampleCurvData():
    '''
    Creates and returns example longitude, latitudes, depth, and data
    for a curvilinear grid.  Assigns grid center point data[i,j,k]
        = -2 * sin(lon[i,j,k] + 20) * cos(lat[i,j,k]) / log(depth[i,j,k] + 10)
            for valid center points
        = 1.0E20 for invalid center points

    Arguments:
        None
    Returns:
        (corner_lons, corner_lats, corner_depths, corner_invalid,
         center_lons, center_lats, center_depths, center_invalid, data) where:
        corner_lons:    numpy 3D array of curvilinear corner point longitude coordinates
        corner_lats:    numpy 3D array of curvilinear corner point latitude coordinates
        corner_depths:  numpy 3D array of curvilinear corner point depth coordinates
        corner_invalid: numpy 3D array of logicals indicating invalid corner points
        center_lons:    numpy 3D array of curvilinear center point longitude coordinates
        center_lats:    numpy 3D array of curvilinear center point latitude coordinates
        center_depths:  numpy 3D array of curvilinear center point depth coordinates
        center_invalid: numpy 3D array of logicals indicating invalid center points
        data:           numpy 3D array of curvilinear center point data values
    '''
    # Longitudes for the Gulf of Mexico ROSE data
    start = GULF_MEX_LONS[0]
    end = GULF_MEX_LONS[1]
    delta = GULF_MEX_LONS[2]
    # numpy.arange does not include the ending value
    end += 0.5 * delta
    center_lons = numpy.arange(start, end, delta)
    corner_lons = numpy.arange(start - 0.5 * delta, end + 0.5 * delta, delta)

    # Latitudes for the Gulf of Mexico ROSE data
    start = GULF_MEX_LATS[0]
    end = GULF_MEX_LATS[1]
    delta = GULF_MEX_LATS[2]
    # numpy.arange does not include the ending value
    end += 0.5 * delta
    center_lats = numpy.arange(start, end, delta)
    corner_lats = numpy.arange(start - 0.5 * delta, end + 0.5 * delta, delta)

    # arbitrary depths as sigma coordinates
    center_sigmas = numpy.array(    (0.200, 0.030, 0.075, 0.150, 0.300, 0.450, 0.600, 0.800) )
    corner_sigmas = numpy.array( (0.015, 0.025, 0.053, 0.113, 0.225, 0.375, 0.525, 0.700, 0.900) )
    center_shape = (center_lons.shape[0], center_lats.shape[0], center_sigmas.shape[0])
    corner_shape = (corner_lons.shape[0], corner_lats.shape[0], corner_sigmas.shape[0])

    # Expand longitudes, latitudes, and sigmas to 3D arrays
    center_lons = numpy.tile(center_lons, center_shape[1] * center_shape[2]) \
                       .reshape(center_shape, order='F')
    center_lats = numpy.tile(numpy.repeat(center_lats, center_shape[0]),
                             center_shape[2]) \
                       .reshape(center_shape, order='F')
    center_sigmas = numpy.repeat(center_sigmas, center_shape[0] * center_shape[1]) \
                         .reshape(center_shape, order='F')
    corner_lons = numpy.tile(corner_lons, corner_shape[1] * corner_shape[2]) \
                       .reshape(corner_shape, order='F')
    corner_lats = numpy.tile(numpy.repeat(corner_lats, corner_shape[0]),
                             corner_shape[2]) \
                       .reshape(corner_shape, order='F')
    corner_sigmas = numpy.repeat(corner_sigmas, corner_shape[0] * corner_shape[1]) \
                         .reshape(corner_shape, order='F')

    # Add a zeta coordinate (sea swell above earth radius) - less than 1.0 meter
    zeta = -1.0 * numpy.sin(numpy.deg2rad(center_lons)) * numpy.cos(numpy.deg2rad(center_lats))

    # Convert sigma, zeta coordinates to center depths
    # Modify the depths so nothing more than 325 meters deep to better match rectilinear data
    gulf_mex_center_rose = numpy.array(GULF_MEX_ROSE).flatten()
    gulf_mex_center_rose[ (gulf_mex_center_rose < -325.0) ] = -325.0
    gulf_mex_center_rose = numpy.tile(gulf_mex_center_rose, center_shape[2]) \
                                .reshape(center_shape, order='F')
    center_invalid = ( gulf_mex_center_rose > -10.0 )
    center_depths = center_sigmas * (zeta - gulf_mex_center_rose) - zeta

    # Compute the data values for these center points
    insea = numpy.logical_not(center_invalid)
    data = numpy.empty(center_shape, dtype=numpy.float64, order='F')
    data[center_invalid] = 1.0E20
    data[insea] = -2.0 * numpy.sin(numpy.deg2rad(center_lons[insea] + 20.0)) \
                       * numpy.cos(numpy.deg2rad(center_lats[insea])) \
                       / numpy.log10(center_depths[insea] + 10.0)

    # Convert sigma, zeta coordinates to corner depths
    # Estimate the corner depths using the center depths;
    # edges not too critical since most are over land
    gulf_mex_corner_rose = numpy.zeros((corner_shape[0], corner_shape[1]),
                                       dtype=numpy.float64, order='F')
    gulf_mex_corner_rose[1:-1,1:-1] = ( gulf_mex_center_rose[:-1, :-1, 0] + \
                                        gulf_mex_center_rose[1:,  :-1, 0] + \
                                        gulf_mex_center_rose[1:,  1:,  0] + \
                                        gulf_mex_center_rose[:-1, 1:,  0] ) / 4.0
    gulf_mex_corner_rose[ 0, 1:-1] = 2.0 * gulf_mex_corner_rose[ 1, 1:-1] - gulf_mex_corner_rose[ 2, 1:-1]
    gulf_mex_corner_rose[-1, 1:-1] = 2.0 * gulf_mex_corner_rose[-2, 1:-1] - gulf_mex_corner_rose[-3, 1:-1]
    gulf_mex_corner_rose[:,  0] = 2.0 * gulf_mex_corner_rose[:,  1] - gulf_mex_corner_rose[:,  2]
    gulf_mex_corner_rose[:, -1] = 2.0 * gulf_mex_corner_rose[:, -2] - gulf_mex_corner_rose[:, -3]
    gulf_mex_corner_rose = numpy.tile(gulf_mex_corner_rose.flatten('F'), corner_shape[2]) \
                                .reshape(corner_shape, order='F')
    zeta = -1.0 * numpy.sin(numpy.deg2rad(corner_lons)) * numpy.cos(numpy.deg2rad(corner_lats))
    corner_invalid = ( gulf_mex_corner_rose > -10.0 )
    corner_depths = corner_sigmas * (zeta - gulf_mex_corner_rose) - zeta

    return (corner_lons, corner_lats, corner_depths, corner_invalid,
            center_lons, center_lats, center_depths, center_invalid, data)

def createExampleRectData():
    '''
    Creates and returns example longitude, latitudes, depth, and data
    for a rectilinear grid.  Covers approximately the same region given
    by createExampleCurvData.  Assigns grid center point data[i,j,k]
        = -2 * sin(lon[i] + 20) * cos(lat[j]) / log(depth[k] + 10)
            for valid center points
        = 1.0E34 for invalid center points

    Arguments:
        None
    Returns:
        (corner_lons, corner_lats, corner_depths,
         center_lons, center_lats, center_depths, data) where:
        corner_lons:    numpy 1D array of rectilinear corner plane longitudes
        corner_lats:    numpy 1D array of rectilinear corner plane latitudes
        corner_depths:  numpy 1D array of rectilinear corner plane depths
        corner_invalid: numpy 3D array of logicals indicating invalid corner points
        center_lons:    numpy 1D array of rectilienar center plane longitudes
        center_lats:    numpy 1D array of rectilinear center plane latitudes
        center_depths:  numpy 1D array of rectilinear center plane depths
        center_invalid: numpy 3D array of logicals indicating invalid center points
        data:           numpy 3D array of rectilinear center point data values
    '''
    # Longitudes for the Gulf of Mexico ROSE data
    start = GULF_MEX_LONS[0]
    end = GULF_MEX_LONS[1]
    delta = GULF_MEX_LONS[2]
    # numpy.arange does not include the ending value
    end += 0.5 * delta
    center_lons = numpy.arange(start, end, delta)
    corner_lons = numpy.arange(start - 0.5 * delta, end + 0.5 * delta, delta)
    # Latitudes for the Gulf of Mexico ROSE data
    start = GULF_MEX_LATS[0]
    end = GULF_MEX_LATS[1]
    delta = GULF_MEX_LATS[2]
    # numpy.arange does not include the ending value
    end += 0.5 * delta
    center_lats = numpy.arange(start, end, delta)
    corner_lats = numpy.arange(start - 0.5 * delta, end + 0.5 * delta, delta)
    # Arbitrarily chosen depths
    center_depths = numpy.array(   (10.0,  25.0,  50.0, 100.0, 150.0, 250.0) )
    # Edges for these irregularily spaced depths
    corner_depths = numpy.array( (5.0,  17.5,  37.5,  75.0, 125.0, 200.0, 300.0) )

    # Create the data values
    center_shape = (center_lons.shape[0], center_lats.shape[0], center_depths.shape[0])
    center_3d_lons = numpy.tile(center_lons, center_shape[1] * center_shape[2]) \
                          .reshape(center_shape, order='F')
    center_3d_lats = numpy.tile(numpy.repeat(center_lats, center_shape[0]),
                                center_shape[2]) \
                          .reshape(center_shape, order='F')
    center_3d_depths = numpy.repeat(center_depths, center_shape[0] * center_shape[1]) \
                            .reshape(center_shape, order='F')
    data = -2.0 * numpy.sin(numpy.deg2rad(center_3d_lons + 20.0)) \
                * numpy.cos(numpy.deg2rad(center_3d_lats)) \
                / numpy.log10(center_3d_depths + 10.0)

    # Use GULD_MEX_ROSE as a land mask to mark undefined data
    gulf_mex_center_rose = numpy.array(GULF_MEX_ROSE).flatten()
    gulf_mex_center_rose = numpy.tile(gulf_mex_center_rose, center_shape[2]) \
                                .reshape(center_shape, order='F')
    center_invalid = numpy.logical_or( ( gulf_mex_center_rose > -10.0 ),
                                       ( center_3d_depths > -gulf_mex_center_rose ) )
    data[ center_invalid ] = 1.0E34

    # Convert sigma, zeta coordinates in corner depths
    # Estimate the corner depths using the center depths;
    # edges not too critical since most are over land
    corner_shape = (corner_lons.shape[0], corner_lats.shape[0], corner_depths.shape[0])
    corner_3d_depths = numpy.repeat(corner_depths, corner_shape[0] * corner_shape[1]) \
                            .reshape(corner_shape, order='F')
    gulf_mex_corner_rose = numpy.zeros((corner_shape[0], corner_shape[1]),
                                       dtype=numpy.float64, order='F')
    gulf_mex_corner_rose[1:-1,1:-1] = ( gulf_mex_center_rose[:-1, :-1, 0] + \
                                        gulf_mex_center_rose[1:,  :-1, 0] + \
                                        gulf_mex_center_rose[1:,  1:,  0] + \
                                        gulf_mex_center_rose[:-1, 1:,  0] ) / 4.0
    gulf_mex_corner_rose[ 0, 1:-1] = 2.0 * gulf_mex_corner_rose[ 1, 1:-1] - gulf_mex_corner_rose[ 2, 1:-1]
    gulf_mex_corner_rose[-1, 1:-1] = 2.0 * gulf_mex_corner_rose[-2, 1:-1] - gulf_mex_corner_rose[-3, 1:-1]
    gulf_mex_corner_rose[:,  0] = 2.0 * gulf_mex_corner_rose[:,  1] - gulf_mex_corner_rose[:,  2]
    gulf_mex_corner_rose[:, -1] = 2.0 * gulf_mex_corner_rose[:, -2] - gulf_mex_corner_rose[:, -3]
    gulf_mex_corner_rose = numpy.tile(gulf_mex_corner_rose.flatten('F'), corner_shape[2]) \
                                .reshape(corner_shape, order='F')
    corner_invalid = numpy.logical_or( ( gulf_mex_corner_rose > -10.0 ),
                                       ( corner_3d_depths > -gulf_mex_corner_rose ) )

    return (corner_lons, corner_lats, corner_depths, corner_invalid,
            center_lons, center_lats, center_depths, center_invalid, data)

def printDiffs(grid_lons, grid_lats, grid_depths, undef_val, max_negl,
                 expect_data, found_data):
    '''
    Prints significant differences between expect_data and found_data
    along with the location of these differences

    Arguments:
        grid_lons:   numpy 3D array of grid longitudes
        grid_lats:   numpy 3D array of grid latitudes
        grid_depths: numpy 3D array of grid depths
        undef_val:   numpy array of one value; the undefined data value
        max_negl:    maximumum negligible absolute difference
        expect_data: numpy 3D array of expected data values
        found_data:  numpy 3D array of data values to check
    Returns:
        None
    Raises:
        ValueError:  if the array shapes do not match
    '''
    if len(grid_lons.shape) != 3:
        raise ValueError("grid_lons is not 3D")
    if grid_lats.shape != grid_lons.shape:
        raise ValueError("grid_lats.shape != grid_lons.shape")
    if grid_depths.shape != grid_lons.shape:
        raise ValueError("grid_depth.shape != grid_lons.shape")
    if expect_data.shape != grid_lons.shape:
        raise ValueError("expect_data.shape != grid_lons.shape")
    if found_data.shape != grid_lons.shape:
        raise ValueError("found_data.shape != grid_lons.shape")
    different = ( numpy.abs(expect_data - found_data) > max_negl )
    diff_lons = grid_lons[different]
    diff_lats = grid_lats[different]
    diff_depths = grid_depths[different]
    diff_expect = expect_data[different]
    diff_found = found_data[different]
    diff_list = [ ]
    for (lon, lat, depth, expect, found) in \
            zip(diff_lons, diff_lats, diff_depths, diff_expect, diff_found):
        if expect == undef_val:
            # most serious - should have been masked out
            diff_list.append([2, lon, lat, depth, expect, found])
        elif found == undef_val:
            # least serious - destination not covered by source
            diff_list.append([0, lon, lat, depth, expect, found])
        else:
            # might be of concern
            diff_list.append([1, lon, lat, depth, expect, found])
    # order primarily from least to most serious,
    # secondarily smallest to largest longitude,
    # thirdly smallest to largest latitude
    # fourthly highest to deepest
    diff_list.sort()
    num_not_undef = 0
    num_undef = 0
    num_diff = 0
    for (_, lon, lat, depth, expect, found) in diff_list:
        if expect == undef_val:
            num_not_undef += 1
            print("lon = %#7.3f, lat = %7.3f, depth = %7.2f, expect =  undef, " \
                  "found = %#6.3f" % (lon, lat, depth, found))
        elif found == undef_val:
            num_undef += 1
            # print "lon = %#7.3f, lat = %7.3f, depth = %7.2f, expect = %#6.3f, " \
            #       "found =  undef" % (lon, lat, depth, expect)
        else:
            num_diff += 1
            print("lon = %#7.3f, lat = %7.3f, depth = %7.2f, expect = %#6.3f, " \
                  "found = %#6.3f, diff = %#6.3f" \
                  % (lon, lat, depth, expect, found, found - expect))
    print("%3d undefined when defined might be expected" % num_undef)
    print("%3d with absolute difference > %#.3f" % (num_diff, max_negl))
    print("%3d defined when undefined expected" % num_not_undef)
    print("%3d values in the grid" \
            % (expect_data.shape[0] * expect_data.shape[1] * expect_data.shape[2]))

if __name__ == '__main__':
    try:
        while True:
            print('cw2r: curvilinear with corners to rectilinear')
            print('co2r: curvilinear without corners to rectilinear')
            print('r2cw: rectilinear to curvilinear with corners')
            print('r2co: rectilinear to curvilinear without corners')
            print('Ctrl-D to quit')
            if sys.version_info[0] > 2:
                direction = input('Regrid test to run? ')
            else:
                direction = raw_input('Regrid test to run? ')
            direction = direction.strip().lower()
            if direction in ('cw2r', 'co2r', 'r2cw', 'r2co'):
                break
    except EOFError:
        raise SystemExit(0)

    # Synthesize curvilinear test data
    (curv_corner_lons, curv_corner_lats, curv_corner_depths, curv_corner_ignore,
     curv_center_lons, curv_center_lats, curv_center_depths, curv_center_ignore,
     curv_data) = createExampleCurvData()

    # Synthesize rectilinear test data
    (rect_corner_lons, rect_corner_lats, rect_corner_depths, rect_corner_ignore,
     rect_center_lons, rect_center_lats, rect_center_depths, rect_center_ignore,
     rect_data) = createExampleRectData()

    undef_val = numpy.array([-1.0E10], dtype=numpy.float64)

    # Create the expected results on the curvilinear grid
    curv_expect_data = curv_data.copy()
    curv_expect_data[curv_center_ignore] = undef_val

    # Create the expected results on the rectilinear grid
    rect_expect_data = rect_data.copy()
    rect_expect_data[rect_center_ignore] = undef_val

    # Initialize ESMP
    if not ESMPControl().startCheckESMP():
        raise RuntimeError("Unexpected failure to start ESMP")

    # Create the regridder
    regridder = CurvRect3DRegridder()

    if direction in ('cw2r', 'r2cw'):
        # Create the curvilinear grid with corner and center points
        regridder.createCurvGrid(curv_center_lons, curv_center_lats, curv_center_depths,
                                 curv_center_ignore, True, curv_corner_lons, curv_corner_lats,
                                 curv_corner_depths, curv_corner_ignore)
    elif direction in ('co2r', 'r2co'):
        # Create the curvilinear grid with only center points
        regridder.createCurvGrid(curv_center_lons, curv_center_lats, curv_center_depths,
                                 curv_center_ignore, True)
    else:
        raise ValueError("unexpected direction of %s" % direction)

    # Create the rectilinear grid with corner and center points
    regridder.createRectGrid(rect_center_lons, rect_center_lats, rect_center_depths,
                             rect_center_ignore, True, rect_corner_lons, rect_corner_lats,
                             rect_corner_depths, rect_corner_ignore)

    if direction in ('cw2r', 'co2r'):

        print("")
        if direction == 'cw2r':
            print("Examining rectilinear results from curvilinear with corners")
        else:
            print("Examining rectilinear results from curvilinear without corners")

        # Create the curvilinear source field
        regridder.assignCurvField(curv_data)

        # Create the rectilinear destination field
        regridder.assignRectField()

        # Generate the 3D rectilinear longitude, latitude, and depth arrays
        # only to simplify printing differences; not used in regridding
        rect_center_shape = (rect_center_lons.shape[0], rect_center_lats.shape[0],
                             rect_center_depths.shape[0])
        rect_3d_center_lons = numpy.tile(rect_center_lons,
                                         rect_center_shape[1] * rect_center_shape[2]) \
                                   .reshape(rect_center_shape, order='F')
        rect_3d_center_lats = numpy.tile(numpy.repeat(rect_center_lats,
                                                      rect_center_shape[0]),
                                         rect_center_shape[2]) \
                                   .reshape(rect_center_shape, order='F')
        rect_3d_center_depths = numpy.repeat(rect_center_depths,
                                             rect_center_shape[0] * rect_center_shape[1]) \
                                     .reshape(rect_center_shape, order='F')

        if direction == 'cw2r':
            # Regrid from curvilinear to rectilinear using the conserve method
            # Corners required for this method
            rect_regrid_data = regridder.regridCurvToRect(undef_val, ESMP.ESMP_REGRIDMETHOD_CONSERVE)
            # Print the differences between the expected and regrid data
            print("")
            print("analytic (expect) versus conserve regridded (found) differences")
            printDiffs(rect_3d_center_lons,
                       rect_3d_center_lats,
                       rect_3d_center_depths,
                       undef_val, 0.001,
                       rect_expect_data,
                       rect_regrid_data)

        # Regrid from curvilinear to rectilinear using the bilinear method
        rect_regrid_data = regridder.regridCurvToRect(undef_val, ESMP.ESMP_REGRIDMETHOD_BILINEAR)
        # Print the differences between the expected and regrid data
        print("")
        print("analytic (expect) versus bilinear regridded (found) differences")
        printDiffs(rect_3d_center_lons,
                   rect_3d_center_lats,
                   rect_3d_center_depths,
                   undef_val, 0.12,
                   rect_expect_data,
                   rect_regrid_data)

        # Regrid from curvilinear to rectilinear using the patch method
        rect_regrid_data = regridder.regridCurvToRect(undef_val, ESMP.ESMP_REGRIDMETHOD_PATCH)
        # Print the differences between the expected and regrid data
        print("")
        print("analytic (expect) versus patch regridded (found) differences")
        printDiffs(rect_3d_center_lons,
                   rect_3d_center_lats,
                   rect_3d_center_depths,
                   undef_val, 1.0,
                   rect_expect_data,
                   rect_regrid_data)

    elif direction in ('r2cw', 'r2co'):

        print("")
        if direction == 'r2cw':
            print("Examining curvilinear with corners results from rectilinear")
        else:
            print("Examining curvilinear without corners results from rectilinear")

        # Create the rectilinear source field
        regridder.assignRectField(rect_data)

        # Create the curvilinear destination field
        regridder.assignCurvField(None)

        if direction == 'r2cw':
            # Regrid from rectilinear to curvilinear using the conserve method
            # Corners required for this method
            curv_regrid_data = regridder.regridRectToCurv(undef_val, ESMP.ESMP_REGRIDMETHOD_CONSERVE)
            # Print the differences between the expected and regrid data
            print("")
            print("analytic (expect) versus conserve regridded (found) differences")
            printDiffs(curv_center_lons[:,:,1:-1],
                       curv_center_lats[:,:,1:-1],
                       curv_center_depths[:,:,1:-1],
                       undef_val, 0.001,
                       curv_expect_data[:,:,1:-1],
                       curv_regrid_data[:,:,1:-1])

        # Regrid from rectilinear to curvilinear using the bilinear method
        curv_regrid_data = regridder.regridRectToCurv(undef_val, ESMP.ESMP_REGRIDMETHOD_BILINEAR)
        # Print the differences between the expected and regrid data
        print("")
        print("analytic (expect) versus bilinear regridded (found) differences")
        printDiffs(curv_center_lons[:,:,1:-1],
                   curv_center_lats[:,:,1:-1],
                   curv_center_depths[:,:,1:-1],
                   undef_val, 0.25,
                   curv_expect_data[:,:,1:-1],
                   curv_regrid_data[:,:,1:-1])

        # Regrid from rectilinear to curvilinear using the patch method
        curv_regrid_data = regridder.regridRectToCurv(undef_val, ESMP.ESMP_REGRIDMETHOD_PATCH)
        # Print the differences between the expected and regrid data
        print("")
        print("analytic (expect) versus patch regridded (found) differences")
        printDiffs(curv_center_lons[:,:,1:-1],
                   curv_center_lats[:,:,1:-1],
                   curv_center_depths[:,:,1:-1],
                   undef_val, 1.1,
                   curv_expect_data[:,:,1:-1],
                   curv_regrid_data[:,:,1:-1])

    else:
        raise ValueError("unexpected direction of %s" % direction)

    # Done with this regridder
    regridder.finalize()

    # Done with ESMP
    ESMPControl().stopESMP(True)

max_stars_repo_name: josborne-noaa/PyFerret

max_issues_repo_name: josborne-noaa/PyFerret

max_forks_repo_name: josborne-noaa/PyFerret


text:
import random
from pymongo import MongoClient, DESCENDING
import sys
from xsys.neural import NeuralExample, NeuralExampleSet
from datetime import datetime
import traceback
import numpy as np
from bson.objectid import ObjectId

'''
import asyncio
from bson.objectid import ObjectId
import neural
from db import XSYSDB
db = XSYSDB()
db.start_db_client()
mlp = db.get_market_limit_pairs(product_id='FIL-USD', N_samples_max=100, do_print=True)
open_orders_dict = db.retrieve_open_orders(product_id='BTC-USD')

db.find_most_recent_prediction(product_id='BTC-USD')
db.find_most_recent_notification(kind='xsys_prediction_notification')
db.find_most_recent_notification(kind='xsys_btc_notification')
db.find_most_recent_alpha_stat(stat_id='alpha_max', resolution_grade='low')

db.store_alpha_stat(stat_id='alpha_max', value=15.9504, time=primary_tz.localize(datetime.now()).isoformat())

db.market_limit_pairs.delete_one({'_id': ObjectId('60c4bc8247515ccccae20485')})
db.delete_alpha_stat(ObjectId('6082cd84127aeedd1e782735'))

D = db.get_neural_examples_since(time_start=107.60)
print(f"{max([d['N_samples'] for d in D])} samples,    {max([float(d['MinuteSpan']) for d in D])/60:.1f} hours")
print([d['N_samples'] for d in D])
print([float(f"{d['MinuteSpan']:.1f}") for d in D])
print(len(D))

import torch
state = torch.load(f'trader_state.BTC-USD.pt'); vwap = state['vwaps'][0]; list(map(np.count_nonzero, [vwap.q_price,vwap.q_size,vwap.q_matches_per_second,vwap.q_vwap]))
plt.plot(0.9*np.array(vwap.q_price))
'''
class XSYSDB:
    def __init__(self, manager=None):
        if manager:
            self.mpm = manager
            self.neural_example_Q_in = self.mpm.Queue()
            self.neural_example_Q_out = self.mpm.Queue()

    def start_db_client(self):
        self.client = MongoClient()
        self.neural_example_collection = self.client.xsysdb.neural_examples
        self.alpha_stats = self.client.xsysdb.alpha_stats
        self.notifications = self.client.xsysdb.notifications
        self.open_orders = self.client.xsysdb.open_orders
        self.predictions = self.client.xsysdb.predictions
        self.market_limit_pairs = self.client.xsysdb.market_limit_pairs
        self.current_prices = self.client.xsysdb.current_prices

    def process_neural_example_Q_out(self):
        if not self.neural_example_Q_out.empty():
            example_set = self.neural_example_Q_out.get()
            self.store_neural_example_set(nes=example_set)
            print(f"[db_store_process()]: Successfully Stored Example Set: {example_set}")

    def store_neural_example_set(self, nes:NeuralExampleSet):
        try:
            D = []
            for e in nes.examples:
                D.append(e.dict_rep())
            self.neural_example_collection.insert_many(D)
        except Exception as e:
            print(f"[XSYSDB]: Error encountered while storing Neural Example Set. {e}")
            for d in D:
                for k,v in d.items():
                    print(f"{k}.shape() = {np.shape(v)}")
            print(traceback.format_exc())

    def delete_neural_example(self, object_id):
        self.neural_example_collection.delete_one({'_id': ObjectId(object_id)})

    def store_alpha_stat(self, document):
        try:
            print(f"[{self.__class__.__name__}.store_alpha_stat()]: document = {document}")
            self.alpha_stats.insert_one(document=document)
        except Exception as e:
            print(traceback.format_exc())

    def store_prediction(self, prediction=None):
        try:
            print(f"[{self.__class__.__name__}.store_prediction()]: prediction = {prediction}")
            self.predictions.insert_one(prediction)
        except Exception as e:
            print(traceback.format_exc())

    def store_market_limit_pair(self, market_limit_pair=None):
        try:
            print(f"[{self.__class__.__name__}.store_market_limit_pair()]: market_limit_pair = {market_limit_pair}")
            self.market_limit_pairs.insert_one(market_limit_pair)
        except Exception as e:
            print(traceback.format_exc())

    def store_notification(self, notification_dict=None):
        try:
            #print(f"[{self.__class__.__name__}.store_notification()]: notification = {notification_dict}")
            self.notifications.insert_one(notification_dict)
        except Exception as e:
            print(f"[XSYSDB]: Error encountered while storing notification. {e}")
            print(f"\tnotification_dict = {notification_dict}")
            print(traceback.format_exc())

    def update_market_limit_pair(self, update_key=None, r_sell=None):
        try:
            d = self.market_limit_pairs.find_one({'update_key':update_key})
            if d != None:
                d.update({'r_sell':r_sell})
                d.update({'update_key': r_sell['id']})
                self.market_limit_pairs.replace_one({'update_key':update_key}, d)
            else:
                raise ValueError('MLP not found')
        except Exception as e:
            print(f"[{self.__class__.__name__}.update_market_limit_pair()]: r_sell = {r_sell}")
            print(traceback.format_exc())

    def update_current_price(self, product_id:str=None, price:float=None):
        try:
            q = {'product_id': product_id}
            d = {
                'product_id': product_id,
                'price':price
            }
            self.current_prices.replace_one(q,d,upsert=True)
        except Exception as e:
            print(f"[{self.__class__.__name__}.update_current_price()]: Error encountered in update_current_price():: {e}")
            print(traceback.format_exc())

    def retrieve_current_prices(self, product_ids=['BTC-USD','ETH-USD','YFI-USD','DOT-USD','FIL-USD','ADA-USD']):
        try:
            product_ids = product_ids if type(product_ids) == type([]) else [product_ids]
            P = {}
            for pid in product_ids:
                q = {'product_id': pid}
                d = self.current_prices.find_one(
                    q,
                    sort=[('_id', DESCENDING)]
                )
                if d:
                    P[pid] = d['price']
            return P
        except Exception as e:
            print(traceback.format_exc())
            return {}

    def update_open_orders(self, product_id=None, open_orders_dict=None):
        try:
            q = {'product_id': product_id}
            open_orders_dict.update(q)
            self.open_orders.replace_one(q,
                                         open_orders_dict,
                                         upsert=True)
        except Exception as e:
            print(f"\topen_orders_dict = {open_orders_dict}")
            print(f"[{self.__class__.__name__}.update_open_orders()]: Error encountered in update_open_orders():: {e}")
            print(traceback.format_exc())

    def retrieve_open_orders(self, product_id=None):
        try:
            q = {'product_id': product_id}
            d = self.open_orders.find_one(
                q,
                sort=[('_id', DESCENDING)]
            )
            return d
        except Exception as e:
            print(traceback.format_exc())
            return None

    def find_most_recent_prediction(self, product_id:str):
        try:
            d = self.predictions.find_one(
                {'product_id': product_id},
                sort=[('_id', DESCENDING)]
            )
            return d
        except Exception as e:
            print(f"product_id: {product_id}")
            print(traceback.format_exc())
            return None

    def find_most_recent_notification(self, product_id:str, kind:str):
        try:
            d = self.notifications.find_one(
                {'kind': kind,
                 'product_id':product_id},
                sort=[('_id', DESCENDING)]
            )
            return d
        except Exception as e:
            print(f"kind: {kind}")
            print(traceback.format_exc())
            return None

    def find_most_recent_market_limit_pair(self, product_id:str):
        try:
            d = self.market_limit_pairs.find_one(
                {'product_id':product_id},
                sort=[('_id', DESCENDING)]
            )
            return d
        except Exception as e:
            print(traceback.format_exc())
            return None

    def find_most_recent_alpha_stat(self,
                                    product_id:str='BTC-USD',
                                    stat_id:str='stat_id',
                                    resolution_grade:str=None):
        try:
            q = {'stat_id': stat_id,
                 'product_id': product_id,
                 'resolution_grade': resolution_grade} if resolution_grade else {'stat_id': stat_id,
                                                                                'product_id':product_id}
            d = self.alpha_stats.find_one(
                q,
                sort=[('_id', DESCENDING)]
            )
            return d
        except Exception as e:
            print(f"query = {q}")
            print(traceback.format_exc())
            return None

    def delete_alpha_stat(self, object_id):
        self.alpha_stats.delete_one({'_id': ObjectId(object_id)})

    def shutdown(self):
        self.client.close()
        print(f"[XSYSDB.shutdown()]: Successfully closed db client.")

    def get_market_limit_pairs(self,
                               product_id: str,
                               N_samples_max: int=10,
                               do_print: bool=False):
        D = self.market_limit_pairs.find(
            filter={'$and': [
                {"product_id": product_id},
            ]},
            sort=[('_id', DESCENDING)],
            limit=N_samples_max
        )
        mlp = {}
        for d in D:
            mlp[d['r_sell']['id']] = d
            del mlp[d['r_sell']['id']]['_id']
            if do_print:
                print(f"\n\t{d['r_sell']['id']}:---------------------------------------\n\t{mlp[d['r_sell']['id']]}")
        return mlp

    # time_start in days since 1-1-2021
    def get_neural_examples_since(self,
                                  product_id: str,
                                  time_start=105.5370,
                                  N_samples_max=100_000):
        D = self.neural_example_collection.find(
            filter={'$and': [
                {"product_id": product_id},
                {"TimeStart": {'$gt': time_start}},
            ]},
            limit=N_samples_max
        )
        return [d for d in D]

    def get_neural_training_samples(self,
                                    product_id: str,
                                    N_backcast_length=1000,
                                    N_forecast_length=100,
                                    min_minute_span=10,
                                    N_samples_max=100_000,
                                    traintest_split=0.9):

        D = self.neural_example_collection.find(
            filter={'$and': [
                {"product_id": product_id},
                {"MinuteSpan": {'$gt': min_minute_span}},
                {"N_samples": {'$gt': N_backcast_length+N_forecast_length+1}},
            ]},
            limit=N_samples_max
        )
        _ = [d for d in D]
        random.shuffle(_)
        xo, y_bc, y_fc = [], [], []
        N_bcfc = N_backcast_length + N_forecast_length
        for s in _:
            offset_R = np.random.randint(1, s['N_samples']-N_bcfc)
            xo.append(np.transpose([
                np.array(s['Time'][-(N_bcfc+offset_R):-(N_forecast_length+offset_R)]) - s['TimeStart'],
                s['Size'][-(N_bcfc+offset_R):-(N_forecast_length+offset_R)],
                s['MPS'][-(N_bcfc+offset_R):-(N_forecast_length+offset_R)],
                s['VWAP'][-(N_bcfc+offset_R):-(N_forecast_length+offset_R)],
            ]))
            y_bc.append(np.transpose([
                s['Price'][-(N_bcfc+offset_R):-(N_forecast_length+offset_R)],
            ]))
            y_fc.append(np.transpose([
                s['Price'][-(N_forecast_length+offset_R):-offset_R],
            ]))

        # Split data into training and testing datasets.
        c = int(np.round((1-traintest_split)*len(_)))
        xo_train, xo_test = xo[c:], xo[:c]
        y_train_bc, y_train_fc, y_test_bc, y_test_fc = y_bc[c:], y_fc[c:], y_bc[:c], y_fc[:c]

        return np.array(xo_train), np.array(xo_test), \
               np.array(y_train_bc), np.array(y_test_bc), \
               np.array(y_train_fc), np.array(y_test_fc)

max_stars_repo_name: MRGLabs/ccxt

max_issues_repo_name: MRGLabs/ccxt

max_forks_repo_name: MRGLabs/ccxt


text:
#! /usr/bin/python2

#### Test PredNet on the pre-processed JAAD (4,30,256,456,3) sequences, on multiple GPUs.
#### Based on kitti_evaluate.py in PredNet original code release.
#### Latest Revision X. Du 2020/01

import hickle as hkl
import numpy as np
from keras import backend as K
from keras.preprocessing.image import Iterator
import os
from six.moves import cPickle
from keras import backend as K
from keras.models import Model
from keras.layers import Input, Dense, Flatten
from keras.layers import LSTM
from keras.layers import TimeDistributed
from keras.callbacks import LearningRateScheduler, ModelCheckpoint
from keras.optimizers import Adam
from keras.utils import multi_gpu_model
from prednet import PredNet
import subprocess
from keras.models import model_from_json

from jaad_settings import *
np.random.seed(random_seed)

print("=========================testing PredNet on JAAD=====================")  #

json_file = open(json_file_name, 'r')
loaded_model_json = json_file.read()
json_file.close()
train_model = model_from_json(loaded_model_json, custom_objects = {'PredNet': PredNet})

# load weights into new model
train_model.load_weights(weights_file)
print("Loaded model from disk")

# Create testing model (to output predictions)
layer_config = train_model.layers[1].get_config()
layer_config['output_mode'] = 'prediction'
data_format = layer_config['data_format'] if 'data_format' in layer_config else layer_config['dim_ordering']
test_prednet = PredNet(weights=train_model.layers[1].get_weights(), **layer_config)
input_shape = list(train_model.layers[0].batch_input_shape[1:])
input_shape[0] = nt
inputs = Input(shape=tuple(input_shape))
predictions = test_prednet(inputs)
test_model = Model(inputs=inputs, outputs=predictions)

X_test = hkl.load(fileList_test[0])
for i in range(1,len(fileList_test)):
    print("==Processing X_test #" + str(i) + " out of " + str(len(fileList_test)) + "==")
    X_each = hkl.load(fileList_test[i])
    X_test= np.concatenate((X_test,X_each),axis=0)

X_hat = test_model.predict(X_test, batch_size) ##(276, 30, 256, 456, 3)
#print(X_hat.shape)

if save_model: ### this step saves the concatenated X_hat (prediction) and X_test (actual), may take a while
    hkl.dump(X_hat, os.path.join(weights_dir, 'X_hat.hkl'))
    hkl.dump(X_test, os.path.join(weights_dir, 'X_test.hkl'))

# Compare MSE and SSIM of PredNet predictions versus copying last frame
mse_model = np.mean( (X_test[:, 1:] - X_hat[:, 1:])**2 )  # look at all timesteps except the first
mse_prev = np.mean( (X_test[:, :-1] - X_test[:, 1:])**2 )

print(mse_model)
print(mse_prev)

####from skimage.metrics import structural_similarity as ssim ###require skimage version>0.16
from skimage.measure import compare_ssim as ssim

ssim_noise=np.empty((len(X_test),nt-1))
ssim_prev=np.empty((len(X_test),nt-1))
for i in range(len(X_test)):
    print("==Computing SSIM for #"+str(i) + ' out of ' + str(len(X_test)) + '==')
    for j in range(1,nt):
        ssim_noise[i,j-1] = ssim(X_test[i, j] , X_hat[i, j],multichannel=True)
        ssim_prev[i,j-1] = ssim(X_test[i, j] , X_test[i, j-1],multichannel=True)

print(np.mean(ssim_noise))
print(np.mean(ssim_prev))

#### the following is grammatically correct, but for this dataset may be out of memory
# ssim_noise = ssim(X_test[:, 1:] , X_hat[:, 1:],
#                   data_range=X_hat[:, 1:].max() - X_hat[:, 1:].min(),multichannel=True)

# ssim_prev = ssim(X_test[:, :-1], X_test[:, 1:],
#                   data_range=X_test[:, 1:].max() - X_test[:, 1:].min())

print("that's all folks!")


text:
# ======================================================================
#  Balancio-Kit (c) 2021 Linar (UdeSA)
#  This code is licensed under MIT license (see LICENSE.txt for details)
# ======================================================================

"""
This module contains different gym wrappers to change the reward function.
"""

from gym import Wrapper
import numpy as np
from typing import Callable

class VelocityPitchRewardWrapper(Wrapper):
    """ Change the environment's reward to contemplate both the pitch and the linear velocity of the robots base.

    Example:
            env = VelocityPitchRewardWrapper(BalancioGymEnv(...))

    @param env: Environment
    """

    def __init__(self, env):
        super().__init__(env)

    def step(self, action):
        observation, reward, done, info = self.env.step(action)
        return observation, self.reward(), done, info

    def reward(self):
        # Weights
        weight_pitch = 1
        weight_vel = 4 / 50

        # Penalize over pitch
        self.env.pitch_angle = self.env.robot.get_pitch()[0]
        # Penalize over velocity
        vel = self.env.robot.get_linear_vel_imu()[1]

        reward = - weight_pitch * np.square(self.env.pitch_angle) - weight_vel * abs(vel)
        return reward

class VelocityPitchYawRewardWrapper(Wrapper):
    """ Change the environment's reward to contemplate the pitch, the linear velocity and the yaw of the robots base.

    Example:
            env = VelocityPitchYawRewardWrapper(BalancioGymEnv(...))

    @param env: Environment
    """

    def __init__(self, env):
        super().__init__(env)

    def step(self, action):
        observation, reward, done, info = self.env.step(action)
        return observation, self.reward(), done, info

    def reward(self):
        # Weights
        weight_pitch = 1
        weight_vel = 4 / 50
        weight_yaw = 4 / 5

        # Penalize over pitch
        self.env.pitch_angle = self.env.robot.get_pitch()[0]
        # Penalize over velocity
        vel = self.env.robot.get_linear_vel_imu()[1]
        # Penalize over yaw
        yaw_rot = self.env.robot.get_angular_vel()[2]

        reward = - weight_pitch * np.square(self.env.pitch_angle) - weight_vel * abs(vel) - weight_yaw * np.square(yaw_rot)
        return reward

def get_reward_wrapper(wrapper_key: str) -> Callable:
    """ Get the class of the selected wrapper.

    @param wrapper_key: Reward wrapper type.
    @return reward_wrapper: Callable of the selected wrapper.
    """

    # Dictionary containing the different wrapper classes
    reward_wrappers = {"None": Wrapper,
                       "VelocityPitchRewardWrapper": VelocityPitchRewardWrapper,
                       "VelocityPitchYawRewardWrapper": VelocityPitchYawRewardWrapper}

    try:
        reward_wrapper = reward_wrappers[wrapper_key]
    except KeyError:
        raise ValueError(
            'Invalid Reward Wrapper. Valid reward wrappers: {}'.format(", ".join(reward_wrappers)))

    return reward_wrapper

max_stars_repo_name: udesa-ai/balancio-kit

max_issues_repo_name: udesa-ai/balancio-kit

max_forks_repo_name: udesa-ai/balancio-kit


text:
#-------------------------------------------------------------------------------
# Name:        module1
# Purpose: This contain functions for two purposes
# 1) Annotate tandem repeats with the features they span
# 2) Extract tandem repeats based on known coordinates
#
# Author:      I am
#
# Created:     06/12/2017
# Copyright:   (c) I am 2017
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import numpy
import HTSeq
import csv
import pyliftover
import sys
##from ReferenceAnalyzer.ReferenceRepeatAnalyzer import *

class TandemLine():
    def __init__(self, row):
        self.id=row[0]
        self.method=row[1]
        self.chrom=row[2]
        self.rpt_length=int(row[3])
        self.copynumber=int(row[4])
        self.info=float(row[5])
        self.identity=float(row[6])
        try:self.repetition=float(row[7])
        except: self.repetition='err'
        self.start=int(row[8])
        self.end=int(row[9])
        self.starts=[int(s) for s in row[10].split(',') ]
        self.ends=[int(e) for e in row[11].split(',') ]
        self.info_string=row[12]
        self.seq=row[13]
        try:
            gene_parts= [item.split(':') for item in row[14].split(',')]
            self.gene=dict(gene_parts)
        except:
            self.gene={}
        try:
            rpt_parts= [item.split(':') for item in row[15].split(',')]
            self.repeat=dict(rpt_parts)
        except:

            self.repeat={}

class RefseqLine():
    def __init__(self, row):
##        pass
        self.bin=int(row[0])
        self.name=row[1]
        self.chrom=row[2]
        self.strand=row[3]
        self.txStart=int(row[4])
        self.txEnd=int( row[5])
        self.cdsStart=int( row[6])
        self.cdsEnd=int( row[7])
        self.exonCount=int( row[8])
        self.exonStarts=[int(i) for i in  row[9].split(',')[:-1]]
        self.exonEnds=[int(i) for i in  row[10].split(',')[:-1]]
        self.score=row[11]
        self.name2=row[12]
        self.cdsStartStat=row[13]
        self.cdsEndStat=row[14]
        self.exonFrames=[int(i) for i in  row[15].split(',')[:-1]]

class WarburtonLine():
    def __init__(self, row):
##        pass
        self.bin=int(row[0])
        self.name=row[1]
        self.chrom=row[2]
        self.strand=row[3]
        self.txStart=int(row[4])
        self.txEnd=int( row[5])
        self.cdsStart=int( row[6])
        self.cdsEnd=int( row[7])
        self.exonCount=int( row[8])
        self.exonStarts=[int(i) for i in  row[9].split(',')[:-1]]
        self.exonEnds=[int(i) for i in  row[10].split(',')[:-1]]
        self.score=row[11]
        self.name2=row[12]
        self.cdsStartStat=row[13]
        self.cdsEndStat=row[14]
        self.exonFrames=[int(i) for i in  row[15].split(',')[:-1]]

class RepeatLine():
    def __init__(self, row):
        pass

        self.chrom=row[5]
        self.start= int(row[6])
        self.end= int(row[7])
        self.name=row[10]

def LoadRefseqAnnotations(infile):
    gen_array=HTSeq.GenomicArrayOfSets('auto', False)
    inhandle=open(infile, 'r')
    intable=csv.reader(inhandle, delimiter='\t')
    header=intable.next()
    for row in intable:

        line=RefseqLine(row)
        for i in range(len( line.exonStarts)):
            gene_iv=HTSeq.GenomicInterval(line.chrom, line.exonStarts[i], line.exonEnds[i])
            gen_array[gene_iv]+=line.name2

    inhandle.close()
    return gen_array

def LoadWarburtonAnnotations(infile):
    gen_array=HTSeq.GenomicArrayOfSets('auto', False)
    inhandle=open(infile, 'r')
    intable=csv.reader(inhandle, delimiter='\t')
    header=intable.next()
    warburton_set=set()
    for row in intable:

        chrom,interval=row[-1].split(':')
##        chrom='Cchrom[1:]
        left, right=interval.split('-')
        try:
            gene_iv=HTSeq.GenomicInterval(chrom,int( left),int( right))
        except: continue

        warburton_set.add(row[-3])
        gen_array[gene_iv]+=row[-3]

    inhandle.close()
    return gen_array, warburton_set

def LoadRepeatAnnotations(infile):
    gen_array=HTSeq.GenomicArrayOfSets('auto', False)
    inhandle=open(infile, 'r')
    intable=csv.reader(inhandle, delimiter='\t')
    header=intable.next()
    for row in intable:

        line=RepeatLine(row)

        gene_iv=HTSeq.GenomicInterval(line.chrom, line.start, line.end)
        gen_array[gene_iv]+=line.name

    inhandle.close()
    return gen_array

def AnnotateWithRefSeq(infile, outfile, annot_file, rpt_file):
    print 'Loading gene annotations...'
    gen_array=LoadRefseqAnnotations(annot_file)
    print 'Loading repeat annotations...'
    rpt_array=LoadRepeatAnnotations(rpt_file)
    print 'Annotations loaded'
    inhandle=open(infile, 'r')
    intable=csv.reader(inhandle, delimiter='\t')
    outhandle=open(outfile, 'w')
    outtable=csv.writer(outhandle, delimiter='\t')
    header=intable.next()
    outtable.writerow(header+['Features'])
    counter=0
    for row in intable:
        if counter%100==0:
            print '.',
        counter+=1
        line=TandemLine(row)
        gene_annotations={}
        rpt_annotations={}
        for i in range(len( line.starts)):
            s,e=line.starts[i], line.ends[i]

            tandem_iv=HTSeq.GenomicInterval(line.chrom, s, e)
            for gen_annot in set.union(* [ seq[1] for seq in gen_array[tandem_iv].steps() ]):
##                for gen_annot in set( sets):
##                    print gen_annot
                if gene_annotations.has_key(gen_annot)==False:
                    gene_annotations[gen_annot]=0
                gene_annotations[gen_annot]+=1
            for gen_annot in set.union(* [ seq[1] for seq in rpt_array[tandem_iv].steps() ]):

                if rpt_annotations.has_key(gen_annot)==False:
                    rpt_annotations[gen_annot]=0
                rpt_annotations[gen_annot]+=1

##        gene_annotations=','.join(list(gene_annotations))
##        rpt_annotations=','.join(list(rpt_annotations))
        gene_annotations=','.join(['{0}:{1}'.format(key, gene_annotations[key]) for key in gene_annotations ])
        rpt_annotations=','.join(['{0}:{1}'.format(key, rpt_annotations[key]) for key in rpt_annotations ])
##        rpt_annotations=''
        outtable.writerow(row+[gene_annotations, rpt_annotations])
    outhandle.close()
    inhandle.close()

def LoadRepeatMaskerAnnotations():
    pass

def UpdateWarburtonTable1(infile, ref_file,outfile):
    inhandle=open(infile,'r')
    intable=csv.reader(inhandle, delimiter='\t')

    outhandle=open(outfile, 'w')

    updated_file='{0}_hg38.tsv'.format( '.'.join( infile.split('.')[:-1]))
    updated_handle=open(updated_file,'w')
    updated_table=csv.writer(updated_handle, delimiter='\t')
    header=intable.next()
    updated_table.writerow(header)
    lo=pyliftover.LiftOver('hg18', 'hg38')

##    seq=GetSeq(ref_file)"
    for row in intable:
        chrom, interval=row[-1].split(':')
        left,right=interval.split('-')
        left=int(''.join(left.split(',')))
        right=int(''.join(right.split(',')))

        coord_left=lo.convert_coordinate(chrom,left)[0][1]
        chromosome,coord_right=lo.convert_coordinate(chrom,right)[0][:2]
        print chromosome, left, coord_left
        print chromosome, right, coord_right

        new_line=row[:-1]+['{0}:{1}-{2}'.format(chromosome, coord_left, coord_right)]
##        seq_name='>{0}_{1}_{2}_Up{3}_{4}_{5}\n',format(row[7],row[2],row[3], chromosome, coord_left, coord_right,)
##        outfile.write(seq_name)
##        outfile.write ( '{0}\n'.format( seq[chromosome][coord_left:coord_right].upper() ) )
        updated_table.writerow(new_line)
    inhandle.close()
    updated_handle.close()
    outfile.close()

def AnnotateWarburton(infile,annot_file):
    print 'Loading gene annotations...'
    gen_array, gen_set=LoadWarburtonAnnotations(annot_file)
##    print gen_set
    inhandle=open(infile, 'r')
    intable=csv.reader(inhandle, delimiter='\t')
##    outhandle=open(outfile, 'w')
##    outtable=csv.writer(outhandle, delimiter='\t')
    header=intable.next()
##    outtable.writerow(header+['Features'])
    counter=0
    found_set=set()
    for row in intable:
##        if counter%100==0:
##            print '.',
        counter+=1
        line=TandemLine(row)
        gene_annotations=set()
        rpt_annotations=set()
        for i in range(len( line.starts)):
            s,e = line.starts[i], line.ends[i]

            tandem_iv = HTSeq.GenomicInterval(line.chrom, s, e)
            gene_annotations |= set.union(* [ seq[1] for seq in gen_array[tandem_iv].steps()])
##            if len( gene_annotations) > 0 : print '.'
            found_set |= gene_annotations
##        gene'_annotations=','.join(list(gene_annotations))

##        rpt_annotations=''

##    outhandle.close()
    print gen_set-found_set
    print '{0}/{1}: {2}'.format(len(found_set), len(gen_set), float(len(found_set))/ len(gen_set))
    inhandle.close()

def ConcatenateAnnotations(file1, file2, outfile):
    handle1=open(file1, 'r')
    handle2=open(file2, 'r')
    table1=csv.reader(handle1, delimiter='\t')
    table2=csv.reader(handle2, delimiter='\t')
    outhandle=open(outfile, 'w+')
    outtable=csv.writer(outhandle, delimiter='\t')
    header=['Repeat ID','Settings']+ table1.next()
    outtable.writerow(header)
    count=0
    for line in table1:
        outtable.writerow([count, '80']+line)
        count+=1
    header=table2.next()
    for line in table2:
        outtable.writerow([count, 'auto']+line)
        count+=1
    handle1.close()
    handle2.close()
    outhandle.close()

def ExtractFASTAs(annot_file, outfile):
    #
    inhandle=open(annot_file, 'r')
    intable=csv.reader(inhandle, delimiter='\t')

    gene_file='{0}_genes.fa'.format(outfile)
    alpha_file='{0}_alpha.fa'.format(outfile)
    rpt_file='{0}_rpt.fa'.format(outfile)

    gene_handle=open(gene_file, 'w')
    rpt_handle=open(rpt_file, 'w')
    alpha_handle=open(alpha_file, 'w')
    intable.next()
    count=0
    for row in intable:
        line=TandemLine(row)
        if int( line.rpt_length)==0: continue
        if len(line.repeat.keys())>0: count+=1
        if count%20==0:
            print line.repeat
            count+=1
        if len( line.gene.keys()) !=0:
            keys,values=line.gene.keys(),numpy.array(  line.gene.values(), int)
            name=keys[numpy.argmax(values)]
            seq_name='>{0}_Name={1}_CN={2}_Length={3}_Identity={4}_Rpt={5}_method={6}_Chr={7}:{8}-{9}\n'.format(line.id,\
            name, line.copynumber, line.rpt_length, line.identity, line.repetition, line.method, line.chrom,\
            line.start,  line.end)
            gene_handle.write(seq_name)
            gene_handle.write('{0}\n'.format(line.seq))
        elif line.repeat.has_key('ALR/Alpha'):
            if int( line.copynumber)<=15: continue
            name='ALR/Alpha'
            seq_name='>{0}_CN={2}_Length={3}_Identity={4}_Rpt={5}_method={6}_Name={1}_Chr={7}:{8}-{9}\n'.format(line.id,\
            name, line.copynumber, line.rpt_length, line.identity, line.repetition, line.method, line.chrom,\
            line.start,  line.end)
            alpha_handle.write(seq_name)
            alpha_handle.write('{0}\n'.format(line.seq))
        else:
            if len( line.repeat.keys()) ==0: name='Unannot'
            elif len(line.repeat.keys())==1: name=line.repeat.keys()[0]
            else: name='Complex'
            seq_name='>{0}_Name={1}_CN={2}_Length={3}_Identity={4}_Rpt={5}_method={6}_Chr={7}:{8}-{9}\n'.format(line.id,\
            name, line.copynumber, line.rpt_length, line.identity, line.repetition, line.method, line.chrom,\
            line.start,  line.end)
            rpt_handle.write(seq_name)
            rpt_handle.write('{0}\n'.format(line.seq))

    inhandle.close()
    gene_handle.close()
    rpt_handle.close()
    alpha_handle.close()
def main(argv):
    param={}
    print argv
    for i in range(1, len(argv), 2):
        param[argv[i]]= argv[i+1]
    print param
    if param=={}: return()
    #inDir, outDir, AssemblyIndex, TEIndex, threads, length=70, ReadIDPosition=1, PositionRange=0, phred=33, shorten=True, Trim=True
    inDir=param['-i']
    outDir=param['-o']
    gene_path=param['-gene']
    rpt_path=param['-rpt']
    if param.has_key('-C')==True:
        cutoff=float( param['-C'])
    else:
        cutoff=.8
    AnnotateWithRefSeq(inDir, outDir, gene_path, rpt_path)
    pass

if __name__ == '__main__':
    main(sys.argv)

max_stars_repo_name: LaptopBiologist/ReferenceAnalyzer

max_issues_repo_name: LaptopBiologist/ReferenceAnalyzer

max_forks_repo_name: LaptopBiologist/ReferenceAnalyzer


text:
#!/usr/bin/env python
"""
--------------------------------
project: code
created: 26/06/2018 17:26
---------------------------------

"""
from collections import defaultdict

import numpy as np

from generic.utils import choose_from

def greedy(state_action_values):
    return max(state_action_values, key=lambda k: state_action_values[k])

# not sure if these policy classes should keep references to the action values. Maybe not...
class GreedyPolicy:
    def __init__(self, action_values, cache=False):
        """
        A greedy policy.

        Args:
            action_values (dict): Action Values
            cache (bool): Whether to cache the policy decisions, useful if you only want to evaluate the policy and
                you won't be updating the action values
        """
        self.action_values = action_values
        self._cache = cache

        if cache:
            self._greedy = dict()

    def _cache_call(self, state):
        if state not in self._greedy:
            self._greedy[state] = greedy(self.action_values[state])
        return self._greedy[state]

    def __call__(self, state):
        return self._cache_call(state) if self._cache else greedy(self.action_values[state])

class EpsilonGreedyPolicy:
    def __init__(self, action_values, epsilon, random_state=None):
        self.action_values = action_values
        self.epsilon = epsilon
        self.random_state = random_state or np.random.RandomState(seed=0)

    def explore(self):
        return self.random_state.binomial(n=1, p=self.epsilon) == 1

    def __call__(self, state):
        if self.explore():
            return choose_from(list(self.action_values[state].keys()), self.random_state)
        else:
            return greedy(self.action_values[state])

class TimeBiasedPolicy:
    def __init__(self, action_values, kappa):
        self.action_values = action_values

        self.kappa = kappa
        self._time_last_visited_table = defaultdict(dict)
        self._time_step = 0

    def time_since_visited(self, state, action):
        try:
            t = self._time_last_visited_table[state][action]
        except KeyError:
            t = -1
        return self._time_step - t

    def __call__(self, state):
        modified_av = {
            a: v + self.kappa * np.sqrt(self.time_since_visited(state, a))
            for a, v in self.action_values[state].items()
        }
        chosen_action = greedy(modified_av)
        self._time_last_visited_table[state][chosen_action] = self._time_step
        self._time_step += 1
        return chosen_action


text:
import numpy as np
import random

from .sobol.sobol_seq import i4_sobol_generate
from .hypercube.hycusampling import halton, korobov_design_matrix
from .hypercube.hycusampling import improved_lhd_matrix, lhd_matrix
from .lhs_sudoku import sudoku
from .quantum import cached_generator, randint
from .ambient import RandomOrgClient

class Randomizer:

    '''Create various random sequences.'''

    def __init__(self, max_value, n):

        '''
        max_value : int
            maximum value in the sequence
        n : int
            number of samples to be drawn
        '''

        self.len = max_value
        self.n = n

    def uniform_mersenne(self):

        '''Regular uniform / pseudorandom sequence'''

        return random.sample(range(self.len), k=self.n)

    def uniform_crypto(self):

        '''Cryptographically sound pseudorandom sequence'''

        try:
            from secrets import randbelow
        except ImportError:
            print('Python3.6 is required >> mersenne returned instead')
            r = Randomizer(self.len, self.n)
            return r.uniform_mersenne()

        out = []
        i = 0
        while i < self.n:
            num = randbelow(self.len)
            if num not in out:
                i += 1
                out.append(num)

        return out

    def latin_sudoku(self, dims=2, sudoku_boxes=1):

        '''Latin Hypercube with Sudoku-style Constraint.
        M. D. McKay, R. J. Beckman, W. J. Conover, 1979.

        dims :: number of dimensions
        sudoku_boxes :: number of boxes to use as constraint

        '''

        n = int(self.len / sudoku_boxes)

        if self.len % sudoku_boxes != 0:
            raise ValueError('Index len must be divisible by sudoku_boxes')

        out = sudoku.sample(dims, sudoku_boxes, n)
        out = [i[0] for i in out[0]]

        return out[:self.n]

    def latin_improved(self):

        out = [i[0] for i in improved_lhd_matrix(self.len, 1)]
        return out[:self.n]

    def latin_matrix(self):

        out = [i[0] for i in lhd_matrix(self.len, 1)]
        return out[:self.n]

    def sobol(self):

        '''Creates an index based on Sobol Sequence'''

        org = [i[0] for i in i4_sobol_generate(1, self.len)]
        return self._match_index(org)[:self.n]

    def halton(self):

        '''Creates an index based on Halton Sequence'''

        org = [i[0] for i in halton(self.len, 1, 5)]
        return self._match_index(org)[:self.n]

    def korobov_matrix(self):

        '''Returns a 1-d array of integeres in the Korobov Design Matrix'''

        out = [i for i in korobov_design_matrix(self.len, 2)[:, 1]]
        return out[:self.n]

    def ambience(self):

        '''An ambient sound based TRNG using RANDOM.ORG API'''

        out = RandomOrgClient('c7d1e0b0-e57c-4fb8-9d5b-382ec9de5a89')
        return out.generate_integers(self.n, 0, self.len)

    def quantum(self):

        '''Quantum Random Number Generator

        NOTE: this method can only return 1024 random numbers.

        DESCRIPTION
        ===========

        The random numbers are generated in real-time in ANU lab by measuring
        the quantum fluctuations of the vacuum. The vacuum is described very
        differently in the quantum mechanical context than in the classical
        context. Traditionally, a vacuum is considered as a space that is
        empty of matter or photons. Quantum mechanically, however, that same
        space resembles a sea of virtual particles appearing and disappearing
        all the time. This result is due to the fact that the vacuum still
        possesses a zero-point energy. Consequently, the electromagnetic
        field of the vacuum exhibits random fluctuations in phase and amplitude
        at all frequencies. By carefully measuring these fluctuations, we are
        able to generate ultra-high bandwidth random numbers.

        EXAMPLE
        =======

        test = quantum_random(200, minimum=10, maximum=20)
        randhist(test)

        PARAMETERS
        ==========

        n = number of integer values to return

        minimum = min value for integers

        maximum = max value for integers

        '''

        out = []

        gen = cached_generator()

        for i in range(self.n):
            out.append(int(randint(min=0, max=self.len, generator=gen)))

        return out

    def _match_index(self, org):

        '''Helper to match sequence with index and
        reorganize index accordingly.'''

        temp = np.array(list(zip(org, list(range(self.len)))))
        out = temp[temp[:, 0].argsort()][:, 1].astype(int).tolist()

        return out

max_stars_repo_name: JohanMollevik/chances

max_issues_repo_name: JohanMollevik/chances

max_forks_repo_name: JohanMollevik/chances