@@ -100,88 +100,94 @@ class DistanceMatrixWrap
100100 ostringstream os;
101101
102102 int nb_cluster = len (clusters);
103- int * cluster_nb_pattern;
104- int ** cluster_pattern;
103+ int * cluster_nb_pattern = NULL ;
104+ int ** cluster_pattern = NULL ;
105105
106106 boost::python::list l;
107107
108108 cluster_nb_pattern = new int [nb_cluster];
109109 cluster_pattern = new int *[nb_cluster];
110110
111- // Build dynamic 2D array
112- try
113- {
114- for (int i = 0 ; i < nb_cluster; i++)
115- {
116- #ifdef DEBUG
117- cout << " Processing cluster " << i << endl;
118- #endif
119- extract<boost::python::list> x (clusters[i]);
120-
121- if (x.check ()) {
122- l = x;
123- int nb_item = len (l);
124-
125- cluster_nb_pattern[i] = nb_item;
126-
127- cluster_pattern[i] = new int [nb_item];
128-
129- for (int j = 0 ; j < cluster_nb_pattern[i]; j++)
130- {
131- cluster_pattern[i][j] = extract<int > ((l)[j]);
132- }
133- } else {
134- status = false ;
111+ if (nb_cluster > 1 ) {
112+ // Build dynamic 2D array
113+ try
114+ {
115+ for (int i = 0 ; i < nb_cluster; i++)
116+ {
117+ #ifdef DEBUG
118+ cout << " Processing cluster " << i << endl;
119+ #endif
120+ extract<boost::python::list> x (clusters[i]);
121+
122+ if (x.check ()) {
123+ l = x;
124+ int nb_item = len (l);
125+
126+ cluster_nb_pattern[i] = nb_item;
127+
128+ cluster_pattern[i] = new int [nb_item];
129+
130+ for (int j = 0 ; j < cluster_nb_pattern[i]; j++)
131+ {
132+ cluster_pattern[i][j] = extract<int > ((l)[j]);
133+ }
134+ } else {
135+ status = false ;
136+ }
135137 }
136138 }
137- }
138- catch (...)
139- {
139+ catch (...)
140+ {
141+ // Free memory
142+ for (int i = 0 ; i < nb_cluster; i++)
143+ delete[] cluster_pattern[i];
144+
145+ delete[] cluster_nb_pattern;
146+ delete[] cluster_pattern;
147+ }
148+
149+ if (status) {
150+ ret = dm.partitioning (error, &os, nb_cluster, cluster_nb_pattern,
151+ cluster_pattern);
152+
140153 // Free memory
141154 for (int i = 0 ; i < nb_cluster; i++)
142155 delete[] cluster_pattern[i];
143156
144157 delete[] cluster_nb_pattern;
145158 delete[] cluster_pattern;
146- }
147-
148- if (status) {
149- ret = dm.partitioning (error, &os, nb_cluster, cluster_nb_pattern,
150- cluster_pattern);
159+ }} else {
160+ ret = dm.partitioning (error, &os, nb_cluster, cluster_nb_pattern,
161+ cluster_pattern);
162+ delete[] cluster_nb_pattern;
163+ delete[] cluster_pattern;
164+ }
151165
152- // Free memory
153- for (int i = 0 ; i < nb_cluster; i++)
154- delete[] cluster_pattern[i];
166+ if (!ret)
167+ stat_tool::wrap_util::throw_error (error);
155168
156- delete[] cluster_nb_pattern;
157- delete[] cluster_pattern;
169+ return ret;
158170 }
159171
160- if (!ret)
161- stat_tool::wrap_util::throw_error (error);
172+ static std::string
173+ hierarchical_clustering (const DistanceMatrix& dm, int ialgorithm,
174+ int icriterion, const std::string path, int iformat)
175+ {
176+ StatError error;
177+ ostringstream os;
178+ hierarchical_strategy algorithm = hierarchical_strategy (ialgorithm);
179+ linkage criterion = linkage (icriterion);
180+ output_format format = output_format (iformat);
162181
163- return ret;
164- }
182+ bool ret;
165183
166- static std::string
167- hierarchical_clustering (const DistanceMatrix& dm, int ialgorithm,
168- int icriterion, const std::string path, int iformat)
169- {
170- StatError error;
171- ostringstream os;
172- hierarchical_strategy algorithm = hierarchical_strategy (ialgorithm);
173- linkage criterion = linkage (icriterion);
174- output_format format = output_format (iformat);
175-
176- bool ret;
177-
178- ret = dm.hierarchical_clustering (error, &os, algorithm, criterion,
179- path, format);
184+ ret = dm.hierarchical_clustering (error, &os, algorithm, criterion,
185+ path, format);
180186
181- if (!ret)
182- stat_tool::wrap_util::throw_error (error);
187+ if (!ret)
188+ stat_tool::wrap_util::throw_error (error);
183189
184- return string (os.str ());
190+ return string (os.str ());
185191
186192 }
187193
@@ -533,7 +539,6 @@ class DistanceMatrixWrap
533539 }
534540
535541
536-
537542};
538543
539544#define WRAP DistanceMatrixWrap
@@ -615,6 +620,31 @@ class_distance_matrix()
615620#undef WRAP
616621#undef CLASS
617622
623+ class ClustersWrap
624+ {
625+
626+ public:
627+
628+ static int
629+ // Return cluster of an (individual ="pattern")
630+ // Indices are between 1 and nb_pattern for individuals
631+ // between 1 and nb_cluster for clusters
632+ get_assignment (const Clusters &cluster, int pattern)
633+ {
634+ StatError error;
635+
636+ if ((0 < pattern) & (pattern <= cluster.get_nb_pattern ()))
637+ return cluster.get_assignment (pattern-1 )+1 ;
638+ else {
639+ error.update (STAT_error[STATR_SAMPLE_INDEX ]);
640+ stat_tool::wrap_util::throw_error (error);
641+ }
642+ }
643+ };
644+
645+ #define WRAP ClustersWrap
646+ #define CLASS Clusters
647+
618648void
619649class_cluster ()
620650{
@@ -623,8 +653,11 @@ class_cluster()
623653
624654 .def (self_ns::str (self)) // __str__
625655
626- ;
656+ .def (" get_nb_cluster" , &CLASS ::get_nb_cluster, " Return number of clusters" )
657+ .def (" get_assignment" , WRAP ::get_assignment,
658+ args (" pattern" )," Get cluster of a vector (index between 0 and nb_pattern)" )
627659
660+ ;
628661 /*
629662 Clusters();
630663 Clusters(const DistanceMatrix &dist_matrix , int inb_cluster ,
@@ -659,8 +692,13 @@ class_cluster()
659692 int get_pattern_length(int pattern , int cluster) const
660693 { return pattern_length[pattern][cluster]; }
661694 */
695+
662696}
663697
698+
699+ #undef WRAP
700+ #undef CLASS
701+
664702void
665703class_dendrogram ()
666704{
0 commit comments