from pyfaust import Faust
import numpy as np
from scipy import sparse
factors = []
is_sparse = False
for i in range(0,5):
if(is_sparse):
factors += [ sparse.random(100,100, dtype=np.float64, format='csr',
density=0.1)]
else:
factors += [ np.random.rand(100, 100).astype(np.float64) ]
is_sparse = not is_sparse
F = Faust(factors)
Note that sparse factors are in CSR format but you can also use CSC format (other formats for sparse matrices are not supported).
It can be interesting to look at the scipy.sparse module documentation, in particular: csr_matrix, csc_matrix.
F
Faust size 100x100, density 3.2, nnz_sum 32000, 5 factor(s): - FACTOR 0 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 1 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 2 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 3 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 4 (double) DENSE, size 100x100, density 1, nnz 10000
As you noticed, the Faust output contains the list of factors and their features but also a header describing the Faust operator itself: its size, its density, the number of nonzeros it contains (nnz_sum
) along with all its factors.
You can also call F.display() or simply print(F)
to get all the information.
F.display()
print(F)
Faust size 100x100, density 3.2, nnz_sum 32000, 5 factor(s): - FACTOR 0 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 1 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 2 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 3 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 4 (double) DENSE, size 100x100, density 1, nnz 10000 Faust size 100x100, density 3.2, nnz_sum 32000, 5 factor(s): - FACTOR 0 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 1 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 2 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 3 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 4 (double) DENSE, size 100x100, density 1, nnz 10000
Note that when you don't need F
anymore, you can delete it with the instruction del F
. Normally, that's the role of the garbage collector but it might happen that your Faust is very eager in memory and a manual deletion with a manual garbage collection is necessary (calling gc.collect()
after one or several del
).
It's really handy to create a Faust and retrieve it from a file later. Let's see in the code below how to proceed.
First save the Faust.
F.save('F.mat')
Now let's get it back from file.
file_F = Faust(filepath='F.mat')
file_F
Faust size 100x100, density 3.2, nnz_sum 32000, 5 factor(s): - FACTOR 0 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 1 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 2 (double) DENSE, size 100x100, density 1, nnz 10000 - FACTOR 3 (double) SPARSE, size 100x100, density 0.1, nnz 1000 - FACTOR 4 (double) DENSE, size 100x100, density 1, nnz 10000
The file storage format used is matlab v7. This format allows the compatibility between pyfaust and matfaust, the Matlab wrapper. You can easily reload the F.mat
file from matfaust.
The pyfaust package provides functions for generating Faust objects by many manners. One noticeable function of this package is rand(). This function allows to generate a random Faust satisfying certain constraints; the number of factors, the size of these factors, the density, the scalar type, etc.
Below are examples of (pseudo-)random generations.
from pyfaust import rand
F = rand(2, 10, num_factors=2, density=.5, dtype='complex')
G = rand(10, 20, num_factors=[2, 5], dim_sizes=[10, 20], density=.5, fac_type='dense')
F is a complex Faust. Its rate of nonzeros, for each factor, is about 0.5. Since the fac_type
argument is not set, each factor is sparse.
F
Faust size 2x10, density 0.6, nnz_sum 12, 2 factor(s): - FACTOR 0 (complex) SPARSE, size 2x2, density 0.5, nnz 2 - FACTOR 1 (complex) SPARSE, size 2x10, density 0.5, nnz 10
G is a real Faust (the default scalar type is double). The rand() call num_factors
value ([2, 5]
) defines the bounds in which we want the number of factors to be and likewise the dim_sizes
argument defines the bounds in which to randomly choose the sizes of the two dimensions of each intermediary factor composing the Faust. The sizes are chosen to keep G consistent though, this is a matrix product!
G
Faust size 10x20, density 1.4, nnz_sum 280, 2 factor(s): - FACTOR 0 (double) DENSE, size 10x19, density 0.473684, nnz 90 - FACTOR 1 (double) DENSE, size 19x20, density 0.5, nnz 190
The rand() function will surely be of a great help if you want to test the Faust member functions. A second notebook might guide you in this road: How to Manipulate a Faust.
There exist many ways to create a Faust, please look at the package documentation for more information. It would be the topic of another notebook! If you want, for example, to learn about Faust generation based on the FAµST's factorization algorithms, check this notebook: Using the FAµST Factorization Wrappers.
Note: this notebook was executed using the following pyfaust version:
import pyfaust
pyfaust.version()
'3.38.9'