def union()

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


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

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

    :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 union 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.")

    # Union uids
    uids = copy.deepcopy(segment1.uids)
    for uid in segment2.uids:
        if uid not in uids:
            uids.append(uid)
        
    # Get earliest starting val and latest end val
    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]

    # Create segment to return
    segment = Segment(segment_name, (start_time, end_time), len(uids), uids)
    segment.segment_type = Segment_Type.UNION
    segment.generate_field_name = None
    segment.generate_matched_values = None
    return segment