Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 80e429fb authored by hhakim's avatar hhakim
Browse files

Add functions in the Py wrapper: size(), get_factor(), save() and adapt the...

Add functions in the Py wrapper: size(), get_factor(), save() and adapt the constructor to read a Faust from file.

The FaustPy.Faust constructor is able to receive a matlab file as a Faust definition (saved before with FaustPy.Faust.save() or the Faust matlab wrapper).

Adding also needed functions in Py wrapper C++ backend (FaustCoreCpp): get_fact_nb_rows(), get_fact_nb_cols(), get_fact().
Adding a piece of code in quickstart.py for testing.
parent 7dc4cc62
No related branches found
No related tags found
No related merge requests found
......@@ -43,12 +43,13 @@
import copy
import numpy as np
from scipy.io import savemat, loadmat
import FaustCorePy
class Faust:
""" This class represents a dense matrix by a product of 'sparse' factors (i.e Faust)
The aim of the Faust representatio is to speed-up multiplication by this matrix
The aim of the Faust representation is to speed-up multiplication by this matrix
"""
def __init__(F,list_factors):
......@@ -56,8 +57,12 @@ class Faust:
Parameter
---------
list_factors : list/tuple of numpy matrices
list_factors : list/tuple of numpy matrices or filepath of the Faust in
matlab format.
"""
if(isinstance(list_factors, str)):
contents = loadmat(list_factors)
list_factors = contents['faust_factors'][0]
F.m_faust = FaustCorePy.FaustCore(list_factors);
F.m_transpose_flag=0;
F.shape=F.m_faust.shape(F.m_transpose_flag)
......@@ -71,6 +76,10 @@ class Faust:
""" return the number of column of the current Faust. """
return F.shape[1]
def size(F):
""" Returns the Faust size tuple: getNbRow(), getNbCol().
"""
return F.shape[0], F.shape[1]
def transpose(F):
""" transpose the current Faust. """
......@@ -171,3 +180,23 @@ class Faust:
Returns the Faust's number of factors.
"""
return F.m_faust.get_nb_factors()
def get_factor(F, i):
"""
Returns the Faust's i-th factor as a numpy.ndarray.
"""
if(F.m_transpose_flag):
i = F.get_nb_factors()-1-i
return F.m_faust.get_fact(i)
def save(F, filename, format="Matlab"):
"""
Saves the Faust into file.
"""
if(format != "Matlab"):
raise Exception("Only Matlab format is supported.")
mdict = {'faust_factors':
np.ndarray(shape=(1, F.get_nb_factors()), dtype=object)}
for i in range(0, F.get_nb_factors()):
mdict['faust_factors'][0, i] = F.get_factor(i)
savemat(filename, mdict)
......@@ -59,6 +59,8 @@ list_factor_sparse[1]=int_max*sp.random(dim1,dim2,density=density_per_fact,forma
list_factor[1]=list_factor_sparse[1].todense();
#print(list_factor[0])
#print(list_factor[1])
# create a Faust named F from its factors
......@@ -110,3 +112,12 @@ print("Faust density: "+str(A.density()))
print("Faust RCG: "+str(A.RCG()))
print("Faust norm: "+str(A.norm()))
print("Faust nb of factors: "+str(A.get_nb_factors()))
for i in range(0,A.get_nb_factors()):
print("Faust size of factor ",i,"=",A.get_factor(i).shape)
#print(A.get_factor(i))
A.save("A.mat")
As = FaustPy.Faust("A.mat")
assert((A.get_factor(0) == As.get_factor(0)).all())
assert((A.get_factor(1) == As.get_factor(1)).all())
......@@ -66,6 +66,9 @@ class FaustCoreCpp
unsigned long long nnz()const;
double norm() const;
double get_nb_factors() const;
unsigned int get_fact_nb_rows(unsigned int& i) const;
unsigned int get_fact_nb_cols(unsigned int& i) const;
void get_fact(unsigned int& i, FPP* fact_ptr) const;
private :
Faust::Transform<FPP,Cpu> transform;
......
......@@ -44,6 +44,7 @@
#include "faust_Transform.h"
#include <iostream>
#include <exception>
template<typename FPP>
void FaustCoreCpp<FPP>::push_back(FPP* valueMat, unsigned int nbrow,unsigned int nbcol)
......@@ -146,3 +147,56 @@ double FaustCoreCpp<FPP>::get_nb_factors() const
return nb_fact;
}
template<typename FPP>
unsigned int FaustCoreCpp<FPP>::get_fact_nb_rows(unsigned int& i) const
{
Faust::MatGeneric<FPP,Cpu>* const factor_generic = this->transform.get_fact(i);
unsigned int nb_rows = factor_generic->getNbRow();
delete factor_generic;
return nb_rows;
}
template<typename FPP>
unsigned int FaustCoreCpp<FPP>::get_fact_nb_cols(unsigned int& i) const
{
Faust::MatGeneric<FPP,Cpu>* const factor_generic = this->transform.get_fact(i);
unsigned int nb_cols = factor_generic->getNbCol();
delete factor_generic;
return nb_cols;
}
template<typename FPP>
void FaustCoreCpp<FPP>::get_fact(unsigned int& i, FPP* fact_ptr) const
{
Faust::MatGeneric<FPP,Cpu>* const factor_generic = this->transform.get_fact(i);
Faust::MatDense<FPP,Cpu> dense_factor;
switch (factor_generic->getType())
{
case Dense :
{
Faust::MatDense<FPP,Cpu>* factor_dense_ptr = dynamic_cast<Faust::MatDense<FPP,Cpu>* > (factor_generic);
dense_factor = (*factor_dense_ptr);
}
break;
case Sparse :
{
Faust::MatSparse<FPP,Cpu>* factor_sparse_ptr = dynamic_cast<Faust::MatSparse<FPP,Cpu>* > (factor_generic);
dense_factor = (*factor_sparse_ptr);
}
break;
default:
throw std::runtime_error("get_fact : unknown type of the factor matrix.");
}
memcpy(fact_ptr, dense_factor.getData(),
sizeof(FPP)*factor_generic->getNbCol()*factor_generic->getNbRow());
delete factor_generic;
}
......@@ -51,3 +51,6 @@ cdef extern from "FaustCoreCpp.h" :
unsigned long long nnz() const;
double norm() const;
double get_nb_factors() const;
unsigned int get_fact_nb_rows(unsigned int& i) const;
unsigned int get_fact_nb_cols(unsigned int& i) const;
double get_fact(unsigned int& i, FPP* fact_ptr) const;
......@@ -178,7 +178,15 @@ cdef class FaustCore:
norm = self.m_faust.norm()
return norm
def get_nb_factors(F):
def get_nb_factors(self):
cdef int nb_factors
nb_factors = int(F.m_faust.get_nb_factors())
nb_factors = int(self.m_faust.get_nb_factors())
return nb_factors
def get_fact(self,i):
cdef fact = np.zeros([self.m_faust.get_fact_nb_rows(i),
self.m_faust.get_fact_nb_cols(i)], dtype='d',
order='F')
cdef double[:,:] fact_view = fact
self.m_faust.get_fact(i, &fact_view[0, 0])
return fact
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment