您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_consolidate.py 3.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Test consolidators
  2. Consolidators work on a List of Features, and Features are 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. def move_end(self, new_end):
  15. """Mocked method to move the end of the interval"""
  16. self.end = new_end
  17. class MockFeature():
  18. """Mock feature object for overlap testing -- only has interval attribute"""
  19. def __init__(self, interval):
  20. self.interval = interval
  21. def setUp(self):
  22. """Set up test data"""
  23. self.empty_features = []
  24. self.fakepath = "/tmp/fakepath"
  25. self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  26. self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
  27. self.populated_features = [self.featureA, self.featureB]
  28. def test_init(self):
  29. # test empty features
  30. with self.assertRaises(ValueError):
  31. OverlapConsolidator(self.empty_features)
  32. # test populated features
  33. consolidator = OverlapConsolidator(features=self.populated_features)
  34. self.assertEqual(consolidator.features, self.populated_features)
  35. def test_consolidate(self):
  36. # 4 features for overlap test: ABCD, overlap A-B, C-D (2)
  37. self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  38. self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
  39. self.featureC = self.MockFeature(interval=self.MockInterval(start=20, end=30))
  40. self.featureD = self.MockFeature(interval=self.MockInterval(start=25, end=35))
  41. self.overlapping_features = [self.featureA, self.featureB, self.featureC, self.featureD]
  42. consolidator = OverlapConsolidator(features=self.overlapping_features)
  43. consolidator.consolidate()
  44. # check that A-B and C-D have been merged
  45. self.assertEqual(len(consolidator.features), 2)
  46. # 4 features for no overlap test (4)
  47. self.feature1 = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  48. self.feature2 = self.MockFeature(interval=self.MockInterval(start=15, end=25))
  49. self.feature3 = self.MockFeature(interval=self.MockInterval(start=30, end=40))
  50. self.feature4 = self.MockFeature(interval=self.MockInterval(start=45, end=55))
  51. self.non_overlapping_features = [self.feature1, self.feature2, self.feature3, self.feature4]
  52. consolidator = OverlapConsolidator(features=self.non_overlapping_features)
  53. consolidator.consolidate()
  54. # check that no features have been merged
  55. self.assertEqual(len(consolidator.features), 4)
  56. # 3 features for overlap test: ABC, overlap A-B, B-C (1)
  57. self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
  58. self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
  59. self.featureC = self.MockFeature(interval=self.MockInterval(start=12, end=20))
  60. self.overlapping_features = [self.featureA, self.featureB, self.featureC]
  61. consolidator = OverlapConsolidator(features=self.overlapping_features)
  62. consolidator.consolidate()
  63. # check that A-B and B-C have been merged
  64. self.assertEqual(len(consolidator.features), 1)
  65. if __name__ == '__main__':
  66. unittest.main()