def test_union(): assert I.closed(1, 2).to_atomic() | I.closed(1, 2).to_atomic() == I.closed(1, 2).to_atomic() assert I.closed(1, 4).to_atomic() | I.closed(2, 3).to_atomic() == I.closed(1, 4).to_atomic() assert I.closed(1, 2).to_atomic() | I.closed(2, 3).to_atomic() == I.closed(2, 3).to_atomic() | I.closed(1, 2).to_atomic() assert I.closed(1, 2).to_atomic() | I.closed(3, 4).to_atomic() == I.closed(1, 2) | I.closed(3, 4) assert I.closed(1, 2) | I.closed(1, 2) == I.closed(1, 2) assert I.closed(1, 4) | I.closed(2, 3) == I.closed(2, 3) | I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3) == I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3).to_atomic() == I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3).to_atomic() == I.closed(2, 3).to_atomic() | I.closed(1, 4) assert I.closed(1, 2) | I.open(2, 3) == I.closedopen(1, 3) assert I.closed(1, 3) | I.closed(2, 4) == I.closed(1, 4) assert I.closed(1, 2) | I.closed(2, 3) == I.closed(2, 3) | I.closed(1, 2) assert I.closedopen(1, 2) | I.closed(2, 3) == I.closed(1, 3) assert I.open(1, 2) | I.closed(2, 4) == I.openclosed(1, 4) assert I.closed(1, 2) | I.closed(3, 4) != I.closed(1, 4) assert (I.closed(1, 2) | I.closed(3, 4) | I.closed(2, 3)).is_atomic() assert I.closed(1, 2) | I.closed(3, 4) | I.closed(2, 3) == I.closed(1, 4) assert I.closed(1, 2) | I.closed(0, 4) == I.closed(0, 4) assert (I.closed(0, 1) | I.closed(2, 3) | I.closed(1, 2)).is_atomic() assert I.closed(0, 1) | I.closed(2, 3) | I.closed(1, 2) == I.closed(0, 3) assert I.closed(0, 1) | I.empty() == I.closed(0, 1)
def test_apply(): i = I.closed(0, 1) assert i.apply(lambda s: s) == i assert i.apply(lambda s: (False, -1, 2, False)) == I.open(-1, 2) assert i.apply(lambda s: I.AtomicInterval(False, -1, 2, False)) == I.open( -1, 2) assert i.apply(lambda s: I.open(-1, 2)) == I.open(-1, 2) i = I.closed(0, 1) | I.closed(2, 3) assert i.apply(lambda s: s) == i assert i.apply(lambda s: (False, -1, 2, False)) == I.open(-1, 2) assert i.apply(lambda s: (not s.left, s.lower - 1, s.upper - 1, not s.right )) == I.open(-1, 0) | I.open(1, 2) assert i.apply(lambda s: I.AtomicInterval(False, -1, 2, False)) == I.open( -1, 2) assert i.apply(lambda s: I.open(-1, 2)) == I.open(-1, 2) assert i.apply(lambda s: (s.left, s.lower, s.upper * 2, s.right)) == I.closed(0, 6) assert I.empty().apply(lambda s: (I.CLOSED, 1, 2, I.CLOSED)) == I.closed( 1, 2) with pytest.raises(TypeError): i.apply(lambda s: None) with pytest.raises(TypeError): i.apply(lambda s: 'unsupported')
def test_creation(): assert I.closed(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.CLOSED) assert I.open(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.OPEN) assert I.openclosed(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.CLOSED) assert I.closedopen(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.OPEN) assert I.closed(-I.inf, I.inf) == I.open(-I.inf, I.inf) assert I.singleton(2) == I.closed(2, 2) assert I.Interval() == I.open(0, 0) assert I.empty() == I.Interval() assert I.closed(3, -3) == I.empty() assert I.openclosed(3, 3) == I.empty() # I.empty() is a singleton assert I.empty() is I.empty() assert I.Interval(I.closed(0, 1).to_atomic()) == I.closed(0, 1) assert I.Interval(I.closed(0, 1)) == I.closed(0, 1) assert I.Interval(I.closed(0, 1).to_atomic(), I.closed(2, 3)) == I.closed(0, 1) | I.closed(2, 3) assert I.Interval(I.closed(0, 1) | I.closed(2, 3)) == I.closed(0, 1) | I.closed(2, 3) with pytest.raises(TypeError): I.Interval(1)
def test_intersection(): assert I.closed(0, 1) & I.closed(0, 1) == I.closed(0, 1) assert I.closed(0, 1) & I.closed(0, 1).to_atomic() == I.closed(0, 1) assert I.closed(0, 1) & I.open(0, 1) == I.open(0, 1) assert I.openclosed(0, 1) & I.closedopen(0, 1) == I.open(0, 1) assert (I.closed(0, 1) & I.closed(2, 3)).is_empty() assert I.closed(0, 1) & I.empty() == I.empty()
def interval_contains(a, b): """Condition used to check if an a touches b and partially covers it""" dimensions = len(a) partial = all([ (I.closed(*a[dimension]) & I.open(*b[dimension])).is_empty() == False if not I.open(*b[dimension]).is_empty() else (I.closed(*a[dimension]) & I.closed(*b[dimension])).is_empty() == False for dimension in range(dimensions) ]) return partial
def test_containment_for_atomic_intervals(): # AtomicIntervals assert I.closed(1, 2) in I.closed(0, 3).to_atomic() assert I.closed(1, 2) in I.closed(1, 2).to_atomic() assert I.open(1, 2) in I.closed(1, 2).to_atomic() assert I.closed(1, 2) not in I.open(1, 2).to_atomic() assert I.closed(0, 1) not in I.closed(1, 2).to_atomic() assert I.closed(0, 2) not in I.closed(1, 3).to_atomic() assert I.closed(-I.inf, I.inf) in I.closed(-I.inf, I.inf).to_atomic() assert I.closed(0, 1) in I.closed(-I.inf, I.inf).to_atomic() assert I.closed(-I.inf, I.inf) not in I.closed(0, 1).to_atomic()
def test_creation(): assert I.closed(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.CLOSED) assert I.open(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.OPEN) assert I.openclosed(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.CLOSED) assert I.closedopen(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.OPEN) assert I.closed(-I.inf, I.inf) == I.open(-I.inf, I.inf) with pytest.raises(ValueError): I.closed(1, -1) assert I.singleton(2) == I.closed(2, 2) assert I.Interval() == I.open(0, 0) assert I.empty() == I.Interval()
def test_replace_atomic(): i = I.closed(0, 1).to_atomic() assert i.replace() == i assert i.replace(I.OPEN, 2, 3, I.OPEN) == I.open(2, 3) assert i.replace(upper=2, left=I.OPEN) == I.openclosed(0, 2) assert i.replace(lower=lambda v: 1 + v) == I.singleton(1) assert i.replace(left=lambda v: not v, right=lambda v: not v) == I.open(0, 1) assert I.empty().to_atomic().replace(left=I.CLOSED, right=I.CLOSED) == I.empty() assert I.empty().to_atomic().replace(lower=1, upper=2) == I.open(1, 2) assert I.empty().to_atomic().replace(lower=lambda v: 1, upper=lambda v: 2) == I.empty() assert I.empty().to_atomic().replace(lower=lambda v: 1, upper=lambda v: 2, ignore_inf=False) == I.open(1, 2)
def computeDistancePointToSegment(q,p1,p2): #from homework1 with a little adjustment a,b,c=computeLineThroughTwoPoints(p1,p2) if a==0: #horizontal line x_inter=q[0] y_inter=p1[1] D_to_line=abs(y_inter-q[1]) D_to_p1=mt.sqrt(np.square(q[0]-p1[0])+np.square(q[1]-p1[1])) D_to_p2=mt.sqrt(np.square(q[0]-p2[0])+np.square(q[1]-p2[1])) p_min=min(p1[0],p2[0]) p_max=max(p1[0],p2[0]) if x_inter in I.open(p_min,p_max): return (D_to_line,0) else: if D_to_p1<D_to_p2: return (D_to_p1,1) else: return (D_to_p2,2) if b==0:#vertical line x_inter=p1[0] y_inter=q[1] D_to_line=abs(x_inter-q[0]) D_to_p1=mt.sqrt(np.square(q[0]-p1[0])+np.square(q[1]-p1[1])) D_to_p2=mt.sqrt(np.square(q[0]-p2[0])+np.square(q[1]-p2[1])) p_min=min(p1[1],p2[1]) p_max=max(p1[1],p2[1]) if y_inter in I.open(p_min,p_max): return (D_to_line,0) else: if D_to_p1<D_to_p2: return (D_to_p1,1) else: return (D_to_p2,2) #The last situation,a common line slope1=-a/b intersect1=-c/b slope2=b/a intersect2=q[1]-slope2*q[0] x_inter=(intersect2-intersect1)/(slope1-slope2) y_inter=x_inter*slope2+intersect2 D_to_line=mt.sqrt(np.square(x_inter-q[0])+np.square(y_inter-q[1])) D_to_p1=mt.sqrt(np.square(q[0]-p1[0])+np.square(q[1]-p1[1])) D_to_p2=mt.sqrt(np.square(q[0]-p2[0])+np.square(q[1]-p2[1])) p_min=min(p1[0],p2[0]) p_max=max(p1[0],p2[0]) if x_inter in I.open(p_min,p_max): return (D_to_line,0) else: if D_to_p1<D_to_p2: return (D_to_p1,1) else: return (D_to_p2,2)
class Config: DEBUG = True C_TESS_MODE = False TESSERACT_PATH = "/usr/share/tesseract-ocr/4.00/" if platform.platform( terse=True).startswith("Linux") else "C:\\Program Files\\Tesseract-OCR" TESSERACT_DATA_PATH = os.path.join(TESSERACT_PATH, "tessdata") TESSERACT_LIB = [ os.path.join(TESSERACT_PATH, "libtesseract-5.dll"), "libtesseract.so" ] TEXT_OCR_CONFIGS = ["arknights"] HSV_V_THRESHOLD = 150 HSV_V_TEXT_THRESHOLD = 150 GRAY_THRESHOLD = 135 LINES_MERGE_BLUR = 10 OCR_RESIZE = 1.2 COL_EDGE_INTERVAL = intervals.open(-intervals.inf, -20000 // 920) | intervals.open( 40000 // 920, intervals.inf) LINE_EDGE_INTERVAL = intervals.open(-intervals.inf, -200000 // 2160) | intervals.open( 210000 // 2160, 400000 // 2160) WORD_EDGE_INTERVAL = intervals.open(-intervals.inf, -0.5) | intervals.open( 0.5, intervals.inf) WORD_PROPORTION_INTERVAL = intervals.open(0.75, 1.2) LINE_EDGE_RANGE = intervals.open(0.1, 0.7) CELL_TEXT_REGION = ((396 / 442, 434 / 442), (20 / 203, None)) CELL_LEVEL_REGION = ( (351 / 442, 398 / 442), (17 / 203, 87 / 203)) # ((0, 0), (10, 10)) CELL_PHASE_REGION = ((264 / 442, 322 / 442), (34 / 203, 72 / 203))
def test_replace(): i = I.open(-I.inf, I.inf) assert i.replace(lower=lambda v: 1, upper=lambda v: 1) == I.open(-I.inf, I.inf) assert i.replace(lower=lambda v: 1, upper=lambda v: 2, ignore_inf=False) == I.open(1, 2) assert I.empty().replace(left=I.CLOSED, right=I.CLOSED) == I.empty() assert I.empty().replace(lower=1, upper=2) == I.open(1, 2) assert I.empty().replace(lower=lambda v: 1, upper=lambda v: 2) == I.empty() assert I.empty().replace(lower=lambda v: 1, upper=lambda v: 2, ignore_inf=False) == I.open(1, 2) i = I.closed(0, 1) | I.open(2, 3) assert i.replace() == i assert i.replace(I.OPEN, -1, 4, I.OPEN) == I.openclosed(-1, 1) | I.open(2, 4) assert i.replace(lower=2) == I.closedopen(2, 3) assert i.replace(upper=1) == I.closedopen(0, 1) assert i.replace(lower=5) == I.empty() assert i.replace(upper=-5) == I.empty() assert i.replace( left=lambda v: not v, lower=lambda v: v - 1, upper=lambda v: v + 1, right=lambda v: not v) == I.openclosed(-1, 1) | I.openclosed(2, 4) assert I.empty().replace(lower=2, upper=4) == I.open(2, 4)
def test_overlaps_permissive(): assert I.closed(0, 1).overlaps(I.closed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.open(0, 1), permissive=True) assert I.open(0, 1).overlaps(I.closed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.openclosed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.closedopen(0, 1), permissive=True) assert I.closed(1, 2).overlaps(I.closed(2, 3), permissive=True) assert I.closed(1, 2).overlaps(I.closedopen(2, 3), permissive=True) assert I.openclosed(1, 2).overlaps(I.closed(2, 3), permissive=True) assert I.openclosed(1, 2).overlaps(I.closedopen(2, 3), permissive=True) assert not I.closed(0, 1).overlaps(I.closed(3, 4), permissive=True) assert not I.closed(3, 4).overlaps(I.closed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.open(1, 2), permissive=True) assert I.closed(0, 1).overlaps(I.openclosed(1, 2), permissive=True) assert I.closedopen(0, 1).overlaps(I.closed(1, 2), permissive=True) assert I.closedopen(0, 1).overlaps(I.closedopen(1, 2), permissive=True) assert not I.closedopen(0, 1).overlaps(I.openclosed(1, 2), permissive=True) assert not I.closedopen(0, 1).overlaps(I.open(1, 2), permissive=True) assert not I.open(0, 1).overlaps(I.open(1, 2), permissive=True) assert not I.empty().overlaps(I.open(-I.inf, I.inf), permissive=True) assert not I.open(-I.inf, I.inf).overlaps(I.empty(), permissive=True)
def test_overlaps(): # Overlaps should reject non supported types with pytest.raises(TypeError): I.closed(0, 1).to_atomic().overlaps(1) with pytest.raises(TypeError): I.closed(0, 1).overlaps(1) assert I.closed(0, 1).overlaps(I.closed(0, 1)) assert I.closed(0, 1).overlaps(I.open(0, 1)) assert I.open(0, 1).overlaps(I.closed(0, 1)) assert I.closed(0, 1).overlaps(I.openclosed(0, 1)) assert I.closed(0, 1).overlaps(I.closedopen(0, 1)) assert I.closed(1, 2).overlaps(I.closed(2, 3)) assert I.closed(1, 2).overlaps(I.closedopen(2, 3)) assert I.openclosed(1, 2).overlaps(I.closed(2, 3)) assert I.openclosed(1, 2).overlaps(I.closedopen(2, 3)) assert not I.closed(0, 1).overlaps(I.closed(3, 4)) assert not I.closed(3, 4).overlaps(I.closed(0, 1)) assert not I.closed(0, 1).overlaps(I.open(1, 2)) assert not I.closed(0, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.closed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.closedopen(1, 2)) assert not I.closedopen(0, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.open(1, 2)) assert not I.open(0, 1).overlaps(I.open(1, 2)) assert not I.empty().overlaps(I.open(-I.inf, I.inf)) assert not I.open(-I.inf, I.inf).overlaps(I.empty())
def test_union(): assert I.closed(1, 2).to_atomic() | I.closed(1, 2).to_atomic() == I.closed(1, 2).to_atomic() assert I.closed(1, 4).to_atomic() | I.closed(2, 3).to_atomic() == I.closed(1, 4).to_atomic() assert I.closed(1, 2).to_atomic() | I.closed(2, 3).to_atomic() == I.closed(2, 3).to_atomic() | I.closed(1, 2).to_atomic() assert I.closed(1, 2).to_atomic() | I.closed(3, 4).to_atomic() == I.closed(1, 2) | I.closed(3, 4) assert I.closed(1, 2) | I.closed(1, 2) == I.closed(1, 2) assert I.closed(1, 4) | I.closed(2, 3) == I.closed(2, 3) | I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3) == I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3).to_atomic() == I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3).to_atomic() == I.closed(2, 3).to_atomic() | I.closed(1, 4) assert I.closed(1, 2) | I.open(2, 3) == I.closedopen(1, 3) assert I.closed(1, 3) | I.closed(2, 4) == I.closed(1, 4) assert I.closed(1, 2) | I.closed(2, 3) == I.closed(2, 3) | I.closed(1, 2) assert I.closedopen(1, 2) | I.closed(2, 3) == I.closed(1, 3) assert I.open(1, 2) | I.closed(2, 4) == I.openclosed(1, 4) assert I.closed(1, 2) | I.closed(3, 4) != I.closed(1, 4) assert (I.closed(1, 2) | I.closed(3, 4) | I.closed(2, 3)).is_atomic() assert I.closed(1, 2) | I.closed(3, 4) | I.closed(2, 3) == I.closed(1, 4) assert I.closed(1, 2) | I.closed(0, 4) == I.closed(0, 4) assert (I.closed(0, 1) | I.closed(2, 3) | I.closed(1, 2)).is_atomic() assert I.closed(0, 1) | I.closed(2, 3) | I.closed(1, 2) == I.closed(0, 3) assert I.closed(0, 1) | I.empty() == I.closed(0, 1) # https://github.com/AlexandreDecan/python-intervals/issues/12 assert I.open(0, 2) | I.closed(0, 2) == I.closed(0, 2) assert I.open(0, 2) | I.closed(1, 2) == I.openclosed(0, 2) assert I.open(0, 2) | I.closed(0, 1) == I.closedopen(0, 2) assert I.closed(0, 2) | I.open(0, 2) == I.closed(0, 2) assert I.closed(1, 2) | I.open(0, 2) == I.openclosed(0, 2) assert I.closed(0, 1) | I.open(0, 2) == I.closedopen(0, 2) assert I.closed(0, 2) | I.singleton(2) == I.closed(0, 2) assert I.closedopen(0, 2) | I.singleton(2) == I.closed(0, 2) assert I.openclosed(0, 2) | I.singleton(2) == I.openclosed(0, 2) assert I.openclosed(0, 2) | I.singleton(0) == I.closed(0, 2) assert I.singleton(2) | I.closed(0, 2) == I.closed(0, 2) assert I.singleton(2) | I.closedopen(0, 2) == I.closed(0, 2) assert I.singleton(2) | I.openclosed(0, 2) == I.openclosed(0, 2) assert I.singleton(0) | I.openclosed(0, 2) == I.closed(0, 2) # https://github.com/AlexandreDecan/python-intervals/issues/13 assert I.closed(1, 1) | I.openclosed(1, 2) == I.closed(1, 2) assert I.openclosed(1, 2) | I.closed(1, 1) == I.closed(1, 2) assert I.closed(0, 1) | I.openclosed(1, 2) == I.closed(0, 2) assert I.openclosed(1, 2) | I.closed(0, 1) == I.closed(0, 2) assert I.openclosed(1, 2) | I.closed(1, 1) == I.closed(1, 2) assert I.closed(1, 1) | I.openclosed(1, 2) == I.closed(1, 2) assert I.openclosed(1, 2) | I.closed(0, 1) == I.closed(0, 2) assert I.closed(0, 1) | I.openclosed(1, 2) == I.closed(0, 2)
def test_containment_for_intervals(): # Intervals assert I.closed(1, 2) in I.closed(0, 3) assert I.closed(1, 2) in I.closed(1, 2) assert I.open(1, 2) in I.closed(1, 2) assert I.closed(1, 2) not in I.open(1, 2) assert I.closed(0, 1) not in I.closed(1, 2) assert I.closed(0, 2) not in I.closed(1, 3) assert I.closed(-I.inf, I.inf) in I.closed(-I.inf, I.inf) assert I.closed(0, 1) in I.closed(-I.inf, I.inf) assert I.closed(-I.inf, I.inf) not in I.closed(0, 1) assert I.empty() in I.closed(0, 3) assert I.empty() in I.empty() assert I.closed(0, 0) not in I.empty()
def test_overlaps(): assert I.closed(0, 1).overlaps(I.closed(0, 1)) assert I.closed(0, 1).overlaps(I.open(0, 1)) assert I.closed(0, 1).overlaps(I.closed(1, 2)) assert not I.closed(0, 1).overlaps(I.closed(3, 4)) assert not I.closed(0, 1).overlaps(I.open(1, 2)) assert I.closed(0, 1).overlaps(I.closed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.open(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.closed(1, 2), permissive=True) assert not I.closed(0, 1).overlaps(I.closed(3, 4), permissive=True) assert I.closed(0, 1).overlaps(I.open(1, 2), permissive=True) # Overlaps should reject non supported types with pytest.raises(TypeError): I.closed(0, 1).to_atomic().overlaps(1)
def test_from_string_customized(): i1, i2, i3, i4 = '<"0"-"1">', '<!"0"-"1">', '<"0"-"1"!>', '<!"0"-"1"!>' params = { 'conv': lambda s: int(s[1:-1]), 'disj': ' or ', 'sep': '-', 'left_open': '<!', 'left_closed': '<', 'right_open': '!>', 'right_closed': '>', 'pinf': r'\+oo', 'ninf': '-oo', } assert I.from_string(i1, **params) == I.closed(0, 1) assert I.from_string(i2, **params) == I.openclosed(0, 1) assert I.from_string(i3, **params) == I.closedopen(0, 1) assert I.from_string(i4, **params) == I.open(0, 1) assert I.from_string('<!!>', **params) == I.empty() assert I.from_string('<"1">', **params) == I.singleton(1) assert I.from_string('<!-oo-"1">', **params) == I.openclosed(-I.inf, 1) assert I.from_string('<"1"-+oo!>', **params) == I.closedopen(1, I.inf) assert I.from_string('<"0"-"1"> or <"2"-"3">', **params) == I.closed(0, 1) | I.closed(2, 3)
def test_to_string_customized(): i1, i2, i3, i4 = I.closed(0, 1), I.openclosed(0, 1), I.closedopen(0, 1), I.open(0, 1) params = { 'disj': ' or ', 'sep': '-', 'left_open': '<!', 'left_closed': '<', 'right_open': '!>', 'right_closed': '>', 'conv': lambda s: '"{}"'.format(s), 'pinf': '+oo', 'ninf': '-oo', } assert I.to_string(i1, **params) == '<"0"-"1">' assert I.to_string(i2, **params) == '<!"0"-"1">' assert I.to_string(i3, **params) == '<"0"-"1"!>' assert I.to_string(i4, **params) == '<!"0"-"1"!>' assert I.to_string(I.empty(), **params) == '<!!>' assert I.to_string(I.singleton(1), **params) == '<"1">' assert I.to_string(I.openclosed(-I.inf, 1), **params) == '<!-oo-"1">' assert I.to_string(I.closedopen(1, I.inf), **params) == '<"1"-+oo!>' assert I.to_string(I.closed(0, 1) | I.closed(2, 3), **params) == '<"0"-"1"> or <"2"-"3">'
def newInterval(left, right, lowerClose, upperClose): ##新建一个区间 if (left == '-inf'): left = -I.inf if (right == 'inf'): right = I.inf zoom = I.open(left, right) return zoom
def test_emptiness(): assert I.openclosed(1, 1).is_empty() assert I.closedopen(1, 1).is_empty() assert I.open(1, 1).is_empty() assert not I.closed(1, 1).is_empty() assert I.Interval().is_empty() assert I.empty().is_empty()
def get_curr_block(tn): """Encapsulated function to determine concurrent block and total blocked distance of a train: (at each moment) the accumulative distance traveled by a train. the current block number of a train. Function or returns: Update the (curr_block) dictionary curr_block : dictionary Key: train number 'tn' Value: current block number for this train Notes: ------ Calculate distance[tn] first, and then update the two dictionaries. coord_mp[tn] is the current distance from the origin of coordinates. To-do: ------ The relationships and logics need to be normalized to eliminate the more than/less than signs. Merge two directions into one single judgment logic. """ # if (coord_mp > block_begin), block go further; if (coord_mp < block_end), block go close. if tn == 0: # avoid initializing train number 0 (int) in the default dictionaries. # who? pass else: if self.curr_mp[tn] not in self.blk_boundaries: for i in range(len(self.blk_interval)): if self.blk_interval[i][0] < self.curr_mp[ tn] < self.blk_interval[i][1]: self.curr_occupy[tn] = I.open( self.blk_interval[i][0], self.blk_interval[i][1]) self.curr_block[tn] = i else: if self.curr_spd[tn] != 0: idx = self.blk_boundaries.index(self.curr_mp[tn]) self.curr_occupy[tn] = I.open(blk_interval[i - 1][0], blk_interval[i][1]) self.curr_block[tn] = sorted([i - 1, i]) if self.curr_spd[tn] == 0: pass
def build_interval_draft4(self): if self.exclusiveMinimum and self.exclusiveMaximum: self.interval = I.open(self.minimum, self.maximum) elif self.exclusiveMinimum: self.interval = I.openclosed(self.minimum, self.maximum) elif self.exclusiveMaximum: self.interval = I.closedopen(self.minimum, self.maximum) else: self.interval = I.closed(self.minimum, self.maximum)
def test_containment_for_values(): # Values assert 1 in I.closed(0, 2) assert 1 in I.closed(1, 2) assert 1 in I.closed(0, 1) assert 1 in I.open(0, 2) assert 1 not in I.open(0, 1) assert 1 not in I.open(1, 2) assert 1 in I.closed(-I.inf, I.inf) assert 1 in I.closed(-I.inf, 1) assert 1 in I.closed(1, I.inf) assert 1 not in I.closed(-I.inf, 0) assert 1 not in I.closed(2, I.inf) assert 1 not in I.empty() assert I.inf not in I.empty() assert -I.inf not in I.empty()
def from_cond_and_range(min, max, cond): if cond.op is Op.eq: if min <= cond.threshold <= max: interval = NodeResultRange.EqInterval else: interval = intervals.empty() elif cond.op is Op.lt: if cond.threshold <= max: interval = intervals.open(min, cond.threshold) else: interval = intervals.open(min, max) elif cond.op is Op.gt: if cond.threshold >= min: interval = intervals.open(cond.threshold, max) else: interval = intervals.open(min, max) else: raise NotImplementedError( 'Unimplemented comparison {0}'.format(op)) return NodeResultRange(interval, cond.feature, min, max)
def test_to_string(): i1, i2, i3, i4 = I.closed(0, 1), I.openclosed(0, 1), I.closedopen(0, 1), I.open(0, 1) assert I.to_string(i1) == '[0,1]' assert I.to_string(i2) == '(0,1]' assert I.to_string(i3) == '[0,1)' assert I.to_string(i4) == '(0,1)' assert I.to_string(I.empty()) == '()' assert I.to_string(I.singleton(1)) == '[1]' assert I.to_string(I.closed(0, 1) | I.closed(2, 3)) == '[0,1] | [2,3]'
def test_iterate(): # Default parameters assert list(I.iterate(I.closed(0, 2), incr=1)) == [0, 1, 2] assert list(I.iterate(I.closedopen(0, 2), incr=1)) == [0, 1] assert list(I.iterate(I.openclosed(0, 2), incr=1)) == [1, 2] assert list(I.iterate(I.open(0, 2), incr=1)) == [1] assert list(I.iterate(I.open(0, 2.5), incr=1)) == [1, 2] # Empty intervals or iterations assert list(I.iterate(I.empty(), incr=1)) == [] assert list(I.iterate(I.open(0, 1), incr=1)) == [] # Infinities with pytest.raises(ValueError): list(I.iterate(I.openclosed(-I.inf, 2), incr=1)) gen = I.iterate(I.closedopen(0, I.inf), incr=1) assert next(gen) == 0 assert next(gen) == 1 assert next(gen) == 2 # and so on # Unions assert list(I.iterate(I.closed(0, 1) | I.closed(5, 6), incr=1)) == [0, 1, 5, 6] assert list(I.iterate(I.closed(0, 1) | I.closed(2.5, 4), incr=1)) == [0, 1, 2.5, 3.5] assert list(I.iterate(I.open(0, 1) | I.open(1, 2), incr=1)) == [] assert list(I.iterate(I.open(0.5, 1) | I.open(1, 3), incr=1)) == [2] # Step assert list(I.iterate(I.closed(0, 6), incr=2)) == [0, 2, 4, 6] assert list(I.iterate(I.closed(0, 6), incr=4)) == [0, 4] assert list(I.iterate(I.closed(0, 6), incr=lambda x: x + 2)) == [0, 2, 4, 6] # Base assert list(I.iterate(I.closed(0.4, 2), incr=1, base=lambda x: round(x))) == [1, 2] assert list(I.iterate(I.closed(0.6, 2), incr=1, base=lambda x: round(x))) == [1, 2] # Reversed assert list(I.iterate(I.closed(0, 1), incr=1, reverse=True)) == [1, 0] assert list(I.iterate(I.open(0, 3), incr=1, reverse=True)) == [2, 1] assert list(I.iterate(I.closed(0, 1), incr=0.5, reverse=True)) == [1, 0.5, 0] assert list(I.iterate(I.closed(0, 2), incr=1, base=lambda x: x-1, reverse=True)) == [1, 0] assert list(I.iterate(I.closed(0, 2) | I.closed(4, 5), incr=1, reverse=True)) == [5, 4, 2, 1, 0] with pytest.raises(ValueError): list(I.iterate(I.closedopen(0, I.inf), incr=1, reverse=True)) gen = I.iterate(I.openclosed(-I.inf, 2), incr=1, reverse=True) assert next(gen) == 2 assert next(gen) == 1 assert next(gen) == 0 # and so on
def test_complement(): assert ~I.closed(1, 2) == I.open(-I.inf, 1) | I.open(2, I.inf) assert ~I.open(1, 2) == I.openclosed(-I.inf, 1) | I.closedopen(2, I.inf) intervals = [I.closed(0, 1), I.open(0, 1), I.openclosed(0, 1), I.closedopen(0, 1)] for interval in intervals: assert ~(~interval) == interval assert ~I.open(1, 1) == I.open(-I.inf, I.inf) assert (~I.closed(-I.inf, I.inf)).is_empty() assert ~I.empty() == I.open(-I.inf, I.inf)
def test_interval_to_atomic(): intervals = [I.closed(0, 1), I.open(0, 1), I.openclosed(0, 1), I.closedopen(0, 1)] for interval in intervals: assert interval == I.Interval(interval.to_atomic()) assert interval == interval.to_atomic() assert I.closed(0, 1) | I.closed(2, 3) != I.closed(0, 3) assert (I.closed(0, 1) | I.closed(2, 3)).to_atomic() == I.closed(0, 3) assert I.closed(0, 1).to_atomic() == I.closed(0, 1).enclosure() assert (I.closed(0, 1) | I.closed(2, 3)).enclosure() == I.closed(0, 3) assert I.empty().to_atomic() == I.AtomicInterval(False, I.inf, -I.inf, False)
def test_overlaps_adjacent(): # Check warnings for "permissive" with pytest.warns(DeprecationWarning, match='permissive'): I.closed(0, 1).to_atomic().overlaps(I.open(3, 4).to_atomic(), permissive=True) with pytest.warns(DeprecationWarning, match='permissive'): I.closed(0, 1).overlaps(I.open(3, 4), permissive=True) assert I.closed(0, 1).overlaps(I.closed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.open(0, 1), adjacent=True) assert I.open(0, 1).overlaps(I.closed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.openclosed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.closedopen(0, 1), adjacent=True) assert I.closed(1, 2).overlaps(I.closed(2, 3), adjacent=True) assert I.closed(1, 2).overlaps(I.closedopen(2, 3), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closed(2, 3), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closedopen(2, 3), adjacent=True) assert not I.closed(0, 1).overlaps(I.closed(3, 4), adjacent=True) assert not I.closed(3, 4).overlaps(I.closed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.open(1, 2), adjacent=True) assert I.closed(0, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert I.closedopen(0, 1).overlaps(I.closed(1, 2), adjacent=True) assert I.closedopen(0, 1).overlaps(I.closedopen(1, 2), adjacent=True) assert not I.closedopen(0, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert not I.closedopen(0, 1).overlaps(I.open(1, 2), adjacent=True) assert not I.open(0, 1).overlaps(I.open(1, 2), adjacent=True) assert not I.empty().overlaps(I.open(-I.inf, I.inf), adjacent=True) assert not I.open(-I.inf, I.inf).overlaps(I.empty(), adjacent=True) # https://github.com/AlexandreDecan/python-intervals/issues/13 assert I.closed(0, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert I.closedopen(0, 1).overlaps(I.closed(1, 2), adjacent=True) assert I.closed(1, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closed(0, 1), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closed(1, 1), adjacent=True)
def comparator_interval(op, version): if op == '=': return I.singleton(version) if op == '<': return I.closedopen(Version.FIRST, version) if op == '<=': return I.closed(Version.FIRST, version) if op == '>': return I.open(version, I.inf) if op == '>=': return I.closedopen(version, I.inf) if op == '!=': return I.closedopen(Version.FIRST, version) | I.openclosed( version, I.inf)