def test_score(): # define batch input data: batch_input = { "dat1": np.array([[[0, 0, 1, 0], [0, 1, 0, 0]]]) } # 1 sample, seqlen 2, onehot encoded m = Mutation(DummyModel(), "dat1", ['diff']) scores_ret = m.score(batch_input) # expected output: for smpl_i, smpl in enumerate(scores_ret): for i in range(len(smpl)): for j in range(len(smpl[i])): exp = (np.arange(0, 4) == j).astype(int) - batch_input['dat1'][smpl_i, i, :] if np.all(exp == 0): assert smpl[i][j] is None else: smpl_diff = smpl[i][j][ 0] # select the score i,j and the score 0 which is 'diff' here model_out_diff = smpl_diff['dat1'] assert np.all(model_out_diff[i, :] == exp) # test with selector_fn sel_fn = lambda x: x['dat1'] m = Mutation(DummyModel(), "dat1", ['diff'], output_sel_fn=sel_fn) scores_ret = m.score(batch_input) for smpl_i, smpl in enumerate(scores_ret): for i in range(len(smpl)): for j in range(len(smpl[i])): exp = (np.arange(0, 4) == j).astype(int) - batch_input['dat1'][smpl_i, i, :] if np.all(exp == 0): assert smpl[i][j] is None else: smpl_diff = smpl[i][j][ 0] # select the score i,j and the score 0 which is 'diff' here model_out_diff = smpl_diff assert np.all(model_out_diff[i, :] == exp)
def test_score(): for model_input_type in input_examples: # define batch input data: batch_input = batch_inputs[model_input_type] m = Mutation(get_dummy_model(model_input_type), "dat1", ['diff']) scores_ret = m.score(batch_input) # expected output: for smpl_i, smpl in enumerate(scores_ret): for i in range(len(smpl)): for j in range(len(smpl[i])): bi = batch_input if model_input_type == "dict": bi = batch_input['dat1'] elif model_input_type == "list": bi = batch_input[0] exp = (np.arange(0, 4) == j).astype(int) - bi[smpl_i, i, :] if np.all(exp == 0): assert smpl[i][j] is None else: smpl_diff = smpl[i][j][ 0] # select the score i,j and the score 0 which is 'diff' here model_out_diff = smpl_diff if model_input_type == "dict": model_out_diff = smpl_diff['dat1'] elif model_input_type == "list": model_out_diff = smpl_diff[0] assert np.all(model_out_diff[i, :] == exp) # test with selector_fn if model_input_type == "dict": sel_fn = lambda x: x['dat1'] elif model_input_type == "list": sel_fn = lambda x: x[0] else: sel_fn = lambda x: x m = Mutation(get_dummy_model(model_input_type), "dat1", ['diff'], output_sel_fn=sel_fn) scores_ret = m.score(batch_input) for smpl_i, smpl in enumerate(scores_ret): for i in range(len(smpl)): for j in range(len(smpl[i])): bi = batch_input if model_input_type == "dict": bi = batch_input['dat1'] elif model_input_type == "list": bi = batch_input[0] exp = (np.arange(0, 4) == j).astype(int) - bi[smpl_i, i, :] if np.all(exp == 0): assert smpl[i][j] is None else: smpl_diff = smpl[i][j][ 0] # select the score i,j and the score 0 which is 'diff' here model_out_diff = smpl_diff assert np.all(model_out_diff[i, :] == exp) # test the test_ref_ref functionality m = Mutation(get_dummy_model(model_input_type), "dat1", ['diff'], test_ref_ref=True) scores_ret = m.score(batch_input) # expected output: for smpl_i, smpl in enumerate(scores_ret): for i in range(len(smpl)): for j in range(len(smpl[i])): bi = batch_input if model_input_type == "dict": bi = batch_input['dat1'] elif model_input_type == "list": bi = batch_input[0] exp = (np.arange(0, 4) == j).astype(int) - bi[smpl_i, i, :] # if test_ref_ref is set the score has to be returned for all! smpl_diff = smpl[i][j][ 0] # select the score i,j and the score 0 which is 'diff' here model_out_diff = smpl_diff if model_input_type == "dict": model_out_diff = smpl_diff['dat1'] elif model_input_type == "list": model_out_diff = smpl_diff[0] assert np.all(model_out_diff[i, :] == exp)