def test_stack_config_idx(self): """Test if config_idx is updated correctly after stack.""" stimulus_set = np.array(( (0, 1, 2, 3, -1, -1, -1, -1, -1), (9, 12, 7, 1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 7, -1, -1, -1, -1), (3, 4, 2, 6, 7, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17))) # Create first set of original trials. n_select = np.array((1, 1, 1, 1, 1)) trials_0 = trials.RankDocket(stimulus_set, n_select=n_select) desired_config_idx = np.array((0, 0, 1, 1, 2)) np.testing.assert_array_equal(trials_0.config_idx, desired_config_idx) # Create second set of original trials, with non-overlapping # configuration. n_select = np.array((2, 2, 2, 2, 2)) trials_1 = trials.RankDocket(stimulus_set, n_select=n_select) desired_config_idx = np.array((0, 0, 1, 1, 2)) np.testing.assert_array_equal(trials_1.config_idx, desired_config_idx) # Stack trials trials_stack = trials.stack((trials_0, trials_1)) desired_config_idx = np.array((0, 0, 1, 1, 2, 3, 3, 4, 4, 5)) np.testing.assert_array_equal( trials_stack.config_idx, desired_config_idx)
def test_invalid_stimulus_set(self): """Test handling of invalid `stimulus_set` argument.""" # Non-integer input. stimulus_set = np.array(( (0., 1, 2, -1, -1, -1, -1, -1, -1), (9, 12, 7, -1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 7, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17))) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set) # Contains integers below -1. stimulus_set = np.array(( (0, 1, -2, -1, -1, -1, -1, -1, -1), (9, 12, 7, -1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 7, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17))) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set) # Does not contain enough references for each trial. stimulus_set = np.array(( (0, 1, 2, -1, -1, -1, -1, -1, -1), (9, 12, 7, -1, -1, -1, -1, -1, -1), (3, 4, -1, -1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17))) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set)
def setup_docket_1(): """ """ stimulus_set = np.array(((0, 1, 2, -1, -1, -1, -1, -1, -1), (9, 12, 7, -1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 7, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17)), dtype=np.int32) n_trial = 4 n_select = np.array((1, 1, 1, 2), dtype=np.int32) n_reference = np.array((2, 2, 4, 8), dtype=np.int32) is_ranked = np.array((True, True, True, True)) configurations = pd.DataFrame( { 'n_reference': np.array([2, 4, 8], dtype=np.int32), 'n_select': np.array([1, 1, 2], dtype=np.int32), 'is_ranked': [True, True, True], 'n_outcome': np.array([2, 4, 56], dtype=np.int32) }, index=[0, 2, 3]) configuration_id = np.array((0, 0, 1, 2)) docket = trials.RankDocket(stimulus_set, n_select=n_select) return { 'n_trial': n_trial, 'stimulus_set': stimulus_set, 'n_reference': n_reference, 'n_select': n_select, 'is_ranked': is_ranked, 'docket': docket, 'configurations': configurations, 'configuration_id': configuration_id }
def test_invalid_is_ranked(self): """Test handling of invalid 'is_ranked' argument.""" stimulus_set = np.array(( (0, 1, 2, -1, -1, -1, -1, -1, -1), (9, 12, 7, -1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 7, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17))) # Mismatch in number of trials is_ranked = np.array((True, True, True)) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set, is_ranked=is_ranked) is_ranked = np.array((True, False, True, False)) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set, is_ranked=is_ranked)
def test_possible_outcomes_2c1(self): """Test outcomes 2 choose 1 ranked trial.""" stimulus_set = np.array(((0, 1, 2), (9, 12, 7))) n_select = 1 * np.ones((2)) tasks = trials.RankDocket(stimulus_set, n_select=n_select) po = _possible_rank_outcomes(tasks.config_list.iloc[0]) correct = np.array(((0, 1), (1, 0))) np.testing.assert_array_equal(po, correct)
def test_invalid_n_select(self): """Test handling of invalid 'n_select' argument.""" stimulus_set = np.array(( (0, 1, 2, -1, -1, -1, -1, -1, -1), (9, 12, 7, -1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 7, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17))) # Mismatch in number of trials n_select = np.array((1, 1, 2)) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set, n_select=n_select) # Below support. n_select = np.array((1, 0, 1, 0)) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set, n_select=n_select) # Above support. n_select = np.array((2, 1, 1, 2)) with pytest.raises(Exception) as e_info: docket = trials.RankDocket(stimulus_set, n_select=n_select)
def test_possible_outcomes_4c2(self): """Test outcomes 4 choose 2 ranked trial.""" stimulus_set = np.array(((0, 1, 2, 3, 4), (45, 33, 9, 12, 7))) n_select = 2 * np.ones((2)) tasks = trials.RankDocket(stimulus_set, n_select=n_select) po = _possible_rank_outcomes(tasks.config_list.iloc[0]) correct = np.array(( (0, 1, 2, 3), (0, 2, 1, 3), (0, 3, 1, 2), (1, 0, 2, 3), (1, 2, 0, 3), (1, 3, 0, 2), (2, 0, 1, 3), (2, 1, 0, 3), (2, 3, 0, 1), (3, 0, 1, 2), (3, 1, 0, 2), (3, 2, 0, 1))) np.testing.assert_array_equal(po, correct)
def test_subset_config_idx(self): """Test if config_idx is updated correctly after subset.""" stimulus_set = np.array(( (0, 1, 2, -1, -1, -1, -1, -1, -1), (9, 12, 7, -1, -1, -1, -1, -1, -1), (3, 4, 5, 6, 7, -1, -1, -1, -1), (3, 4, 2, 6, 7, -1, -1, -1, -1), (3, 4, 5, 6, 13, 14, 15, 16, 17))) # Create original trials. n_select = np.array((1, 1, 1, 1, 2)) docket = trials.RankDocket(stimulus_set, n_select=n_select) desired_config_idx = np.array((0, 0, 1, 1, 2)) np.testing.assert_array_equal(docket.config_idx, desired_config_idx) # Grab subset and check that config_idx is updated to start at 0. trials_subset = docket.subset(np.array((2, 3, 4))) desired_config_idx = np.array((0, 0, 1)) np.testing.assert_array_equal( trials_subset.config_idx, desired_config_idx)
def test_possible_outcomes_8c1(self): """Test outcomes 8 choose 1 ranked trial.""" stimulus_set = np.array(( (0, 1, 2, 3, 4, 5, 6, 7, 8), (45, 33, 9, 12, 7, 2, 5, 4, 3))) n_select = 1 * np.ones((2)) tasks = trials.RankDocket(stimulus_set, n_select=n_select) po = _possible_rank_outcomes(tasks.config_list.iloc[0]) correct = np.array(( (0, 1, 2, 3, 4, 5, 6, 7), (1, 0, 2, 3, 4, 5, 6, 7), (2, 0, 1, 3, 4, 5, 6, 7), (3, 0, 1, 2, 4, 5, 6, 7), (4, 0, 1, 2, 3, 5, 6, 7), (5, 0, 1, 2, 3, 4, 6, 7), (6, 0, 1, 2, 3, 4, 5, 7), (7, 0, 1, 2, 3, 4, 5, 6))) np.testing.assert_array_equal(po, correct)