text:
#!/usr/bin/env python

# This source provides the interfaces to call the C/C++ functions from python code

import numpy as np
from ctypes import *
from numpy.ctypeslib import ndpointer
from scipy.sparse import csr_matrix

# A dynamic library containing implementation of server side code
try:
  lib = CDLL("libfrovedis_client_python.so")
except OSError:
  try:
    lib = CDLL("../lib/libfrovedis_client_python.so")
  except OSError:
    raise OSError("libfrovedis_client_python.so: No such dll found (set LD_LIBRARY_PATH)")

# --- Frovedis Server ---
initialize_server = lib.initialize_server
initialize_server.argtypes = [c_char_p]
initialize_server.restype = py_object

get_worker_size = lib.get_worker_size
get_worker_size.argtypes = [c_char_p, c_int]
get_worker_size.restype = c_int

clean_server = lib.clean_server
clean_server.argtypes = [c_char_p, c_int]

finalize_server = lib.finalize_server
finalize_server.argtypes = [c_char_p, c_int]

check_server_exception = lib.check_server_exception
check_server_exception.restype = py_object

# --- Frovedis dvector ---
# create from numpy array
create_frovedis_int_dvector = lib.create_frovedis_int_dvector
create_frovedis_int_dvector.argtypes = [c_char_p, c_int,
                                  ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                  c_int]
create_frovedis_int_dvector.restype = py_object

create_frovedis_long_dvector = lib.create_frovedis_long_dvector
create_frovedis_long_dvector.argtypes = [c_char_p, c_int,
                                  ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                  c_int]
create_frovedis_long_dvector.restype = py_object

create_frovedis_float_dvector = lib.create_frovedis_float_dvector
create_frovedis_float_dvector.argtypes = [c_char_p, c_int,
                                  ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS"),
                                  c_int]
create_frovedis_float_dvector.restype = py_object

create_frovedis_double_dvector = lib.create_frovedis_double_dvector
create_frovedis_double_dvector.argtypes = [c_char_p, c_int,
                                  ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS"),
                                  c_int]
create_frovedis_double_dvector.restype = py_object

create_frovedis_string_dvector = lib.create_frovedis_string_dvector
create_frovedis_string_dvector.argtypes = [c_char_p, c_int, POINTER(c_char_p), c_int]
create_frovedis_string_dvector.restype = py_object

#To Print Created dvector

show_frovedis_dvector = lib.show_frovedis_dvector
show_frovedis_dvector.argtypes = [c_char_p, c_int, c_long, c_int]

release_frovedis_dvector = lib.release_frovedis_dvector
release_frovedis_dvector.argtypes = [c_char_p, c_int, c_long, c_int]

#----Frovedis Dataframe from Python--------------------
create_frovedis_dataframe = lib.create_frovedis_dataframe
create_frovedis_dataframe.argtypes = [c_char_p, c_int, POINTER(c_short),
                                      POINTER(c_char_p), POINTER(c_long), c_int]
create_frovedis_dataframe.restype = c_long

show_frovedis_dataframe = lib.show_frovedis_dataframe
show_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long]

release_frovedis_dataframe = lib.release_frovedis_dataframe
release_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long]

release_dfoperator = lib.release_dfoperator
release_dfoperator.argtypes = [c_char_p, c_int, c_long]

get_frovedis_dfoperator = lib.get_frovedis_dfoperator
get_frovedis_dfoperator.argtypes = [c_char_p, c_int, c_char_p, c_char_p, c_short, c_short, c_bool]
get_frovedis_dfoperator.restype = c_long

get_dfANDoperator = lib.get_frovedis_dfANDoperator
get_dfANDoperator.argtypes = [c_char_p, c_int, c_long, c_long]
get_dfANDoperator.restype = c_long

get_dfORoperator = lib.get_frovedis_dfORoperator
get_dfORoperator.argtypes = [c_char_p, c_int, c_long, c_long]
get_dfORoperator.restype = c_long

filter_frovedis_dataframe = lib.filter_frovedis_dataframe
filter_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long, c_long]
filter_frovedis_dataframe.restype = c_long

select_frovedis_dataframe = lib.select_frovedis_dataframe
select_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long, POINTER(c_char_p), c_int]
select_frovedis_dataframe.restype = c_long

sort_frovedis_dataframe = lib.sort_frovedis_dataframe
sort_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long, POINTER(c_char_p), c_int, c_bool]
sort_frovedis_dataframe.restype = c_long

group_frovedis_dataframe = lib.group_frovedis_dataframe
group_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long, POINTER(c_char_p), c_int]
group_frovedis_dataframe.restype = c_long

agg_grouped_dataframe = lib.agg_grouped_dataframe
agg_grouped_dataframe.argtypes = [c_char_p, c_int, c_long,
                                  POINTER(c_char_p), c_ulong,
                                  POINTER(c_char_p), POINTER(c_char_p),
                                  POINTER(c_char_p), c_ulong]
agg_grouped_dataframe.restype = c_long

merge_frovedis_dataframe = lib.join_frovedis_dataframe
merge_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long, c_long, c_long,
                                     c_char_p, c_char_p]
merge_frovedis_dataframe.restype = c_long

rename_frovedis_dataframe = lib.rename_frovedis_dataframe
rename_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long,
                                      POINTER(c_char_p), POINTER(c_char_p), c_int]
rename_frovedis_dataframe.restype = c_long

get_min_frovedis_dataframe = lib.min_frovedis_dataframe
get_min_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long,
                                       POINTER(c_char_p), POINTER(c_short), c_int]
get_min_frovedis_dataframe.restype = py_object

get_max_frovedis_dataframe = lib.max_frovedis_dataframe
get_max_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long,
                                       POINTER(c_char_p), POINTER(c_short), c_int]
get_max_frovedis_dataframe.restype = py_object

get_sum_frovedis_dataframe = lib.sum_frovedis_dataframe
get_sum_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long,
                                       POINTER(c_char_p), POINTER(c_short), c_int]
get_sum_frovedis_dataframe.restype = py_object

get_avg_frovedis_dataframe = lib.avg_frovedis_dataframe
get_avg_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long,
                                       POINTER(c_char_p), c_int]
get_avg_frovedis_dataframe.restype = py_object

get_cnt_frovedis_dataframe = lib.cnt_frovedis_dataframe
get_cnt_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long,
                                       POINTER(c_char_p), c_int]
get_cnt_frovedis_dataframe.restype = py_object

get_std_frovedis_dataframe = lib.std_frovedis_dataframe
get_std_frovedis_dataframe.argtypes = [c_char_p, c_int, c_long,
                                       POINTER(c_char_p), POINTER(c_short), c_int]
get_std_frovedis_dataframe.restype = py_object

get_frovedis_col = lib.get_frovedis_col
get_frovedis_col.argtypes = [c_char_p, c_int, c_long, c_char_p, c_short]
get_frovedis_col.restype = py_object

df_to_rowmajor = lib.df_to_rowmajor
df_to_rowmajor.argtypes = [c_char_p, c_int, c_long,  #host, port, proxy
                           POINTER(c_char_p), c_int, #t_cols_arr, size
                           c_short]                  #dtype
df_to_rowmajor.restype = py_object

df_to_colmajor = lib.df_to_colmajor
df_to_colmajor.argtypes = [c_char_p, c_int, c_long,  #host, port, proxy
                           POINTER(c_char_p), c_int, #t_cols_arr, size
                           c_short]                  #dtype
df_to_colmajor.restype = py_object

df_to_crs = lib.df_to_crs
df_to_crs.argtypes = [c_char_p, c_int, c_long,  #host, port, proxy
                      POINTER(c_char_p), c_int, #t_cols_arr, size1
                      POINTER(c_char_p), c_int, #cat_cols_arr, size2
                      c_long, c_short]          #info_id, dtype
df_to_crs.restype = py_object

df_to_crs_using_info = lib.df_to_crs_using_info
df_to_crs_using_info.argtypes = [c_char_p, c_int, c_long,  #host, port, proxy
                                 c_long, c_short]          #info_id, dtype
df_to_crs_using_info.restype = py_object

# --- Frovedis dftable_to_sparse_info ---
load_dftable_to_sparse_info = lib.load_dftable_to_sparse_info
load_dftable_to_sparse_info.argtypes = [c_char_p, c_int,  #host, port
                                        c_long, c_char_p] #info_id, dirname

save_dftable_to_sparse_info = lib.save_dftable_to_sparse_info
save_dftable_to_sparse_info.argtypes = [c_char_p, c_int,  #host, port
                                        c_long, c_char_p] #info_id, dirname

release_dftable_to_sparse_info = lib.release_dftable_to_sparse_info
release_dftable_to_sparse_info.argtypes = [c_char_p, c_int, c_long]  #host, port, info_id

# --- Frovedis sparse matrices ---
# create from scipy matrix
create_frovedis_crs_II_matrix = lib.create_frovedis_crs_II_matrix
create_frovedis_crs_II_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_II_matrix.restype = py_object

create_frovedis_crs_IL_matrix = lib.create_frovedis_crs_IL_matrix
create_frovedis_crs_IL_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_IL_matrix.restype = py_object

create_frovedis_crs_LI_matrix = lib.create_frovedis_crs_LI_matrix
create_frovedis_crs_LI_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_LI_matrix.restype = py_object

create_frovedis_crs_LL_matrix = lib.create_frovedis_crs_LL_matrix
create_frovedis_crs_LL_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_LL_matrix.restype = py_object

create_frovedis_crs_FI_matrix = lib.create_frovedis_crs_FI_matrix
create_frovedis_crs_FI_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_FI_matrix.restype = py_object

create_frovedis_crs_FL_matrix = lib.create_frovedis_crs_FL_matrix
create_frovedis_crs_FL_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_FL_matrix.restype = py_object

create_frovedis_crs_DI_matrix = lib.create_frovedis_crs_DI_matrix
create_frovedis_crs_DI_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_DI_matrix.restype = py_object

create_frovedis_crs_DL_matrix = lib.create_frovedis_crs_DL_matrix
create_frovedis_crs_DL_matrix.argtypes = [c_char_p, c_int,
                                          c_ulong, c_ulong,
                                          ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                          c_ulong]
create_frovedis_crs_DL_matrix.restype = py_object

# load from text/bin file
load_frovedis_crs_matrix = lib.load_frovedis_crs_matrix
load_frovedis_crs_matrix.argtypes = [c_char_p, c_int,
                                     c_char_p, c_bool,
                                     c_short, c_short]
load_frovedis_crs_matrix.restype = py_object

save_frovedis_crs_matrix = lib.save_frovedis_crs_matrix
save_frovedis_crs_matrix.argtypes = [c_char_p, c_int,
                                     c_long, c_char_p, c_bool,
                                     c_short, c_short]

release_frovedis_crs_matrix = lib.release_frovedis_crs_matrix
release_frovedis_crs_matrix.argtypes = [c_char_p, c_int, c_long,
                                        c_short, c_short]

show_frovedis_crs_matrix = lib.show_frovedis_crs_matrix
show_frovedis_crs_matrix.argtypes = [c_char_p, c_int, c_long,
                                     c_short, c_short]

# --- Frovedis Dense matrices ---
# create from numpy matrix
create_frovedis_double_dense_matrix = lib.create_frovedis_double_dense_matrix
create_frovedis_double_dense_matrix.argtypes = [c_char_p, c_int,
                                       c_ulong, c_ulong,
                                       ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS"),
                                       c_char]
create_frovedis_double_dense_matrix.restype = py_object

create_frovedis_float_dense_matrix = lib.create_frovedis_float_dense_matrix
create_frovedis_float_dense_matrix.argtypes = [c_char_p, c_int,
                                       c_ulong, c_ulong,
                                       ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS"),
                                       c_char]
create_frovedis_float_dense_matrix.restype = py_object

