Pyhton API Reference¶
libscientific.matrix module¶
matrix.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.matrix.MATRIX¶
Bases:
Structure
matrix data structure
- col¶
Structure/Union member
- data¶
Structure/Union member
- row¶
Structure/Union member
- class libscientific.matrix.Matrix(mx_=None)¶
Bases:
object
A class for working with libscientific matrices.
This class provides methods to perform various operations on matrices, including creating, accessing, modifying, and transforming matrices using the libscientific library.
- mtx¶
The underlying libscientific matrix data.
- Type
CDataType
- __init__(self, mx_=None)¶
- __del__(self)¶
- __getitem__(self, keys)¶
- __setitem__(self, keys, value)¶
- nrow(self)¶
- ncol(self)¶
- data_ptr(self)¶
- tolist(self)¶
- fromlist(self, mtx_)¶
- fromnumpy(self, npmx)¶
- appendrow(self, row_lst_)¶
- appendcol(self, col_lst_)¶
- transpose(self)¶
- get_evect_evals(self)¶
- svd(self)¶
- appendcol(col_lst_)¶
Append a column vector to the matrix.
- Parameters
col_lst (list) – A list representing the column vector to be appended.
- appendrow(row_lst_)¶
Append a row to the matrix.
- Parameters
row_lst (list) – A list representing the row to be appended.
- data_ptr()¶
Get the data pointer of the matrix.
- Returns
The data pointer of the matrix.
- Return type
CDataType
- debug()¶
Debug the matrix content
- fromlist(mtx_)¶
Set the matrix data from a list of lists.
- Parameters
mtx (list[list]) – A list of lists representing the new matrix data.
- fromnumpy(npmx)¶
Set the matrix data from a NumPy array.
- Parameters
npmx (numpy.ndarray) – A NumPy array representing the new matrix data.
- get_evect_evals()¶
Get the eigenvectors and eigenvalues of the matrix.
- Returns
A tuple containing two lists - eigenvectors and eigenvalues.
- Return type
tuple
- ncol()¶
Get the number of columns in the matrix.
- Returns
The number of columns in the matrix.
- Return type
int
- nrow()¶
Get the number of rows in the matrix.
- Returns
The number of rows in the matrix.
- Return type
int
- svd()¶
Perform Singular Value Decomposition (SVD) on the matrix.
- Returns
A tuple containing three lists - U, S, and VT matrices.
- Return type
tuple
- tolist()¶
Convert the matrix to a list of lists.
- Returns
A list of lists representing the matrix.
- Return type
list[list]
- transpose()¶
Transpose the matrix in-place.
- libscientific.matrix.del_matrix(mtx)¶
del_matrix: Delete an allocated libscientific matrix
- libscientific.matrix.dvector_matrix_dot_product(mtx, dvect, res)¶
Calculate a vector-matrix dot product
- libscientific.matrix.dvector_transposed_dvector_dot_product(mtx, dvect, res)¶
Calculate a dvector transposed dvector dot product
- libscientific.matrix.dvector_transposed_matrix_division(dvect, mtx, res)¶
Calculate a dvector transposed-matrix division
- libscientific.matrix.evect_eval(mtx, mtx_e_vect, mtx_e_val)¶
Eigenvectors and Eigenvalues using lapack
- libscientific.matrix.get_matrix_column(mtx, jcol)¶
getMatrixColumn: Python version of getting a column from a libscientific matrix
- libscientific.matrix.get_matrix_row(mtx, irow)¶
get_matrix_row: Python version of getting a row from a libscientific matrix
- libscientific.matrix.get_matrix_value(mtx, irow, jcol)¶
get_matrix_value: Get a value in the irow and jcol of a libscientific matrix
- libscientific.matrix.init_matrix()¶
init_matrix: Allocate in memory an empty libscientific matrix
- libscientific.matrix.matrix_append_col(mtx, col_vect)¶
Append to a libscientific matrix a column list
- libscientific.matrix.matrix_append_row(mtx, row_vect)¶
Append to a libscientific matrix a row list
- libscientific.matrix.matrix_check(mtx)¶
matrix_check: Find infinite and nan numbers and sobstitute with MISSING value.
- libscientific.matrix.matrix_copy(msrc, mdst)¶
matrix_copy: Copy a libscientifi matrix to another allocated one
- libscientific.matrix.matrix_dot_product(mtx_t, mtx, res)¶
Calculate the matrix-matrix product
- libscientific.matrix.matrix_dvector_dot_product(mtx, dvect, res)¶
Calculate a matrix-vector dot product
- libscientific.matrix.matrix_from_numpy(npm)¶
Convert a numpy matrix into a libscientific matrix
- libscientific.matrix.matrix_inversion(mtx, mtx_inv)¶
Invert a matrix
- libscientific.matrix.matrix_set(mtx, val)¶
matrix_set: Set all values of a libscientific matrix to “val”
- libscientific.matrix.matrix_to_list(mtx)¶
matrix_to_list: Convert a libscientific matrix to list of list
- libscientific.matrix.matrix_transpose(mtx, mtx_t)¶
Transpose a matrix
- libscientific.matrix.mt_dvector_matrix_dot_product(mtx, dvect, res)¶
Calculate a vector-matrix dot product in multi-threads
- libscientific.matrix.mt_matrix_dvector_dot_product(mtx, dvect, res)¶
Calculate a matrix-vector dot product in multi-threads
- libscientific.matrix.new_matrix(mtx_input_)¶
new_matrix: Allocate in memory a libscientific matrix from a list of lists
- libscientific.matrix.new_matrix_from_csv(fcsv)¶
Import a matrix directly from CSV file
- libscientific.matrix.new_matrix_from_list(mx_lst)¶
Import a matrix from a list
- libscientific.matrix.print_matrix(mtx)¶
print_matrix: Print to video a libscientific matrix
- libscientific.matrix.resize_matrix(mtx, nrows, ncols)¶
resize_matrix: Resize an already allocated or reallocate a libscientific matrix
- libscientific.matrix.row_col_outer_product(dvect_a, dvect_b, res)¶
Calculate the row-column outer product of two dvectors
- libscientific.matrix.set_matrix_value(mtx, irow, jcol, value)¶
set_matrix_value: Set/modify a value in the irow and jcol of a libscientific matrix
- libscientific.matrix.set_missing_matrix_value(mtx, row_id, col_id)¶
Set a value of a matrix into a missing value
- libscientific.matrix.svd(mtx, u_mtx, s_mtx, vt_mtx)¶
Calculate the SVD of a matrix mtx using the eigenvector/values lapack method
- libscientific.matrix.svd_lapack(mtx, u_mtx, s_mtx, vt_mtx)¶
Calculate the SVD of a matrix mtx using the SVD lapack method
- libscientific.matrix.val_in_matrix(mtx, val)¶
val_in_matrix: Check if a libscientific matrix contains an exact value “val” and return 1 or 0 respectivelly for yes or no.
libscientific.tensor module¶
tensor.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.tensor.TENSOR¶
Bases:
Structure
tensor data structure
- m¶
Structure/Union member
- order¶
Structure/Union member
- class libscientific.tensor.Tensor(tns_)¶
Bases:
object
Tensor Class
This class provides methods for creating and manipulating tensors using libscientific.
- tns¶
The tensor data.
- Type
CDataType
- __init__(self, tns_)¶
- __del__(self)¶
- __getitem__(self, keys)¶
- __setitem__(self, keys, value)¶
- order(self)¶
- nrow(self, k)¶
- ncol(self, k)¶
- data_ptr(self)¶
- tolist(self)¶
- fromlist(self, tns_)¶
- fromnumpy(self, npt)¶
- debug(self)¶
- data_ptr()¶
Get the tensor data pointer.
- Returns
The tensor data pointer.
- Return type
CDataType
- debug()¶
Print the tensor for debugging purposes.
- fromlist(tns_)¶
Initialize the tensor from a nested list.
- Parameters
tns – list of list of list The tensor data as a nested list.
- fromnumpy(npt)¶
Initialize the tensor from a NumPy array.
- Parameters
npt – numpy.ndarray The NumPy array to initialize the tensor from.
- ncol(k)¶
Get the number of columns in the specified matrix (k) of the tensor.
- Parameters
k – int Index of the matrix in the tensor.
- Returns
Number of columns in the specified matrix.
- Return type
int
- nrow(k)¶
Get the number of rows in the specified matrix (k) of the tensor.
- Parameters
k – int Index of the matrix in the tensor.
- Returns
Number of rows in the specified matrix.
- Return type
int
- order()¶
Get the order of the tensor, i.e., the number of matrices constituting the tensor.
- Returns
The order of the tensor.
- Return type
int
- tolist()¶
Convert the tensor to a nested list.
- Returns
The tensor as a nested list.
- Return type
list
- libscientific.tensor.add_tensor_matrix(tns, num_row, num_col)¶
Add a new matrix to the tensor tns
- libscientific.tensor.del_tensor(tns)¶
Delete the moemory allocated tensor
- libscientific.tensor.init_tensor()¶
initTensor: Allocate in memory an empty libscientific tensor
- libscientific.tensor.new_tensor(tns_input_)¶
NewTensor: Allocate in memory a libscientific tensor from a list of lists of lists
- libscientific.tensor.new_tensor_matrix(tns, k_indx, num_row, num_col)¶
Create a new matrix on tensor tns at index k_indx
- libscientific.tensor.print_tensor(tns)¶
Debug the tensor content
- libscientific.tensor.set_tensor_value(tns, k_indx, i_indx, j_indx, val)¶
Set the tensor value at indexes k_indx, i_indx, j_indx
- libscientific.tensor.tensor_append_column(tns, k_indx, dvect)¶
Append a colum to the matrix at index k_indx inside the tensor tns
- libscientific.tensor.tensor_append_row(tns, k_indx, dvect)¶
Append a row to the matrix at index k_indx inside the tensor tns
- libscientific.tensor.tensor_copy(src_tns, dst_tns)¶
Copy a source tensor to a destination tensor
- libscientific.tensor.tensor_set(tns, val)¶
Set all the tensor values to a given value val
- libscientific.tensor.tensor_tolist(tns)¶
Convert a tensor to list
libscientific.vectlist module¶
vectlist.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.vectlist.DVECTLIST¶
Bases:
Structure
DVECTLIST data structure
- dvector¶
Structure/Union member
- size¶
Structure/Union member
- class libscientific.vectlist.DVectorList(dvl_)¶
Bases:
object
DVectorList Class
This class provides methods for creating and manipulating a list of libscientific double vectors.
- dvl¶
The double vector list.
- Type
DVECTORLIST
- __init__(self, dvl_)¶
- __del__(self)¶
- __getitem__(self, key)¶
- __setitem__(self, key, v_lst)¶
- size(self)¶
- data_ptr(self)¶
- append(self, v_lst)¶
- tolist(self)¶
- fromlist(self, v_lists)¶
- debug(self)¶
- append(v_lst)¶
Append a value to the double vector list.
- Parameters
v_lst – DVECTOR or list DVECTOR or list of values to append to the double vector list.
- data_ptr()¶
Get the pointer to the data.
- Returns
The pointer to the data.
- Return type
DVECTORLIST
- debug()¶
Print the double vector list for debugging purposes.
- fromlist(v_lists)¶
Convert a list of lists to a double vector list.
- Parameters
v_lists – list of lists List of lists to convert to a double vector list.
- size()¶
Get the size of the double vector list.
- Returns
Size of the double vector list.
- Return type
int
- tolist()¶
Convert the double vector list to a list of lists.
- Returns
The double vector list as a list of lists.
- Return type
list
- libscientific.vectlist.del_dvector_list(dvl)¶
del_dvector_list: Delete an allocated libscientific double vector
- libscientific.vectlist.dvector_list_append(dvl, dvect)¶
dvector_list_append: Append a double vector to a DvectorList
- libscientific.vectlist.dvector_list_tolist(dvl)¶
convert a divector list into a list of lists
- libscientific.vectlist.init_dvector_list()¶
init_dvector_list: Allocate in memory an empty libscientific double vector list
- libscientific.vectlist.new_dvector_list(dvl_)¶
new_dvector_list: Allocate in memory a libscientific double vector list from a list of lists
libscientific.vector module¶
vector.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.vector.DVECTOR¶
Bases:
Structure
double vector data structure
- data¶
Structure/Union member
- size¶
Structure/Union member
- class libscientific.vector.DVector(dvect_=None)¶
Bases:
object
DVector Class
This class provides methods for creating and manipulating a libscientific double vector.
- __init__(self, dvect_)¶
- __del__(self)¶
- __getitem__(self, key)¶
- __setitem__(self, key, value)¶
- size(self)¶
- data_ptr(self)¶
- append(self, value)¶
- extend(self, v_lst)¶
- tolist(self)¶
- fromlist(self, vlst_)¶
- debug(self)¶
- append(value)¶
Append a value to the double vector.
- Parameters
value – float The value to append to the double vector.
- Returns
Status code (0 on success).
- Return type
int
- debug()¶
Print the double vector for debugging purposes.
- extend(v_lst)¶
Extend the double vector by adding a list of values.
- Parameters
v_lst – list List of values to extend the double vector with.
- fromlist(vlst_)¶
Initialize the double vector from a list of values.
- Parameters
vlst – list List of values to initialize the double vector with.
- size()¶
Get the size of the double vector.
- Returns
Size of the double vector.
- Return type
int
- tolist()¶
Convert the double vector to a Python list.
- Returns
The double vector as a Python list.
- Return type
list
- class libscientific.vector.IVECTOR¶
Bases:
Structure
unsigned int vector data structure
- data¶
Structure/Union member
- size¶
Structure/Union member
- class libscientific.vector.STRVECTOR¶
Bases:
Structure
string vector data structure
- data¶
Structure/Union member
- size¶
Structure/Union member
- class libscientific.vector.UIVECTOR¶
Bases:
Structure
unsigned int vector data structure
- data¶
Structure/Union member
- size¶
Structure/Union member
- class libscientific.vector.UIVector(uivect_)¶
Bases:
object
UIVector Class
This class provides methods for creating and manipulating a libscientific unsigned int vector.
- __init__(self, uivect_)¶
- __del__(self)¶
- __getitem__(self, key)¶
- __setitem__(self, key, value)¶
- size(self)¶
- data_ptr(self)¶
- append(self, value)¶
- extend(self, v_lst)¶
- tolist(self)¶
- fromlist(self, vlst_)¶
- debug(self)¶
- append(value)¶
Append a value to the unsigned int vector.
- Parameters
value – int The value to append to the unsigned int vector.
- Returns
Status code (0 on success).
- Return type
int
- debug()¶
Print the unsigned int vector for debugging purposes.
- extend(v_lst)¶
Extend the unsigned int vector by adding a list of values.
- Parameters
v_lst – list List of values to extend the unsigned int vector with.
- fromlist(vlst_)¶
Initialize the unsigned int vector from a list of values.
- Parameters
vlst – list List of values to initialize the unsigned int vector with.
- size()¶
Get the size of the unsigned int vector.
- Returns
Size of the unsigned int vector.
- Return type
int
- tolist()¶
Convert the unsigned int vector to a Python list.
- Returns
The unsigned int vector as a Python list.
- Return type
list
- libscientific.vector.del_dvector(dvect)¶
DelDVector: Delete an allocated libscientific double vector
- libscientific.vector.del_ivector(ivect)¶
DelIVector: Delete an allocated libscientific int vector
- libscientific.vector.del_uivector(uivect)¶
DelUIVector: Delete an allocated libscientific unsigned int vector
- libscientific.vector.dvector_append(dvect, val)¶
Append a value to a double vector d
- libscientific.vector.dvector_copy(dvect_src)¶
Create a copy of dvector d to a
- libscientific.vector.dvector_has_value(dvect, val)¶
- DVectorHasValue: Check if a libscientific double vector contains an exact
value “val” and return 0 or 1 respectivelly for have or not have.
- libscientific.vector.dvector_remove_at(dvect, indx)¶
Remove a value from a double vector d at index indx
- libscientific.vector.dvector_resize(dvect, size)¶
- DVectorResize: Resize an already allocated or reallocate a libscientific
double vector
- libscientific.vector.dvector_set(dvect, val)¶
DVectorSet: Set all values of a libscientific double vector to “val”
- libscientific.vector.dvector_tolist(dvect)¶
Double vector to python list
- libscientific.vector.get_dvector_value(dvect, indx)¶
- getDVectorValue: Get a value in the row of a libscientific
double vector
- libscientific.vector.get_ivector_value(ivect, indx)¶
- getIVectorValue: Get a value in the row of a libscientific
double vector
- libscientific.vector.get_uivector_value(uivect, indx)¶
- getUIVectorValue: Get a value in the row of a libscientific
double vector
- libscientific.vector.init_dvector()¶
initDVector: Allocate in memory an empty libscientific double vector
- libscientific.vector.init_ivector()¶
initIVector: Allocate in memory an empty libscientific double vector
- libscientific.vector.init_uivector()¶
initUIVector: Allocate in memory an empty libscientific double vector
- libscientific.vector.ivector_append(ivect, val)¶
Append a value to an int vector
- libscientific.vector.ivector_remove_at(ivect, indx)¶
Remove a value from an int vector at index indx
- libscientific.vector.ivector_tolist(ivect)¶
Int vector to python list
- libscientific.vector.new_dvector(v_lst)¶
NewDVector: Allocate in memory a libscientific double vector from a list
- libscientific.vector.new_ivector(v_lst)¶
NewIVector: Allocate in memory a libscientific int vector from a list
- libscientific.vector.new_uivector(v_lst)¶
NewUIVector: Allocate in memory a libscientific unsigned int vector from a list
- libscientific.vector.print_dvector(dvect)¶
PrintDVector: Print to video a libscientific double vector
- libscientific.vector.print_uivector(uivect)¶
PrintUIVector: Print to video a libscientific unsigned int vector
- libscientific.vector.set_dvector_value(dvect, indx, val)¶
- setDVectorValue: Set/modify a value in the row of a libscientific
double vector
- libscientific.vector.set_ivector_value(ivect, indx, val)¶
- setIVectorValue: Set/modify a value in the row of a libscientific
int vector
- libscientific.vector.set_uivector_value(uivect, indx, val)¶
- setUIVectorValue: Set/modify a value in the row of a libscientific
unsigned int vector
- libscientific.vector.uivector_append(uivect, val)¶
Append a value to an unsigned int vector
- libscientific.vector.uivector_remove_at(uivect, indx)¶
Remove a value from an unsigned int vector at index indx
- libscientific.vector.uivector_resize(uivect, size)¶
- DVectorResize: Resize an already allocated or reallocate a libscientific
unsigned int vector
- libscientific.vector.uivector_tolist(uivect)¶
Unsigned int vector to python list
- libscientific.vector.xvector_tolist(dvect)¶
Convert a libscientific double/unsigned int/int vector into a python list
libscientific.clustering module¶
clustering.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- libscientific.clustering.k_means_plus_plus(x_input, n_clusters)¶
K-Means++ Clustering Algorithm (kmeans + David Arthur initialization).
This function performs K-Means++ clustering on the given input matrix to create the specified number of clusters.
- Parameters
x_input – List[List] Input matrix.
n_clusters – int Number of clusters.
- Returns
A list containing the cluster assignments for each input point.
- Return type
List[int]
Examples: >>> np.random.seed(12345) >>> x = np.random.rand(100, 2) >>> clusters = k_means_plus_plus(x, 3) >>> clusters [1, 2, 0, 0, 0, 0, 2, 2, 0, 0, 0, 1, …, 0, 1, 1, 2, 2]
- libscientific.clustering.max_dissimilarity_selection(x_input, nobjects)¶
Max Dissimilarity Compound Selection Algorithm.
This function selects compounds/rows from an input matrix in a way that maximizes their dissimilarity based on the specified number of objects to select.
- Parameters
x_input – List[List] Input matrix.
nobjects – int Number of objects to select.
- Returns
A list of selected object/row IDs.
- Return type
List[int]
Examples: >>> np.random.seed(12345) >>> x = np.random.rand(100, 2) >>> mdis_ids = max_dissimilarity_selection(x, 10) >>> mdis_ids [57, 89, 88, 6, 23, 94, 56, 61, 39, 24]
- libscientific.clustering.most_descriptive_compound(x_input, nobjects)¶
Most Descriptive Compound Selection Algorithm.
This function selects the most descriptive compounds/rows from an input matrix based on the specified number of objects to select.
- Parameters
x_input – List[List] Input matrix.
nobjects – int Number of objects to select.
- Returns
A list of selected object/row IDs.
- Return type
List[int]
Examples: >>> np.random.seed(12345) >>> x = np.random.rand(100, 2) >>> mdc_ids = most_descriptive_compound(x, 10) >>> mdc_ids [74, 97, 95, 7, 35, 25, 50, 10, 32, 59]
libscientific.cpca module¶
cpca.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.cpca.CPCA(scaling=1, npc=2)¶
Bases:
object
CPCA Class (Consensus Principal Component Analysis)
This class provides methods for creating and manipulating a Consensus Principal Component Analysis model.
- model¶
CPCA_MODEL The CPCA model.
- scaling¶
int Scaling type to apply. Available scalings: 0: No scaling. Only mean centering 1: Mean centering and STDEV scaling 2: Mean centering and Root-Mean-Square column scaling 3: Mean centering and Pareto scaling 4: Mean centering and min-max range scaling 5: Mean centering and level scaling
- npc¶
int Number of principal components.
- __init__(self, scaling, npc)¶
- __del__(self)¶
- fit(self, t_input)¶
- get_super_scores(self)¶
- get_super_weights(self)¶
- get_block_scores(self)¶
- get_block_loadings(self)¶
- get_block_expvar(self)¶
- get_total_exp_variance(self)¶
- predict(self, t_input)¶
- fit(t_input)¶
Fit the consensus PCA model.
- Parameters
t_input – Tensor The input tensor.
- Returns
The fitted CPCA model instance.
- Return type
- get_block_expvar()¶
Get the CPCA block explained variance.
- Returns
CPCA block explained variance.
- Return type
List[List[List]]
- get_block_loadings()¶
Get the CPCA block loadings.
- Returns
CPCA block loadings.
- Return type
List[List[List]]
- get_block_scores()¶
Get the CPCA block scores.
- Returns
CPCA block scores.
- Return type
List[List[List]]
- get_super_scores()¶
Get the CPCA super scores.
- Returns
CPCA super scores.
- Return type
List[List]
- get_super_weights()¶
Get the CPCA super weights.
- Returns
CPCA super weights.
- Return type
List[List]
- get_total_exp_variance()¶
Get the CPCA total explained variance.
- Returns
CPCA total explained variance.
- Return type
List[List[List]]
- load(dbpath)¶
Load CPCA model from a sqlite3 file
- Parameters
(str) (dbpath) – model will be stored.
- predict(t_input)¶
Predict super scores and block scores using the CPCA model.
- Parameters
t_input – Tensor The input tensor.
- Returns
- List[List]
CPCA predicted super scores.
- p_block_scoresList[List[List]]
CPCA predicted block scores.
- Return type
p_super_scores
- save(dbpath)¶
Save CPCA model to a sqlite3 file
- Parameters
(str) (dbpath) – model will be stored.
- class libscientific.cpca.CPCAMODEL¶
Bases:
Structure
CPCA model data structure
- block_expvar¶
Structure/Union member
- block_loadings¶
Structure/Union member
- block_scores¶
Structure/Union member
- colaverage¶
Structure/Union member
- colscaling¶
Structure/Union member
- scaling_factor¶
Structure/Union member
- super_scores¶
Structure/Union member
- super_weights¶
Structure/Union member
- total_expvar¶
Structure/Union member
- libscientific.cpca.cpca_algorithm(t_input, scaling, npc, mpca)¶
CPCA: Calculate the CPCA model for a matrix m using the NIPALS algorithm
- libscientific.cpca.cpca_score_predictor(t_input, model, npc, p_super_scores, p_block_scores)¶
cpca_score_predictor: Predict scores for a matrix m in the computed CPCA modes
- libscientific.cpca.del_cpca_model(mpca)¶
del_cpca_model: Delete an allocated libscientific CPCA model
- libscientific.cpca.new_cpca_model()¶
new_cpca_model: Allocate in memory an empty libscientific CPCA model
- libscientific.cpca.print_cpca(mpca)¶
PrintCPCA: Print to video the CPCA Model
- libscientific.cpca.read_cpca(dbpath: str, cpca: CPCAMODEL)¶
read_cpca(dbpath, cpca):
Reads a CPCA (Consensus Principal Component Analysis) model from a SQLite database.
- Parameters
dbpath (str) – The path to the SQLite database file from which the CPCA model will be read.
cpca (CPCAMODEL) – The libscientific data structure where the CPCA model will be loaded.
- Returns
None
- libscientific.cpca.write_cpca(dbpath: str, cpca: CPCAMODEL)¶
write_pca(dbpath, cpca):
Writes a CPCA (Consensus Principal Component Analysis) model to a SQLite database.
- Parameters
dbpath (str) – The path to the SQLite database file where the CPCA model will be stored.
cpca (CPCAMODEL) – The libscientific data structure representing the CPCA model.
- Returns
None
libscientific.interpolate module¶
interpolate.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.interpolate.CubicSplineInterpolation¶
Bases:
object
Cubic Spline Interpolation
This class provides a cubic spline interpolation model for fitting and predicting values.
Examples
>>> from libscientific import interpolate >>> xy = [[-1.82, 0.63], ... [-0.73, 0.19], ... [-0.17, 0.01], ... [-0.09, 0.00], ... [0.15, 0.01], ... [0.39, 0.06], ... [0.86, 0.24], ... [1.44, 0.49]] >>> x = [-1.82, -0.73, -0.17, -0.09, 0.15, 0.39, 0.86, 1.44] >>> csi = interpolate.CubicSplineInterpolation() >>> csi.fit(xy) >>> yp = csi.predict(x) >>> yp [0.63, 0.19, 0.010000000000000002, 1.463672932855431e-18, 0.01, 0.06, 0.23999999999999996, 0.49]
- fit(XY):
Fit the cubic spline interpolation model with a 2D matrix x and y.
- predict(X):
Predict targets of given samples.
- s_interp¶
The interpolation matrix.
- Type
CDataType
- libscientific.interpolate.cubic_spline_interpolation(x_y, s_interp)¶
- CubicSplineInterpolation: Given a matrix x_y, interpolate the npoints
using a natural cubic spline approach
- libscientific.interpolate.cubic_spline_predict(x_cc, s_interp, y_pred)¶
Natural cubic spline prediction
libscientific.loadlibrary module¶
loadlibrary.py method to load libscientific library
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- libscientific.loadlibrary.get_posix_library()¶
Find the path to the libscientific POSIX library.
- Returns
The path to the libscientific library file if found, or None if not found.
- Return type
str or None
- libscientific.loadlibrary.load_library_for_nt()¶
Load the libscientific library for NT systems
- libscientific.loadlibrary.load_library_for_posix()¶
Load the libscientific library for posix systems
- libscientific.loadlibrary.load_libscientific_library()¶
Load the libscientific library dynamically.
- Returns
A ctypes dynamic link library object representing the loaded libscientific library.
- Return type
CDLL
- Raises
RuntimeError – If the library cannot be loaded or the platform is not supported.
libscientific.misc module¶
misc.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- libscientific.misc.missing_value()¶
Get the standard missing value in libscientific.
This function returns the standard missing value used in libscientific. This value is particularly useful when performing PCA or PLS with missing values.
- Returns
The standard missing value used in libscientific.
- Return type
float
libscientific.pca module¶
pca.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.pca.PCA(scaling=1, npc=2)¶
Bases:
object
Principal Component Analysis (PCA) Model
This class provides methods for creating and utilizing a PCA model for dimensionality reduction and feature extraction.
- mpca¶
The PCA model data.
- Type
CDataType
- scaling¶
Scaling option for PCA.
- Type
int
- npc¶
Number of principal components.
- Type
int
- __init__(self, scaling, npc)¶
- __del__(self)¶
- fit(self, m_input)¶
- get_scores(self)¶
- get_loadings(self)¶
- get_exp_variance(self)¶
- predict(self, m_input)¶
- reconstruct_original_matrix(self, npc_input=None, scores_input=None)¶
- fit(m_input)¶
Fit the PCA model using an input matrix.
- Parameters
m_input (Matrix or List[List]) – Input matrix for fitting the PCA model.
- get_exp_variance()¶
Get the explained variance of each principal component.
- Returns
The explained variance of each principal component.
- Return type
List[float]
- get_loadings()¶
Get the PCA loadings.
- Returns
The PCA loadings.
- Return type
List[List[float]]
- get_scores()¶
Get the PCA scores.
- Returns
The PCA scores.
- Return type
List[List[float]]
- load(dbpath)¶
Load PCA model from a sqlite3 file
- Parameters
(str) (dbpath) – model will be stored.
- predict(m_input)¶
Project an input matrix into the PCA model.
- Parameters
m_input (Matrix or List[List]) – Input matrix for projecting into the PCA model.
- Returns
Projected scores using the PCA model.
- Return type
List[List[float]]
- reconstruct_original_matrix(npc_input=None, scores_input=None)¶
Reconstruct the original input matrix using a specified number of principal components.
- Parameters
npc_input (int) – Number of principal components to be used. Default is None.
scores_input (CDataType) – Scores used for reconstruction. Default is None.
- Returns
Reconstructed matrix using the specified principal components.
- Return type
List[List[float]]
- save(dbpath)¶
Save PCA model to a sqlite3 file
- Parameters
(str) (dbpath) – model will be stored.
- class libscientific.pca.PCAMODEL¶
Bases:
Structure
PCA model data structure
- colaverage¶
Structure/Union member
- colscaling¶
Structure/Union member
- dmodx¶
Structure/Union member
- loadings¶
Structure/Union member
- scores¶
Structure/Union member
- varexp¶
Structure/Union member
- libscientific.pca.del_pca_model(mpca)¶
del_pca_model: Delete an allocated libscientific PCA model
- libscientific.pca.new_pca_model()¶
new_pca_model: Allocate in memory an empty libscientific PCA model
- libscientific.pca.pca_algorithm(m_input, scaling, npc, mpca)¶
PCA: Calculate the PCA model for a matrix m using the NIPALS algorithm
- libscientific.pca.pca_ind_var_predictor(scores_input, pca_model, npc, ind_vars_out)¶
- pca_ind_var_predictor: Reconstruct the original matrix
from PCA model using scores and loadings
- libscientific.pca.pca_score_predictor(m_input, mpca, npc, pscores)¶
pca_score_predictor: Predict scores for a matrix m in the computed PCA modes
- libscientific.pca.print_pca(mpca)¶
PrintPCA: Print to video the PCA Model
- libscientific.pca.read_pca(dbpath: str, pca: PCAMODEL)¶
read_pca(dbpath, pca):
Reads a PCA (Principal Component Analysis) model from a SQLite database.
- Parameters
dbpath (str) – The path to the SQLite database file from which the PCA model will be read.
pca (PCAMODEL) – The libscientific data structure where the PCA model will be loaded.
- Returns
None
- libscientific.pca.write_pca(dbpath: str, pca: PCAMODEL)¶
write_pca(dbpath, pca):
Writes a PCA (Principal Component Analysis) model to a SQLite database.
- Parameters
dbpath (str) – The path to the SQLite database file where the PCA model will be stored.
pca (PCAMODEL) – The libscientific data structure representing the PCA model.
- Returns
None
libscientific.pls module¶
pls.py libscientific python binding
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- class libscientific.pls.PLS(nlv=2, xscaling=1, yscaling=0)¶
Bases:
object
Partial Least Squares (PLS) Model
This class provides methods for creating and utilizing a PLS model for regression analysis.
- Parameters
(int) (yscaling) –
(int) –
(int) –
Note
Scaling options: - 0: No scaling - 1: Standard deviation scaling - 2: Root mean squared scaling - 3: Pareto scaling - 4: Range scaling - 5: Level scaling
- mpls(CDataType)¶
- Type
The PLS model data.
- nlv(int)¶
- Type
Number of latent variables.
- xscaling(int)¶
- Type
Scaling type for x matrix.
- yscaling(int)¶
- Type
Scaling type for y matrix.
- __init__(self, nlv, xscaling=1, yscaling=0)¶
- __del__(self)¶
- fit(self, x_input, y_input)¶
- get_tscores(self)¶
- get_uscores(self)¶
- get_ploadings(self)¶
- get_qloadings(self)¶
- get_weights(self)¶
- get_beta_coefficients(self, nlv: int = 1)¶
- get_exp_variance(self)¶
- get_x_column_scaling(self)¶
- get_x_averages(self)¶
- get_y_column_scaling(self)¶
- get_y_averages(self)¶
- predict(self, x_input, nlv_=None)¶
- fit(x_input, y_input)¶
Fit a PLS model using x and y matrices.
- get_beta_coefficients(nlv: int = 1)¶
Get the Beta Coefficients for fast predictions.
- Parameters
nlv (int, optional) – Number of latent variables. Default is 1.
- Returns
The Beta Coefficients.
- Return type
List[float]
- get_exp_variance()¶
Get the explained variance.
- Returns
The explained variance.
- Return type
List[float]
- get_ploadings()¶
Get the P-Loadings.
- Returns
The P-Loadings.
- Return type
List[List[float]]
- get_qloadings()¶
Get the Q-Loadings.
- Returns
The Q-Loadings.
- Return type
List[List[float]]
- get_tscores()¶
Get the T-Scores.
- Returns
The T-Scores.
- Return type
List[List[float]]
- get_uscores()¶
Get the U-Scores.
- Returns
The U-Scores.
- Return type
List[List[float]]
- get_weights()¶
Get the W-Weights.
- Returns
The W-Weights.
- Return type
List[List[float]]
- get_x_averages()¶
Get the feature averages for x.
- Returns
The feature averages for x.
- Return type
List[float]
- get_x_column_scaling()¶
Get the model column scaling for x.
- Returns
The model column scaling for x.
- Return type
List[float]
- get_y_averages()¶
Get the feature averages for y.
- Returns
The feature averages for y.
- Return type
List[float]
- get_y_column_scaling()¶
Get the model column scaling for y.
- Returns
The model column scaling for y.
- Return type
List[float]
- load(dbpath)¶
Load PLS model from a sqlite3 file
- Parameters
(str) (dbpath) – model will be stored.
- predict(x_input, nlv_=None)¶
Predict the y values given an x matrix.
- Parameters
x_input (Matrix or List[List]) – Input x matrix for prediction.
nlv (int, optional) – Number of latent variables for prediction. Default is None.
- Returns
The predicted y values and T-Scores.
- Return type
List[List[float]], List[List[float]]
- save(dbpath)¶
Save PLS model to a sqlite3 file
- Parameters
(str) (dbpath) – model will be stored.
- class libscientific.pls.PLSMODEL¶
Bases:
Structure
PLSMODEL data structure
- b¶
Structure/Union member
- bias¶
Structure/Union member
- precision_recall_ap_recalculated¶
Structure/Union member
- precision_recall_ap_validation¶
Structure/Union member
- precision_recall_recalculated¶
Structure/Union member
- precision_recall_validation¶
Structure/Union member
- pred_residuals¶
Structure/Union member
- predicted_y¶
Structure/Union member
- q2y¶
Structure/Union member
- r2y_recalculated¶
Structure/Union member
- r2y_validation¶
Structure/Union member
- recalc_residuals¶
Structure/Union member
- recalculated_y¶
Structure/Union member
- roc_auc_recalculated¶
Structure/Union member
- roc_auc_validation¶
Structure/Union member
- roc_recalculated¶
Structure/Union member
- roc_validation¶
Structure/Union member
- sdec¶
Structure/Union member
- sdep¶
Structure/Union member
- xcolaverage¶
Structure/Union member
- xcolscaling¶
Structure/Union member
- xloadings¶
Structure/Union member
- xscores¶
Structure/Union member
- xvarexp¶
Structure/Union member
- xweights¶
Structure/Union member
- ycolaverage¶
Structure/Union member
- ycolscaling¶
Structure/Union member
- yloadings¶
Structure/Union member
- yscores¶
Structure/Union member
- yscrambling¶
Structure/Union member
- libscientific.pls.apply_scaling(x_input, x_avg, x_scal)¶
Apply scaling to a matrix
- libscientific.pls.del_pls_model(mpls)¶
del_pls_model: Delete an allocated libscientific PLS model
- libscientific.pls.new_pls_model()¶
new_pls_model: Allocate in memory an empty libscientific PLS model
- libscientific.pls.pls_algorithm(x_input, y_input, mpls, **kwargs)¶
PLS: Calculate the PLS model using a matrix x and a matrix y according to the NIPALS algorithm
- libscientific.pls.pls_beta_coefficients(mpls, nlv, bcoeff)¶
PLSBetasCoeff calculation
- libscientific.pls.pls_beta_inference(x_input, **kwargs)¶
Predict using PLS beta coefficients and not the whole PLS model.
- Parameters
x_input (list) – Input matrix to predict.
**kwargs (dict) – Additional keyword arguments: - x_averages (list): Column averages from the training x matrix. - x_scaling (list): Column scaling from the training x matrix. - y_averages (list): Column averages from the training y matrix. - y_scaling (list): Column scaling from the training y matrix. - beta_coeff (list): PLS beta coefficients extracted from the trained PLS model.
- Returns
predicted_y – Predicted values.
- Return type
list
Examples
>>> model = PLS(nlv=2, xscaling=1, yscaling=0) >>> model.fit(x, y) >>> pby = pls_beta_inference(xp, x_averages=model.get_x_averages(), x_scaling=model.get_x_column_scaling(), y_averages=model.get_y_averages(), y_scaling=model.get_y_column_scaling(), beta_coeff=model.get_beta_coefficients(2))
- libscientific.pls.pls_score_predictor(x_input, mpls, nlv, p_scores)¶
PLSScorePredictor: Predict scores for a matrix m in the computed PLS modes
- libscientific.pls.pls_y_predictor(x_scores, mpls, nlv, predicted_y)¶
PLSYPredictor: Predict the Y according the predicted scores and the calculated pls model.
- libscientific.pls.pls_y_predictor_all_lv(x_input, mpls, predicted_scores, predicted_y)¶
PLSYPredictorAllLV: Predict the Y according the original feature matrix and the calculated pls model. This function is NOT dependent on PLSScorePredictor.
- libscientific.pls.print_pls(mpls)¶
PrintPLS: Print to video the PLS Model
- libscientific.pls.read_pls(dbpath: str, pls: PLSMODEL)¶
read_pls(dbpath, pls):
Reads a PLS (Partial Least Squares) model from a SQLite database.
- Parameters
dbpath (str) – The path to the SQLite database file from which the PLS model will be read.
pls (PLSMODEL) – The libscientific data structure where the PLS model will be loaded.
- Returns
None
- libscientific.pls.revert_scaling(x_input, x_avg, x_scal)¶
Revert scaling to a matrix
- libscientific.pls.write_pls(dbpath: str, pls: PLSMODEL)¶
write_pls(dbpath, pls):
Writes a PLS (Partial Least Squares) model to a SQLite database.
- Parameters
dbpath (str) – The path to the SQLite database file where the PLS model will be stored.
pls (PLSMODEL) – The libscientific data structure representing the PLS model.
- Returns
None
Module contents¶
init.py
Library Initialization for LibScientific
Copyright (C) <2023> Giuseppe Marco Randazzo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
Submodules¶
info : Module providing information about the LibScientific library.
vector : Module for handling vector-related operations.
matrix : Module for handling matrix-related operations.
tensor : Module for handling tensor-related operations.
pca : Module for Principal Component Analysis (PCA).
pls : Module for Partial Least Squares (PLS).
vectlist : Module for handling lists of double vectors.
clustering : Module for clustering algorithms.
- libscientific.__version__¶
The version of the LibScientific library.
- Type
str
Examples
The following example demonstrates how to import and use the LibScientific library:
>>> import libscientific
>>> print(libscientific.__version__)
'1.5.1'
>>> x = [[1.0, 2.0], [3.0, 4.0]]
>>> vec = libscientific.vector.new_dvector(x)
>>> print(libscientific.vector.dvector_tolist(vec))
[[1.0, 2.0], [3.0, 4.0]]
Note
This module serves as the main entry point for using the LibScientific library. Users can import submodules and access various functionalities provided by the library.