This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_stats.py
1798 lines (1523 loc) · 80.9 KB
/
test_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
from __future__ import division
__author__ = "Michael Dwan"
__copyright__ = "Copyright 2012, The QIIME project"
__credits__ = ["Jai Ram Rideout", "Michael Dwan", "Logan Knecht",
"Damien Coy", "Levi McCracken", "Andrew Cochran"]
__license__ = "GPL"
__version__ = "1.8.0"
__maintainer__ = "Jai Ram Rideout"
__email__ = "jai.rideout@gmail.com"
"""Test suite for classes, methods and functions of the stats module."""
from shutil import rmtree
from os.path import exists, join
from string import digits
from cogent.util.unit_test import TestCase, main
from cogent.util.misc import remove_files, create_dir
from numpy import array, asarray, roll, median, nan
from numpy.random import permutation, shuffle
from biom.parse import parse_biom_table
from qiime.stats import (all_pairs_t_test, _perform_pairwise_tests,
Anosim, Best, CategoryStats, CorrelationStats, DistanceMatrixStats,
MantelCorrelogram, Mantel, PartialMantel, Permanova, quantile,
_quantile,paired_difference_analyses)
from qiime.util import (DistanceMatrix, MetadataMap, get_qiime_temp_dir,
get_tmp_filename)
class TestHelper(TestCase):
"""Helper class that instantiates some commonly-used objects.
This class should be subclassed by any test classes that want to use its
members.
"""
def setUp(self):
"""Define some useful test objects."""
# The unweighted unifrac distance matrix from the overview tutorial.
self.overview_dm_str = ["\tPC.354\tPC.355\tPC.356\tPC.481\tPC.593\
\tPC.607\tPC.634\tPC.635\tPC.636",
"PC.354\t0.0\t0.595483768391\t0.618074717633\
\t0.582763100909\t0.566949022108\
\t0.714717232268\t0.772001731764\
\t0.690237118413\t0.740681707488",
"PC.355\t0.595483768391\t0.0\t0.581427669668\
\t0.613726772383\t0.65945132763\
\t0.745176523638\t0.733836123821\
\t0.720305073505\t0.680785600439",
"PC.356\t0.618074717633\t0.581427669668\t0.0\
\t0.672149021573\t0.699416863323\
\t0.71405573754\t0.759178215168\
\t0.689701276341\t0.725100672826",
"PC.481\t0.582763100909\t0.613726772383\
\t0.672149021573\t0.0\t0.64756120797\
\t0.666018240373\t0.66532968784\
\t0.650464714994\t0.632524644216",
"PC.593\t0.566949022108\t0.65945132763\
\t0.699416863323\t0.64756120797\t0.0\
\t0.703720200713\t0.748240937349\
\t0.73416971958\t0.727154987937",
"PC.607\t0.714717232268\t0.745176523638\
\t0.71405573754\t0.666018240373\
\t0.703720200713\t0.0\t0.707316869557\
\t0.636288883818\t0.699880573956",
"PC.634\t0.772001731764\t0.733836123821\
\t0.759178215168\t0.66532968784\
\t0.748240937349\t0.707316869557\t0.0\
\t0.565875193399\t0.560605525642",
"PC.635\t0.690237118413\t0.720305073505\
\t0.689701276341\t0.650464714994\
\t0.73416971958\t0.636288883818\
\t0.565875193399\t0.0\t0.575788039321",
"PC.636\t0.740681707488\t0.680785600439\
\t0.725100672826\t0.632524644216\
\t0.727154987937\t0.699880573956\
\t0.560605525642\t0.575788039321\t0.0"]
self.overview_dm = DistanceMatrix.parseDistanceMatrix(
self.overview_dm_str)
# The overview tutorial's metadata mapping file.
self.overview_map_str = ["#SampleID\tBarcodeSequence\tTreatment\tDOB",
"PC.354\tAGCACGAGCCTA\tControl\t20061218",
"PC.355\tAACTCGTCGATG\tControl\t20061218",
"PC.356\tACAGACCACTCA\tControl\t20061126",
"PC.481\tACCAGCGACTAG\tControl\t20070314",
"PC.593\tAGCAGCACTTGT\tControl\t20071210",
"PC.607\tAACTGTGCGTAC\tFast\t20071112",
"PC.634\tACAGAGTCGGCT\tFast\t20080116",
"PC.635\tACCGCAGAGTCA\tFast\t20080116",
"PC.636\tACGGTGAGTGTC\tFast\t20080116"]
self.overview_map = MetadataMap.parseMetadataMap(self.overview_map_str)
self.test_map_str = [
"#SampleID\tBarcodeSequence\tFoo\tBar\tDescription",
"PC.354\tAGCACGAGCCTA\tfoo\ta\t354",
"PC.355\tAACTCGTCGATG\tfoo\ta\t355",
"PC.356\tACAGACCACTCA\tbar\ta\t356",
"PC.481\tACCAGCGACTAG\tfoo\ta\t481",
"PC.593\tAGCAGCACTTGT\tbar\ta\t593",
"PC.607\tAACTGTGCGTAC\tbar\ta\t607",
"PC.634\tACAGAGTCGGCT\tbar\ta\t634",
"PC.635\tACCGCAGAGTCA\tfoo\ta\t635",
"PC.636\tACGGTGAGTGTC\tbar\ta\t636"]
self.test_map = MetadataMap.parseMetadataMap(self.test_map_str)
# A 1x1 dm.
self.single_ele_dm = DistanceMatrix(array([[0]]), ['s1'], ['s1'])
# How many times to test a p-value.
self.p_val_tests = 10
def assertCorrectPValue(self, exp_min, exp_max, fn, num_perms=None,
p_val_key='p_value'):
"""Tests that the stochastic p-value falls in the specified range.
Performs the test self.p_val_tests times and fails if the observed
p-value does not fall into the specified range at least once. Each
p-value is also tested that it falls in the range 0.0 to 1.0.
This method assumes that fn is callable, and will pass num_perms to fn
if num_perms is provided. p_val_key specifies the key that will be used
to retrieve the p-value from the results dict that is returned by fn.
"""
found_match = False
for i in range(self.p_val_tests):
if num_perms is not None:
obs = fn(num_perms)
else:
obs = fn()
p_val = obs[p_val_key]
self.assertIsProb(p_val)
if p_val >= exp_min and p_val <= exp_max:
found_match = True
break
self.assertTrue(found_match)
class NonRandomShuffler(object):
"""Helper class for testing p-values that are calculated by permutations.
Since p-values rely on randomness, it may be useful to use a non-random
function (such as that provided by this class) to generate permutations
so that p-values can be accurately tested.
This code is heavily based on Andrew Cochran's original version.
"""
def __init__(self):
"""Default constructor initializes the number of calls to zero."""
self.num_calls = 0
def permutation(self, x):
"""Non-random permutation function to test p-test code.
Returns the 'permuted' version of x.
Arguments:
x - the array to be 'permuted'
"""
x = array(x)
x = roll(x, self.num_calls)
self.num_calls += 1
return x
class StatsTests(TestCase):
"""Tests for top-level functions in the stats module."""
def setUp(self):
"""Set up data that will be used by the tests."""
# For testing Monte Carlo functionality.
# Single comp.
self.labels1 = ['foo', 'bar']
self.dists1 = [[1, 2, 3], [7, 8]]
# Multiple comps.
self.labels2 = ['foo', 'bar', 'baz']
self.dists2 = [[1, 2, 3], [7, 8], [9, 10, 11]]
# Too few obs.
self.labels3 = ['foo', 'bar', 'baz']
self.dists3 = [[1], [7], [9, 10, 11]]
def remove_nums(self, text):
"""Removes all digits from the given string.
Returns the string will all digits removed. Useful for testing strings
for equality in unit tests where you don't care about numeric values,
or if some values are random.
This code was taken from http://bytes.com/topic/python/answers/
850562-finding-all-numbers-string-replacing
Arguments:
text - the string to remove digits from
"""
return text.translate(None, digits)
def test_all_pairs_t_test(self):
"""Test performing Monte Carlo tests on valid dataset."""
# We aren't testing the numeric values here, as they've already been
# tested in the functions that compute them. We are interested in the
# format of the returned string.
exp = """# The tests of significance were performed using a two-sided Student's two-sample t-test.
# Alternative hypothesis: Group 1 mean != Group 2 mean
# The nonparametric p-values were calculated using 999 Monte Carlo permutations.
# The nonparametric p-values contain the correct number of significant digits.
# Entries marked with "N/A" could not be calculated because at least one of the groups
# of distances was empty, both groups each contained only a single distance, or
# the test could not be performed (e.g. no variance in groups with the same mean).
Group 1 Group 2 t statistic Parametric p-value Parametric p-value (Bonferroni-corrected) Nonparametric p-value Nonparametric p-value (Bonferroni-corrected)
foo bar -6.6 0.00708047956412 0.0212414386924 0.095 0.285
foo baz -9.79795897113 0.000608184944463 0.00182455483339 0.101 0.303
bar baz -3.0 0.0576688856224 0.173006656867 0.217 0.651
"""
obs = all_pairs_t_test(self.labels2, self.dists2)
self.assertEqual(self.remove_nums(obs), self.remove_nums(exp))
def test_all_pairs_t_test_no_perms(self):
"""Test performing Monte Carlo tests on valid dataset with no perms."""
exp = """# The tests of significance were performed using a two-sided Student's two-sample t-test.
# Alternative hypothesis: Group 1 mean != Group 2 mean
# Entries marked with "N/A" could not be calculated because at least one of the groups
# of distances was empty, both groups each contained only a single distance, or
# the test could not be performed (e.g. no variance in groups with the same mean).
Group 1 Group 2 t statistic Parametric p-value Parametric p-value (Bonferroni-corrected) Nonparametric p-value Nonparametric p-value (Bonferroni-corrected)
foo bar -6.6 0.00708047956412 0.0212414386924 N/A N/A
foo baz -9.79795897113 0.000608184944463 0.00182455483339 N/A N/A
bar baz -3.0 0.0576688856224 0.173006656867 N/A N/A
"""
obs = all_pairs_t_test(self.labels2, self.dists2,
num_permutations=0)
self.assertEqual(self.remove_nums(obs), self.remove_nums(exp))
def test_all_pairs_t_test_few_perms(self):
"""Test performing Monte Carlo tests on dataset with a few perms."""
exp = """# The tests of significance were performed using a one-sided (low) Student's two-sample t-test.
# Alternative hypothesis: Group 1 mean < Group 2 mean
# The nonparametric p-values were calculated using 5 Monte Carlo permutations.
# The nonparametric p-values contain the correct number of significant digits.
# Entries marked with "N/A" could not be calculated because at least one of the groups
# of distances was empty, both groups each contained only a single distance, or
# the test could not be performed (e.g. no variance in groups with the same mean).
Group 1 Group 2 t statistic Parametric p-value Parametric p-value (Bonferroni-corrected) Nonparametric p-value Nonparametric p-value (Bonferroni-corrected)
foo bar -6.6 0.00354023978206 0.0106207193462 Too few iters to compute p-value (num_iters=5) Too few iters to compute p-value (num_iters=5)
foo baz -9.79795897113 0.000304092472232 0.000912277416695 Too few iters to compute p-value (num_iters=5) Too few iters to compute p-value (num_iters=5)
bar baz -3.0 0.0288344428112 0.0865033284337 Too few iters to compute p-value (num_iters=5) Too few iters to compute p-value (num_iters=5)
"""
obs = all_pairs_t_test(self.labels2, self.dists2,
num_permutations=5, tail_type='low')
self.assertEqual(self.remove_nums(obs), self.remove_nums(exp))
def test_all_pairs_t_test_invalid_tests(self):
"""Test performing Monte Carlo tests with some invalid tests."""
exp = """# The tests of significance were performed using a one-sided (high) Student's two-sample t-test.
# Alternative hypothesis: Group 1 mean > Group 2 mean
# The nonparametric p-values were calculated using 20 Monte Carlo permutations.
# The nonparametric p-values contain the correct number of significant digits.
# Entries marked with "N/A" could not be calculated because at least one of the groups
# of distances was empty, both groups each contained only a single distance, or
# the test could not be performed (e.g. no variance in groups with the same mean).
Group 1 Group 2 t statistic Parametric p-value Parametric p-value (Bonferroni-corrected) Nonparametric p-value Nonparametric p-value (Bonferroni-corrected)
foo bar N/A N/A N/A N/A N/A
"""
obs = all_pairs_t_test(['foo', 'bar'], [[], [1, 2, 4]],
'high', 20)
self.assertEqual(self.remove_nums(obs), self.remove_nums(exp))
def test_all_pairs_t_test_invalid_input(self):
"""Test performing Monte Carlo tests on invalid input."""
# Number of labels and distance groups do not match.
self.assertRaises(ValueError, all_pairs_t_test,
['foo', 'bar'], [[1, 2, 3], [4, 5, 6], [7, 8]])
# Invalid tail type.
self.assertRaises(ValueError, all_pairs_t_test,
['foo', 'bar'], [[1, 2, 3], [4, 5, 6]], 'foo')
# Invalid number of permutations.
self.assertRaises(ValueError, all_pairs_t_test,
['foo', 'bar'], [[1, 2, 3], [4, 5, 6]], num_permutations=-1)
def test_perform_pairwise_tests_single_comp(self):
"""Test on valid dataset w/ 1 comp."""
# Verified with R's t.test function.
exp = [['foo', 'bar', -6.5999999999999996, 0.0070804795641244006,
0.0070804795641244006, 0.10199999999999999, 0.10199999999999999]]
obs = _perform_pairwise_tests(self.labels1, self.dists1, 'two-sided',
999)
self.assertEqual(len(obs), len(exp))
self.assertFloatEqual(obs[0][:5], exp[0][:5])
self.assertIsProb(obs[0][5])
self.assertFloatEqual(obs[0][5], obs[0][6])
def test_perform_pairwise_tests_multi_comp(self):
"""Test on valid dataset w/ multiple comps."""
# Verified with R's t.test function.
exp = [['foo', 'bar', -6.5999999999999996, 0.0070804795641244006,
0.021241438692373202, nan, nan], ['foo', 'baz',
-9.7979589711327115, 0.00060818494446333643, 0.0018245548333900093,
nan, nan], ['bar', 'baz', -3.0, 0.05766888562243732,
0.17300665686731195, nan, nan]]
obs = _perform_pairwise_tests(self.labels2, self.dists2, 'two-sided',
0)
self.assertFloatEqual(obs, exp)
def test_perform_pairwise_tests_too_few_obs(self):
"""Test on dataset w/ too few observations."""
exp = [['foo', 'bar', nan, nan, nan, nan, nan], ['foo', 'baz',
-7.794228634059948, 0.008032650971672552, 0.016065301943345104,
nan, nan], ['bar', 'baz',
-2.598076211353316, 0.060844967173160069, 0.12168993434632014,
nan, nan]]
obs = _perform_pairwise_tests(self.labels3, self.dists3, 'low', 0)
self.assertFloatEqual(obs, exp)
exp = [['foo', 'bar', nan, nan, nan, nan, nan]]
obs = _perform_pairwise_tests(['foo', 'bar'], [[], [1, 2, 4]], 'high',
20)
self.assertFloatEqual(obs, exp)
class DistanceMatrixStatsTests(TestHelper):
"""Tests for the DistanceMatrixStats class."""
def setUp(self):
"""Define some dm stats instances that will be used by the tests."""
super(DistanceMatrixStatsTests, self).setUp()
self.empty_dms = DistanceMatrixStats([])
self.single_dms = DistanceMatrixStats([self.overview_dm])
self.double_dms = DistanceMatrixStats(
[self.overview_dm, self.single_ele_dm])
# For testing the requirement that two distance matrices are set.
self.two_dms = DistanceMatrixStats(
[self.overview_dm, self.single_ele_dm], 2)
# For testing the requirement that the distance matrices meet the
# minimum size requirements.
self.size_dms = DistanceMatrixStats(
[self.overview_dm, self.overview_dm], 2, 4)
def test_DistanceMatrices_getter(self):
"""Test getter for distmats."""
self.assertEqual(self.empty_dms.DistanceMatrices, [])
self.assertEqual(self.single_dms.DistanceMatrices, [self.overview_dm])
self.assertEqual(self.double_dms.DistanceMatrices,
[self.overview_dm, self.single_ele_dm])
def test_DistanceMatrices_setter(self):
"""Test setter for dms on valid input data."""
self.empty_dms.DistanceMatrices = []
self.assertEqual(self.empty_dms.DistanceMatrices, [])
self.empty_dms.DistanceMatrices = [self.overview_dm]
self.assertEqual(self.empty_dms.DistanceMatrices, [self.overview_dm])
self.empty_dms.DistanceMatrices = [self.overview_dm, self.overview_dm]
self.assertEqual(self.empty_dms.DistanceMatrices,
[self.overview_dm, self.overview_dm])
def test_DistanceMatrices_setter_invalid(self):
"""Test setter for dms on invalid input data."""
# Allows testing of non-callable property setter that raises errors.
# Idea was obtained from http://stackoverflow.com/a/3073049
self.assertRaises(TypeError, setattr, self.empty_dms,
'DistanceMatrices', None)
self.assertRaises(TypeError, setattr, self.empty_dms,
'DistanceMatrices', 10)
self.assertRaises(TypeError, setattr, self.empty_dms,
'DistanceMatrices', 20.0)
self.assertRaises(TypeError, setattr, self.empty_dms,
'DistanceMatrices', "foo")
self.assertRaises(TypeError, setattr, self.empty_dms,
'DistanceMatrices', {})
self.assertRaises(TypeError, setattr, self.empty_dms,
'DistanceMatrices', self.overview_dm)
self.assertRaises(TypeError, setattr, self.empty_dms,
'DistanceMatrices', [1])
self.assertRaises(ValueError, setattr, self.empty_dms,
'DistanceMatrices',
[DistanceMatrix(array([[1, 2], [3, 4]]), ['foo', 'bar'],
['foo', 'bar']),
DistanceMatrix(array([[1, 2], [3, 4.5]]), ['foo', 'bar'],
['foo', 'bar'])])
# Test constructor as well.
self.assertRaises(TypeError, DistanceMatrixStats, None)
self.assertRaises(TypeError, DistanceMatrixStats, 10)
self.assertRaises(TypeError, DistanceMatrixStats, 20.0)
self.assertRaises(TypeError, DistanceMatrixStats, "foo")
self.assertRaises(TypeError, DistanceMatrixStats, {})
self.assertRaises(TypeError, DistanceMatrixStats, self.overview_dm)
self.assertRaises(TypeError, DistanceMatrixStats, [1])
self.assertRaises(ValueError, DistanceMatrixStats,
[DistanceMatrix(array([[1, 2], [3, 4]]), ['foo', 'bar'],
['foo', 'bar']),
DistanceMatrix(array([[1, 2], [3, 4.5]]), ['foo', 'bar'],
['foo', 'bar'])])
def test_DistanceMatrices_setter_wrong_number(self):
"""Test setting an invalid number of distance matrices."""
self.assertRaises(ValueError, setattr, self.two_dms,
'DistanceMatrices', [self.overview_dm])
self.assertRaises(ValueError, setattr, self.two_dms,
'DistanceMatrices', [self.overview_dm, self.overview_dm,
self.overview_dm])
def test_DistanceMatrices_setter_too_small(self):
"""Test setting distance matrices that are too small."""
self.assertRaises(ValueError, setattr, self.size_dms,
'DistanceMatrices', [self.single_ele_dm, self.single_ele_dm])
def test_DistanceMatrices_setter_suppress_symmetry_check(self):
"""Test suppressing symmetry check."""
dms = DistanceMatrixStats([],
suppress_symmetry_and_hollowness_check=True)
dms.DistanceMatrices = [
DistanceMatrix(array([[1, 2], [3, 4]]), ['foo', 'bar'],
['foo', 'bar']),
DistanceMatrix(array([[1, 2], [3, 4.5]]), ['foo', 'bar'],
['foo', 'bar'])]
dms = DistanceMatrixStats([
DistanceMatrix(array([[1, 2], [3, 4]]), ['foo', 'bar'],
['foo', 'bar']),
DistanceMatrix(array([[1, 2], [3, 4.5]]), ['foo', 'bar'],
['foo', 'bar'])],
suppress_symmetry_and_hollowness_check=True)
def test_call(self):
"""Test __call__() returns an empty result set."""
self.assertEqual(self.single_dms(), {})
self.assertEqual(self.single_dms(10), {})
self.assertEqual(self.single_dms(0), {})
def test_call_bad_perms(self):
"""Test __call__() fails upon receiving invalid number of perms."""
self.assertRaises(ValueError, self.single_dms, -1)
class CorrelationStatsTests(TestHelper):
"""Tests for the CorrelationStats class."""
def setUp(self):
"""Set up correlation stats instances for use in tests."""
super(CorrelationStatsTests, self).setUp()
self.cs = CorrelationStats([self.overview_dm, self.overview_dm])
def test_DistanceMatrices_setter(self):
"""Test setting valid distance matrices."""
dms = [self.overview_dm, self.overview_dm]
self.cs.DistanceMatrices = dms
self.assertEqual(self.cs.DistanceMatrices, dms)
dms = [self.overview_dm, self.overview_dm, self.overview_dm]
self.cs.DistanceMatrices = dms
self.assertEqual(self.cs.DistanceMatrices, dms)
def test_DistanceMatrices_setter_mismatched_labels(self):
"""Test setting dms with mismatching sample ID labels."""
mismatch = DistanceMatrix(array([[0]]), ['s2'], ['s2'])
self.assertRaises(ValueError, setattr, self.cs, 'DistanceMatrices',
[self.single_ele_dm, mismatch])
# Also test that constructor raises this error.
self.assertRaises(ValueError, CorrelationStats, [self.single_ele_dm,
mismatch])
def test_DistanceMatrices_setter_wrong_dims(self):
"""Test setting dms with mismatching dimensions."""
self.assertRaises(ValueError, setattr, self.cs, 'DistanceMatrices',
[self.overview_dm, self.single_ele_dm])
# Also test that constructor raises this error.
self.assertRaises(ValueError, CorrelationStats, [self.overview_dm,
self.single_ele_dm])
def test_DistanceMatrices_setter_too_few(self):
"""Test setting dms with not enough of them."""
self.assertRaises(ValueError, setattr, self.cs, 'DistanceMatrices', [])
# Also test that constructor raises this error.
self.assertRaises(ValueError, CorrelationStats, [])
def test_call(self):
"""Test __call__() returns an empty result set."""
self.assertEqual(self.cs(), {})
class CategoryStatsTests(TestHelper):
"""Tests for the CategoryStats class."""
def setUp(self):
"""Define some useful data to use in testing."""
super(CategoryStatsTests, self).setUp()
self.cs_overview = CategoryStats(self.overview_map, [self.overview_dm],
["Treatment", "DOB"])
def test_MetadataMap_setter(self):
"""Should set the mdmap property."""
self.cs_overview.MetadataMap = self.overview_map
self.assertEqual(self.cs_overview.MetadataMap, self.overview_map)
def test_MetadataMap_setter_invalid_input(self):
"""Setter must receive the correct and compatible object types."""
self.assertRaises(TypeError, setattr, self.cs_overview, 'MetadataMap',
"foo")
self.assertRaises(TypeError, setattr, self.cs_overview, 'MetadataMap',
[])
self.assertRaises(TypeError, setattr, self.cs_overview, 'MetadataMap',
{})
self.assertRaises(TypeError, setattr, self.cs_overview, 'MetadataMap',
None)
self.assertRaises(TypeError, setattr, self.cs_overview, 'MetadataMap',
self.overview_dm)
def test_MetadataMap_getter(self):
"""Test valid return of MetadataMap property."""
self.assertEqual(self.cs_overview.MetadataMap, self.overview_map)
def test_Categories_setter_invalid_input(self):
"""Must receive a list of categories that are in the mapping file."""
self.assertRaises(TypeError, setattr, self.cs_overview, 'Categories',
"Hello!")
self.assertRaises(TypeError, setattr, self.cs_overview, 'Categories',
self.overview_dm)
self.assertRaises(ValueError, setattr, self.cs_overview, 'Categories',
["hehehe", 123, "hello"])
self.assertRaises(ValueError, setattr, self.cs_overview, 'Categories',
["foo"])
# Test setting a unique category.
self.assertRaises(ValueError, CategoryStats, self.test_map,
[self.overview_dm], ["Description"])
cs_test = CategoryStats(self.test_map, [self.overview_dm], ["Foo"])
self.assertRaises(ValueError, setattr, cs_test, 'Categories',
["Description"])
# Test setting a category with only a single value.
self.assertRaises(ValueError, setattr, cs_test, 'Categories', ["Bar"])
# Test setting a category that is non-numeric.
self.assertRaises(TypeError, CategoryStats, self.overview_map,
[self.overview_dm], ["Treatment"],
suppress_numeric_category_check=False)
def test_Categories_getter(self):
"""Test valid return of Categories property."""
expected = ['Treatment', 'DOB']
observed = self.cs_overview.Categories
self.assertEqual(observed, expected)
def test_RandomFunction_getter(self):
"""Test retrieval of a random function reference."""
self.assertEqual(self.cs_overview.RandomFunction, permutation)
def test_RandomFunction_setter(self):
"""Test setter for the random function to use in p-value calc."""
self.assertEqual(self.cs_overview.RandomFunction, permutation)
nrs = NonRandomShuffler()
self.cs_overview.RandomFunction = nrs.permutation
self.assertEqual(self.cs_overview.RandomFunction, nrs.permutation)
def test_RandomFunction_setter_invalid_input(self):
"""Test setter for the random function with non-callable input."""
self.assertRaises(TypeError, setattr, self.cs_overview,
'RandomFunction', 42)
self.assertRaises(TypeError, setattr, self.cs_overview,
'RandomFunction', 42.0)
self.assertRaises(TypeError, setattr, self.cs_overview,
'RandomFunction', "j")
self.assertRaises(TypeError, setattr, self.cs_overview,
'RandomFunction', None)
self.assertRaises(TypeError, setattr, self.cs_overview,
'RandomFunction', [])
self.assertRaises(TypeError, setattr, self.cs_overview,
'RandomFunction', ())
self.assertRaises(TypeError, setattr, self.cs_overview,
'RandomFunction', {})
def test_validate_compatibility(self):
"""Test for compatible sample IDs between dms and mdmap."""
self.assertEqual(self.cs_overview._validate_compatibility(), None)
self.cs_overview.DistanceMatrices = [self.single_ele_dm]
self.assertRaises(ValueError, self.cs_overview._validate_compatibility)
self.cs_overview.DistanceMatrices = [self.overview_dm]
self.cs_overview.MetadataMap = self.test_map
self.assertRaises(ValueError, self.cs_overview._validate_compatibility)
def test_call(self):
"""Test _call__() returns an empty result set."""
self.assertEqual(self.cs_overview(), {})
self.assertEqual(self.cs_overview(10), {})
def test_call_bad_perms(self):
"""Test __call__() fails upon receiving invalid number of perms."""
self.assertRaises(ValueError, self.cs_overview, -1)
def test_call_incompatible_data(self):
"""Test __call__() fails after incompatible dms/mdmap pair is set."""
self.cs_overview.DistanceMatrices = [self.single_ele_dm,
self.single_ele_dm]
self.assertRaises(ValueError, self.cs_overview)
class AnosimTests(TestHelper):
"""Tests for the Anosim class.
This testing code is heavily based on Andrew Cochran's original suite of
unit tests for ANOSIM.
"""
def setUp(self):
"""Define some useful data to use in testing."""
super(AnosimTests, self).setUp()
# Define two small dms for easy testing. One has ties in the ranks.
self.small_dm_str = ["\tsam1\tsam2\tsam3\tsam4",
"sam1\t0\t1\t5\t4",
"sam2\t1\t0\t3\t2",
"sam3\t5\t3\t0\t3",
"sam4\t4\t2\t3\t0"]
self.small_dm = DistanceMatrix.parseDistanceMatrix(self.small_dm_str)
self.small_dm_tie_str = ["\tsam1\tsam2\tsam3\tsam4",
"sam1\t0\t1\t1\t4",
"sam2\t1\t0\t3\t2",
"sam3\t1\t3\t0\t3",
"sam4\t4\t2\t3\t0"]
self.small_dm_tie = DistanceMatrix.parseDistanceMatrix(
self.small_dm_tie_str)
self.small_map_str = ["#SampleID\tBarcodeSequence\
\tLinkerPrimerSequence\tTreatment\tDOB\
\tDescription",
"sam1\tAGCACGAGCCTA\tYATGCTGCCTCCCGTAGGAGT\
\tControl\t20061218\tControl_mouse_I.D._354",
"sam2\tAACTCGTCGATG\tYATGCTGCCTCCCGTAGGAGT\
\tControl\t20061218\tControl_mouse_I.D._355",
"sam3\tACAGACCACTCA\tYATGCTGCCTCCCGTAGGAGT\
\tFast\t20061126\tControl_mouse_I.D._356",
"sam4\tACCAGCGACTAG\tYATGCTGCCTCCCGTAGGAGT\
\tFast\t20070314\tControl_mouse_I.D._481"]
self.small_map = MetadataMap.parseMetadataMap(self.small_map_str)
# Create a group map, which maps sample ID to category value (e.g.
# sample 1 to 'control' and sample 2 to 'fast'). This comes in handy
# for testing some of the private methods in the Anosim class. This
# group map can be used for testing both the small dm data and the
# small dm with ties data.
self.small_group_map = {}
for samp_id in self.small_dm.SampleIds:
self.small_group_map[samp_id] = self.small_map.getCategoryValue(
samp_id, 'Treatment')
# Create three Anosim instances: one for the small dm, one for the
# small dm with ties, and one for the overview tutorial dataset.
self.anosim_small = Anosim(self.small_map, self.small_dm, 'Treatment')
self.anosim_small_tie = Anosim(self.small_map, self.small_dm_tie,
'Treatment')
self.anosim_overview = Anosim(self.overview_map, self.overview_dm,
'Treatment')
def test_call_overview(self):
"""Test __call__() on overview data with Treatment category."""
# These results were verified with R.
exp = {'method_name': 'ANOSIM', 'p_value': 0.0080000000000000002,
'r_value': 0.8125}
obs = self.anosim_overview()
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['r_value'], exp['r_value'])
self.assertCorrectPValue(0, 0.06, self.anosim_overview)
def test_call_small(self):
"""Test __call__() on small dm."""
# These results were verified with R.
exp = {'method_name': 'ANOSIM', 'p_value': 0.31, 'r_value': 0.625}
obs = self.anosim_small()
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['r_value'], exp['r_value'])
self.assertCorrectPValue(0.28, 0.42, self.anosim_small)
def test_call_small_ties(self):
"""Test __call__() on small dm with ties in ranks."""
# These results were verified with R.
exp = {'method_name': 'ANOSIM', 'p_value': 0.67600000000000005,
'r_value': 0.25}
obs = self.anosim_small_tie()
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['r_value'], exp['r_value'])
self.assertCorrectPValue(0.56, 0.75, self.anosim_small_tie)
def test_call_no_perms(self):
"""Test __call__() on small dm with no permutations."""
# These results were verified with R.
exp = {'method_name': 'ANOSIM', 'p_value': 1.0, 'r_value': 0.625}
obs = self.anosim_small(0)
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['r_value'], exp['r_value'])
self.assertFloatEqual(obs['p_value'], exp['p_value'])
def test_call_incompatible_data(self):
"""Should fail on incompatible mdmap/dm combo and bad perms."""
self.assertRaises(ValueError, self.anosim_small, -1)
self.anosim_small.DistanceMatrices = [self.single_ele_dm]
self.assertRaises(ValueError, self.anosim_small)
def test_anosim_small(self):
"""Test _anosim() on small dm."""
# These results were verified with R.
exp = 0.625
obs = self.anosim_small._anosim(self.small_group_map)
self.assertFloatEqual(obs, exp)
def test_anosim_small_ties(self):
"""Test _anosim() on small dm with ties."""
# These results were verified with R.
exp = 0.25
obs = self.anosim_small_tie._anosim(self.small_group_map)
self.assertFloatEqual(obs, exp)
def test_remove_ties1(self):
"""Test removal of ties. Should return [1.5,1.5]."""
result = self.anosim_small._remove_ties([1,1],[1,2])
self.assertEqual(result, [1.5,1.5])
def test_remove_ties2(self):
"""Should return [3.5,3.5,3.5,3.5,3.5,3.5]."""
result = self.anosim_small._remove_ties([1,1,1,1,1,1],[1,2,3,4,5,6])
self.assertEqual(result, [3.5,3.5,3.5,3.5,3.5,3.5])
def test_remove_ties3(self):
"""Should return [1,3.5,3.5,3.5,3.5,6]."""
result = self.anosim_small._remove_ties([1,3,3,3,3,8],[1,2,3,4,5,6])
self.assertEqual(result, [1,3.5,3.5,3.5,3.5,6])
def test_remove_ties4(self):
"""Should return [1,2,3,4]."""
result = self.anosim_small._remove_ties([1,2,3,4],[1,2,3,4])
self.assertEqual(result, [1,2,3,4])
def test_remove_ties5(self):
"""Should return [1,3,3,3,5.5,5.5,7]."""
result = self.anosim_small._remove_ties([1,2,2,2,3,3,5],
[1,2,3,4,5,6,7])
self.assertEqual(result, [1,3,3,3,5.5,5.5,7])
def test_remove_ties6(self):
"""Should return [1.5,1.5,3.5,3.5]."""
result = self.anosim_small._remove_ties([1,1,2,2],[1,2,3,4])
self.assertEqual(result,[1.5,1.5,3.5,3.5])
def test_get_adjusted_vals(self):
"""Test computing adjusted ranks for ties."""
exp = [4, 4, 4]
obs = self.anosim_small._get_adjusted_vals([3, 4, 5], 0, 2)
self.assertEqual(obs, exp)
def test_compute_r1(self):
"""Should return .625 for the R statistic on the small dm."""
sorted_rank = [1.0,2.0,3.5,3.5,5.0,6.0]
sorted_group = [1.0,0.0,0.0,1.0,0.0,0.0]
sorted_rank = array(sorted_rank)
sorted_group = array(sorted_group)
result = self.anosim_small._compute_r_value(sorted_rank,sorted_group,4)
self.assertEqual(result, .625)
def test_anosim_p_test(self):
"""p-value should be .5 for this test."""
nrs = NonRandomShuffler()
self.anosim_small.RandomFunction = nrs.permutation
exp = {'method_name': 'ANOSIM', 'p_value': 0.5, 'r_value': 0.625}
obs = self.anosim_small(3)
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['r_value'], exp['r_value'])
self.assertFloatEqual(obs['p_value'], exp['p_value'])
class PermanovaTests(TestHelper):
def setUp(self):
"""Define some useful data to use in testing."""
super(PermanovaTests, self).setUp()
# Some distance matrices to help test Permanova.
self.distmtx_str = ["\tsam1\tsam2\tsam3\tsam4",
"sam1\t0\t1\t5\t4",
"sam2\t1\t0\t3\t2",
"sam3\t5\t3\t0\t3",
"sam4\t4\t2\t3\t0"]
self.distmtx = DistanceMatrix.parseDistanceMatrix(self.distmtx_str)
self.distmtx_samples = self.distmtx.SampleIds
self.distmtx_tie_str = ["\tsam1\tsam2\tsam3\tsam4",
"sam1\t0\t1\t1\t4",
"sam2\t1\t0\t3\t2",
"sam3\t1\t3\t0\t3",
"sam4\t4\t2\t3\t0"]
self.distmtx_tie = DistanceMatrix.parseDistanceMatrix(
self.distmtx_tie_str)
self.distmtx_tie_samples = self.distmtx_tie.SampleIds
# For testing with uneven group sizes.
self.distmtx_uneven_str = ["\tsam1\tsam2\tsam3\tsam4\tsam5",
"sam1\t0\t3\t7\t2\t1",
"sam2\t3\t0\t5\t4\t1",
"sam3\t7\t5\t0\t2\t6",
"sam4\t2\t4\t2\t0\t2",
"sam5\t1\t1\t6\t2\t0"]
self.distmtx_uneven = DistanceMatrix.parseDistanceMatrix(
self.distmtx_uneven_str)
self.distmtx_uneven_samples = self.distmtx_uneven.SampleIds
# Some group maps to help test Permanova, data_map can be used with
# distmtx and distmtx_tie while data_map_uneven can only be used
# with distmtx_uneven.
self.data_map_str = ["#SampleID\tBarcodeSequence\tLinkerPrimerSequence\
\tTreatment\tDOB\tDescription",
"sam1\tAGCACGAGCCTA\tYATGCTGCCTCCCGTAGGAGT\tControl\t20061218\
\tControl_mouse_I.D._354",
"sam2\tAACTCGTCGATG\tYATGCTGCCTCCCGTAGGAGT\tControl\t20061218\
\tControl_mouse_I.D._355",
"sam3\tACAGACCACTCA\tYATGCTGCCTCCCGTAGGAGT\tFast\t20061126\
\tControl_mouse_I.D._356",
"sam4\tACCAGCGACTAG\tYATGCTGCCTCCCGTAGGAGT\tFast\t20070314\
\tControl_mouse_I.D._481"]
self.data_map = MetadataMap.parseMetadataMap(self.data_map_str)
# For testing with uneven group sizes.
self.data_map_uneven_str=["#SampleID\tBarcodeSequence\
\tLinkerPrimerSequence\tTreatment\tDOB\tDescription",
"sam1\tAGCACGAGCCTA\tYATGCTGCCTCCCGTAGGAGT\tControl\t20061218\
\tControl_mouse_I.D._354",
"sam2\tAACTCGTCGATG\tYATGCTGCCTCCCGTAGGAGT\tControl\t20061218\
\tControl_mouse_I.D._355",
"sam3\tACAGACCACTCA\tYATGCTGCCTCCCGTAGGAGT\tFast\t20061126\
\tControl_mouse_I.D._356",
"sam4\tACCAGCGACTAG\tYATGCTGCCTCCCGTAGGAGT\tAwesome\t20070314\
\tControl_mouse_I.D._481",
"sam5\tACCAGCGACTAG\tYATGCTGCCTCCCCTATADST\tAwesome\t202020\
\tcontrolmouseid"]
self.data_map_uneven = MetadataMap.parseMetadataMap(
self.data_map_uneven_str)
# Formatting the two data_maps to meet permanova requirements.
self.map = {}
for samp_id in self.data_map.SampleIds:
self.map[samp_id] = self.data_map.getCategoryValue(samp_id,
'Treatment')
self.map_uneven = {}
for samp_id in self.data_map_uneven.SampleIds:
self.map_uneven[samp_id] = self.data_map_uneven.getCategoryValue(
samp_id, 'Treatment')
# Creating instances of Permanova to run the tests on.
self.permanova_plain = Permanova(self.data_map, self.distmtx,
'Treatment')
self.permanova_tie = Permanova(self.data_map, self.distmtx_tie,
'Treatment')
self.permanova_uneven = Permanova(self.data_map_uneven,
self.distmtx_uneven, 'Treatment')
self.permanova_overview = Permanova(self.overview_map,
self.overview_dm,'Treatment')
def test_permanova1(self):
"""permanova() should return 4.4."""
exp = 4.4
obs = self.permanova_plain._permanova(self.map)
self.assertEqual(obs, exp)
def test_permanova2(self):
"""Should result in 2."""
exp = 2
obs = self.permanova_tie._permanova(self.map)
self.assertEqual(obs, exp)
def test_permanova3(self):
"""Should result in 3.58462."""
exp = 3.58462
obs = self.permanova_uneven._permanova(self.map_uneven)
self.assertFloatEqual(obs, exp)
def test_compute_f1(self):
"""Should return 4.4, testing just function."""
distances = [1,5,4,3,2,3]
grouping = [0,-1,-1,-1,-1,1]
distances = array(distances)
grouping = array(grouping)
result = self.permanova_plain._compute_f_value(distances,grouping,4,2,
[2,2])
self.assertEqual(result, 4.4)
def test_call_plain(self):
"""Test __call__() on plain dm."""
exp = {'method_name': 'PERMANOVA', 'p_value': "?", 'f_value': 4.4}
obs = self.permanova_plain()
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['f_value'], exp['f_value'])
self.assertCorrectPValue(0.28, 0.42, self.permanova_plain)
def test_call_tie(self):
"""Test __call__() on dm with ties in ranks."""
exp = {'method_name': 'PERMANOVA', 'p_value': "?", 'f_value': 2}
obs = self.permanova_tie()
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['f_value'], exp['f_value'])
self.assertCorrectPValue(0.56, 0.75, self.permanova_tie)
def test_call_uneven(self):
"""Test __call__() on uneven group sizes with no permutations."""
exp = {'method_name': 'PERMANOVA', 'p_value': 1.0, 'f_value': 3.58462}
obs = self.permanova_uneven(0)
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['f_value'], exp['f_value'])
self.assertFloatEqual(obs['p_value'], exp['p_value'])
def test_call_overview(self):
"""Test __call__() on the overview dataset."""
exp = {'method_name': 'PERMANOVA', 'p_value': 0.039215686274509803,
'f_value': 2.2966506517077487}
obs = self.permanova_overview(50)
self.assertEqual(obs['method_name'], exp['method_name'])
self.assertFloatEqual(obs['f_value'], exp['f_value'])
self.assertCorrectPValue(0.005, 0.07, self.permanova_overview, 50)
def test_call_incompatible_data(self):
"""Should fail on incompatible mdmap/dm combo and bad perms."""
self.assertRaises(ValueError, self.permanova_plain, -1)
self.permanova_plain.DistanceMatrices = [self.single_ele_dm]
self.assertRaises(ValueError, self.permanova_plain)
class BestTests(TestHelper):
"""Tests for the Best class."""
def setUp(self):
"""Define some useful data to use in testing."""
super(BestTests, self).setUp()
self.bv_dm_88soils_str = ["\tMT2.141698\tCA1.141704\tBB2.141659\t"
"CO2.141657\tTL3.141709\tSN3.141650", "MT2.141698\t0.0\t"
"0.623818643706\t0.750015427505\t0.585201193913\t0.729023583672\t"
"0.622135587669", "CA1.141704\t0.623818643706\t0.0\t0.774881224555"
"\t0.649822398416\t0.777203137034\t0.629507320436", "BB2.141659\t"
"0.750015427505\t0.774881224555\t0.0\t0.688845424001\t0.567470311282"
"\t0.721707516043", "CO2.141657\t0.585201193913\t0.649822398416\t"
"0.688845424001\t0.0\t0.658853575764\t0.661223617505", "TL3.141709\t"
"0.729023583672\t0.777203137034\t0.567470311282\t0.658853575764\t0.0\t"
"0.711173405838", "SN3.141650\t0.622135587669\t0.629507320436\t"
"0.721707516043\t0.661223617505\t0.711173405838\t0.0"]
self.bv_dm_88soils = DistanceMatrix.parseDistanceMatrix(
self.bv_dm_88soils_str)
self.bv_map_88soils_str = ["#SampleId\tTOT_ORG_CARB\tSILT_CLAY\t"
"ELEVATION\tSOIL_MOISTURE_DEFICIT\tCARB_NITRO_RATIO\t"
"ANNUAL_SEASON_TEMP\tANNUAL_SEASON_PRECPT\tPH\tCMIN_RATE\tLONGITUDE\t"
"LATITUDE", "MT2.141698\t39.1\t35\t1000\t70\t23.087\t7\t450\t6.66\t"
"19.7\t-114\t46.8", "CA1.141704\t16.7\t73\t2003\t198\t13\t10.3\t400\t"
"7.27\t2.276\t-111.7666667\t36.05", "BB2.141659\t52.2\t44\t400\t-680\t"
"21.4\t6.1\t1200\t4.6\t2.223\t-68.1\t44.86666667", "CO2.141657\t18.1\t"
"24\t2400\t104\t31.8\t6.1\t350\t5.68\t9.223\t-105.3333333\t"
"40.58333333", "TL3.141709\t53.9\t52\t894\t-212\t24.6\t-9.3\t400\t"
"4.23\t16.456\t-149.5833333\t68.63333333", "SN3.141650\t16.6\t20\t"
"3000\t-252\t13.9\t3.6\t600\t5.74\t6.289\t-118.1666667\t36.45"]
self.bv_map_88soils = MetadataMap.parseMetadataMap(
self.bv_map_88soils_str)
self.cats = ['TOT_ORG_CARB', 'SILT_CLAY', 'ELEVATION',
'SOIL_MOISTURE_DEFICIT', 'CARB_NITRO_RATIO',
'ANNUAL_SEASON_TEMP', 'ANNUAL_SEASON_PRECPT', 'PH',
'CMIN_RATE', 'LONGITUDE', 'LATITUDE']
self.best = Best(self.bv_dm_88soils, self.bv_map_88soils, self.cats)
def test_vector_dist(self):
"""Test the _vector_dist helper method."""
v1 = [1,4,2]
v2 = [-1,12,4]
exp = 8.48528137424
obs = self.best._vector_dist(v1,v2)
self.assertFloatEqual(exp, obs)
v1 = [1,2,100,4,2]
v2 = [-1,12,4,12,99]
exp = 137.087563258
obs = self.best._vector_dist(v1,v2)
self.assertFloatEqual(exp, obs)