create_frovedis_long_dense_matrix = lib.create_frovedis_long_dense_matrix
create_frovedis_long_dense_matrix.argtypes = [c_char_p, c_int,
                                       c_ulong, c_ulong,
                                       ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                                       c_char]
create_frovedis_long_dense_matrix.restype = py_object

create_frovedis_int_dense_matrix = lib.create_frovedis_int_dense_matrix
create_frovedis_int_dense_matrix.argtypes = [c_char_p, c_int,
                                       c_ulong, c_ulong,
                                       ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                       c_char]
create_frovedis_int_dense_matrix.restype = py_object

# load from text/bin file
load_frovedis_dense_matrix = lib.load_frovedis_dense_matrix
load_frovedis_dense_matrix.argtypes = [c_char_p, c_int,
                                       c_char_p, c_bool, c_char, c_short]
load_frovedis_dense_matrix.restype = py_object

save_frovedis_dense_matrix = lib.save_frovedis_dense_matrix
save_frovedis_dense_matrix.argtypes = [c_char_p, c_int,
                                       c_long, c_char_p,
                                       c_bool, c_char, c_short]

transpose_frovedis_dense_matrix = lib.transpose_frovedis_dense_matrix
transpose_frovedis_dense_matrix.argtypes = [c_char_p, c_int,
                                            c_long, c_char, c_short]
transpose_frovedis_dense_matrix.restype = py_object

copy_frovedis_dense_matrix = lib.copy_frovedis_dense_matrix
copy_frovedis_dense_matrix.argtypes = [c_char_p, c_int, c_long, c_char, c_short]
copy_frovedis_dense_matrix.restype = py_object

release_frovedis_dense_matrix = lib.release_frovedis_dense_matrix
release_frovedis_dense_matrix.argtypes = [c_char_p, c_int, c_long, c_char, c_short]

show_frovedis_dense_matrix = lib.show_frovedis_dense_matrix
show_frovedis_dense_matrix.argtypes = [c_char_p, c_int, c_long, c_char, c_short]

get_frovedis_rowmatrix = lib.get_frovedis_rowmatrix
get_frovedis_rowmatrix.argtypes = [c_char_p, c_int, c_long,
                                   c_ulong, c_ulong,
                                   c_char, c_short]
get_frovedis_rowmatrix.restype = py_object

get_double_rowmajor_array = lib.get_double_rowmajor_array
get_double_rowmajor_array.argtypes = [c_char_p, c_int, c_long, c_char,
                               ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS"),
                               c_ulong]

get_float_rowmajor_array = lib.get_float_rowmajor_array
get_float_rowmajor_array.argtypes = [c_char_p, c_int, c_long, c_char,
                               ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS"),
                               c_ulong]

get_long_rowmajor_array = lib.get_long_rowmajor_array
get_long_rowmajor_array.argtypes = [c_char_p, c_int, c_long, c_char,
                               ndpointer(c_long,ndim=1,flags="C_CONTIGUOUS"),
                               c_ulong]

get_int_rowmajor_array = lib.get_int_rowmajor_array
get_int_rowmajor_array.argtypes = [c_char_p, c_int, c_long, c_char,
                               ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                               c_ulong]

# --- Frovedis ML Models ---

parallel_float_glm_predict = lib.parallel_float_glm_predict
parallel_float_glm_predict.argtypes = [c_char_p, c_int, c_int, c_short, c_long,
                                 ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS"),
                                 c_ulong, c_bool, c_short, c_bool]

parallel_double_glm_predict = lib.parallel_double_glm_predict
parallel_double_glm_predict.argtypes = [c_char_p, c_int, c_int, c_short, c_long,
                                 ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS"),
                                 c_ulong, c_bool, c_short, c_bool]

# kmeans predict returns int always:
parallel_kmeans_predict = lib.parallel_kmeans_predict
parallel_kmeans_predict.argtypes = [c_char_p, c_int, c_int,
                                    c_short, c_long,
                                    ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                                    c_ulong, c_short, c_bool]

als_float_predict = lib.als_float_predict
als_float_predict.argtypes = [c_char_p, c_int, c_int,
                        ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                        ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS"),
                        c_ulong]

als_double_predict = lib.als_double_predict
als_double_predict.argtypes = [c_char_p, c_int, c_int,
                        ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                        ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS"),
                        c_ulong]

als_float_rec_users = lib.als_float_rec_users
als_float_rec_users.argtypes = [c_char_p, c_int, c_int, c_int, c_int,
                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                          ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS")]

als_double_rec_users = lib.als_double_rec_users
als_double_rec_users.argtypes = [c_char_p, c_int, c_int, c_int, c_int,
                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                          ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS")]

als_float_rec_prods = lib.als_float_rec_prods
als_float_rec_prods.argtypes = [c_char_p, c_int, c_int, c_int, c_int,
                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                          ndpointer(c_float,ndim=1,flags="C_CONTIGUOUS")]

als_double_rec_prods = lib.als_double_rec_prods
als_double_rec_prods.argtypes = [c_char_p, c_int, c_int, c_int, c_int,
                          ndpointer(c_int,ndim=1,flags="C_CONTIGUOUS"),
                          ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS")]

release_frovedis_model = lib.release_frovedis_model
release_frovedis_model.argtypes = [c_char_p,c_int,c_int,c_short,c_short]

show_frovedis_model = lib.show_frovedis_model
show_frovedis_model.argtypes = [c_char_p,c_int,c_int,c_short,c_short]

load_frovedis_model = lib.load_frovedis_model
load_frovedis_model.argtypes = [c_char_p,c_int,c_int,c_short,c_short,c_char_p]

load_frovedis_nbm = lib.load_frovedis_nbm
load_frovedis_nbm.argtypes = [c_char_p,c_int,c_int,c_short,c_char_p]
load_frovedis_nbm.restype = py_object

load_frovedis_mfm = lib.load_frovedis_mfm
load_frovedis_mfm.argtypes = [c_char_p,c_int,c_int,c_short,c_char_p]
load_frovedis_mfm.restype = py_object

save_frovedis_model = lib.save_frovedis_model
save_frovedis_model.argtypes = [c_char_p,c_int,c_int,c_short,c_short,c_char_p]

# --- Frovedis ML Trainers ---
lr_sgd = lib.lr_sgd
lr_sgd.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                   c_int, c_bool, c_double, c_int, c_int,
                   c_short, c_short, c_bool]

lr_lbfgs = lib.lr_lbfgs
lr_lbfgs.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                     c_int, c_bool, c_double, c_int, c_int,
                     c_short, c_short, c_bool]

svm_sgd = lib.svm_sgd
svm_sgd.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                    c_int, c_bool, c_double, c_int, c_int,
                    c_short, c_short, c_bool]

svm_lbfgs = lib.svm_lbfgs
svm_lbfgs.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                      c_int, c_bool, c_double, c_int, c_int,
                      c_short, c_short, c_bool]

dt_train = lib.dt_trainer
dt_train.argtypes = [c_char_p, c_int, c_long,
                     c_long, c_char_p, c_char_p,
                     c_int, c_int, c_int, c_int,
                     c_float, c_int, c_int,
                     c_short, c_short, c_bool]

nb_train = lib.nb_trainer
nb_train.argtypes = [c_char_p, c_int, c_long,
                     c_long, c_double, c_int, c_char_p, c_int,
		     c_short, c_short, c_bool]

lnr_sgd = lib.lnr_sgd
lnr_sgd.argtypes = [c_char_p, c_int, c_long, c_long, c_bool, c_int, c_int,
                    c_short, c_short, c_bool]

lnr_lbfgs = lib.lnr_lbfgs
lnr_lbfgs.argtypes = [c_char_p, c_int, c_long, c_long, c_bool, c_int, c_int,
                      c_short, c_short, c_bool]

lasso_sgd = lib.lasso_sgd
lasso_sgd.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                      c_bool, c_double, c_int, c_int,
                      c_short, c_short, c_bool]

lasso_lbfgs = lib.lasso_lbfgs
lasso_lbfgs.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                        c_bool, c_double, c_int, c_int,
                        c_short, c_short, c_bool]

ridge_sgd = lib.ridge_sgd
ridge_sgd.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                      c_bool, c_double, c_int, c_int,
                      c_short, c_short, c_bool]

ridge_lbfgs = lib.ridge_lbfgs
ridge_lbfgs.argtypes = [c_char_p, c_int, c_long, c_long, c_int, c_double,
                        c_bool, c_double, c_int, c_int,
                        c_short, c_short, c_bool]

kmeans_train = lib.kmeans_train
kmeans_train.argtypes= [c_char_p, c_int, c_long, c_int,
                        c_int, c_long, c_double, c_int, c_int,
                        c_short, c_short, c_bool]

# als will always be trained with sparse data
als_train = lib.als_train
als_train.argtypes = [c_char_p, c_int, c_long, c_int, c_int,
                      c_double, c_double, c_long, c_int, c_int,
                      c_short, c_short]

fm_train = lib.fm_trainer
fm_train.argtypes = [c_char_p, c_int,
                     c_long, c_long,
                     c_double, c_int,
                     c_double, c_char_p,
                     c_bool, c_bool, c_int,
                     c_double, c_double, c_double,
                     c_int, c_int, c_bool, c_int,
                     c_short, c_short]

# --- Frovedis PBLAS Wrappers ---
pswap = lib.pswap
pswap.argtypes = [c_char_p, c_int, c_long, c_long]

pcopy = lib.pcopy
pcopy.argtypes = [c_char_p, c_int, c_long, c_long]

pscal = lib.pscal
pscal.argtypes = [c_char_p, c_int, c_long, c_double]

paxpy = lib.paxpy
paxpy.argtypes = [c_char_p, c_int, c_long, c_long, c_double]

pdot = lib.pdot
pdot.argtypes = [c_char_p, c_int, c_long, c_long]
pdot.restype = c_double

pnrm2 = lib.pnrm2
pnrm2.argtypes = [c_char_p, c_int, c_long]
pnrm2.restype = c_double

pgemv = lib.pgemv
pgemv.argtypes = [c_char_p, c_int, c_long, c_long,
                  c_bool, c_double, c_double]
pgemv.restype = py_object

pger = lib.pger
pger.argtypes = [c_char_p, c_int, c_long, c_long, c_double]
pger.restype = py_object

pgemm = lib.pgemm
pgemm.argtypes = [c_char_p, c_int, c_long, c_long,
                  c_bool, c_bool, c_double, c_double]
pgemm.restype = py_object

pgeadd = lib.pgeadd
pgeadd.argtypes = [c_char_p, c_int, c_long, c_long,
                   c_bool, c_double, c_double]

# --- Frovedis SCALAPACK Wrappers ---
pgetrf = lib.pgetrf
pgetrf.argtypes = [c_char_p, c_int, c_long]
pgetrf.restype = py_object

pgetri = lib.pgetri
pgetri.argtypes = [c_char_p, c_int, c_long, c_long]
pgetri.restype = c_int

pgetrs = lib.pgetrs
pgetrs.argtypes = [c_char_p, c_int, c_long, c_long, c_long, c_bool]
pgetrs.restype = c_int

pgesv = lib.pgesv
pgesv.argtypes = [c_char_p, c_int, c_long, c_long]
pgesv.restype = c_int

pgels = lib.pgels
pgels.argtypes = [c_char_p, c_int, c_long, c_long, c_bool]
pgels.restype = c_int

pgesvd = lib.pgesvd
pgesvd.argtypes = [c_char_p, c_int, c_long, c_bool, c_bool]
pgesvd.restype = py_object

# --- Frovedis ARPACK Wrappers ---
compute_sparse_svd = lib.compute_sparse_svd
compute_sparse_svd.argtypes = [c_char_p, c_int, c_long, c_int,
                               c_short, c_short]
compute_sparse_svd.restype = py_object

# --- Scalapack Results ---
release_ipiv = lib.release_ipiv
release_ipiv.argtypes = [c_char_p, c_int, c_char, c_long]

