MCPcopy Create free account
hub / github.com/DrugLT/ImDrug / create_open_split_byclass

Function create_open_split_byclass

lib/utils/split.py:75–109  ·  view source on GitHub ↗

split whole dataset into closed set and open set (for testing only), class by class Args: frac (float): fraction of number of open classes Returns:

(df, open_frac, label_name='Y', label_weight_name='Y_Weight', lt_label_name='LT_Class', num_class=10, label_type='classification', scale=None)

Source from the content-addressed store, hash-verified

73 return cls_label_lst.index(lbl)
74
75def create_open_split_byclass(df, open_frac, label_name='Y', label_weight_name='Y_Weight', lt_label_name='LT_Class', num_class=10, label_type='classification', scale=None):
76 """split whole dataset into closed set and open set (for testing only), class by class
77
78 Args:
79 frac (float): fraction of number of open classes
80
81 Returns:
82 """
83 cls_labels, bins, hist = get_cls_labels(df=df, label_name=label_name, num_class=num_class, label_type=label_type, scale=scale)
84 num_open_classes = int(np.ceil(len(cls_labels) * open_frac))
85 # open_cls_indices = np.argpartition(hist, num_open_classes)[:num_open_classes]
86 open_cls_indices = cls_labels[-num_open_classes:]
87 print('Label distribution: ', hist)
88 print('Open classes: ', open_cls_indices)
89
90 df_cpy = copy.deepcopy(df)
91 for i, open_cls_idx in enumerate(open_cls_indices):
92 df_cpy[lt_label_name] = 'open{}'.format(i)
93
94 if label_type == "regression":
95 label_name = "reg_cls"
96 open_df = df_cpy[df_cpy[label_name].isin(open_cls_indices)]
97
98 # regard all open classes as one unknown class, as in https://arxiv.org/pdf/1904.05160.pdf
99 open_df = get_label_weight(open_df, label_weight_name)
100
101 closed_df = df[~df.index.isin(open_df.index)]
102
103 # relabel indices of the closed set for training/validation, to avoid index mismatch when calculating losses such as cross entropy
104 closed_df[label_name] = closed_df[label_name].apply(lambda x: relabel(x, cls_labels))
105
106 # relabel indices of the open set as one outlier class, thus formulating testing as anomalu detection
107 open_df[label_name] = len(cls_labels) - num_open_classes
108 open_df[lt_label_name] = 'open'
109 return closed_df, open_df
110
111
112def create_fold_byclass(df, seed, frac, lt_frac, label_name='Y', label_weight_name='Y_Weight', lt_label_name='LT_Class', num_class=10, label_type='classification', scale=None):

Callers 3

get_splitMethod · 0.90
get_splitMethod · 0.90
get_splitMethod · 0.90

Calls 3

get_cls_labelsFunction · 0.85
get_label_weightFunction · 0.85
relabelFunction · 0.85

Tested by

no test coverage detected