def test_basic_unmerge(self): in1 = BooleanTimeSeries([0, 1, 2, 3], [True, False, True, False], 4) in2 = BooleanTimeSeries([0, 1, 2.5, 3], [True, False, True, False], 4) t = [0, 1, 2, 2.5, 3] y = [[True, True], [False, False], [True, False], [True, True], [False, False]] [out1, out2] = BooleanTimeSeries.unmerge(t, y, 4) self.assertEqual([0, 1, 2, 3], out1.t) self.assertEqual([True, False, True, False], out1.y) self.assertEqual(4, out1.end) self.assertEqual([0, 1, 2.5, 3], out2.t) self.assertEqual([True, False, True, False], out2.y) self.assertEqual(4, out2.end)
def validate(self, start, end): """ Validates the outputs of the simulation satisfy the model equations. Parameters ---------- start : float start time of the simulation (i.e. the list time point of the history) end : float end time of the simulation Returns ------- Sum of the the period of time each variable is in an invalid state. The maximum value is the length of the simulation multiplied by the number of variables. The minimum value is 0. """ candidate_switch_points = ValidatorCandidateSwitchPoints( self.delays, start, end) for bts in self.variables_bts: candidate_switch_points.add_boolean_time_series(bts) if self.inputs: for bts in self.inputs: candidate_switch_points.add_boolean_time_series(bts) candidate_switch_points.add_random(1000) candidate_switch_points.add(start) res_times = [] res_variables_states = [] for t in candidate_switch_points.times(): z = [] z2 = [] for d in self.delays: states = [] for v_bts in self.variables_bts: states.append(v_bts.get_state(t - d)) z.append(states) if self.inputs: z2_states = [] for input_bts in self.inputs: z2_states.append(input_bts.get_state(t - d)) z2.append(z2_states) if self.inputs: s = self.func(z, z2) else: s = self.func(z) if (not res_times) or res_variables_states[-1] != s: res_times.append(t) res_variables_states.append(s) candidate_switch_points.add_delays(t) # Turn into Boolean Time Series res_variables_bts = BooleanTimeSeries.unmerge(res_times, res_variables_states, end) # Calculate the accuracy accuracy = 0 for v_i, res_var_bts in enumerate(res_variables_bts): bts_from_start = self.variables_bts[v_i].cut(start, end) accuracy += res_var_bts.hamming_distance(bts_from_start) return accuracy