get_double_array = lib.get_double_array
get_double_array.argtypes = [c_char_p, c_int, c_long,
                             ndpointer(c_double,ndim=1,flags="C_CONTIGUOUS")]

release_double_array = lib.release_double_array
release_double_array.argtypes = [c_char_p, c_int, c_long]

save_as_diag_matrix = lib.save_as_diag_matrix
save_as_diag_matrix.argtypes = [c_char_p, c_int, c_long, c_char_p, c_bool]

get_svd_results_from_file = lib.get_svd_results_from_file
get_svd_results_from_file.argtypes = [c_char_p, c_int,
                                      c_char_p, c_char_p, c_char_p,
                                      c_bool, c_bool, c_bool, c_char]
get_svd_results_from_file.restype = py_object

max_stars_repo_name: wmeddie/frovedis

max_issues_repo_name: wmeddie/frovedis

max_forks_repo_name: wmeddie/frovedis

reason: import numpy,from numpy,from scipy


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

## Iris dataset
import pandas as pd
from sklearn import datasets

iris = datasets.load_iris()

# Use pandas' DataFrame
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target
df['target_names'] = iris.target_names[iris.target]
print(df)

print("# of records:",len(df))
print("value_counts:")
print(df.target_names.value_counts())

## Training data
# Use petal length, petal width
#x_train = df.loc[:,['petal length (cm)', 'petal width (cm)']].to_numpy() # shape:(150, 2)
#x_train = df.loc[:,['petal length (cm)', 'petal width (cm)']].values # shape:(150, 2)
x_train = df.loc[:,['sepal length (cm)', 'sepal width (cm)']].values # shape:(150, 2)
#x_train = df.loc[:,['petal length (cm)', 'sepal length (cm)']].values # shape:(150, 2)
y_train = np.eye(3)[iris.target] # one-hot representation - shape:(150, 3)

print('x_train=',x_train)
print('y_train=',y_train)

# Plot training data
plt.figure(figsize=(8, 5))
for t in range(3):
    x = x_train[iris.target==t][:,0]
    y = x_train[iris.target==t][:,1]
    cm = [plt.cm.Paired([c]) for c in [0,6,11]]
    plt.scatter(x, y, c=cm[t], edgecolors='k', label=iris.target_names[t])
# label
plt.title('Iris dataset')
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)')
#plt.xlabel('sepal length (cm)')
#plt.ylabel('sepal width (cm)')
plt.legend()
#plt.show()
plt.savefig('Truth.pdf')

from qcl_classification import QclClassification

# Random number
random_seed = 0
np.random.seed(random_seed)

#b Circuit paramter
nqubit = 2
c_depth = 2
num_class = 3

# Instance of QclClassification
qcl = QclClassification(nqubit, c_depth, num_class)

# Training with BFGS
res, theta_init, theta_opt = qcl.fit(x_train, y_train, maxiter=5)

# Plot
h = .05  # step size in the mesh
X = x_train
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

# Plot model prediction
def decision_boundary(X, y, theta, title='(title here)'):
    plt.figure(figsize=(8, 5))

    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    qcl.set_input_state(np.c_[xx.ravel(), yy.ravel()])
    Z = qcl.pred(theta) # Update model parameter theta
    Z = np.argmax(Z, axis=1)

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    #plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)
    #print("xx,yy,Z :",len(xx[0]),len(yy[:,0]),len(Z[0]),len(Z[:,0]))

    label_pred = []
    for i in range(len(x_train[:,0])):
        for ix in range(len(xx[0])-1):
            for iy in range(len(yy[:,0])-1):
                if x_train[:,0][i] > xx[0][ix] and x_train[:,0][i] <= xx[0][ix+1] and \
                        x_train[:,1][i] > yy[:,0][iy] and x_train[:,1][i] <= yy[:,0][iy+1]:
                    label_pred.append(Z[iy][ix])

    # Plot also the training points
    #plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Paired)
    plt.scatter(X[:, 0], X[:, 1], c=label_pred, edgecolors='k', cmap=plt.cm.Paired)

    # label
    plt.title(title)
    plt.xlabel('petal length (cm)')
    plt.ylabel('petal width (cm)')
    #plt.xlabel('sepal length (cm)')
    #plt.ylabel('sepal width (cm)')
    #plt.show()
    plt.savefig(title+'.pdf')

# Plot with initial parameter
decision_boundary(x_train, iris.target, theta_init, title='Initial')

# Plot with optimized parameter
decision_boundary(x_train, iris.target, theta_opt, title='Optimized')

ext: py

reason: import numpy


text:
## this calculates the raw eigenvector cenrality values for each level

import timecorr as tc
from timecorr.helpers import isfc, reduce, autofc
from scipy.io import loadmat
import numpy as np
import sys
import os
import subprocess
from config import config

delta = {'name': '$\delta$', 'weights': tc.eye_weights, 'params': tc.eye_params}

cond = sys.argv[1]
level = 10
cfun = 'autofc'
rfun = 'eigenvector_centrality'
raw = 'delta'

raw_parameter = eval(raw)

raw_fun = raw_parameter['weights']
raw_params = raw_parameter['params']

p_cfun = eval(cfun)

result_name = 'corrs_ordered_ec'

corrsdir = os.path.join(config['resultsdir'], result_name, cfun + '_' + rfun)

try:
    if not os.path.exists(corrsdir):
        os.makedirs(corrsdir)
except OSError as err:
   print(err)

factors = 100

if factors == 100:
    pieman_name = 'pieman_ica100.mat'
else:
    pieman_name = 'pieman_data.mat'

pieman_data = loadmat(os.path.join(config['datadir'], pieman_name))

pieman_conds = ['intact', 'paragraph', 'word', 'rest']

data = []
conds = []

if cond == 'paragraph':
    if factors == 700:
        next_data = list(
            map(lambda i: pieman_data[cond][:, i][0], np.where(np.arange(pieman_data[cond].shape[1]) != 3)[0]))
    else:
        next_data = list(
            map(lambda i: pieman_data[cond][:, i][0], np.where(np.arange(pieman_data[cond].shape[1]) != 0)[0]))
else:
    next_data = list(map(lambda i: pieman_data[cond][:, i][0], np.arange(pieman_data[cond].shape[1])))

data.extend(next_data)
conds.extend([cond] * len(next_data))

all_data = np.array(data)
conds = np.array(conds)

levels = range(int(level))

print(levels)

for lev in levels:

    if os.path.exists(os.path.join(corrsdir, 'd_' + str(lev + 1) + '_' + cond + '.npy')):
        data = np.load(os.path.join(corrsdir, 'd_' + str(lev + 1) + '_' + cond + '.npy'))
        subprocess.run(['rm', os.path.join(corrsdir, 'd_' + str(lev + 1) + '_' + cond + '.npy')])

        print(str(lev) + '_data loaded')
    else:

        if lev == 0:
            lev_data = all_data[conds == cond]
        else:
            #assert data_r, 'data_r not defined'
            lev_data = data_r

        print('lev_data: ' + str(lev_data[0][0]))

        data = np.asarray(tc.timecorr([x for x in lev_data], cfun=p_cfun, rfun=None,
                                      weights_function=raw_fun, weights_params=raw_params))

        print(str(lev + 1) + '_raw data calculated')

        np.save(os.path.join(corrsdir, 'd_' + str(lev + 1) + '_' + cond + '.npy'), data)

    if os.path.exists(os.path.join(corrsdir, 'd_' + str(lev + 1) + '_r_' + cond + '.npy')):
        data_r = np.load(os.path.join(corrsdir, 'd_' + str(lev + 1) + '_r_' + cond + '.npy'))
        print(str(lev + 1) + '_reduced data loaded')
    else:
        data_r = np.asarray(reduce([x for x in data], rfun=rfun))
        print(str(lev + 1) + '_reduced data calculated')
        np.save(os.path.join(corrsdir, 'd_' + str(lev + 1) + '_r_' + cond + '.npy'), data_r)
        subprocess.run(['rm', os.path.join(corrsdir, 'd_' + str(lev + 1) + '_' + cond + '.npy')])
        print('data_r: ' + str(data_r[0][0]))
    del data

reason: import numpy,from scipy


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

#
# IBM model 1 with alignment type implementation of HMM Aligner
# Simon Fraser University
# NLP Lab
#
# This is the implementation of IBM model 1 word aligner with alignment type.
#
import numpy as np
from collections import defaultdict
from loggers import logging
from models.IBM1Base import AlignmentModelBase as IBM1Base
from evaluators.evaluator import evaluate
__version__ = "0.5a"

class AlignmentModel(IBM1Base):
    def __init__(self):
        self.modelName = "IBM1WithPOSTagAndAlignmentType"
        self.version = "0.4b"
        self.logger = logging.getLogger('IBM1')
        self.evaluate = evaluate
        self.fe = ()

        self.s = []
        self.sTag = []
        self.typeList = []
        self.typeIndex = {}
        self.typeDist = np.zeros(0)
        self.lambd = 1 - 1e-20
        self.lambda1 = 0.9999999999
        self.lambda2 = 9.999900827395436E-11
        self.lambda3 = 1.000000082740371E-15
        self.fLex = self.eLex = self.fIndex = self.eIndex = None

        self.loadTypeDist = {"SEM": .401, "FUN": .264, "PDE": .004,
                             "CDE": .004, "MDE": .012, "GIS": .205,
                             "GIF": .031, "COI": .008, "TIN": .003,
                             "NTR": .086, "MTA": .002}

        self.modelComponents = ["t", "s", "sTag",
                                "fLex", "eLex", "fIndex", "eIndex",
                                "typeList", "typeIndex", "typeDist",
                                "lambd", "lambda1", "lambda2", "lambda3"]
        IBM1Base.__init__(self)
        return

    def _beginningOfIteration(self, index=0):
        self.c = [defaultdict(float) for i in range(len(self.fLex[index]))]
        self.total = np.zeros(len(self.eLex[index]))
        self.c_feh = [defaultdict(lambda: np.zeros(len(self.typeIndex)))
                      for i in range(len(self.fLex[index]))]
        return

    def _updateCount(self, f, e, index):
        fLen = len(f)
        eLen = len(e)
        fWords = np.array([f[i][index] for i in range(fLen)])
        eWords = np.array([e[j][index] for j in range(eLen)])
        tSmall = self.tProbability(f, e, index)
        tSmall = tSmall / tSmall.sum(axis=1)[:, None]
        score = self.sProbability(f, e, index) * tSmall[:, :, None]
        for i in range(fLen):
            tmp = tSmall[i]
            tmps = score[i]
            for j in range(eLen):
                self.c[fWords[i]][eWords[j]] += tmp[j]
                self.c_feh[fWords[i]][eWords[j]] += tmps[j]
        eDupli = (eWords[:, np.newaxis] == eWords).sum(axis=0)
        tSmall = (tSmall * eDupli).sum(axis=0)
        self.total[eWords] += tSmall
        return

    def _updateEndOfIteration(self, index):
        self.logger.info("Iteration complete, updating parameters")
        # Update t
        for i in range(len(self.fLex[index])):
            for j in self.c[i]:
                self.t[i][j] = self.c[i][j] / self.total[j]

        # Update s
        if index == 0:
            del self.s
            self.s = self.c_feh
        else:
            del self.sTag
            self.sTag = self.c_feh
        for i in range(len(self.c_feh)):
            for j in self.c_feh[i]:
                self.c_feh[i][j] /= self.c[i][j]
        return

    def sProbability(self, f, e, index=0):
        sTag = np.tile((1 - self.lambd) * self.typeDist, (len(f), len(e), 1))
        for j in range(len(e)):
            for i in range(len(f)):
                if f[i][1] < len(self.sTag) and e[j][1] in self.sTag[f[i][1]]:
                    sTag[i][j] += self.lambd * self.sTag[f[i][1]][e[j][1]]
        if index == 1:
            return sTag

        s = np.tile((1 - self.lambd) * self.typeDist, (len(f), len(e), 1))
        for j in range(len(e)):
            for i in range(len(f)):
                if f[i][0] < len(self.s) and e[j][0] in self.s[f[i][0]]:
                    s[i][j] += self.lambd * self.s[f[i][0]][e[j][0]]

        return (self.lambda1 * s +
                self.lambda2 * sTag +
                np.tile(self.lambda3 * self.typeDist, (len(f), len(e), 1)))

    def decodeSentence(self, sentence):
        f, e, align = self.lexiSentence(sentence)
        sentenceAlignment = []
        t = self.tProbability(f, e)
        sPr = self.sProbability(f, e)
        types = np.argmax(sPr, axis=2)
        score = np.max(sPr, axis=2) * t
        for i in range(len(f)):
            jBest = np.argmax(score[i])
            hBest = types[i][jBest]
            sentenceAlignment.append(
                (i + 1, jBest + 1, self.typeList[hBest]))
        return sentenceAlignment, score

    def trainStage1(self, dataset, iterations=5):
        self.logger.info("Stage 1 Start Training with POS Tags")
        self.logger.info("Initialising model with POS Tags")
        self.initialiseBiwordCount(dataset, 1)
        self.sTag = self.calculateS(dataset, 1)
        self.logger.info("Initialisation complete")
        self.EM(dataset, iterations, 1)
        self.logger.info("Stage 1 Complete")
        return

    def trainStage2(self, dataset, iterations=5):
        self.logger.info("Stage 2 Start Training with FORM")
        self.logger.info("Initialising model with FORM")
        self.initialiseBiwordCount(dataset, 0)
        self.s = self.calculateS(dataset, 0)
        self.logger.info("Initialisation complete")
        self.EM(dataset, iterations, 0)
        self.logger.info("Stage 2 Complete")
        return

    def train(self, dataset, iterations=5):
        dataset = self.initialiseLexikon(dataset)
        self.logger.info("Initialising Alignment Type Distribution")
        self.initialiseAlignTypeDist(dataset, self.loadTypeDist)
        self.trainStage1(dataset, iterations)
        self.trainStage2(dataset, iterations)
        return

