Skip to content

Commit d95d403

Browse files
committed
Fixing test_matrix.py
1 parent 1d639e9 commit d95d403

4 files changed

Lines changed: 80 additions & 64 deletions

File tree

src/wrapper/export_distance_matrix.cpp

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
184+
ret = dm.hierarchical_clustering(error, &os, algorithm, criterion,
185+
path, format);
175186

176-
bool ret;
177-
178-
ret = dm.hierarchical_clustering(error, &os, algorithm, criterion,
179-
path, format);
180-
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

test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test_distribution.py DONE
1818
test_error.py DONE
1919
test_estimate.py DONE
2020
test_histogram.py DONE
21-
test_matrix.py TOBECLEANED/FINALISED
21+
test_matrix.py DONE
2222
test_mixture_functional.py DONE
2323
test_mixture.py DONE
2424
test_multivariate_mixture_functional.py DONE

test/cpp/test_matrix.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ int main(void) {
5757
delete [] cluster_pattern[1];
5858
if (error.get_nb_error() > 0)
5959
cout << error;
60+
delete [] cluster_pattern;
61+
delete [] cluster_nb_pattern;
62+
cluster_pattern = new int*[1];
63+
cluster_pattern[0] = new int[1];
64+
cluster_pattern[0][0] = 0;
65+
cluster_nb_pattern = new int[1];
66+
cluster_nb_pattern[0] = 0;
67+
clust = matrix10->partitioning(error, &cout, 1, cluster_nb_pattern,
68+
cluster_pattern);
69+
delete [] cluster_pattern[0];
6070
} else {
6171
cout << error;
6272
}

test/test_matrix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ def test_select_individual(myi):
9797

9898
def test_wrong_partitioning_clusters(myi):
9999
try:
100-
# TODO: uncomment
101-
#myi.data.partitioning_clusters([0])
100+
myi.data.partitioning_clusters([0])
102101
myi.data.partitioning_clusters([])
103102
myi.data.partitioning_clusters([[1,2], [3,4]])
104103
assert False
@@ -164,4 +163,5 @@ def data():
164163

165164
test_get_column_identifier(data())
166165
test_select_individual(myi)
167-
test_partitioning_clusters(myi)
166+
test_partitioning_clusters(myi)
167+
test_wrong_partitioning_clusters(myi)

0 commit comments

Comments
 (0)