Best Python code snippet using tappy_python
__init__.py
Source:__init__.py  
1from functools import reduce2from operator import eq, ne, or_, and_3from typing import Dict, Union, Sequence, Iterable4import numpy as np5import torch6from sklearn.metrics import roc_auc_score, roc_curve7from sklearn.preprocessing import label_binarize8# todo wrap the class-mapping from multi_class_roc_auc9def is_array_class(array):10    return isinstance(array, np.ndarray) or isinstance(array, torch.Tensor)11def validate_array_class(array: Union[torch.Tensor, np.ndarray]):12    assert is_array_class(array), f"Type disagree: expect" \13        f"np.ndarray or torch.Tensor, got: {type(array)}"14def array_to_numpy(array: Union[torch.Tensor, np.ndarray]):15    validate_array_class(array), f"not ndarray or tensor. {type(array)}"16    if isinstance(array, np.ndarray):17        return array18    return array.cpu().detach().numpy()19def to_long(array: Union[torch.Tensor, np.ndarray]):20    validate_array_class(array)21    if not isinstance(array, torch.Tensor):22        type_field = 'astype'23        dtype = np.int6424    else:25        type_field = 'type'26        dtype = torch.LongTensor27    return getattr(array, type_field)(dtype)28def is_sequence_alike(obj):29    return isinstance(obj, Sequence) or isinstance(obj, Iterable) or is_array_class(obj)30def validate_parititon_structure(label_group):31    assert is_sequence_alike(label_group), f"1st level structure must be sequence-alike. Got{type(label_group)}"32    for x in label_group:33        assert is_sequence_alike(x), f"2nd level must be sequence-alike. Got{type(x)}"34def label_group_elements(label_group: Sequence[Sequence[int]]):35    set_list = [set(x) for x in label_group]36    set_combined = reduce(or_, set_list)37    return set_list, set_combined38def is_disjoint_partition(label_group: Sequence[Sequence[int]]):39    validate_parititon_structure(label_group)40    set_list, set_combined = label_group_elements(label_group)41    sum_len = np.asarray([len(set_obj) for set_obj in set_list]).sum()42    length_combined = len(set_combined)43    # if length of combined set mismatch sum_len, then there are repetitive elements44    return length_combined == sum_len45def validate_disjoint_partition(label_group: Sequence[Sequence[int]]):46    assert is_disjoint_partition(label_group), f"Not Disjoint: {label_group}"47def is_label_defined(labels: Union[np.ndarray, torch.Tensor], label_group: Sequence[Sequence[int]]):48    set_list, set_combined = label_group_elements(label_group)49    labels = array_to_numpy(labels)50    check_result = np.asarray([label_value in set_combined for label_value in labels])51    bool_all_defined = check_result.all()52    set_undefined_labels = set(labels[~check_result])53    return bool_all_defined, set_undefined_labels54def validate_label_definition(labels: Union[np.ndarray, torch.Tensor], label_group: Sequence[Sequence[int]]):55    is_all_defined, set_undefined_labels = is_label_defined(labels, label_group)56    assert is_all_defined, f"Unexpected Label Encounted: {set_undefined_labels}"57def label_encode_by_partition(labels: Union[np.ndarray, torch.Tensor], label_group: Sequence[Sequence[int]]):58    # precondition_1: disjoint59    validate_disjoint_partition(label_group)60    # precondition_2: all labels must be defined in label_group61    validate_label_definition(labels, label_group)62    # x in labels as 0/1 assignment. idx as the target encoded label value defined by the order of label_group63    # if pack/partition in label_group are disjoint, and there are no unexpected labels64    # that are not defined in the label_group then the corresponding65    # idx * (x in pack) across each pack (column) in label_group should have exactly one non-zero value66    # use sum to get the final encoded label.67    labels = array_to_numpy(labels)68    final_label = np.asarray([[idx * (x in pack) for x in labels] for idx, pack in enumerate(label_group)])\69        .sum(axis=0)70    return final_label71def label_binarize_group(labels: Union[np.ndarray, torch.Tensor], anchor_group, anchor_positive: bool):72    if not isinstance(anchor_group, np.ndarray) or not isinstance(anchor_group, torch.Tensor) \73            or not isinstance(anchor_group, Sequence):74        anchor_group = np.atleast_1d(anchor_group)75    if isinstance(labels, torch.Tensor):76        anchor_group = torch.from_numpy(anchor_group).type("torch.LongTensor")77    labels = to_long(labels)78    anchor_group = to_long(anchor_group)79    if not anchor_positive:80        connector = and_81        comparator = ne82    else:83        connector = or_84        comparator = eq85    # mask array is a tuple of arrays, where each array performs the comparison86    # between label and anchor element87    # for positive anchor --> comparator is eq, as any label == anchor yields 1, and 0 otherwise.88    # for negative anchor --> use ne: label != anchor yields 1, and 0 otherwise.89    # connector: for positive anchor, label is mapped to 1 if it matches any of the anchor element90    # for negative anchor: not [or] ==> and.91    # not ( label == anchor1 or label== anchor2... ) ==> label != anchor1 and label!= anchor2 ...92    mask_array = tuple(comparator(labels, x) for x in anchor_group)93    labels = reduce(connector, mask_array)94    return labels95    # if anchor_positive, then True=Positive, otherwise, True=Neg96def array_copy_heper(array: Union[torch.Tensor, np.ndarray]):97    if not isinstance(array, torch.Tensor):98        copy_func = array.copy99    else:100        copy_func = array.clone101    return copy_func()102# buggy. There appears to be an existing implementation in sklearn/metrics/ranking/label_binarize103def label_binarize_vs_rest(labels: Union[np.ndarray, torch.Tensor],104                           anchor_class: int, anchor_positive: bool = True):105    assert isinstance(labels, np.ndarray) or isinstance(labels, torch.Tensor), f"Type disagree: expect" \106        f"np.ndarray or torch.Tensor, got: {type(labels)}"107    labels = array_copy_heper(labels)108    offset_subtract = 1 if anchor_positive else -1109    comparator = eq if anchor_positive else ne110    labels[labels != anchor_class] = anchor_class - offset_subtract111    # assumption that labels here contains two unique values are false112    # labels = (labels == labels.max())113    labels = comparator(labels, anchor_class)114    labels = to_long(labels)115    return labels116def roc_auc_result_dict(auc, fpr, tpr, threshold):117    return {118        "auc": auc,119        "fpr": fpr,120        "tpr": tpr,121        "thresholds": threshold122    }123def roc_auc_helper(y_true, y_pred, original_class_id, roc_auc_dict=None):124    if roc_auc_dict is None:125        roc_auc_dict = dict()126    auc = roc_auc_score(y_true, y_pred)127    fpr, tpr, thresholds = roc_curve(y_true, y_pred)128    pk_data = roc_auc_result_dict(auc, fpr, tpr, thresholds)129    roc_auc_dict[original_class_id] = pk_data130    return roc_auc_dict131def multi_class_roc_auc_vs_all(y_true: Union[np.ndarray, Sequence[int]],132                               y_pred_all_classes: Union[np.ndarray, Sequence[int]],133                               positive_classes: Union[np.ndarray, Sequence[int]]134                               ) -> Dict[int, Dict[str, Union[float, np.ndarray]]]:135    roc_auc_dict: Dict[int, Dict] = dict()136    y_true = np.atleast_1d(y_true)137    y_pred_all_classes = np.atleast_2d(y_pred_all_classes)138    assert y_pred_all_classes.shape[0] == y_true.size, f"# of data point disagree." \139        f" {y_pred_all_classes.shape[0]} vs. {y_true.size}"140    if y_pred_all_classes.shape[0] == 1:141        raise ValueError(f"y_pred_all_classes has only one row. Expected to be >=2")142    label_binary_array = label_binarize(positive_classes, y_true)143    score_merged = y_pred_all_classes.shape[1] <= 2144    for original_class, y_true in zip(positive_classes, label_binary_array):145        if not score_merged:146            y_score = np.atleast_1d(y_pred_all_classes[:, original_class]).squeeze()147        else:148            y_score = np.atleast_1d(y_pred_all_classes[:, -1]).squeeze()149        roc_auc_dict = roc_auc_helper(y_true, y_score, original_class, roc_auc_dict=roc_auc_dict)...solution.py
Source:solution.py  
1import timeit2import numpy as np3from collections  import defaultdict4DO_TEST = 05def read_file(filename):6    data = []7    with open(filename, 'r') as file:8        data_string = file.read()9    10    for line in data_string.split('\n'):11        combs =  line.split('|')[0]12        outs = line.split('|')[1]13        data.append((combs, outs))14    15    return data16def do_part1(file):17    data = read_file(file)18    one = 019    four = 020    seven = 021    eight = 022    for line in data:23        for out in line[1].strip().split():24            if len(out) == 2: one += 125            if len(out) == 4 : four += 126            if len(out) == 3 : seven += 127            if len(out) == 7 : eight += 1 28    # print(one, four, seven, eight)29    result = one + four + seven + eight30    print(f"part 1 = {result}")31    32def do_part2(file):33    data = read_file(file)34    unknown = []35    sum = 036    for line in data:37        # get the combinations38        digits = defaultdict(str)39        for combs in line[0].strip().split():40            if len(combs) == 2:41                digits[1] = combs42            elif len(combs) == 3:43                digits[7] = combs44            elif len(combs) == 4:45                digits[4]= combs46            elif len(combs) == 7:47                digits[8] = combs48            else:49                unknown.append(combs)50        # detect unknown51        for combs in unknown:52            if len(combs) == 6:53                combined = combs + digits[4]54                # set_combined = remove_dups(combined)55                if count_unique(combined) == 2:56                    digits[9] = combs57                else:58                    combined = combs + digits[4] + digits[7]59                    # set_combined = list(set(combined))60                    if count_unique(combined) == 2:61                        digits[6] = combs62                    else:63                        digits[0] = combs64            if len(combs) == 5:65                combined = combs + digits[1]             66                # set_combined = list(set(combined))67                if count_unique(combined)== 3:68                    digits[3]= combs69                else:70                    combined = combs +digits[4]71                    # set_combined = list(set(combined))72                    if count_unique(combined) == 3:73                        digits[5] = combs74                    else:75                        digits[2] = combs76        # convert77        str_num = ""78        for out in  line[1].strip().split():79            str_num += str(get_key(digits,out))80        81        sum += int(str_num)82                    83        84    result = sum85    print(f"part 2 = {result}")86def get_key(dict, val):87    for key, value in dict.items():88        combined = value + val89        if count_unique(combined) == 0:90             return key91    return -192def count_unique(str_val):93    single = []94    double = []95    for let in str_val:96        if let in single:97            if let not in double:98                double.append(let)99        else:100            single.append(let)101    for d in double:102        single.remove(d)103    return len(single)104# main105test = ("test.txt")106real = ("input.txt")107data = real108if DO_TEST == 1:109    data = test110print('results : ')111starttime_1 = timeit.default_timer()112do_part1(data)113endtime_1 = timeit.default_timer()114starttime_2 = timeit.default_timer()115do_part2(data)116endtime_2 = timeit.default_timer()117print('performance :')118print('part1 = ', (endtime_1-starttime_1), " sec")...core.py
Source:core.py  
...41        return Criteria(42            f"({str(self)} or {str(criteria)})",43            lambda *kargs, **kwargs: self.fn(*kargs, **kwargs)44            or criteria.fn(*kargs, **kwargs),45        ).set_combined()46    def __and__(self, criteria):47        return Criteria(48            f"({str(self)} and {str(criteria)})",49            lambda *kargs, **kwargs: self.fn(*kargs, **kwargs)50            and criteria.fn(*kargs, **kwargs),51        ).set_combined()52    def __rshift__(self, ref: Storage):53        self.ref = ref54        return self55    def __invert__(self):56        return Criteria(57            f"(not {str(self)})", lambda *kargs, **kwargs: not self.fn(*kargs, **kwargs)58        ).set_combined()59    def set_combined(self):60        self.combined = True61        return self62def fn_to_criteria(fn):63    def fn_to_criteria_wrapper(*kargs, **kwargs):64        return Criteria(fn.__name__, fn(*kargs, **kwargs), *kargs, **kwargs)65    return fn_to_criteria_wrapper66@fn_to_criteria67def expected(x, optional):...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!!