max_stars_repo_name: sfu-natlang/HMM-Aligner

max_issues_repo_name: sfu-natlang/HMM-Aligner

max_forks_repo_name: sfu-natlang/HMM-Aligner


text:
import numpy as np
from game import ANSWER_FILE, Game
from tqdm import tqdm
from data import WORD_FILE, Data
import itertools as it
import random
import matplotlib.pyplot as plt
from colorama import init
from  matplotlib.colors import LinearSegmentedColormap

"""
This file holds functions that were used to build the plots in the poster and report. These are the functions run in
the 'plots' mode.
"""

def letterDistributions():
    """
    Builds letter/position heatmaps and saves them in the plots directory.
    """
    with open(WORD_FILE, 'r') as f:
        answerList = list(f.read().upper().split())

    letters = np.array(list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
    engFreq = np.array([0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, 0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406,
                        0.06749, 0.07507, 0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, 0.00978, 0.02360, 0.00150, 0.01974, 0.00074])
    engInd = np.argsort(engFreq)[::-1]
    alphabet = dict(zip(letters, it.count()))
    heatMap = np.zeros((26,5))
    letterBuckets = np.zeros(26)
    for word in answerList:
        for i, l in enumerate(word):
            heatMap[alphabet[l]][i] += 1
            letterBuckets[alphabet[l]] += 1
    byLetter = heatMap / heatMap.sum(1).reshape(-1,1)
    byPosition = heatMap / heatMap.sum(0).reshape(1,-1)
    letterFreq = letterBuckets / letterBuckets.sum()
    wordleInd = np.argsort(letterFreq)[::-1]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    plt.bar(letters[engInd], engFreq[engInd], color='green')
    plt.ylabel('Frequency')
    fig.tight_layout()
    plt.savefig("plots/engFreq", dpi=300, bbox_inches="tight")

    cmap=LinearSegmentedColormap.from_list('bg',["gold", "green"], N=256)

    fig = plt.figure()
    ax = fig.add_subplot(111)
    im = ax.imshow(byLetter, cmap=cmap)
    plt.colorbar(im)
    ax.set_xticks(np.arange(5), labels=[1,2,3,4,5])
    ax.set_yticks(np.arange(26), labels=letters)
    plt.setp(ax.get_xticklabels(), ha="center")
    ax.set_title("Letter Distributions Over Positions")
    fig.tight_layout()
    plt.savefig("plots/byLetter", dpi=300, bbox_inches="tight")

    fig = plt.figure()
    ax = fig.add_subplot(111)
    im = ax.imshow(byPosition, cmap=cmap)
    plt.colorbar(im)
    ax.set_xticks(np.arange(5), labels=[1,2,3,4,5])
    ax.set_yticks(np.arange(26), labels=letters)
    plt.setp(ax.get_xticklabels(), ha="center")
    ax.set_title("Position Distributions Over Letters")
    fig.tight_layout()
    plt.savefig("plots/byPostion", dpi=300, bbox_inches="tight")

    fig = plt.figure()
    ax = fig.add_subplot(111)
    plt.bar(letters[wordleInd], letterFreq[wordleInd], color='green')
    plt.ylabel('Frequency')
    fig.tight_layout()
    plt.savefig("plots/wordleFreq", dpi=300, bbox_inches="tight")

def simulateAll(guessList):
    """
    Simulates 500 games starting from each of the words in the guessList parameter.
    """
    random.seed(123)
    dataStore = dict()
    dataStore["wordsToIndex"] = dict(zip(guessList, it.count()))
    dataStore["avg"] = np.zeros(len(guessList))
    dataStore["buckets"] = np.zeros((len(guessList), 10))

    with open(ANSWER_FILE, "r") as f:
        answerList = random.sample(list(f.read().upper().split()), 500)

    for guess in guessList:
        guessBuckets = [0 for _ in range(10)]
        print(f"Start Word:{guess}")
        numerator = 0
        denominator = 0
        avg = 1
        for i in tqdm(range(len(answerList)), ncols=100, leave=True):
        # for i in range(len(answerList)):
            answer = answerList[i]
            game = Game(answer)
            trial = game.simulate(guess)
            guessBuckets[trial - 1] += 1
            numerator += trial
            denominator += 1
            avg = numerator / denominator
        idx = dataStore["wordsToIndex"][guess]
        dataStore["avg"][idx] = avg
        dataStore["buckets"][idx] = guessBuckets
        print(f"Avg # of guesses to answer:{avg}")
    return dataStore

def buildPlots():
    """
    Builds the histograms from the simulated games.
    """
    # Candidate words are those of the top 100 starting entropy
    data = Data("DUMMY")
    candidates = []
    top = data.giveTop(100)
    for row in top:
        candidates.append(row[0])

    # Simulate these and plot their histograms and display their averages
    allData = simulateAll(candidates)
    averages = allData["avg"]

    # Get the best 2 and then add the popular starting words
    wordsToPlot = np.argsort(averages)[:6]

    # Change last 4 indices to those of the popular starting words
    wordsToPlot[2] = allData["wordsToIndex"]["ARISE"]
    wordsToPlot[3] = allData["wordsToIndex"]["ADIEU"]
    wordsToPlot[4] = allData["wordsToIndex"]["CRATE"]
    wordsToPlot[5] = allData["wordsToIndex"]["SOARE"]
    for idx in wordsToPlot:
        word = candidates[idx]
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.set_xticks(np.arange(10), labels=[1,2,3,4,5,6,7,8,9,10])
        plt.bar(np.arange(10), allData["buckets"][idx], color='green')
        plt.title(f"{word} Histogram | Avg: {averages[idx]}")
        plt.ylabel('# of games')
        plt.xlabel('# of guesses to get answer')
        fig.tight_layout()
        plt.savefig(f"plots/{word}histogram", dpi=300, bbox_inches="tight")


text:
from matplotlib import pyplot
from math import pi
from numpy import fromfile, sin, cos, tan, arctan, arccos, arcsin, sinh, cosh, tanh, sqrt, exp, abs, log10, log
from sys import exit

def decimal_places(f_expected, f_actual):
    error = abs(f_expected - f_actual)
    fraction_error = error / abs(f_expected)
    return -log10(fraction_error)

x = fromfile("x", dtype="float", count=-1, sep="\n")
sin_x = fromfile("sin_x", dtype="float", count=-1, sep="\n")
sin_x_error = decimal_places(sin(x), sin_x)
cos_x = fromfile("cos_x", dtype="float", count=-1, sep="\n")
cos_x_error = decimal_places(cos(x), cos_x)
tan_x = fromfile("tan_x", dtype="float", count=-1, sep="\n")
tan_x_error = decimal_places(tan(x), tan_x)

x_2 = fromfile("x_2", dtype="float", count=-1, sep="\n")
atan_x = fromfile("atan_x", dtype="float", count=-1, sep="\n")
atan_x_error = decimal_places(arctan(x_2), atan_x)
x_3 = fromfile("x_3", dtype="float", count=-1, sep="\n")
asin_x = fromfile("asin_x", dtype="float", count=-1, sep="\n")
asin_x_error = decimal_places(arcsin(x_3), asin_x)
acos_x = fromfile("acos_x", dtype="float", count=-1, sep="\n")
acos_x_error = decimal_places(arccos(x_3), acos_x)

x_4 = fromfile("x_4", dtype="float", count=-1, sep="\n")
sinh_x = fromfile("sinh_x", dtype="float", count=-1, sep="\n")
sinh_x_error = decimal_places(sinh(x_4), sinh_x)
cosh_x = fromfile("cosh_x", dtype="float", count=-1, sep="\n")
cosh_x_error = decimal_places(cosh(x_4), cosh_x)
tanh_x = fromfile("tanh_x", dtype="float", count=-1, sep="\n")
tanh_x_error = decimal_places(tanh(x_4), tanh_x)

x_5 = fromfile("x_5", dtype="float", count=-1, sep="\n")
sqrt_x = fromfile("sqrt_x", dtype="float", count=-1, sep="\n")
sqrt_x_error = decimal_places(sqrt(x_5), sqrt_x)

x_6 = fromfile("x_6", dtype="float", count=-1, sep="\n")
exp_x = fromfile("exp_x", dtype="float", count=-1, sep="\n")
exp_x_error = decimal_places(exp(x_6), exp_x)

x_7 = fromfile("x_7", dtype="float", count=-1, sep="\n")
log_x = fromfile("log_x", dtype="float", count=-1, sep="\n")
log_x_error = decimal_places(log(x_7), log_x)

pyplot.xticks(
    [-2.0*pi, -pi, 0, pi,  2.0*pi],
    [r'$-2\pi$', r"$-\pi$", r'$0$', r'$\pi$', r'$2\pi$'])

pyplot.figure(1)

ax = pyplot.subplot(231)
pyplot.plot(x, cos_x)
ax2 = ax.twinx()
ax2.plot(x, cos_x_error, "0.75")
pyplot.ylim(-5.5, 5.5)
ax2.set_ylim(0, 16)
pyplot.xlim(-2.2 * pi, 2.2 * pi)
pyplot.title("cos(x)")
pyplot.xlabel("x (radians)")

ax = pyplot.subplot(232)
pyplot.plot(x, sin_x)
ax2 = ax.twinx()
ax2.plot(x, sin_x_error, "0.75")
pyplot.ylim(-5.5, 5.5)
ax2.set_ylim(0, 16)
pyplot.xlim(-2.2 * pi, 2.2 * pi)
pyplot.title("sin(x)")
pyplot.xlabel("x (radians)")

ax = pyplot.subplot(233)
pyplot.plot(x, tan_x)
ax2 = ax.twinx()
ax2.plot(x, tan_x_error, "0.75")
ax.set_ylim(-5.5, 5.5)
ax2.set_ylim(0, 16)
pyplot.xlim(-2.2 * pi, 2.2 * pi)
pyplot.title("tan(x)")
pyplot.xlabel("x (radians)")

ax = pyplot.subplot(234)
pyplot.plot(x_2, atan_x)
ax2 = ax.twinx()
ax2.plot(x_2, atan_x_error, "0.75")
ax.set_xlim(-5.5, 5.5)
ax.set_ylim(-2, 2)
ax2.set_ylim(0, 16)
pyplot.title("atan(x)")
pyplot.xlabel("x (radians)")

ax = pyplot.subplot(235)
pyplot.plot(x_3, asin_x)
ax2 = ax.twinx()
ax2.plot(x_3, asin_x_error, "0.75")
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-2, 2)
ax2.set_ylim(0, 16)
pyplot.title("asin(x)")
pyplot.xlabel("x (radians)")

ax = pyplot.subplot(236)
pyplot.plot(x_3, acos_x)
ax2 = ax.twinx()
ax2.plot(x_3, acos_x_error, "0.75")
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-0.5, 3.5)
ax2.set_ylim(0, 16)
pyplot.title("acos(x)")
pyplot.xlabel("x (radians)")

pyplot.figure(2)

ax = pyplot.subplot(231)
pyplot.plot(x_4, cosh_x)
ax2 = ax.twinx()
ax2.plot(x_4, cosh_x_error, "0.75")
ax2.set_ylim(0, 16)
pyplot.xlim(-15, 15)
pyplot.title("cosh(x)")
pyplot.xlabel("x (radians)")

ax = pyplot.subplot(232)
pyplot.plot(x_4, sinh_x)
ax2 = ax.twinx()
ax2.plot(x_4, sinh_x_error, "0.75")
ax2.set_ylim(0, 16)
pyplot.xlim(-15, 15)
pyplot.title("sinh(x)")
pyplot.xlabel("x (radians)")

ax = pyplot.subplot(233)
pyplot.plot(x_4, tanh_x)
ax2 = ax.twinx()
ax2.plot(x_4, tanh_x_error, "0.75")
ax2.set_ylim(0, 16)
pyplot.xlim(-15, 15)
pyplot.title("tanh(x)")
pyplot.xlabel("x (radians)")

pyplot.figure(3)

pyplot.plot(x_5, sqrt_x)
ax2 = pyplot.twinx()
ax2.plot(x_5, sqrt_x_error, "0.75")
ax2.set_ylim(0, 16)
pyplot.xlim(0, 10)
pyplot.title("sqrt(x)")
pyplot.xlabel("x")

pyplot.figure(4)

pyplot.plot(x_6, exp_x)
ax2 = pyplot.twinx()
ax2.plot(x_6, exp_x_error, "0.75")
ax2.set_xlim(0, 10)
ax2.set_ylim(0, 16)
pyplot.xlim(-10, 10)
pyplot.title("exp(x)")
pyplot.xlabel("x")

pyplot.figure(5)

pyplot.plot(x_7, log_x)
ax2 = pyplot.twinx()
ax2.plot(x_7, log_x_error, "0.75")
ax2.set_xlim(0, 10)
ax2.set_ylim(0, 16)
pyplot.xlim(0, 10)
pyplot.title("log(x)")
pyplot.xlabel("x")

pyplot.show()

reason: from numpy


text:
import json
import os
import sys
import argparse
import numpy as np
from spacy.lang.en import English
from tqdm import tqdm

nlp = English()
nlp.add_pipe(nlp.create_pipe('sentencizer'))

def main(args):
    pred_file = os.path.join(args.model_dir, 'pred', args.pred_file)
    my_pred = json.load(open(pred_file))

    my_target = []
    avg_len = []
    for qid, pred in tqdm(enumerate(my_pred.values())):
        my_dict = {"id": str(qid), "question": None, "answers": [], "ctxs": []}

        # truncate
        pred = {key: val[:args.psg_top_k] if key in ['evidence', 'title', 'se_pos', 'prediction'] else val for key, val in pred.items()}

        # TODO: need to add id for predictions.pred in the future
        my_dict["question"] = pred["question"]
        my_dict["answers"] = pred["answer"]
        pred["title"] = [titles[0] for titles in pred["title"]]

        assert len(set(pred["evidence"])) == len(pred["evidence"]) == len(pred["title"]), "Should use opt2 for aggregation"
        # assert all(pr in evd for pr, evd in zip(pred["prediction"], pred["evidence"]))  # prediction included

        # Pad up to top-k
        if not(len(pred["prediction"]) == len(pred["evidence"]) == len(pred["title"]) == args.psg_top_k):
            assert len(pred["prediction"]) == len(pred["evidence"]) == len(pred["title"]) < args.psg_top_k, \
                (len(pred["prediction"]), len(pred["evidence"]), len(pred["title"]))
            print(len(pred["prediction"]), len(pred["evidence"]), len(pred["title"]))

            pred["evidence"] += [pred["evidence"][-1]] * (args.psg_top_k - len(pred["prediction"]))
            pred["title"] += [pred["title"][-1]] * (args.psg_top_k - len(pred["prediction"]))
            pred["se_pos"] += [pred["se_pos"][-1]] * (args.psg_top_k - len(pred["prediction"]))
            pred["prediction"] += [pred["prediction"][-1]] * (args.psg_top_k - len(pred["prediction"]))
            assert len(pred["prediction"]) == len(pred["evidence"]) == len(pred["title"]) == args.psg_top_k

        # Used for markers
        START = '<p_start>'
        END = '<p_end>'
        se_idxs = [[se_pos[0], max(se_pos[0], se_pos[1])] for se_pos in pred["se_pos"]]

        # Return sentence
        if args.return_sent:
            sents = [[(X.text, X[0].idx) for X in nlp(evidence).sents] for evidence in pred['evidence']]
            sent_idxs = [
                sorted(set([sum(np.array([st[1] for st in sent]) <= se_idx[0]) - 1] + [sum(np.array([st[1] for st in sent]) <= se_idx[1]-1) - 1]))
                for se_idx, sent in zip(se_idxs, sents)
            ]
            se_idxs = [[se_pos[0]-sent[sent_idx[0]][1], se_pos[1]-sent[sent_idx[0]][1]] for se_pos, sent_idx, sent in zip(se_idxs, sent_idxs, sents)]
            if not all(pred.replace(' ', '') in ' '.join([sent[sidx][0] for sidx in range(sent_idx[0], sent_idx[-1]+1)]).replace(' ', '')
                       for pred, sent, sent_idx in zip(pred['prediction'], sents, sent_idxs)):
                import pdb; pdb.set_trace()
                pass

            # get sentence based on the window
            max_context_len = args.max_context_len - 2 if args.mark_phrase else args.max_context_len
            my_dict["ctxs"] = [
                # {"title": title, "text": ' '.join(' '.join([sent[sidx][0] for sidx in range(sent_idx[0], sent_idx[-1]+1)]).split()[:max_context_len])}
                {"title": title, "text": ' '.join(' '.join([sent[sidx][0] for sidx in range(
                    max(0, sent_idx[0]-args.sent_window), min(sent_idx[-1]+1+args.sent_window, len(sent)))]
                    ).split()[:max_context_len])
                }
                for title, sent, sent_idx in zip(pred["title"], sents, sent_idxs)
            ]
        # Return passagae
        else:
            my_dict["ctxs"] = [
                {"title": title, "text": ' '.join(evd.split()[:args.max_context_len])}
                for evd, title in zip(pred["evidence"], pred["title"])
            ]

        # Add markers for predicted phrases
        if args.mark_phrase:
            my_dict["ctxs"] = [
                {"title": ctx["title"], "text": ctx["text"][:se[0]] + f"{START} " + ctx["text"][se[0]:se[1]] + f" {END}" + ctx["text"][se[1]:]}
                for ctx, se in zip(my_dict["ctxs"], se_idxs)
            ]

        my_target.append(my_dict)
        avg_len += [len(ctx['text'].split()) for ctx in my_dict["ctxs"]]
        assert len(my_dict["ctxs"]) == args.psg_top_k
        assert all(len(ctx['text'].split()) <= args.max_context_len for ctx in my_dict["ctxs"])

    print(f"avg ctx len={sum(avg_len)/len(avg_len):.2f} for {len(my_pred)} preds")

    out_file = os.path.join(
        args.model_dir, 'pred',
        os.path.splitext(args.pred_file)[0] +
        f'_{"sent" if args.return_sent else "psg"}-top{args.psg_top_k}{"_mark" if args.mark_phrase else ""}.json'
    )
    print(f"dump to {out_file}")
    json.dump(my_target, open(out_file, 'w'), indent=4)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('--model_dir', type=str, default='')
    parser.add_argument('--pred_file', type=str, default='')
    parser.add_argument('--psg_top_k', type=int, default=100)
    parser.add_argument('--max_context_len', type=int, default=999999999)
    parser.add_argument('--mark_phrase', default=False, action='store_true')
    parser.add_argument('--return_sent', default=False, action='store_true')
    parser.add_argument('--sent_window', type=int, default=0)
    args = parser.parse_args()

    main(args)

max_stars_repo_name: howard-yen/DensePhrases

max_issues_repo_name: howard-yen/DensePhrases

max_forks_repo_name: howard-yen/DensePhrases


text:
''' This module contains basic neural network algorithms implemented using the NumPy library. This allows for
    drastically improved performance over the pure Python implementation. '''

import numpy

from nlplib.core.control import neuralnetwork as python
from nlplib.general import composite

__all__ = ['Array', 'Matrix', 'Feedforward', 'Backpropagate']

class Array (python.Array) :

    __slots__ = ('_values',)

    def __init__ (self, values=()) :
        self._values = numpy.array(list(values))

class Matrix (python.Matrix) :

    __slots__ = ('_values',)

    def __init__ (self, values=()) :
        self._values = numpy.matrix(list(values))

    def __iter__ (self) :
        for row in self._values :
            yield tuple(row.flat)

    def transpose (self) :
        matrix = self.__class__.__new__(self.__class__)
        matrix._values = self._values.transpose()
        return matrix

    def width (self) :
        return self._values.shape[1]

    def height (self) :
        return self._values.shape[0]

class Feedforward (python.Feedforward) :
    ''' A fast implementation of the feedforward neural network algorithm, using the NumPy library. '''

    def _feedforward (self) :
        activation = numpy.vectorize(self.activation)
        for inputs, connection, outputs in self.structure :
            outputs._charges._values[:] = activation(numpy.asarray(self._excitement(connection,
                                                                                inputs.charges)).flatten())
            outputs._charges = outputs._charges

        return outputs

    def _excitement (self, connection, charges) :
        return numpy.dot(connection._weights._values, charges._values)

    def _reset_input_charges (self) :
        inputs = self.structure.inputs()

        inputs._charges._values.fill(self.inactive)
        inputs._charges._values[list(self.input_indexes)] = self.active
        inputs._charges = inputs._charges

class Backpropagate (python.Backpropagate) :
    ''' A fast implementation of the backpropagation neural network training algorithm, using the NumPy library. '''

    def _total_error (self, differences) :
        return (0.5 * differences ** 2).sum()

    @composite(lambda self : (self.activation_derivative,))
    def _vectorized_activation_derivative (self) :
        return numpy.vectorize(self.activation_derivative)

    def _output_errors (self) :
        activation_derivative = self._vectorized_activation_derivative

        outputs = self.structure.outputs()

        correct = numpy.full(len(outputs), self.inactive)
        correct[list(self.output_indexes)] = self.active

        differences = correct - outputs._charges._values

        outputs._errors._values[:] = activation_derivative(outputs._charges._values) * differences

        # Due to a quirk in SQLAlchemy, mutations may not get written to the database. Reassigning the name, makes sure
        # that the mutations are recorded.
        outputs._errors = outputs._errors

        return differences

    def _excitement_error (self, connection, errors) :
        return numpy.asarray(numpy.dot(connection._weights._values.transpose(), errors._values)).flatten()

    def _hidden_errors (self) :

        activation_derivative = self._vectorized_activation_derivative

        for inputs, connection, outputs in reversed(list(self.structure)[1:]) :

            errors = self._excitement_error(connection, outputs.errors)

            inputs._errors._values[:] = activation_derivative(inputs._charges._values) * errors

            # This step is necessary, see the comment in <Backpropagate._output_errors>.
            inputs._errors = inputs._errors

    def _update_connection_weights (self) :
        for inputs, connection, outputs in reversed(self.structure) :
            weights = self.rate * (inputs._charges._values * outputs._errors._values[:,numpy.newaxis])

            connection._weights._values += weights

            # This step is necessary, see the comment in <Backpropagate._output_errors>.
            connection._weights = connection._weights

def __test__ (ut) :
    from nlplib.core.control.neuralnetwork import _test_collections, _test_feedforward_and_backpropagate

    # The same tests work for the NumPy neural network algorithms.
    _test_collections(ut, array_cls=Array, matrix_cls=Matrix)
    _test_feedforward_and_backpropagate(ut, feedforward_cls=Feedforward, backpropagate_cls=Backpropagate)

if __name__ == '__main__' :
    from nlplib.general.unittest import UnitTest
    __test__(UnitTest())

max_stars_repo_name: rectangletangle/nlplib

max_issues_repo_name: rectangletangle/nlplib

max_forks_repo_name: rectangletangle/nlplib


text:
from win32com.client import CastTo, constants
import os
import matplotlib.pyplot as plt
import numpy as np
import inspect

from zosapi.base import PythonStandaloneApplication
from zosapi.objects import Parameter, Component
from zosapi.util import extractZernikeCoefficents

class ZOSAPIAnalysis(object):

    def __init__(self, fileName):
        self.zosapi = PythonStandaloneApplication()
        value = self.zosapi.ExampleConstants()

        self.TheSystem = self.zosapi.TheSystem
        self.TheApplication = self.zosapi.TheApplication

        # Set up primary optical system
        import os
        cwd = os.getcwd()
        file = cwd + "\\" + fileName
        self.TheSystem.LoadFile(file, False)
        print("File Imported")

    def definePertubationAndGetResults(self, component, params=["dx"]):
        import os
        cwd = os.getcwd()

        param1 = component.params[0]
        vals1 = np.linspace(param1.minval, param1.maxval, num=((param1.maxval-param1.minval)/param1.step)+1)
        param2 = component.params[1]
        vals2 = np.linspace(param2.minval, param2.maxval, num=((param2.maxval-param2.minval)/param2.step)+1)

        for i in vals1:
            if i == 0:
                continue

            for j in vals2:
                if j == 0:
                    continue
                print(param1.name + " = " + "{0:.3f}".format(i) + " " + param2.name + " = " + "{0:.3f}".format(j))
                self.datadir = cwd + "\\data\\" + component.compName + "\\" + param1.name + "{0:.3f}".format(i) + param2.name + "{0:.3f}".format(j) + "\\"
                if not os.path.exists(self.datadir):
                    os.makedirs(self.datadir)

                Decenter_X = Decenter_Y = Tilt_About_X = Tilt_About_Y = Tilt_About_Z = 0

                if param == "dx":
                    Decenter_X = optimum1 + i
                if param == "dy":
                    Decenter_Y = optimum1 + i
                if param == "tx":
                    Tilt_About_X = optimum1 + i
                if param == "ty":
                    Tilt_About_Y = optimum1 + i
                if param == "tz":
                    Tilt_About_Z = optimum1 + i

                self.CreatePertubationInSurface(surfaceNumber, Decenter_X=Decenter_X, Decenter_Y=Decenter_Y,
                                                Tilt_About_X=Tilt_About_X, Tilt_About_Y=Tilt_About_Y, Tilt_About_Z=Tilt_About_Z)

                if revSurfaceNumber is not None:
                    Decenter_X = Decenter_Y = Tilt_About_X = Tilt_About_Y = Tilt_About_Z = 0

                    if param == "dx":
                        Decenter_X = optimum2 - i
                    if param == "dy":
                        Decenter_Y = optimum2 - i
                    if param == "tx":
                        Tilt_About_X = optimum2 - i
                    if param == "ty":
                        Tilt_About_Y = optimum2 - i
                    if param == "tz":
                        Tilt_About_Z = optimum2 - i

                    self.CreatePertubationInSurface(revSurfaceNumber, Decenter_X=Decenter_X, Decenter_Y=Decenter_Y,
                                                    Tilt_About_X=Tilt_About_X, Tilt_About_Y=Tilt_About_Y, Tilt_About_Z=Tilt_About_Z)

                self.SpotDiagramAnalysisResults()
                self.ZernikeCoefficients()
                # self.CreateBatchRayTrace()
                # print("aaa")

    def createComponents(self):
        TheLDE = self.TheSystem.LDE

        # Primary mirror
        Surface1 = TheLDE.GetSurfaceAt(5)
        Surface2 = TheLDE.GetSurfaceAt(7)

        self.pm = Component(Surface1, Surface2)
        self.pm.createParameter("dx", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par1).DoubleValue, -0.05, 0.05, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par1).DoubleValue)
        self.pm.createParameter("dy", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par2).DoubleValue, -0.05, 0.05, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par2).DoubleValue)
        self.pm.createParameter("tx", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par3).DoubleValue, -0.017, 0.017, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par3).DoubleValue)
        self.pm.createParameter("ty", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par4).DoubleValue, -0.017, 0.017, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par4).DoubleValue)

        # Secondary mirror
        Surface1 = TheLDE.GetSurfaceAt(8)
        Surface2 = TheLDE.GetSurfaceAt(10)

        self.sm = Component(Surface1, Surface2)
        self.sm.createParameter("dx", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par1).DoubleValue, -0.05, 0.05, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par1).DoubleValue)
        self.sm.createParameter("dy", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par2).DoubleValue, -0.05, 0.05, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par2).DoubleValue)
        self.sm.createParameter("tx", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par3).DoubleValue, -0.017, 0.017, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par3).DoubleValue)
        self.sm.createParameter("ty", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par4).DoubleValue, -0.017, 0.017, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par4).DoubleValue)

        # Lens
        Surface1 = TheLDE.GetSurfaceAt(20)
        Surface2 = TheLDE.GetSurfaceAt(23)

        self.lens = Component(Surface1, Surface2)
        self.lens.createParameter("dx", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par1).DoubleValue, -0.02, 0.02, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par1).DoubleValue)
        self.lens.createParameter("dy", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par2).DoubleValue, -0.02, 0.02, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par2).DoubleValue)
        self.lens.createParameter("tx", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par3).DoubleValue, -0.017, 0.017, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par3).DoubleValue)
        self.lens.createParameter("ty", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par4).DoubleValue, -0.017, 0.017, 0.001, Surface2.GetSurfaceCell(constants.SurfaceColumn_Par4).DoubleValue)

        # CCD
        Surface1 = TheLDE.GetSurfaceAt(23)

        self.ccd = Component(Surface1)
        self.ccd.createParameter("dx", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par1).DoubleValue, -2, 2, 0.001)
        self.ccd.createParameter("dy", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par2).DoubleValue, -0.05, 0.05, 0.001)
        self.ccd.createParameter("ty", Surface1.GetSurfaceCell(constants.SurfaceColumn_Par3).DoubleValue, -0.05, 0.05, 0.001)

    def CreatePertubationInSurface(self, surfaceNumber, Decenter_X=0.0, Decenter_Y=0.0,
                                   Tilt_About_X=0.0, Tilt_About_Y=0.0,
                                   Tilt_About_Z=0.0):
        # Get Surfaces
        TheLDE = self.TheSystem.LDE
        Surface = TheLDE.GetSurfaceAt(surfaceNumber)

        Surface.GetSurfaceCell(
            constants.SurfaceColumn_Par1).DoubleValue = float(Decenter_X)
        Surface.GetSurfaceCell(
            constants.SurfaceColumn_Par2).DoubleValue = float(Decenter_Y)
        Surface.GetSurfaceCell(
            constants.SurfaceColumn_Par3).DoubleValue = float(Tilt_About_X)
        Surface.GetSurfaceCell(
            constants.SurfaceColumn_Par4).DoubleValue = float(Tilt_About_Y)
        Surface.GetSurfaceCell(
            constants.SurfaceColumn_Par5).DoubleValue = float(Tilt_About_Z)

        # print('Pertubation in Surface {} Done!'.format(surfaceNumber))

    def ZernikeCoefficients(self, fileName=None):
        # Zernike Standard Coefficients Analysis Results
        zernike = self.TheSystem.Analyses.New_Analysis(
            constants.AnalysisIDM_ZernikeStandardCoefficients)
        zernike_setting = zernike.GetSettings()
        baseSetting = CastTo(
            zernike_setting, 'IAS_ZernikeStandardCoefficients')
        baseSetting.Field.SetFieldNumber(0)
        baseSetting.Wavelength.UseAllWavelengths()
        baseSetting.Surface.UseImageSurface()

        # Extract Zernike Coefficients
        base = CastTo(zernike, 'IA_')
        base.ApplyAndWaitForCompletion()
        zernike_results = base.GetResults()
        results = CastTo(zernike_results, 'IAR_')

        file = fileName if fileName is not None else "res.txt"
        import os
        cwd = os.getcwd()
        path = self.datadir + file
        results.GetTextFile(path)
        coefficients = extractZernikeCoefficents(path)
        np.savetxt(self.datadir + "zernike.csv", coefficients, delimiter=",")
        zernike.Close()

    def CreateBatchRayTrace(self, max_rays=30):
        # Set up Batch Ray Trace
        raytrace = self.TheSystem.Tools.OpenBatchRayTrace()
        nsur = self.TheSystem.LDE.NumberOfSurfaces
        normUnPolData = raytrace.CreateNormUnpol(
            (max_rays + 1) * (max_rays + 1), constants.RaysType_Real, nsur)

        # Define batch ray trace constants
        hx = 0.0
        hy = 0.0
        max_wave = self.TheSystem.SystemData.Wavelengths.NumberOfWavelengths

        # Initialize x/y image plane arrays
        x_ary = np.empty((max_wave, ((max_rays + 1) * (max_rays + 1))))
        y_ary = np.empty((max_wave, ((max_rays + 1) * (max_rays + 1))))

        plt.rcParams["figure.figsize"] = (5, 5)
        colors = ('k', 'g', 'r')
        markers = ('+', 's', '^')

        if self.TheSystem.SystemData.Fields.GetFieldType() == constants.FieldType_Angle:
            field_type = 'Angle'
        elif self.TheSystem.SystemData.Fields.GetFieldType() == constants.FieldType_ObjectHeight:
            field_type = 'Height'
        elif self.TheSystem.SystemData.Fields.GetFieldType() == constants.FieldType_ParaxialImageHeight:
            field_type = 'Height'
        elif self.TheSystem.SystemData.Fields.GetFieldType() == constants.FieldType_RealImageHeight:
            field_type = 'Height'

        for wave in range(1, max_wave + 1):
            # Adding Rays to Batch
            normUnPolData.ClearData()
            for i in range(1, (max_rays + 1) * (max_rays + 1) + 1):

                px = np.random.random() * 2 - 1
                py = np.random.random() * 2 - 1

                while (px*px + py*py > 1):
                    py = np.random.random() * 2 - 1
                normUnPolData.AddRay(
                    wave, hx, hy, px, py, constants.OPDMode_None)

            baseTool = CastTo(raytrace, 'ISystemTool')
            baseTool.RunAndWaitForCompletion()

            # Read batch raytrace and display results
            normUnPolData.StartReadingResults()
            output = normUnPolData.ReadNextResult()

            while output[0]:    # success
                # ErrorCode & vignetteCode
                if ((output[2] == 0) and (output[3] == 0)):
                    x_ary[wave - 1, output[1] - 1] = output[4]   # X
                    y_ary[wave - 1, output[1] - 1] = output[5]   # Y
                output = normUnPolData.ReadNextResult()
            temp = plt.plot(np.squeeze(x_ary[wave - 1, :]), np.squeeze(
                y_ary[wave - 1, :]), '.', ms=3, c=colors[wave - 1],
                marker=markers[wave - 1])

        np.savetxt(self.datadir + "x.csv", x_ary, delimiter=",")
        np.savetxt(self.datadir + "y.csv", y_ary, delimiter=",")
        plt.title('Spot Diagram')
        plt.savefig(self.datadir + "SpotDiagram.png")
        raytrace.Close()

    def SpotDiagramAnalysisResults(self):
        # Spot Diagram Analysis Results
        spot = self.TheSystem.Analyses.New_Analysis(
            constants.AnalysisIDM_StandardSpot)
        spot_setting = spot.GetSettings()
        baseSetting = CastTo(spot_setting, 'IAS_Spot')
        baseSetting.Field.SetFieldNumber(0)
        baseSetting.Wavelength.UseAllWavelengths()
        baseSetting.Surface.UseImageSurface()

        # extract RMS & Geo spot size for field points
        base = CastTo(spot, 'IA_')
        base.ApplyAndWaitForCompletion()
        spot_results = base.GetResults()
        f = open(self.datadir + "rmsgeo.txt", "w+")
        f.write(str(spot_results.SpotData.GetRMSSpotSizeFor(1, 1)) + "\n" +
                str(spot_results.SpotData.GetGeoSpotSizeFor(1, 1)))
        # print('RMS radius: %6.3f' %
        #       (spot_results.SpotData.GetRMSSpotSizeFor(1, 1)))
        # print('GEO radius: %6.3f' %
        #       (spot_results.SpotData.GetGeoSpotSizeFor(1, 1)))
        spot.Close()

