Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

test_consolidate.py 3.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Test consolidators
  2. Consolidators work on a List of Faetures, and Features a constructed from Interval objects.
  3. As such, we need to mock an Interval object and a Feature object to test the consolidators.
  4. """
  5. import unittest
  6. from pipeline.consolidators import OverlapConsolidator
  7. class TestOverlapConsolidation(unittest.TestCase):
  8. """overlap consolidator only uses Interval .start and .end attributes, so we can mock them here"""
  9. class MockInterval():
  10. """Mock interval object for overlap testing -- only has start and end attributes"""
  11. def __init__(self, start, end):
  12. self.start = start
  13. self.end = end
  14. class MockFeature():
  15. """Mock feature object for overlap testing -- only has interval attribute"""
  16. def __init__(self, interval):
  17. self.interval = interval
  18. def setUp(self):
  19. """Set up test data"""
  20. self.empty_features = []
  21. self.fakepath = "/tmp/fakepath"
  22. self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  23. self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
  24. self.populated_features = [self.featureA, self.featureB]
  25. def test_init(self):
  26. # test empty features
  27. with self.assertRaises(ValueError):
  28. OverlapConsolidator(self.empty_features)
  29. # test populated features
  30. consolidator = OverlapConsolidator(features=self.populated_features)
  31. self.assertEqual(consolidator.features, self.populated_features)
  32. def test_consolidate(self):
  33. # 4 features for overlap test: ABCD, overlap A-B, C-D
  34. self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  35. self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
  36. self.featureC = self.MockFeature(interval=self.MockInterval(start=20, end=30))
  37. self.featureD = self.MockFeature(interval=self.MockInterval(start=25, end=35))
  38. self.overlapping_features = [self.featureA, self.featureB, self.featureC, self.featureD]
  39. consolidator = OverlapConsolidator(features=self.overlapping_features)
  40. consolidator.consolidate()
  41. # check that A-B and C-D have been merged
  42. self.assertEqual(len(consolidator.features), 2)
  43. # 4 features for no overlap test
  44. self.feature1 = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  45. self.feature2 = self.MockFeature(interval=self.MockInterval(start=15, end=25))
  46. self.feature3 = self.MockFeature(interval=self.MockInterval(start=30, end=40))
  47. self.feature4 = self.MockFeature(interval=self.MockInterval(start=45, end=55))
  48. self.non_overlapping_features = [self.feature1, self.feature2, self.feature3, self.feature4]
  49. consolidator = OverlapConsolidator(features=self.non_overlapping_features)
  50. consolidator.consolidate()
  51. # check that no features have been merged
  52. self.assertEqual(len(consolidator.features), 4)
  53. # 3 features for overlap test: ABC, overlap A-B, B-C
  54. self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  55. self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
  56. self.featureC = self.MockFeature(interval=self.MockInterval(start=12, end=20))
  57. self.overlapping_features = [self.featureA, self.featureB, self.featureC]
  58. consolidator = OverlapConsolidator(features=self.overlapping_features)
  59. consolidator.consolidate()
  60. # check that A-B and B-C have been merged
  61. self.assertEqual(len(consolidator.features), 1)
  62. if __name__ == '__main__':
  63. unittest.main()