24 #ifndef SOM_SERIALIZE_H
25 #define SOM_SERIALIZE_H
30 #include <CCfits/CCfits>
31 #include <boost/archive/binary_iarchive.hpp>
32 #include <boost/archive/binary_oarchive.hpp>
38 template <
typename OArchive, std::
size_t ND,
typename DistFunc>
46 template <std::
size_t ND,
typename DistFunc>
48 somExport<boost::archive::binary_oarchive>(out, som);
51 template <
typename IArchive, std::
size_t ND,
typename DistFunc = Distance::L2<ND>>
63 template <std::
size_t ND,
typename DistFunc = Distance::L2<ND>>
65 return somImport<boost::archive::binary_iarchive, ND, DistFunc>(in);
68 template <std::
size_t ND,
typename DistFunc>
76 long ax_sizes[3] = {long(x), long(y), long(ND)};
77 CCfits::FITS fits(filename, DOUBLE_IMG, n_axes, ax_sizes);
80 fits.pHDU().addKey(
"DISTFUNC",
typeid(DistFunc).name(),
"");
87 for (
auto& w_arr : som) {
92 fits.pHDU().write(1, total_size, data);
95 template <std::
size_t ND,
typename DistFunc = Distance::L2<ND>>
98 CCfits::FITS fits(filename, CCfits::Read);
102 fits.pHDU().readKey(
"DISTFUNC", dist_func_type);
103 if (dist_func_type !=
typeid(DistFunc).name()) {
104 throw Elements::Exception() <<
"Incompatible DistFunc parameter. File contains SOM with " << dist_func_type
105 <<
" and is read as " <<
typeid(DistFunc).name();
109 if (fits.pHDU().axes() != 3) {
110 throw Elements::Exception() <<
"Data array in file " << filename <<
" does not have 3 dimensions";
112 if (fits.pHDU().axis(2) != ND) {
113 throw Elements::Exception() <<
"Weights dimension of array in file " << filename <<
" should have size " << ND <<
" but was "
114 << fits.pHDU().axis(2);
121 fits.pHDU().read(data);
127 for (
auto& w_arr : result) {
128 w_arr[w_i] = data[i];
void somExport(std::ostream &out, const SOM< ND, DistFunc > &som)
SOM< ND, DistFunc > somBinaryImport(std::istream &in)
SOM< ND, DistFunc > somFitsImport(const std::string &filename)
void somFitsExport(const std::string &filename, const SOM< ND, DistFunc > &som)
void somBinaryExport(std::ostream &out, const SOM< ND, DistFunc > &som)
SOM< ND, DistFunc > somImport(std::istream &in)