if __name__ == '__main__':
    file = "test2.zmx"
    ob = ZOSAPIAnalysis(file)

    ob.createComponents()

    # # Perturbation in Primary Mirror (Surface-5,7)
    # print('Perturbation in Primary Mirror')
    # ob.definePertubationAndGetResults(
    #     5, 7, element="pm", param="dx", minval=-0.05, maxval=0.05, step=0.001)
    # ob.definePertubationAndGetResults(
    #     5, 7, element="pm", param="dy", minval=-0.05, maxval=0.05, step=0.001)
    # ob.definePertubationAndGetResults(
    #     5, 7, element="pm", param="tx", minval=-0.017, maxval=0.017, step=0.001)
    # ob.definePertubationAndGetResults(
    #     5, 7, element="pm", param="ty", minval=-0.017, maxval=0.017, step=0.001)

    # # Perturbation in Secondary Mirror (Surface-8,10)
    # print('Perturbation in Secondary Mirror')
    # ob.definePertubationAndGetResults(
    #     8, 10, element="sm", param="dx", minval=-0.05, maxval=0.05, step=0.001)
    # ob.definePertubationAndGetResults(
    #     8, 10, element="sm", param="dy", minval=-0.05, maxval=0.05, step=0.001)
    # ob.definePertubationAndGetResults(
    #     8, 10, element="sm", param="tx", minval=-0.017, maxval=0.017, step=0.001)
    # ob.definePertubationAndGetResults(
    #     8, 10, element="sm", param="ty", minval=-0.017, maxval=0.017, step=0.001)

    # # Perturbation in Lens (Surface-20,23)
    # print('Perturbation in Lens')
    # ob.definePertubationAndGetResults(
    #     20, 23, element="lens", param="dx", minval=-0.02, maxval=0.02, step=0.001)
    # ob.definePertubationAndGetResults(
    #     20, 23, element="lens", param="dy", minval=-0.02, maxval=0.02, step=0.001)
    # ob.definePertubationAndGetResults(
    #     20, 23, element="lens", param="tx", minval=-0.017, maxval=0.017, step=0.001)
    # ob.definePertubationAndGetResults(
    #     20, 23, element="lens", param="ty", minval=-0.017, maxval=0.017, step=0.001)

    # # Perturbation in CCD (Surface-23)
    # print('Perturbation in CCD')
    # ob.definePertubationAndGetResults(
    #     23, element="ccd", param="dx", minval=-2, maxval=2, step=0.01)
    # ob.definePertubationAndGetResults(
    #     23, element="ccd", param="dy", minval=-2, maxval=2, step=0.01)
    # ob.definePertubationAndGetResults(
    #     23, element="ccd", param="ty", minval=-0.05, maxval=0.05, step=0.001)

    '''This will clean up the connection to OpticStudio.
    Note that it closes down the server instance of OpticStudio,
    so you for maximum performance do not do this until you need to.'''
    del ob.zosapi
    ob.zosapi = None


text:
from bob.pipelines import (
    DelayedSample,
    SampleSet,
    Sample,
    DelayedSampleSet,
    DelayedSampleSetCached,
)
import bob.io.base
import os
import dask
import functools
from .score_writers import FourColumnsScoreWriter
from .abstract_classes import BioAlgorithm
import bob.pipelines
import numpy as np
import h5py
from .zt_norm import ZTNormPipeline, ZTNormDaskWrapper
from .legacy import BioAlgorithmLegacy
from bob.bio.base.transformers import (
    PreprocessorTransformer,
    ExtractorTransformer,
    AlgorithmTransformer,
)
from bob.pipelines.wrappers import SampleWrapper, CheckpointWrapper
from bob.pipelines.distributed.sge import SGEMultipleQueuesCluster
import logging
from bob.pipelines.utils import isinstance_nested
import gc
import time
from . import pickle_compress, uncompress_unpickle

logger = logging.getLogger(__name__)

class BioAlgorithmCheckpointWrapper(BioAlgorithm):
    """Wrapper used to checkpoint enrolled and Scoring samples.

    Parameters
    ----------
        biometric_algorithm: :any:`bob.bio.base.pipelines.vanilla_biometrics.BioAlgorithm`
           An implemented :any:`bob.bio.base.pipelines.vanilla_biometrics.BioAlgorithm`

        base_dir: str
           Path to store biometric references and scores

        extension: str
            File extension

        force: bool
          If True, will recompute scores and biometric references no matter if a file exists

        hash_fn
        Pointer to a hash function. This hash function maps
        `sample.key` to a hash code and this hash code corresponds a relative directory
        where a single `sample` will be checkpointed.
        This is useful when is desirable file directories with less than
        a certain number of files.

    Examples
    --------

    >>> from bob.bio.base.pipelines.vanilla_biometrics import BioAlgorithmCheckpointWrapper, Distance
    >>> biometric_algorithm = BioAlgorithmCheckpointWrapper(Distance(), base_dir="./")
    >>> biometric_algorithm.enroll(sample) # doctest: +SKIP

    """

    def __init__(
        self,
        biometric_algorithm,
        base_dir,
        group=None,
        force=False,
        hash_fn=None,
        **kwargs
    ):
        super().__init__(**kwargs)

        self.base_dir = base_dir
        self.set_score_references_path(group)

        self.biometric_algorithm = biometric_algorithm
        self.force = force
        self._biometric_reference_extension = ".hdf5"
        self._score_extension = ".pickle.gz"
        self.hash_fn = hash_fn

    def clear_caches(self):
        self.biometric_algorithm.clear_caches()

    def set_score_references_path(self, group):
        if group is None:
            self.biometric_reference_dir = os.path.join(
                self.base_dir, "biometric_references"
            )
            self.score_dir = os.path.join(self.base_dir, "scores")
        else:
            self.biometric_reference_dir = os.path.join(
                self.base_dir, group, "biometric_references"
            )
            self.score_dir = os.path.join(self.base_dir, group, "scores")

    def enroll(self, enroll_features):
        return self.biometric_algorithm.enroll(enroll_features)

    def score(self, biometric_reference, data):
        return self.biometric_algorithm.score(biometric_reference, data)

    def score_multiple_biometric_references(self, biometric_references, data):
        return self.biometric_algorithm.score_multiple_biometric_references(
            biometric_references, data
        )

    def write_biometric_reference(self, sample, path):
        return bob.io.base.save(sample.data, path, create_directories=True)

    def write_scores(self, samples, path):
        pickle_compress(path, samples)

    def _enroll_sample_set(self, sampleset):
        """
        Enroll a sample set with checkpointing
        """

        # Amending `models` directory
        hash_dir_name = (
            self.hash_fn(str(sampleset.key)) if self.hash_fn is not None else ""
        )

        path = os.path.join(
            self.biometric_reference_dir,
            hash_dir_name,
            str(sampleset.key) + self._biometric_reference_extension,
        )

        if self.force or not os.path.exists(path):

            enrolled_sample = self.biometric_algorithm._enroll_sample_set(sampleset)

            # saving the new sample
            self.write_biometric_reference(enrolled_sample, path)

        # This seems inefficient, but it's crucial for large datasets
        delayed_enrolled_sample = DelayedSample(
            functools.partial(bob.io.base.load, path), parent=sampleset
        )

        return delayed_enrolled_sample

    def _score_sample_set(
        self,
        sampleset,
        biometric_references,
        allow_scoring_with_all_biometric_references=False,
    ):
        """Given a sampleset for probing, compute the scores and returns a sample set with the scores
        """

        def _load(path):
            return uncompress_unpickle(path)

        def _make_name(sampleset, biometric_references):
            # The score file name is composed by sampleset key and the
            # first 3 biometric_references
            reference_id = str(sampleset.reference_id)
            name = str(sampleset.key)
            suffix = "_".join([str(s.key) for s in biometric_references[0:3]])
            return os.path.join(reference_id, name + suffix)

        # Amending `models` directory
        hash_dir_name = (
            self.hash_fn(str(sampleset.key)) if self.hash_fn is not None else ""
        )

        path = os.path.join(
            self.score_dir,
            hash_dir_name,
            _make_name(sampleset, biometric_references) + self._score_extension,
        )

        parent = sampleset
        if self.force or not os.path.exists(path):

            # Computing score
            scored_sample_set = self.biometric_algorithm._score_sample_set(
                sampleset,
                biometric_references,
                allow_scoring_with_all_biometric_references=allow_scoring_with_all_biometric_references,
            )
            self.write_scores(scored_sample_set.samples, path)
            parent = scored_sample_set

        scored_sample_set = DelayedSampleSetCached(
            functools.partial(_load, path), parent=parent
        )

        return scored_sample_set

class BioAlgorithmDaskWrapper(BioAlgorithm):
    """
    Wrap :any:`bob.bio.base.pipelines.vanilla_biometrics.BioAlgorithm` to work with DASK
    """

    def __init__(self, biometric_algorithm, **kwargs):
        self.biometric_algorithm = biometric_algorithm

    def clear_caches(self):
        self.biometric_algorithm.clear_caches()

    def enroll_samples(self, biometric_reference_features):

        biometric_references = biometric_reference_features.map_partitions(
            self.biometric_algorithm.enroll_samples
        )

        return biometric_references

    def score_samples(
        self,
        probe_features,
        biometric_references,
        allow_scoring_with_all_biometric_references=False,
    ):

        # TODO: Here, we are sending all computed biometric references to all
        # probes.  It would be more efficient if only the models related to each
        # probe are sent to the probing split.  An option would be to use caching
        # and allow the ``score`` function above to load the required data from
        # the disk, directly.  A second option would be to generate named delays
        # for each model and then associate them here.

        all_references = dask.delayed(list)(biometric_references)
        scores = probe_features.map_partitions(
            self.biometric_algorithm.score_samples,
            all_references,
            allow_scoring_with_all_biometric_references=allow_scoring_with_all_biometric_references,
        )
        return scores

    def enroll(self, data):
        return self.biometric_algorithm.enroll(data)

    def score(self, biometric_reference, data):
        return self.biometric_algorithm.score(biometric_reference, data)

    def score_multiple_biometric_references(self, biometric_references, data):
        return self.biometric_algorithm.score_multiple_biometric_references(
            biometric_references, data
        )

    def set_score_references_path(self, group):
        self.biometric_algorithm.set_score_references_path(group)

