Mentions légales du service

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

Add pyfaust.Faust.load_native (wrapper to load a Faust from a .mat).

parent a27e0f67
Branches
Tags
No related merge requests found
...@@ -1869,6 +1869,27 @@ class Faust(numpy.lib.mixins.NDArrayOperatorsMixin): ...@@ -1869,6 +1869,27 @@ class Faust(numpy.lib.mixins.NDArrayOperatorsMixin):
ncols)) ncols))
return Faust(factors) return Faust(factors)
def load_native(filepath):
"""
The format is Matlab format version 5 and the filepath should end with
a .mat extension (native C++ version).
The Faust must have been saved before with Faust.save.
Args:
filepath: the filepath of the .mat file.
"""
_type = _FaustCorePy.FaustCoreGenDblCPU.get_mat_file_type(filepath)
if _type == -1:
raise Exception("Invalid .mat file")
elif _type == 0:
F = Faust( core_obj=_FaustCorePy.FaustCoreGenFltCPU.read_from_mat_file(filepath))
elif _type == 1:
F = Faust( core_obj=_FaustCorePy.FaustCoreGenDblCPU.read_from_mat_file(filepath))
elif _type == 2:
F = Faust( core_obj=_FaustCorePy.FaustCoreGenCplxDblCPU.read_from_mat_file(filepath))
return F
def astype(F, dtype): def astype(F, dtype):
""" """
......
...@@ -147,6 +147,8 @@ class FaustCoreCpp ...@@ -147,6 +147,8 @@ class FaustCoreCpp
num_rows, unsigned long int* col_ids, num_rows, unsigned long int* col_ids,
unsigned long int num_cols) const; unsigned long int num_cols) const;
bool save_mat_file(const char* filepath) const; bool save_mat_file(const char* filepath) const;
static FaustCoreCpp<FPP, DEV>* read_from_mat_file(const char* filepath);
static int get_mat_file_type(const char* filepath);
FaustCoreCpp<FPP,DEV>* swap_cols(const unsigned int id1, const unsigned int id2, FaustCoreCpp<FPP,DEV>* swap_cols(const unsigned int id1, const unsigned int id2,
const bool permutation, const bool inplace); const bool permutation, const bool inplace);
FaustCoreCpp<FPP,DEV>* swap_rows(const unsigned int id1, const unsigned int id2, FaustCoreCpp<FPP,DEV>* swap_rows(const unsigned int id1, const unsigned int id2,
......
...@@ -374,12 +374,40 @@ bool FaustCoreCpp<FPP,DEV>::save_mat_file(const char* filepath) const ...@@ -374,12 +374,40 @@ bool FaustCoreCpp<FPP,DEV>::save_mat_file(const char* filepath) const
this->transform->save_mat_file(filepath); this->transform->save_mat_file(filepath);
return true; return true;
} }
catch(exception& e) { catch(exception& e)
{
//cerr << e.what() << endl; //cerr << e.what() << endl;
return false; return false;
} }
} }
template<typename FPP, FDevice DEV>
FaustCoreCpp<FPP,DEV>* FaustCoreCpp<FPP,DEV>::read_from_mat_file(const char* filepath)
{
FaustCoreCpp<FPP, DEV>* core = nullptr;
try
{
auto th = new Faust::TransformHelper<FPP, Cpu>();
th->read_from_mat_file(filepath);
core = new FaustCoreCpp<FPP,DEV>(th);
}
catch(exception& e)
{
if(core != nullptr)
{
delete core;
core = nullptr;
}
}
return core;
}
template<typename FPP, FDevice DEV>
int FaustCoreCpp<FPP,DEV>::get_mat_file_type(const char* filepath)
{
return Faust::TransformHelper<FPP,DEV>::get_mat_file_type(filepath);
}
template<typename FPP, FDevice DEV> template<typename FPP, FDevice DEV>
unsigned int FaustCoreCpp<FPP,DEV>::getNbRow() const unsigned int FaustCoreCpp<FPP,DEV>::getNbRow() const
{ {
......
...@@ -62,6 +62,10 @@ cdef extern from "FaustCoreCpp.h": ...@@ -62,6 +62,10 @@ cdef extern from "FaustCoreCpp.h":
num_rows, unsigned long int* col_ids, num_rows, unsigned long int* col_ids,
unsigned long int num_cols) const unsigned long int num_cols) const
bool save_mat_file(const char* filepath) const bool save_mat_file(const char* filepath) const
@staticmethod
@CPP_CORE_CLASS@[FPP]* read_from_mat_file(const char* filepath);
@staticmethod
int get_mat_file_type(const char* filepath);
@CPP_CORE_CLASS@[FPP]* swap_cols(const unsigned int id1, const unsigned int id2, @CPP_CORE_CLASS@[FPP]* swap_cols(const unsigned int id1, const unsigned int id2,
const bool permutation, const bool inplace); const bool permutation, const bool inplace);
@CPP_CORE_CLASS@[FPP]* swap_rows(const unsigned int id1, const unsigned int id2, @CPP_CORE_CLASS@[FPP]* swap_rows(const unsigned int id1, const unsigned int id2,
......
...@@ -572,6 +572,33 @@ cdef class FaustCoreGen@TYPE_NAME@@PROC@: ...@@ -572,6 +572,33 @@ cdef class FaustCoreGen@TYPE_NAME@@PROC@:
raise Exception("Failed to save the file: "+filepath) raise Exception("Failed to save the file: "+filepath)
PyMem_Free(cfilepath) PyMem_Free(cfilepath)
@staticmethod
def read_from_mat_file(filepath):
cdef char * cfilepath = <char*> PyMem_Malloc(sizeof(char) *
(len(filepath)+1))
fparr = bytearray(filepath, "UTF-8");
for i in range(0,len(filepath)):
cfilepath[i] = fparr[i]
cfilepath[i+1] = 0
core = @CORE_CLASS@(core=True)
core.@CORE_OBJ@ = FaustCoreCy.@CPP_CORE_CLASS@[@TYPE@].read_from_mat_file(cfilepath)
PyMem_Free(cfilepath)
return core
@staticmethod
def get_mat_file_type(filepath):
# TODO: refactor this code with the two methods above
cdef char * cfilepath = <char*> PyMem_Malloc(sizeof(char) *
(len(filepath)+1))
fparr = bytearray(filepath, "UTF-8");
for i in range(0,len(filepath)):
cfilepath[i] = fparr[i]
cfilepath[i+1] = 0
_type = FaustCoreCy.@CPP_CORE_CLASS@[@TYPE@].get_mat_file_type(cfilepath)
PyMem_Free(cfilepath)
return _type
def transpose(self): def transpose(self):
core = @CORE_CLASS@(core=True) core = @CORE_CLASS@(core=True)
core.@CORE_OBJ@ = self.@CORE_OBJ@.transpose() core.@CORE_OBJ@ = self.@CORE_OBJ@.transpose()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment