def intersection()

in distill/segmentation/segment.py [0:0]


def intersection(segment_name, segment1, segment2):
    """
    Creates a new segment based on the intersection of given segments' uids.

    :param segment_name: Name associated with the new segment
    :param segment1: First segment involved in intersection.
    :param segment2: Second segment involved in intersection.

    :return: A new segment with the given segment_name, start and end values based on the smallest client time and
    largest client time of the given segments, and a list of the intersection of the uids of segment1 and segment2.
    """

    if not isinstance(segment1.start_end_val[0], type(segment2.start_end_val[0])) or \
            not isinstance(segment1.start_end_val[1], type(segment2.start_end_val[1])):
        raise TypeError("Segment start and end values must be of the same type between segments.")

    # intersections uids
    uids = []
    for uid in segment2.uids:
        if uid in segment1.uids:
            uids.append(uid)

    # Get the earliest start and latest end value
    start_time = segment1.start_end_val[0]
    end_time = segment1.start_end_val[1]
    if segment1.start_end_val[0] > segment2.start_end_val[0]:
        start_time = segment2.start_end_val[0]
    if segment1.start_end_val[1] < segment2.start_end_val[1]:
        end_time = segment2.start_end_val[1]

    segment = Segment(segment_name, (start_time, end_time), len(uids), uids)
    segment.segment_type = Segment_Type.INTERSECTION
    segment.generate_field_name = None
    segment.generate_matched_values = None
    return segment