def dask_vanilla_biometrics(pipeline, npartitions=None, partition_size=None):
    """
    Given a :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline`, wraps :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline` and
    :any:`bob.bio.base.pipelines.vanilla_biometrics.BioAlgorithm` to be executed with dask

    Parameters
    ----------

    pipeline: :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline`
       Vanilla Biometrics based pipeline to be dasked

    npartitions: int
       Number of partitions for the initial `dask.bag`

    partition_size: int
       Size of the partition for the initial `dask.bag`
    """

    if isinstance(pipeline, ZTNormPipeline):
        # Dasking the first part of the pipelines
        pipeline.vanilla_biometrics_pipeline = dask_vanilla_biometrics(
            pipeline.vanilla_biometrics_pipeline,
            npartitions=npartitions,
            partition_size=partition_size,
        )
        pipeline.biometric_algorithm = (
            pipeline.vanilla_biometrics_pipeline.biometric_algorithm
        )
        pipeline.transformer = pipeline.vanilla_biometrics_pipeline.transformer

        pipeline.ztnorm_solver = ZTNormDaskWrapper(pipeline.ztnorm_solver)

    else:

        if partition_size is None:
            pipeline.transformer = bob.pipelines.wrap(
                ["dask"], pipeline.transformer, npartitions=npartitions
            )
        else:
            pipeline.transformer = bob.pipelines.wrap(
                ["dask"], pipeline.transformer, partition_size=partition_size
            )
        pipeline.biometric_algorithm = BioAlgorithmDaskWrapper(
            pipeline.biometric_algorithm
        )

        def _write_scores(scores):
            return scores.map_partitions(pipeline.write_scores_on_dask)

        pipeline.write_scores_on_dask = pipeline.write_scores
        pipeline.write_scores = _write_scores

    return pipeline

def checkpoint_vanilla_biometrics(
    pipeline, base_dir, biometric_algorithm_dir=None, hash_fn=None
):
    """
    Given a :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline`, wraps :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline` and
    :any:`bob.bio.base.pipelines.vanilla_biometrics.BioAlgorithm` to be checkpointed

    Parameters
    ----------

    pipeline: :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline`
       Vanilla Biometrics based pipeline to be checkpointed

    base_dir: str
       Path to store transformed input data and possibly biometric references and scores

    biometric_algorithm_dir: str
       If set, it will checkpoint the biometric references and scores to this path.
       If not, `base_dir` will be used.
       This is useful when it's suitable to have the transformed data path, and biometric references and scores
       in different paths.

    hash_fn
       Pointer to a hash function. This hash function will map
       `sample.key` to a hash code and this hash code will be the
       relative directory where a single `sample` will be checkpointed.
       This is useful when is desireable file directories with more than
       a certain number of files.
    """

    sk_pipeline = pipeline.transformer

    bio_ref_scores_dir = (
        base_dir if biometric_algorithm_dir is None else biometric_algorithm_dir
    )

    for i, name, estimator in sk_pipeline._iter():

        wraped_estimator = bob.pipelines.wrap(
            ["checkpoint"],
            estimator,
            features_dir=os.path.join(base_dir, name),
            hash_fn=hash_fn,
        )

        sk_pipeline.steps[i] = (name, wraped_estimator)

    if isinstance(pipeline.biometric_algorithm, BioAlgorithmLegacy):
        pipeline.biometric_algorithm.base_dir = bio_ref_scores_dir

        # Here we need to check if the LAST transformer is
        # 1. is instance of CheckpointWrapper
        # 2. Its estimator is instance of AlgorithmTransformer
        if (
            isinstance(pipeline.transformer[-1], CheckpointWrapper)
            and hasattr(pipeline.transformer[-1].estimator, "estimator")
            and isinstance(
                pipeline.transformer[-1].estimator.estimator, AlgorithmTransformer
            )
        ):

            pipeline.transformer[
                -1
            ].estimator.estimator.projector_file = bio_ref_scores_dir

    else:
        pipeline.biometric_algorithm = BioAlgorithmCheckpointWrapper(
            pipeline.biometric_algorithm, base_dir=bio_ref_scores_dir, hash_fn=hash_fn
        )

    return pipeline

def is_checkpointed(pipeline):
    """
    Check if :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline` is checkpointed

    Parameters
    ----------

    pipeline: :any:`bob.bio.base.pipelines.vanilla_biometrics.VanillaBiometricsPipeline`
       Vanilla Biometrics based pipeline to be checkpointed

    """

    # We have to check if is BioAlgorithmCheckpointWrapper OR
    # If it BioAlgorithmLegacy and the transformer of BioAlgorithmLegacy is also checkpointable
    return isinstance_nested(
        pipeline, "biometric_algorithm", BioAlgorithmCheckpointWrapper
    ) or (
        isinstance_nested(pipeline, "biometric_algorithm", BioAlgorithmLegacy)
        and isinstance_nested(pipeline, "transformer", CheckpointWrapper)
    )


text:
import numpy as np
import numba
import pyximport
pyximport.install(
    setup_args={'include_dirs': [np.get_include()]}
)
from cont_frac import golden_ratio as cy_golden_ratio
from cont_frac import cy_cont_frac
from cont_frac import square_erf
# from cont_frac import hello

@numba.jit
def golden_ratio(n=1000):
    c = 1
    for _ in range(n-1):
        c = 1 + 1/c
    return c

phi = 0.5 * (np.sqrt(5) + 1)
assert abs(phi - golden_ratio()) < 1e-6
assert abs(phi - cy_golden_ratio(1000)) < 1e-6
print(square_erf(0), square_erf(1), square_erf(np.inf))
#hello()

a = np.ones(1000, dtype=np.float64)
print(cy_cont_frac(a))

reason: import numpy,import numba


text:
# -*- coding: utf-8 -*-
"""
An example of a configuration file to launch some the simulations, for the single-player case.
"""
from __future__ import division, print_function  # Python 2 compatibility

__author__ = "Lilian Besson"
__version__ = "0.9"

CPU_COUNT = 4

from os import getenv
import numpy as np

# Import arms
from Arms import *
# Import contained classes
from Environment import MAB
# Import single-player algorithms
from Policies import *

#: HORIZON : number of time steps of the experiments.
#: Warning Should be >= 10000 to be interesting "asymptotically".
HORIZON = 10000
HORIZON = int(getenv('T', HORIZON))

#: REPETITIONS : number of repetitions of the experiments.
#: Warning: Should be >= 10 to be statistically trustworthy.
REPETITIONS = 10
REPETITIONS = int(getenv('N', REPETITIONS))

#: To profile the code, turn down parallel computing
DO_PARALLEL = True

#: Number of jobs to use for the parallel computations. -1 means all the CPU cores, 1 means no parallelization.
N_JOBS = -1 if DO_PARALLEL else 1
N_JOBS = int(getenv('N_JOBS', N_JOBS))

#: Number of arms for non-hard-coded problems (Bayesian problems)
NB_ARMS = 3
NB_ARMS = int(getenv('K', NB_ARMS))
NB_ARMS = int(getenv('NB_ARMS', NB_ARMS))

#: Default value for the lower value of means
LOWER = 0.
#: Default value for the amplitude value of means
AMPLITUDE = 1.

#: Type of arms for non-hard-coded problems (Bayesian problems)
ARM_TYPE = "Bernoulli"
ARM_TYPE = str(getenv('ARM_TYPE', ARM_TYPE))
if ARM_TYPE in ["UnboundedGaussian"]:
    LOWER = -5
    AMPLITUDE = 10

LOWER = float(getenv('LOWER', LOWER))
AMPLITUDE = float(getenv('AMPLITUDE', AMPLITUDE))
assert AMPLITUDE > 0, "Error: invalid amplitude = {:.3g} but has to be > 0."  # DEBUG

ARM_TYPE_str = str(ARM_TYPE)
ARM_TYPE = mapping_ARM_TYPE[ARM_TYPE]

#: True to use bayesian problem
ENVIRONMENT_BAYESIAN = False
ENVIRONMENT_BAYESIAN = getenv('BAYES', str(ENVIRONMENT_BAYESIAN)) == 'True'

#: Means of arms for non-hard-coded problems (non Bayesian)
MEANS = uniformMeans(nbArms=NB_ARMS, delta=0.05, lower=LOWER, amplitude=AMPLITUDE, isSorted=True)

import numpy as np
# more parametric? Read from cli?
MEANS_STR = getenv('MEANS', '')
if MEANS_STR != '':
    MEANS = [ float(m) for m in MEANS_STR.replace('[', '').replace(']', '').split(',') ]
    print("Using cli env variable to use MEANS = {}.".format(MEANS))  # DEBUG

#: This dictionary configures the experiments
configuration = {
    # --- Duration of the experiment
    "horizon": HORIZON,
    # --- Number of repetition of the experiment (to have an average)
    "repetitions": REPETITIONS,
    # --- Parameters for the use of joblib.Parallel
    "n_jobs": N_JOBS,    # = nb of CPU cores
    "verbosity": 6,      # Max joblib verbosity
    # --- Other parameters for the Evaluator
    "finalRanksOnAverage": True,  # Use an average instead of the last value for the final ranking of the tested players
    "averageOn": 1e-3,  # Average the final rank on the 1.% last time steps
    # --- Should we plot the lower-bounds or not?
    "plot_lowerbounds": True,  # XXX Default
    # --- Arms
    "environment": [
        {   # Use vector from command line
            "arm_type": ARM_TYPE,
            "params": MEANS
        },
    ],
}

if ENVIRONMENT_BAYESIAN:
    configuration["environment"] = [  # XXX Bernoulli arms
        {   # A Bayesian problem: every repetition use a different mean vectors!
            "arm_type": ARM_TYPE,
            "params": {
                "function": randomMeans,
                "args": {
                    "nbArms": NB_ARMS,
                    "mingap": 1. / (3 * NB_ARMS),
                    "lower": LOWER,
                    "amplitude": AMPLITUDE,
                    "isSorted": True,
                }
            }
        },
    ]

try:
    #: Number of arms *in the first environment*
    nbArms = int(configuration['environment'][0]['params']['args']['nbArms'])
except (TypeError, KeyError):
    nbArms = len(configuration['environment'][0]['params'])

if len(configuration['environment']) > 1:
    print("WARNING do not use this hack if you try to use more than one environment.")

configuration.update({
    "policies": [
        # --- Stupid algorithms
        {
            "archtype": Uniform,   # The stupidest policy, fully uniform
            "params": {}
        },
        # # --- Full or partial knowledge algorithms
        { "archtype": TakeFixedArm, "params": { "armIndex": 0 }},  # Take worse arm!
        { "archtype": TakeFixedArm, "params": { "armIndex": 1 }},  # Take second worse arm!
        { "archtype": TakeFixedArm, "params": { "armIndex": min(2, nbArms - 1) }},  # Take third worse arm!
        {
            "archtype": UCB,   # UCB with alpha=1 parameter
            "params": {}
        },
        # --- Thompson algorithms
        {
            "archtype": Thompson,
            "params": {}
        },
        # --- KL UCB algorithms
        {
            "archtype": klUCB,
            "params": {}
        },
    ]}
)

# DONE
print("Loaded experiments configuration from 'example_of_configuration_singleplayer.py' :")
print("configuration =", configuration)  # DEBUG