]> Creatis software - STMS.git/blob - Src/STMS_generateClosestMap.cxx
V 1.5
[STMS.git] / Src / STMS_generateClosestMap.cxx
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <algorithm>
5 #include <cmath>
6 #include <fstream> 
7
8 #include <omp.h>
9
10 typedef std::vector< float > TimeSerieType;
11 typedef std::pair< unsigned int , float>  DistanceIndexType;
12
13 #pragma omp declare simd 
14 float distsq(float x, float y)
15 {
16         return (x-y) * (x-y);
17 }
18
19 #pragma omp declare simd 
20 bool DistancePairComp (const DistanceIndexType &x, const DistanceIndexType &y) 
21 {
22         return  x.second < y.second; 
23 }
24
25 float EuclidianDistanceTimeSerie( const TimeSerieType &a, const TimeSerieType &b)
26 {
27         float dist = 0;
28         unsigned int n = a.size();
29         #pragma omp parallel for simd reduction(+:dist) schedule(simd:static, 5)
30         for (unsigned int i=0; i<n ; i++)
31                         dist += distsq( a[i], b[i]) ;
32                 
33         return std::sqrt(dist);
34 }
35
36
37 int main(int argc, char *argv[])
38 {
39 std::vector< TimeSerieType > ClassSeries;
40 std::vector< unsigned int > ClassLabels;
41 std::vector< unsigned int > ClassEffectif;
42
43 unsigned int K = 5;
44 unsigned int N = 2;
45 std::cout << "argv[1] : csv filemane " << std::endl;
46 std::cout << "argv[2] : K the number of neigboors" << std::endl;
47 std::cout << "argv[3] : N the minimum effectif of classes "<< std::endl;
48
49 ///////////////////////////////////////////////////////////////
50 /// \brief reading csv file
51 ///////////////////////////////////////////////////////////////
52 std::ifstream ifs;
53 ifs.open (argv[1], std::ifstream::in);
54 std::cout << "argv[1] : csv filemane = " << argv[1] << std::endl;
55
56
57 std::string ID_str, Effectif_str, Value_str;
58
59 std::string::size_type sz = 0; // use to identify the endline
60 while( ifs.good() )
61 {
62     if(sz == 0)    std::getline(ifs, ID_str, ',');
63 //    std::cout << ID_str << " " ;
64     ClassLabels.push_back( std::stof(ID_str) );
65
66     std::getline(ifs, Effectif_str, ',');
67   //  std::cout << Effectif_str << " " ;
68     ClassEffectif.push_back( std::stof(Effectif_str) );
69         
70         TimeSerieType tmpSerie;
71     int a;
72     do
73                 {                       
74         std::getline(ifs, Value_str, ',');
75 //        std::cout << Value_str << " "  ;
76         a= Value_str.find('\n');
77         tmpSerie.push_back( std::stof(Value_str, &sz) );
78          }      while( a == -1 );
79 //    std::cout << std::endl;
80     ClassSeries.push_back( tmpSerie );
81     ID_str = Value_str.substr(sz+1);  //  " 2\n12" -> "12"
82 }
83 ifs.close();
84 ///////////////////////////////////////////////////////////////
85
86
87 std::cout << " starting analysis " << std::endl;
88 ///////////////////////////////////////////////////////////////
89 /// \brief compute k nearest neigboor of each entry (effectif > N)
90 ///////////////////////////////////////////////////////////////
91 for(unsigned int i=0; i<ClassSeries.size(); i++)
92 {
93
94     if( ClassEffectif[i] > N ) // if class's cardinal is > to N
95                 {
96                 std::vector< DistanceIndexType > DistanceToSeries(ClassSeries.size()) ; // we will use the current sample to validate (so sizes are the same)
97                 unsigned int n= ClassSeries.size();
98                 #pragma omp parallel for shared(DistanceToSeries)
99                 for(unsigned int j=0; j<n; j++)  // compute each distance
100             {
101                         DistanceIndexType tmp;
102                         tmp.first  = ClassLabels[j];
103                         tmp.second =  EuclidianDistanceTimeSerie( ClassSeries[i], ClassSeries[j]);
104                         DistanceToSeries[j] = tmp;
105             }
106                 
107                 // now sort the vector DistanceToSeries according to ::second
108                 std::sort(DistanceToSeries.begin(), DistanceToSeries.end(), DistancePairComp);
109         
110                 // copy 2 to K in a file
111                 std::cout << " [" << ClassLabels[i] << "] : ";
112                 for( unsigned int j=0 ; j<K; j++)
113                         std::cout << DistanceToSeries[j].first << " - ";
114                 std::cout << std::endl; 
115                 //std::cin.get();
116                 }
117         else  { std::cout << " [" << ClassLabels[i] << "] : X " << std::endl; }
118         
119 }
120
121 return 0;
122 }