def generate_features_with_ref_segments(self, selected_tuple, patient_no, subs_flag=False): #print("Inside train") if subs_flag: print("Using Substitute") selected_label = selected_tuple[0] selected_segment = selected_tuple[1] s1 = np.array(selected_segment) F_avg = [] for ref_segment in self.ref_segments[self.ref_label]: s2 = np.array(ref_segment) try: F = feature_gen(s1, s2) F_avg.append(F) except Warning: print("Warning encountered..") print("*************************") substitute_tuple = self.extract_random_segments_for_given_patient_during_warning( selected_label, patient_no) self.generate_features_with_ref_segments( substitute_tuple, patient_no, subs_flag=True ) #recursively call this function till Warningless segment is found (in practice Warning is rarely encountered, hence more than 1 recursive call is extremely rare) return #important, else the local s1(which is the source of Warning) will continue executing, thus calling the functions in except block again and again self.trainset_list.append((selected_label, np.mean(F_avg, axis=0))) np.save(f"clf{self.ref_label}.npy", self.trainset_list)
def generate_features_with_ref_segments(self, selected_tuple,patient_no): selected_label = selected_tuple[0] selected_segment = selected_tuple[1] t, s = np.arange(len(selected_segment)), np.array(selected_segment) #l = 1 if selected_label == self.ref_label else 0 for ref in range(NUM_SLEEP_STAGES): F_avg = [] for ref_segment in self.ref_segments[ref]: t1, s1 = t, s t2, s2 = np.arange(len(ref_segment)), np.array(ref_segment) # print(s1.shape) # print(s2.shape) #converting segments to equal lengths S1 = s1[np.argwhere((t1 >= min(t2)) & (t1 <= max(t2))).flatten()] S2 = s2[np.argwhere((t2 >= min(t1)) & (t2 <= max(t1))).flatten()] # print(s1.shape) # print(s2.shape) # print("*************************") try: F = feature_gen(t1, S1, t2, S2, ref) F_avg.append(F) except Warning: global no_of_errors_encountered no_of_errors_encountered+=1 substitute=extract_random_segments_for_given_patient_during_warning(selected_label,patient_no) self.generate_features_with_ref_segments(substitute,patient_no) self.testset_dict[ref].append((selected_label, np.mean(F_avg, axis=0)))
def generate_features_with_ref_segments(self, selected_tuple, patient_no): selected_label = selected_tuple[0] selected_segment = selected_tuple[1] t, s = np.arange(len(selected_segment)), np.array(selected_segment) #l = 1 if selected_label == self.ref_label else 0 F_avg = [] for ref_segment in self.ref_segments[self.ref_label]: t1, s1 = t, s t2, s2 = np.arange(len(ref_segment)), np.array(ref_segment) # print(s1.shape) # print(s2.shape) #converting segments to equal lengths S1 = s1[np.argwhere((t1 >= min(t2)) & (t1 <= max(t2))).flatten()] S2 = s2[np.argwhere((t2 >= min(t1)) & (t2 <= max(t1))).flatten()] # print(s1.shape) # print(s2.shape) # print("*************************") try: F = feature_gen(t1, S1, t2, S2, self.ref_label) for i in range(len(F)): if math.isnan(F[i]): F[i] = 0 F_avg.append(F) #for i in range(len(F)): #print(f"{F[i]}({i})", end=", ") except Warning: global no_of_errors_encountered no_of_errors_encountered += 1 substitute = self.extract_random_segments_for_given_patient_during_warning( selected_label, patient_no) return self.generate_features_with_ref_segments( substitute, patient_no) #print(f"Feature vector:{np.mean(F_avg,axis=0)}") return selected_label self.data_list.append((selected_label, np.mean(F_avg, axis=0)))
def generate_features_with_ref_segments(self, selected_tuple, patient_no, mean, std): selected_label = selected_tuple[0] selected_segment = selected_tuple[1] s1 = np.array(selected_segment) for ref in range(NUM_SLEEP_STAGES): #extra loop over all ref labels F_avg = [] for ref_segment in self.ref_segments[ref]: s2 = np.array(ref_segment) try: F = feature_gen(s1, s2, mean, std) F_avg.append(F) except Warning: print("Warning encountered") return self.testset_dict[ref].append( (selected_label, np.mean(F_avg, axis=0)))
def generate_features_with_ref_segments(self, selected_tuple, mean, std): selected_label = selected_tuple[0] selected_segment = selected_tuple[1] s1 = np.array(selected_segment) F_avg = [] for ref_segment in self.ref_segments[self.ref_label]: s2 = np.array(ref_segment) try: F = feature_gen(s1, s2, mean, std) F_avg.append(F) except Warning: print("Warning encountered..") print("*************************") return #print(np.mean(F_avg, axis=0).shape) self.trainset_list.append((selected_label, np.mean(F_avg, axis=0))) np.save( os.path.join(save_path, f"ref{len(self.ref_segments[0])}_dj{DJ}", f"clf{self.ref_label}_2.npy"), self.trainset_list)