def test_choose_next_parameter_categorical(self): """Tests that the selection of the next parameter behaves as expected whenever there are any categorical variables. """ ranges = np.array( [[1, 2, 3, 4, 5, 6], ["titi", "toto", "tutu"], ["popo", "jojo"]], dtype=object) fake_history = { "fitness": np.array([10, 5, 4, 2, 15]), "parameters": np.array( [[1, "tutu", "popo"], [2, "toto", "popo"], [4, "toto", "popo"], [2, "titi", "jojo"], [3, "tutu", "popo"]], dtype=object), "truncated": np.array([True, True, False, False, False]), } expected_new_parameter = np.array([1, 'titi', 'jojo'], dtype=object) surrogate_model = SurrogateModel( regression_model=GaussianProcessRegressor, next_parameter_strategy=expected_improvement, ) real_new_parameter = surrogate_model.choose_next_parameter( fake_history, ranges) assert_array_equal(expected_new_parameter, real_new_parameter)
def test_infer_type(self): """Tests that the static method built to infer the types of an array. """ bb_obj = BBOptimizer( black_box=self.parabola, heuristic="surrogate_model", max_iteration=50, initial_sample_size=2, parameter_space=parameter_space, next_parameter_strategy=expected_improvement, regression_model=GaussianProcessRegressor, stop_criterion="improvement_criterion", stop_window=4, improvement_estimator=min, improvement_threshold=0.1 ) int_array = np.array([1, 2, 3]) inferred_int_array = bb_obj.infer_type(int_array) assert_array_equal(int_array, inferred_int_array) mixed_type_array = np.array(['1', '2', 'toto']) expected_mixed_type_array = np.array([1, 2, 'toto'], dtype=object) inferred_mixed_type_array = bb_obj.infer_type(mixed_type_array) assert_array_equal(inferred_mixed_type_array, expected_mixed_type_array)
def test_to_absolute_int(idx: int, freq: str): """Test converting between relative and absolute.""" # Converts from relative to absolute and back to relative train = pd.Series(1, index=pd.date_range("2021-10-06", freq=freq, periods=5)) fh = ForecastingHorizon([1, 2, 3]) absolute_int = fh.to_absolute_int(start=train.index[0], cutoff=train.index[idx]) assert_array_equal(fh + idx, absolute_int)
def test_to_relative(freq: str): """Test conversion to relative. Fixes bug in https://github.com/alan-turing-institute/sktime/issues/1935#issue-1114814142 """ freq = "2H" t = pd.date_range(start="2021-01-01", freq=freq, periods=5) fh_abs = ForecastingHorizon(t, is_relative=False) fh_rel = fh_abs.to_relative(cutoff=t.min()) assert_array_equal(fh_rel, np.arange(5))
def test_relative_to_relative(freqstr): """Test converting between relative and absolute.""" # Converts from relative to absolute and back to relative train = pd.Series(1, index=pd.date_range("2021-10-06", freq=freqstr, periods=3)) fh = ForecastingHorizon([1, 2, 3]) abs_fh = fh.to_absolute(train.index[-1]) converted_rel_fh = abs_fh.to_relative(train.index[-1]) assert_array_equal(fh, converted_rel_fh)
def test_estimator_fh(freqstr): """Test model fitting with anchored frequency.""" train = pd.Series( np.random.uniform(low=2000, high=7000, size=(104, )), index=pd.date_range("2019-01-02", freq=freqstr, periods=104), ) forecaster = AutoETS(auto=True, sp=52, n_jobs=-1, restrict=True) forecaster.fit(train) pred = forecaster.predict(np.arange(1, 27)) expected_fh = ForecastingHorizon(np.arange(1, 27)).to_absolute( train.index[-1]) assert_array_equal(pred.index.to_numpy(), expected_fh.to_numpy())
def test_absolute_to_absolute_with_integer_horizon(freqstr): """Test converting between absolute and relative with integer horizon.""" # Converts from absolute to relative and back to absolute train = pd.Series(1, index=pd.date_range("2021-10-06", freq=freqstr, periods=3)) fh = ForecastingHorizon([1, 2, 3]) abs_fh = fh.to_absolute(train.index[-1]) converted_abs_fh = abs_fh.to_relative(train.index[-1]).to_absolute( train.index[-1]) assert_array_equal(abs_fh, converted_abs_fh) assert converted_abs_fh._values.freqstr == freqstr
def test_ZThickness(self, universe): z_thickness = ZThickness(universe, **self.kwargs) z_thickness.run() reference = { 'n_residues': 50, 'n_frames': 1, 'z_thickness': np.full( (50, 1), fill_value=20) # all lipids have a thickness of 20 Angstrom } assert z_thickness.z_thickness.shape == (reference['n_residues'], reference['n_frames']) assert_array_equal(z_thickness.z_thickness, reference['z_thickness'])
def test_relative_to_relative_with_timedelta_horizon(freqstr): """Test converting between relative and absolute with timedelta horizons.""" # Converts from relative to absolute and back to relative train = pd.Series(1, index=pd.date_range("2021-10-06", freq=freqstr, periods=3)) count, unit = _get_intervals_count_and_unit(freq=freqstr) fh = ForecastingHorizon( pd.timedelta_range(pd.to_timedelta(count, unit=unit), freq=freqstr, periods=3)) abs_fh = fh.to_absolute(train.index[-1]) converted_rel_fh = abs_fh.to_relative(train.index[-1]) assert_array_equal(converted_rel_fh, np.arange(1, 4))
def test_flip_flop(self, universe, leaflets, lipid_sel="name ROH"): # 3 flip-flop events with the second one a failure (remains in the upper leaflet) flip_flop = FlipFlop( universe=universe, lipid_sel=lipid_sel, leaflets=leaflets, frame_cutoff=1, ) flip_flop.run() reference = { 'events': [[0, 5, 8, 1], [0, 8, 11, 1], [0, 17, 19, -1]], 'success': ["Success", "Fail", "Success"] } assert_array_equal(flip_flop.flip_flops, reference['events']) assert_array_equal(flip_flop.flip_flop_success, reference['success'])
def test_flip_flop_same_leaflet(self, universe, leaflets, lipid_sel="name ROH"): flip_flop = FlipFlop( universe=universe, lipid_sel=lipid_sel, leaflets=np.ones_like(leaflets, dtype=np.int8), ) flip_flop.run() reference = { 'events': np.array([[], [], [], []]).T, 'success': np.array([]) } assert_array_equal(flip_flop.flip_flops, reference['events']) assert_array_equal(flip_flop.flip_flop_success, reference['success'])
def test_flip_flop_lower_mid_only(self, universe, leaflets, lipid_sel="name ROH"): leaflets = np.ones_like(leaflets, dtype=np.int8) * -1 leaflets[0, 1] = 0 flip_flop = FlipFlop(universe=universe, lipid_sel=lipid_sel, leaflets=leaflets) flip_flop.run() reference = { 'events': np.array([[0], [0], [2], [-1]]).T, 'success': np.array(["Fail"]) } assert_array_equal(flip_flop.flip_flops, reference['events']) assert_array_equal(flip_flop.flip_flop_success, reference['success'])
def test_hot_encoding(self): """Tests that the hot encoding feature works as expected by hot encoding an array. """ surrogate_model = SurrogateModel( regression_model=GaussianProcessRegressor, next_parameter_strategy=expected_improvement, ) ranges = np.array( [[1, 2, 3], ["titi", "toto", "tutu"], ["popo", "jojo"]], dtype=object) # manually get categorical ranges surrogate_model.get_categorical_ranges(ranges) parameter_array = np.array( [[1, "tutu", "popo"], [2, "toto", "popo"], [4, "toto", "popo"]], dtype=object) hot_encoded = surrogate_model.hot_encode(parameter_array) assert_array_equal( [[1, 0, 0, 1, 1, 0], [2, 0, 1, 0, 1, 0], [4, 0, 1, 0, 1, 0]], hot_encoded)
def test_flip_flop_framecut2(self, universe, leaflets, lipid_sel="name ROH"): # only two flip-flop events found now, both successful flip_flop = FlipFlop( universe=universe, lipid_sel=lipid_sel, leaflets=leaflets, frame_cutoff=2, ) flip_flop.run() reference = { 'events': [[0, 5, 11, 1], [0, 17, 19, -1]], 'success': ["Success", "Success"] } assert_array_equal(flip_flop.flip_flops, reference['events']) assert_array_equal(flip_flop.flip_flop_success, reference['success'])
def test_flip_flop_framecut8(self, universe, leaflets, lipid_sel="name ROH"): # No flip-flop events found now # Cholesterol doesn't remain in opposing lealet for long enough flip_flop = FlipFlop( universe=universe, lipid_sel=lipid_sel, leaflets=leaflets, frame_cutoff=8, ) flip_flop.run() reference = { 'events': np.array([[], [], [], []]).T, 'success': np.array([]) } assert_array_equal(flip_flop.flip_flops, reference['events']) assert_array_equal(flip_flop.flip_flop_success, reference['success'])
def test_infer_type_arrays(self): """Tests that the static method built to infer the types of an array of arrays. """ bb_obj = BBOptimizer( black_box=self.parabola, heuristic="surrogate_model", max_iteration=50, initial_sample_size=2, parameter_space=parameter_space, next_parameter_strategy=expected_improvement, regression_model=GaussianProcessRegressor, stop_criterion="improvement_criterion", stop_window=4, improvement_estimator=min, improvement_threshold=0.1 ) array_to_infer = np.array([ ["1", "2", "3"], ["1", "toto", "2"]]) expected_inference = np.array( [np.array([1, 2, 3]), np.array([1, "toto", 2])]) assert_array_equal(array_to_infer, expected_inference)
def test_stratified_groups_kfold(): n_samples, n_features = 200, 20 X = np.random.rand(n_samples, n_features) y = np.random.rand(n_samples) bins = np.digitize(y, bins=[0, 0.2, 0.4, 0.6, 0.8, 1]) skcv = StratifiedKFold(n_splits=3, shuffle=True, random_state=42) jucv = StratifiedGroupsKFold(n_splits=3, shuffle=True, random_state=42) for (sk_train, sk_test), (ju_train, ju_test) in zip( skcv.split(X, bins), jucv.split(X, y, groups=bins)): assert_array_equal(sk_train, ju_train) assert_array_equal(sk_test, ju_test) skcv = RepeatedStratifiedKFold(n_repeats=4, n_splits=3, random_state=42) jucv = RepeatedStratifiedGroupsKFold( n_repeats=4, n_splits=3, random_state=42) for (sk_train, sk_test), (ju_train, ju_test) in zip( skcv.split(X, bins), jucv.split(X, y, groups=bins)): assert_array_equal(sk_train, ju_train) assert_array_equal(sk_test, ju_test)
def test_stoichiometry(tfa_rxn): assert_array_equal( tfa_rxn.cal_stoichiometric_matrix().tolist(), [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 3.0, -4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ], )