def __generate_frame_from_values(self, x_vals, y_vals): frame = Frame(self.current_frame_start_time) if len(self.x_mono_list) >= len(self.y_mono_list): previous_index = 0 for mono_change_index in self.x_mono_list: frame.add_correlation( len(x_vals[previous_index:mono_change_index]), spearmanr(x_vals[previous_index:mono_change_index], y_vals[previous_index:mono_change_index])[0]) previous_index = mono_change_index else: previous_index = 0 for mono_change_index in self.y_mono_list: frame.add_correlation( len(x_vals[previous_index:mono_change_index]), spearmanr(x_vals[previous_index:mono_change_index], y_vals[previous_index:mono_change_index])[0]) previous_index = mono_change_index # Reset values.. self.x_mono_list.clear() self.y_mono_list.clear() self.current_iteration[self.sensor_x.get_uuid()].clear() self.current_iteration[self.sensor_y.get_uuid()].clear() self.x_last_direction = 0 self.y_last_direction = 0 self.current_frame_start_time += frame.get_total_time() return frame
def get_correlation_coefficient(self): if self.get_last_pushed_value() is None: return 0.0 elif not self.current_iteration[self.sensor_x.get_uuid()] \ and not self.current_iteration[self.sensor_y.get_uuid()]: return self.get_last_pushed_value() else: frame = Frame(self.current_frame_start_time) x_vals = self.current_iteration[self.sensor_x.get_uuid()] y_vals = self.current_iteration[self.sensor_y.get_uuid()] if len(self.x_mono_list) >= len(self.y_mono_list): previous_index = 0 for mono_change_index in self.x_mono_list: frame.add_correlation( len(x_vals[previous_index:mono_change_index]), spearmanr(x_vals[previous_index:mono_change_index], y_vals[previous_index:mono_change_index])[0]) previous_index = mono_change_index else: previous_index = 0 for mono_change_index in self.y_mono_list: frame.add_correlation( len(x_vals[previous_index:mono_change_index]), spearmanr(x_vals[previous_index:mono_change_index], y_vals[previous_index:mono_change_index])[0]) previous_index = mono_change_index if frame.get_final_correlation() is not None: current_iter_association = generate_association([frame]) current_iter_time = frame.get_total_time() total_iter_time = self.summed_frame.get_total_time() current_iter_ratio = current_iter_time / \ (current_iter_time + total_iter_time) total_iter_ratio = total_iter_time / \ (current_iter_time + total_iter_time) current_iter = current_iter_association * current_iter_ratio total_iter = self.get_last_pushed_value() * total_iter_ratio return current_iter + total_iter else: return self.get_last_pushed_value()
def test_should_not_allow_corr_greater_than_1(): with pytest.raises(ValueError): frame = Frame(datetime.datetime.now()) frame.add_correlation(1, 1.1)
def test_should_avg_to_0_when_equal_opposite_corr_are_added(): frame = Frame(datetime.datetime.now()) frame.add_correlation(1, 1) frame.add_correlation(1, -1) assert frame.get_final_correlation() == 0
def test_should_avg_pos_when_duration_is_longer(): frame = Frame(datetime.datetime.now()) frame.add_correlation(4, 1) frame.add_correlation(1, -1) assert frame.get_final_correlation() == .6
def test_should_not_allow_negative_durations(): with pytest.raises(ValueError): frame = Frame(datetime.datetime.now()) frame.add_correlation(-1, 1)