Beispiel #1
0
 def test_interval_random_values(self):
   interval = Interval(-5, 15)
   randoms = interval.random_values(5)
   #print(f'{interval}:')
   for value in randoms:
     #print(f'- {value}')
     self.assertTrue(interval.contains(value, inc_upper=False))
Beispiel #2
0
  def test_interval_from_object(self):
    test_interval = Interval(10.5, 20)
    objects = []
    objects.append([10.5, 20])
    objects.append((10.5, 20))
    objects.append({'lower': 10.5, 'upper': 20})

    for object in objects:
      #print(f'{object}')
      self.assertEqual(test_interval, Interval.from_object(object))
Beispiel #3
0
 def test_interval_contains(self):
   interval = Interval(-5, 15)
   self.assertTrue(interval.lower in interval)
   self.assertTrue(interval.upper in interval)
   self.assertTrue(interval.midpoint in interval)
   self.assertTrue((interval.lower + 0.1) in interval)
   self.assertTrue((interval.upper - 0.1) in interval)
   self.assertFalse(interval.contains(interval.lower, inc_lower=False))
   self.assertFalse(interval.contains(interval.upper, inc_upper=False))
   self.assertFalse((interval.lower - 0.1) in interval)
   self.assertFalse((interval.upper + 0.1) in interval)
Beispiel #4
0
  def test_interval_from_intersect(self):
    intervals = [Interval(-x, x) for x in range(5, 1, -1)]
    for i in range(1, len(intervals)):
      intersect = Interval.from_intersect(intervals[0:i+1])
      #print(f'{intersect}')
      self.assertEqual(intervals[i], intersect)

    intervals = [Interval(x, x + 1) for x in range(5)]
    intersect = Interval.from_intersect(intervals)
    #print(f'{intersect}')
    self.assertEqual(None, intersect)
Beispiel #5
0
  def test_create_interval(self):
    test_intervals = []
    test_intervals.append(Interval(10.5, 20))
    test_intervals.append(Interval(20, '10.5'))
    test_intervals.append(Interval(**{'lower': 10.5, 'upper': 20}))
    test_intervals.append(Interval(*('20', '10.5')))

    for interval in test_intervals:
      self.assertEqual(interval.lower, 10.5)
      self.assertEqual(interval.upper, 20)
      self.assertTrue(isinstance(interval.lower, float))
      self.assertTrue(isinstance(interval.upper, float))
Beispiel #6
0
  def test_interval_from_text(self):
    test_interval = Interval(10.5, 20)
    texts = []
    texts.append(('[10.5,20]', 'json'))
    texts.append(('[10.5,20]', 'literal'))
    texts.append(('(10.5,20)', 'literal'))
    texts.append(('{"lower":10.5,"upper":20}', 'json'))
    texts.append(('{"lower":10.5,"upper":20}', 'literal'))
    texts.append(("{'lower':10.5,'upper':20}", 'literal'))

    for text in texts:
      #print(f'text="{text[0]}"' if "'" in text else f"text='{text[0]}'")
      #print(f'format={text[1]}')
      self.assertEqual(test_interval, Interval.from_text(*text))
Beispiel #7
0
 def test_region_from_intervals(self):
     ndimens = 5
     base_interval = Interval(1, 5)
     intervals = [
         Interval(base_interval.lower + d, base_interval.upper + d)
         for d in range(ndimens)
     ]
     region = Region.from_intervals(intervals)
     #print(f'{region}')
     self.assertEqual(region.dimension, ndimens)
     self.assertEqual(region.lower,
                      [base_interval.lower + d for d in range(ndimens)])
     self.assertEqual(region.upper,
                      [base_interval.upper + d for d in range(ndimens)])
     for d, dimension in enumerate(region.dimensions):
         self.assertEqual(dimension, intervals[d])
Beispiel #8
0
    def test_create_region(self):
        test_regions = []
        test_regions.append(Region([0], [5]))
        test_regions.append(Region([0, 0], [5, 5]))
        test_regions.append(Region([0, 5, 0], [5, 0, 5]))

        for i, region in enumerate(test_regions):
            #print(region)
            self.assertEqual(region.dimension, i + 1)
            self.assertEqual(region.dimension, len(region.dimensions))
            self.assertEqual(region.dimension, len(region.lower))
            self.assertEqual(region.dimension, len(region.upper))
            self.assertTrue(
                all([d == Interval(0, 5) for d in region.dimensions]))
            self.assertTrue(all([d == 0 for d in region.lower]))
            self.assertTrue(all([d == 5 for d in region.upper]))
            self.assertTrue(
                all([
                    region.lower[i] <= region.upper[i]
                    for i in range(region.dimension)
                ]))
            self.assertTrue(
                all([
                    region.lower[i] == region[i].lower
                    for i in range(region.dimension)
                ]))
            self.assertTrue(
                all([
                    region.upper[i] == region[i].upper
                    for i in range(region.dimension)
                ]))
Beispiel #9
0
 def test_region_from_interval(self):
     ndimens = 5
     interval = Interval(-5, 5)
     for d in range(1, ndimens):
         region = Region.from_interval(interval, d)
         #print(f'{region}')
         self.assertEqual(region.dimension, d)
         for dimen in region.dimensions:
             self.assertEqual(dimen, interval)
Beispiel #10
0
 def test_interval_hash(self):
   intervals = {}
   for interval in self.test_intervals:
     intervals[interval] = interval.length
   for interval in self.test_intervals:
     other = Interval(interval.lower, interval.upper)
     self.assertTrue(interval in intervals)
     self.assertEqual(interval.length, intervals[interval])
     self.assertTrue(interval == other)
     self.assertTrue(hash(interval) == hash(other))
