55 */
66
77#include " SOM.h"
8+ #include " UtilitiesLib/Filler.h"
89
910namespace pink {
1011
@@ -20,7 +21,59 @@ SOM<CartesianLayout<2>, CartesianLayout<2>, float>::SOM(InputData const& input_d
2021 : som_layout{{input_data.som_width , input_data.som_height }},
2122 neuron_layout{{input_data.neuron_dim , input_data.neuron_dim }},
2223 data (som_layout.size() * neuron_layout.size())
23- {}
24+ {
25+ // Initialize SOM
26+ if (input_data.init == SOMInitialization::ZERO)
27+ fill_value (&data[0 ], data.size ());
28+ else if (input_data.init == SOMInitialization::RANDOM)
29+ fill_random_uniform (&data[0 ], data.size (), input_data.seed );
30+ else if (input_data.init == SOMInitialization::RANDOM_WITH_PREFERRED_DIRECTION) {
31+ fill_random_uniform (&data[0 ], data.size (), input_data.seed );
32+ for (int n = 0 ; n < input_data.som_size ; ++n)
33+ for (uint32_t i = 0 ; i < input_data.neuron_dim ; ++i)
34+ data[n * input_data.neuron_size + i * input_data.neuron_dim + i] = 1.0 ;
35+ }
36+ else if (input_data.init == SOMInitialization::FILEINIT) {
37+ std::ifstream is (input_data.som_filename );
38+ if (!is) throw pink::exception (" Error opening " + input_data.som_filename );
39+
40+ // Skip all header lines starting with #
41+ std::string line;
42+ int last_position = is.tellg ();
43+ while (std::getline (is, line)) {
44+ if (line[0 ] != ' #' ) break ;
45+ header += line + ' \n ' ;
46+ last_position = is.tellg ();
47+ }
48+
49+ is.seekg (last_position, is.beg );
50+
51+ // <file format version> 1 <data-type> <som layout> <neuron layout> <data>
52+ int tmp;
53+ is.read ((char *)&tmp, sizeof (int ));
54+ if (tmp != 2 ) throw pink::exception (" read SOM: wrong binary file version" );
55+ is.read ((char *)&tmp, sizeof (int ));
56+ if (tmp != 1 ) throw pink::exception (" read SOM: wrong file type" );
57+ is.read ((char *)&tmp, sizeof (int ));
58+ if (tmp != 0 ) throw pink::exception (" read SOM: wrong data type" );
59+ is.read ((char *)&tmp, sizeof (int ));
60+ if (tmp != 0 ) throw pink::exception (" read SOM: wrong SOM layout" );
61+ is.read ((char *)&tmp, sizeof (int ));
62+ if (tmp != som_layout.dimensionality ) throw pink::exception (" read SOM: wrong SOM dimensionality" );
63+ is.read ((char *)&tmp, sizeof (int ));
64+ if (tmp != static_cast <int >(som_layout.dimension [0 ])) throw pink::exception (" read SOM: wrong SOM dimension[0]" );
65+ is.read ((char *)&tmp, sizeof (int ));
66+ if (tmp != static_cast <int >(som_layout.dimension [0 ])) throw pink::exception (" read SOM: wrong SOM dimension[1]" );
67+ is.read ((char *)&tmp, sizeof (int ));
68+ if (tmp != neuron_layout.dimensionality ) throw pink::exception (" read SOM: wrong neuron dimensionality" );
69+ is.read ((char *)&tmp, sizeof (int ));
70+ if (tmp != static_cast <int >(neuron_layout.dimension [0 ])) throw pink::exception (" read SOM: wrong neuron dimension[0]" );
71+ is.read ((char *)&tmp, sizeof (int ));
72+ if (tmp != static_cast <int >(neuron_layout.dimension [0 ])) throw pink::exception (" read SOM: wrong neuron dimension[1]" );
73+ is.read ((char *)&data[0 ], data.size () * sizeof (float ));
74+ } else
75+ throw pink::exception (" Unknown SOMInitialization" );
76+ }
2477
2578template <>
2679SOM<CartesianLayout<3 >, CartesianLayout<2 >, float >::SOM(InputData const & input_data)
0 commit comments