def sample_period_idx()

in dvd_generation/utils/dialogue_utils.py [0:0]


def sample_period_idx(dialogue, template, scene_struct, used_periods, turn_dependencies, last_unique_obj_idx):
    last_unique_obj = False
    earlier_unique_obj = False
    cutoff = template['cutoff']
    whole_video_period = (None, cutoff)
    
    if template['interval_type'] != 'none': 
        curr_periods = scene_struct[intervals_to_periods[template['interval_type']]] 
        trial = 0 
        
        while(True):
            trial += 1 
            if whole_video_period not in curr_periods and template['interval_type']=='compositional': pdb.set_trace()
            # to avoid explosion of memory when sampling the next interval 
            if trial==max_period_sampling_attempts: 
                return -1 
            # encouring period as a whole video, limit to one repetition over contiguous turns 
            if sample_by_prop(whole_video_p) and whole_video_period in curr_periods:
                if len(used_periods)==0 \
                or (len(used_periods)>0 and used_periods[-1] != whole_video_period): 
                    new_period = whole_video_period 
                    used_periods.append(new_period)
                    period_idx = curr_periods.index(new_period)
                    break
            # sample the remaining except for whole video period
            period_idx = random.choice(range(len(curr_periods)))
            new_period = curr_periods[period_idx]            
            if not is_valid_event(new_period, cutoff): 
                continue            
            # sampled cases are new unseen period only
            if sample_by_prop(unique_interval_p) and new_period in used_periods: continue 
                
            # if there is last unique object, encouraging temporal dependency with reference to the last unique object 
            if sample_by_prop(last_unique_period_p) and len(dialogue)>0 and last_unique_obj_idx!=-1: 
                # check if can refer to the interval of the same object mentioned previously 
                if not is_obj_of_period(last_unique_obj_idx, curr_periods[period_idx]): continue 
                last_unique_obj = True 
                template['temporal_obj_id'], template['temporal_obj_attr'] = \
                    last_unique_obj_idx, template['prior_unique_obj_attr']
                #template['temporal_obj_id_2'], template['temporal_obj_attr_2'] = \
                #    last_unique_obj_idx, template['prior_unique_obj_attr']
                    
            # if there is earlier unique object, encouraging temporal dependency with reference to the earlier mentioned object 
            elif sample_by_prop(earlier_unique_period_p) and len(dialogue)>0 and len(template['used_objects'])>0:
                dial_obj_identifiers = template['_dial_minimal_object_identifiers']
                temp = copy.deepcopy(list(template['used_objects'].keys()))
                random.shuffle(temp)
                found_earlier = False 
                for earlier_unique_obj_idx in temp:
                    if dial_obj_identifiers[earlier_unique_obj_idx] != [None]:
                        found_earlier = True 
                        break 
                if not found_earlier: continue
                # check if can refer to the interval of the same object mentioned previously 
                if not is_obj_of_period(earlier_unique_obj_idx, curr_periods[period_idx]): continue 
                earlier_unique_obj_attr = random.choice(dial_obj_identifiers[earlier_unique_obj_idx])
                if earlier_unique_obj_attr is None: pdb.set_trace()
                earlier_unique_obj_attr = {identifier_attrs[k]:v for k,v in enumerate(earlier_unique_obj_attr) \
                                           if v is not None}
                earlier_unique_obj = True 
                template['temporal_obj_id'], template['temporal_obj_attr'] = \
                    earlier_unique_obj_idx, earlier_unique_obj_attr
                #template['temporal_obj_id_2'], template['temporal_obj_attr_2'] = \
                #    earlier_unique_obj_idx, earlier_unique_obj_attr

            else:
                oi, oa = sample_temporal_object_attr(scene_struct, curr_periods[period_idx], cutoff)
                #if (oi1 is not None and oa1 is None) or (oi2 is not None and oa2 is None): continue 
                if oi is not None and oa is None: continue 
                template['temporal_obj_id'], template['temporal_obj_attr'] = oi, oa
                #template['temporal_obj_id_2'], template['temporal_obj_attr_2'] = oi2, oa2 
            used_periods.append(new_period)
            break
    else:
        used_periods.append(None)
        period_idx = -1
        
    if last_unique_obj: 
        turn_dependencies['temporal'] = 'last_unique_obj_none'
    elif earlier_unique_obj:
        turn_dependencies['temporal'] = 'earlier_unique_obj_none'
    else:
        turn_dependencies['temporal'] = 'none'

    return period_idx