Alexandria  2.14.1
Please provide a description of the project.
serialize.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * @file serialize.h
21  * @author nikoapos
22  */
23 
24 #ifndef SOM_SERIALIZE_H
25 #define SOM_SERIALIZE_H
26 
27 #include <iostream>
28 #include <boost/archive/binary_iarchive.hpp>
29 #include <boost/archive/binary_oarchive.hpp>
30 #include <CCfits/CCfits>
32 #include "SOM/Distance.h"
33 #include "SOM/serialization/SOM.h"
34 
35 namespace Euclid {
36 namespace SOM {
37 
38 template <std::size_t ND, typename DistFunc>
40  // Do NOT delete this pointer!!! It points to the actual som
41  const SOM<ND, DistFunc>* ptr = &som;
42  boost::archive::binary_oarchive boa {out};
43  boa << ptr;
44 }
45 
46 template <std::size_t ND, typename DistFunc=Distance::L2<ND>>
48  boost::archive::binary_iarchive bia {in};
49  // Do NOT delete manually this pointer. It is wrapped with a unique_ptr later.
50  SOM<ND, DistFunc>* ptr;
51  bia >> ptr;
52  std::unique_ptr<SOM<ND, DistFunc>> smart_ptr {ptr};
53  // We move out to the result the som pointed by the pointer. The unique_ptr
54  // will delete the (now empty) pointed object
55  return std::move(*smart_ptr);
56 }
57 
58 template <std::size_t ND, typename DistFunc>
59 void somFitsExport(const std::string& filename, const SOM<ND, DistFunc>& som) {
60 
61  // Create the output file and the array HDU
62  int n_axes = 3;
63  std::size_t x;
64  std::size_t y;
65  std::tie(x, y) = som.getSize();
66  long ax_sizes[3] = {(long)x, (long)y, (long)ND};
67  CCfits::FITS fits (filename, DOUBLE_IMG, n_axes, ax_sizes);
68 
69  // Write in the header the DistFunc type
70  fits.pHDU().addKey("DISTFUNC", typeid(DistFunc).name(), "");
71 
72  // Create a valarray with the SOM data
73  std::size_t total_size = x * y * ND;
74  std::valarray<double> data (total_size);
75  int i = 0;
76  for (std::size_t w_i = 0; w_i < ND; ++w_i) {
77  for (auto& w_arr : som) {
78  data[i] = w_arr[w_i];
79  ++i;
80  }
81  }
82  fits.pHDU().write(1, total_size, data);
83 
84 }
85 
86 template <std::size_t ND, typename DistFunc=Distance::L2<ND>>
88 
89  CCfits::FITS fits (filename, CCfits::Read);
90 
91  // Check that the type of the DistFunc is correct
92  std::string dist_func_type;
93  fits.pHDU().readKey("DISTFUNC", dist_func_type);
94  if (dist_func_type != typeid(DistFunc).name()) {
95  throw Elements::Exception() << "Incompatible DistFunc parameter. File contains SOM with "
96  << dist_func_type << " and is read as " << typeid(DistFunc).name();
97  }
98 
99  // Get the dimensions of the data in the file
100  if (fits.pHDU().axes() != 3) {
101  throw Elements::Exception() << "Data array in file " << filename << " does not have 3 dimensions";
102  }
103  if (fits.pHDU().axis(2) != ND) {
104  throw Elements::Exception() << "Weights dimension of array in file " << filename <<
105  " should have size " << ND << " but was " << fits.pHDU().axis(2);
106  }
107  std::size_t x = fits.pHDU().axis(0);
108  std::size_t y = fits.pHDU().axis(1);
109 
110  // Read the data from the file
112  fits.pHDU().read(data);
113 
114  // Copy the data in a SOM object
115  SOM<ND, DistFunc> result {x, y};
116  int i = 0;
117  for (std::size_t w_i = 0; w_i < ND; ++w_i) {
118  for (auto& w_arr : result) {
119  w_arr[w_i] = data[i];
120  ++i;
121  }
122  }
123 
124  return std::move(result);
125 }
126 
127 }
128 }
129 
130 #endif /* SOM_SERIALIZE_H */
131 
Euclid::SOM::somBinaryExport
void somBinaryExport(std::ostream &out, const SOM< ND, DistFunc > &som)
Definition: serialize.h:39
std::string
STL class.
std::move
T move(T... args)
Distance.h
std::tie
T tie(T... args)
Exception.h
std::ostream
STL class.
SOM.h
Euclid::SOM::SOM
Definition: SOM.h:46
std::valarray
STL class.
Elements::Exception
Euclid::SOM::somFitsImport
SOM< ND, DistFunc > somFitsImport(const std::string &filename)
Definition: serialize.h:87
std::size_t
Euclid::SOM::somFitsExport
void somFitsExport(const std::string &filename, const SOM< ND, DistFunc > &som)
Definition: serialize.h:59
Euclid::SOM::somBinaryImport
SOM< ND, DistFunc > somBinaryImport(std::istream &in)
Definition: serialize.h:47
std::istream
STL class.
std::unique_ptr
STL class.
Euclid
Definition: InstOrRefHolder.h:29