Beispiel #11
0
 def test_region_project(self):
     dimlimit = 7
     test_interval = Interval(1, 5)
     region = Region([-5, 5, 0], [15, 10, 50])
     for d in range(1, dimlimit):
         new_region = region.project(d, test_interval)
         #print(f'{new_region}')
         self.assertEqual(new_region.dimension, d)
         for i in range(0, d):
             interval = region[i] if i < region.dimension else test_interval
             self.assertEqual(new_region[i], interval)
Beispiel #12
0
    def test_region_getsetitem(self):
        data = {
            'data': 'value',
            'datalist': ['list', 'of', 'items'],
            'dataprop': 'dataprop'
        }
        intervals = [Interval(0, 10)] * 3
        region = Region.from_intervals(intervals, dataprop=data['dataprop'])

        def check_region(region: Region, intervals: List[Interval]):
            self.assertTrue(
                all([
                    region[i] == interval
                    for i, interval in enumerate(intervals)
                ]))
            self.assertEqual(list(map(lambda i: i.lower, intervals)),
                             region.lower)
            self.assertEqual(list(map(lambda i: i.upper, intervals)),
                             region.upper)

        check_region(region, intervals)
        #print(f'{region}')
        #print(f'{region.data}')
        #print(f'{asdict(region)}')
        intervals = [
            Interval(interval.lower, interval.upper + i)
            for i, interval in enumerate(intervals)
        ]
        for i, interval in enumerate(intervals):
            region[i] = interval
        check_region(region, intervals)

        region['data'] = data['data']
        region['datalist'] = data['datalist']
        #print(f'{region}')
        #print(f'{region.data}')
        #print(f'{asdict(region)}')
        self.assertEqual(data['data'], region['data'])
        self.assertEqual(data['datalist'], region['datalist'])
        self.assertEqual(data['dataprop'], region['dataprop'])
        self.assertDictEqual(data, region.data)
Beispiel #13
0
 def test_interval_random_interval(self):
   interval = Interval(-5, 15)
   randoms  = interval.random_intervals(5, Interval(0.25, 0.75))
   randoms += interval.random_intervals(5, Interval(0.25, 0.75), precision=0)
   #print(f'{interval}:')
   for subinterval in randoms:
     #print(f'- {subinterval}')
     self.assertTrue(subinterval in interval)
Beispiel #14
0
  def test_interval_encloses(self):
    interval = Interval(-5, 5)
    test_intervals = []
    test_intervals.append(Interval(-6, 4))
    test_intervals.append(Interval(-4, 4))
    test_intervals.append(Interval(-2, 2))
    test_intervals.append(Interval(4, 6))
    test_intervals.append(Interval(-5, 5))
    test_intervals.append(Interval(-6, 6))

    for subinterval in test_intervals:
      comparsion = interval.lower <= subinterval.lower <= subinterval.upper <= interval.upper
      #print(f'{subinterval} in {interval}: expect={comparsion} actual={subinterval in interval}')
      self.assertEqual(subinterval in interval, comparsion)
Beispiel #15
0
  def test_interval_intersect(self):
    for i, first in enumerate(self.test_intervals):
      for j, second in enumerate(self.test_intervals):
        intersect = first.intersect(second)
        if self.overlaps[i][j]:
          expected = first if first == second \
                           else Interval(max(first.lower, second.lower),
                                         min(first.upper, second.upper))

          #print(f'{first} and {second}:')
          #print(f'  expect={expected}')
          #print(f'  actual={intersect}')
          #print(f'  length={intersect.length}')
          self.assertEqual(intersect, expected)
        else:
          #print(f'{first} and {second}:')
          #print(f'  expect=None')
          #print(f'  actual={intersect}')
          self.assertEqual(intersect, None)
Beispiel #16
0
 def setUp(self):
   self.test_intervals = []
   self.test_intervals.append(Interval(-10, 25))
   self.test_intervals.append(Interval(10.5, 45))
   self.test_intervals.append(Interval(-10.5, 45))
   self.test_intervals.append(Interval(10.5, -45))
   self.test_intervals.append(Interval(-10.5, -45))
   self.test_intervals.append(Interval(5, 5))
   self.test_intervals.append(Interval(5, 6))
   self.overlaps = [
     [True,  True,  True,  True,  False, True,  True],
     [True,  True,  True,  False, False, False, False],
     [True,  True,  True,  True,  False, True,  True],
     [True,  False, True,  True,  True,  True,  True],
     [False, False, False, True,  True,  False, False],
     [True,  False, True,  True,  False, True,  False],
     [True,  False, True,  True,  False, False, True]
   ]
Beispiel #17
0
 def test_region_properties(self):
     for region in self.test_regions:
         #print('\n  '.join([
         #  f'{region}:',
         #  f'dimensions={[astuple(d) for d in region.dimensions]}',
         #  f'lengths={region.lengths}',
         #  f'midpoint={region.midpoint}',
         #  f'size={region.size}'
         #]))
         self.assertEqual(region.dimensions, [
             Interval(region.lower[i], region.upper[i])
             for i in range(region.dimension)
         ])
         self.assertEqual(region.lengths,
                          [d.upper - d.lower for d in region.dimensions])
         self.assertEqual(
             region.midpoint,
             [mean([d.lower, d.upper]) for d in region.dimensions])
         self.assertEqual(region.size,
                          reduce(lambda x, y: x * y, region.lengths))
Beispiel #18
0
 def test_interval_from_union(self):
   intervals = [Interval(x, x + 1) for x in range(5)]
   for i in range(1, len(intervals)):
     union = Interval.from_union(intervals[0:i+1])
     #print(f'{union}')
     self.assertEqual(union, Interval(0, i + 1))