10 typedef std::vector< float > TimeSerieType;
11 typedef std::pair< unsigned int , float> DistanceIndexType;
13 #pragma omp declare simd
14 float distsq(float x, float y)
19 #pragma omp declare simd
20 bool DistancePairComp (const DistanceIndexType &x, const DistanceIndexType &y)
22 return x.second < y.second;
25 float EuclidianDistanceTimeSerie( const TimeSerieType &a, const TimeSerieType &b)
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]) ;
33 return std::sqrt(dist);
37 int main(int argc, char *argv[])
39 std::vector< TimeSerieType > ClassSeries;
40 std::vector< unsigned int > ClassLabels;
41 std::vector< unsigned int > ClassEffectif;
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;
49 ///////////////////////////////////////////////////////////////
50 /// \brief reading csv file
51 ///////////////////////////////////////////////////////////////
53 ifs.open (argv[1], std::ifstream::in);
54 std::cout << "argv[1] : csv filemane = " << argv[1] << std::endl;
57 std::string ID_str, Effectif_str, Value_str;
59 std::string::size_type sz = 0; // use to identify the endline
62 if(sz == 0) std::getline(ifs, ID_str, ',');
63 // std::cout << ID_str << " " ;
64 ClassLabels.push_back( std::stof(ID_str) );
66 std::getline(ifs, Effectif_str, ',');
67 // std::cout << Effectif_str << " " ;
68 ClassEffectif.push_back( std::stof(Effectif_str) );
70 TimeSerieType tmpSerie;
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) );
79 // std::cout << std::endl;
80 ClassSeries.push_back( tmpSerie );
81 ID_str = Value_str.substr(sz+1); // " 2\n12" -> "12"
84 ///////////////////////////////////////////////////////////////
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++)
94 if( ClassEffectif[i] > N ) // if class's cardinal is > to N
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
101 DistanceIndexType tmp;
102 tmp.first = ClassLabels[j];
103 tmp.second = EuclidianDistanceTimeSerie( ClassSeries[i], ClassSeries[j]);
104 DistanceToSeries[j] = tmp;
107 // now sort the vector DistanceToSeries according to ::second
108 std::sort(DistanceToSeries.begin(), DistanceToSeries.end(), DistancePairComp);
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;
117 else { std::cout << " [" << ClassLabels[i] << "] : X " << std::endl; }