Best Python code snippet using lemoncheesecake
loader.py
Source:loader.py  
...19        train_classes, val_classes, test_classes = _get_fewrel_classes()20    elif dataset_name == 'amazon':21        train_classes, val_classes, test_classes = _get_amazon_classes()22    elif dataset_name == 'event':23        train_classes, val_classes, test_classes = _get_event_classes()24    else:25        raise ValueError(26            'args.dataset should be one of'27            '[reuters,huffpost,20newsgroup,fewrel,amazon]')28        29    tprint('Loading data')30    all_data = _load_json(dataset_path,args)31    32    # Split into meta-train, meta-val, meta-test data33    train_data, val_data, test_data = _meta_split(34            all_data, train_classes, val_classes, test_classes,args)35    tprint('#train {}, #val {}, #test {}'.format(36        len(train_data), len(val_data), len(test_data)))37    # Convert everything into np array for fast data loading38    train_data = _data_to_bert_features(train_data,args)39    val_data = _data_to_bert_features(val_data,args)40    test_data = _data_to_bert_features(test_data,args)41    train_data['is_train'] = True42    # this tag is used for distinguishing train/val/test when creating source pool43    44    return train_data, val_data, test_data45    46    47def _get_20newsgroup_classes():48    '''49        @return list of classes associated with each split50    '''51    label_dict = {52            'talk.politics.mideast': 0,53            'sci.space': 1,54            'misc.forsale': 2,55            'talk.politics.misc': 3,56            'comp.graphics': 4,57            'sci.crypt': 5,58            'comp.windows.x': 6,59            'comp.os.ms-windows.misc': 7,60            'talk.politics.guns': 8,61            'talk.religion.misc': 9,62            'rec.autos': 10,63            'sci.med': 11,64            'comp.sys.mac.hardware': 12,65            'sci.electronics': 13,66            'rec.sport.hockey': 14,67            'alt.atheism': 15,68            'rec.motorcycles': 16,69            'comp.sys.ibm.pc.hardware': 17,70            'rec.sport.baseball': 18,71            'soc.religion.christian': 19,72        }73    train_classes = []74    for key in label_dict.keys():75        if key[:key.find('.')] in ['sci', 'rec']:76            train_classes.append(label_dict[key])77    val_classes = []78    for key in label_dict.keys():79        if key[:key.find('.')] in ['comp']:80            val_classes.append(label_dict[key])81    test_classes = []82    for key in label_dict.keys():83        if key[:key.find('.')] not in ['comp', 'sci', 'rec']:84            test_classes.append(label_dict[key])85    return train_classes, val_classes, test_classes86    87    88def _get_reuters_classes():89    '''90        @return list of classes associated with each split91    '''92    train_classes = list(range(15))93    val_classes = list(range(15,20))94    test_classes = list(range(20,31))95    return train_classes, val_classes, test_classes96def _get_event_classes():97    '''98        @return list of classes associated with each split99    '''100    train_classes = list(range(20))101    val_classes = [20,21,22,23,24]102    test_classes = list(range(25,35))103    return train_classes, val_classes, test_classes104def _get_huffpost_classes():105    '''106        @return list of classes associated with each split107    '''108    train_classes = list(range(20))109    val_classes = list(range(20,25))110    test_classes = list(range(25,41))...events.py
Source:events.py  
...32class EventManager(object):33    def __init__(self):34        self._event_types = {}35    @staticmethod36    def _get_event_classes():37        """38        Get available event classes by introspecting this module to get available event classes.39        """40        current_module = sys.modules[__name__]41        for sym_name in dir(current_module):42            if sym_name.startswith("_"):43                continue44            sym = getattr(current_module, sym_name)45            if inspect.isclass(sym) and sym is not Event and issubclass(sym, Event):46                yield sym47    @classmethod48    def load(cls):49        eventmgr = cls()50        for event_class in eventmgr._get_event_classes():51            eventmgr.register_event(event_class)52        return eventmgr53    @staticmethod54    def _get_event_name(val):55        # val can be either an Event-based class or the name of the event itself (as an str)56        return val.get_name() if inspect.isclass(val) and issubclass(val, Event) else val57    def register_event(self, *event_classes):58        for event_class in event_classes:59            self._event_types[event_class.get_name()] = EventType(event_class)60    def subscribe_to_event(self, event, handler):61        self._event_types[self._get_event_name(event)].subscribe(handler)62    def subscribe_to_events(self, handlers):63        for event, handler in handlers.items():64            self.subscribe_to_event(event, handler)...log_simulation.py
Source:log_simulation.py  
...68        log_cpy = EventLog()69        for i in range(0, log_size):70            log_cpy.append(deepcopy(log[i % len(log)]))71        log = log_cpy72    classes = _get_event_classes(log)73    log_new = EventLog()74    for trace in log:75        if len(trace) > 0:76            trace_cpy = deepcopy(trace)77            # check if trace makes random selection78            if random.random() <= noisy_trace_prob:79                insert_more_noise = True80                while insert_more_noise:81                    # randomly select which kind of noise to insert82                    noise_type = random.randint(0, 2)83                    if noise_type == 0:84                        _remove_event(trace_cpy)85                    if noise_type == 1:86                        _insert_event(trace_cpy, classes)87                    if noise_type == 2:88                        _swap_events(trace_cpy)89                    # flip coin to see if more noise will be inserted90                    insert_more_noise = (random.random() <= noisy_event_prob)91            log_new.append(trace_cpy)92    #print('Noisy log size: ' + str(len(log_new)))93    return log_new94def _remove_event(trace: Trace):95    del_index = random.randint(0, len(trace) - 1)96    trace2 = Trace()97    for i in range(0, len(trace)):98        if i != del_index:99            trace2.append(trace[i])100    return trace2101def _insert_event(trace: Trace, tasks):102    ins_index = random.randint(0, len(trace))103    task = random.choice(list(tasks))104    e = Event()105    e["concept:name"] = task106    trace.insert(ins_index, e)107    return trace108def _swap_events(trace: Trace):109    if len(trace) == 1:110        return trace111    indices = list(range(len(trace)))112    index1 = random.choice(indices)113    indices.remove(index1)114    index2 = random.choice(indices)115    trace2 = Trace()116    for i in range(len(trace)):117        if i == index1:118            trace2.append(trace[index2])119        elif i == index2:120            trace2.append(trace[index1])121        else:122            trace2.append(trace[i])123    return trace2124def _get_event_classes(log):125    classes = set()126    for trace in log:127        for event in trace:128            classes.add(event["concept:name"])...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
