def test_execute_predict(workdir, create_models): _, model_filenames = create_models model_name = 'model' output_model_path = workdir / 'model' / model_name pred_path = workdir / 'pred' / 'pred' # first train models assert not pred_path.exists() command = ['train'] command.extend(model_filenames) algo.execute(DummyAlgo(), sysargs=command) assert output_model_path.exists() assert pred_path.exists() with open(pred_path, 'r') as f: pred = json.load(f) assert pred == [3, 4, 5] pred_path.unlink() # do predict on output model pred_path = workdir / 'pred' / 'pred' assert not pred_path.exists() algo.execute(DummyAlgo(), sysargs=['predict', model_name]) assert pred_path.exists() with open(pred_path, 'r') as f: pred = json.load(f) assert pred == [0, 1, 2, 0, 1, 2, 0, 1, 2]
def test_execute_train(workdir): output_model_path = workdir / 'model' / 'model' assert not output_model_path.exists() algo.execute(DummyAlgo(), sysargs=['train']) assert output_model_path.exists() algo.execute(DummyAlgo(), sysargs=['train', '--dry-run'])
def test_execute_aggregate(workdir): output_model_path = workdir / 'model' / 'model' assert not output_model_path.exists() algo.execute(DummyAggregateAlgo(), sysargs=['aggregate']) assert output_model_path.exists() output_model_path.unlink() algo.execute(DummyAggregateAlgo(), sysargs=['aggregate', '--debug']) assert output_model_path.exists()
def test_models_generator(mocker, workdir, create_models, use_models_generator, models_type): _, model_filenames = create_models command = ['train'] command.extend(model_filenames) a = DummyAlgo() a.use_models_generator = use_models_generator mocker.patch.object(a, 'train', autospec=True, return_value={}) algo.execute(a, sysargs=command) models = a.train.call_args[0][2] assert isinstance(models, models_type)
def test_execute_aggregate_multiple_models(workdir, create_models): _, model_filenames = create_models output_model_path = workdir / 'model' / 'model' assert not output_model_path.exists() command = ['aggregate'] command.extend(model_filenames) algo.execute(DummyAggregateAlgo(), sysargs=command) assert output_model_path.exists() with open(output_model_path, 'r') as f: model = json.load(f) assert model['value'] == 3
def test_execute_predict(workdir, create_models): _, input_models_folder, head_filename, trunk_filename = create_models pred_path = workdir / 'pred' / 'pred' assert not pred_path.exists() command = [ 'predict', '--input-models-path', str(input_models_folder), '--input-head-model-filename', head_filename, '--input-trunk-model-filename', trunk_filename, ] algo.execute(DummyCompositeAlgo(), sysargs=command) assert pred_path.exists() with open(pred_path, 'r') as f: pred = json.load(f) assert pred == [] pred_path.unlink()
def test_execute_train(workdir): output_models_path = workdir / 'output_models' output_head_model_filename = 'head_model' output_trunk_model_filename = 'trunk_model' output_head_model_path = output_models_path / output_head_model_filename assert not output_head_model_path.exists() output_trunk_model_path = output_models_path / output_trunk_model_filename assert not output_trunk_model_path.exists() common_args = [ '--output-models-path', str(output_models_path), '--output-head-model-filename', output_head_model_filename, '--output-trunk-model-filename', output_trunk_model_filename, ] algo.execute(DummyCompositeAlgo(), sysargs=['train'] + common_args) assert output_head_model_path.exists() assert output_trunk_model_path.exists()
def test_execute_train_multiple_models(workdir, create_models): _, model_filenames = create_models output_model_path = workdir / 'model' / 'model' assert not output_model_path.exists() pred_path = workdir / 'pred' / 'pred' assert not pred_path.exists() command = ['train'] command.extend(model_filenames) algo.execute(DummyAlgo(), sysargs=command) assert output_model_path.exists() with open(output_model_path, 'r') as f: model = json.load(f) assert model['value'] == 3 assert pred_path.exists() with open(pred_path, 'r') as f: pred = json.load(f) assert pred == [3, 4, 5]
def test_execute_train_multiple_models(workdir, create_models): _, input_models_folder, head_filename, trunk_filename = create_models output_models_folder_path = workdir / 'output_models' output_head_model_filename = 'output_head_model' output_head_model_path = output_models_folder_path / output_head_model_filename assert not output_head_model_path.exists() output_trunk_model_filename = 'output_trunk_model' output_trunk_model_path = output_models_folder_path / output_trunk_model_filename assert not output_trunk_model_path.exists() pred_path = workdir / 'pred' / 'pred' assert not pred_path.exists() command = [ 'train', '--input-models-path', str(input_models_folder), '--input-head-model-filename', head_filename, '--input-trunk-model-filename', trunk_filename, '--output-models-path', str(output_models_folder_path), '--output-head-model-filename', output_head_model_filename, '--output-trunk-model-filename', output_trunk_model_filename, ] algo.execute(DummyCompositeAlgo(), sysargs=command) assert output_head_model_path.exists() with open(output_head_model_path, 'r') as f: head_model = json.load(f) assert head_model['value'] == 2 assert output_trunk_model_path.exists() with open(output_trunk_model_path, 'r') as f: trunk_model = json.load(f) assert trunk_model['value'] == -2 assert not pred_path.exists()
def test_execute_train(workdir): output_model_path = workdir / 'model' / 'model' assert not output_model_path.exists() algo.execute(DummyAlgo(), sysargs=['train']) assert output_model_path.exists() algo.execute(DummyAlgo(), sysargs=['train', '--fake-data', '--n-fake-samples', '1']) assert output_model_path.exists() algo.execute(DummyAlgo(), sysargs=['train', '--debug']) assert output_model_path.exists()
def test_execute_predict(workdir, create_models): _, model_filenames = create_models model_name = 'model' output_model_path = workdir / 'model' / model_name pred_path = workdir / 'pred' / 'pred' # first train models assert not pred_path.exists() command = ['train'] command.extend(model_filenames) algo.execute(DummyAlgo(), sysargs=command) assert output_model_path.exists() # do predict on output model assert not pred_path.exists() algo.execute(DummyAlgo(), sysargs=['predict', model_name]) assert pred_path.exists() with open(pred_path, 'r') as f: pred = json.load(f) assert pred == 'XXX' pred_path.unlink() # do predict with different model paths input_models_dir = workdir / 'other_models' input_models_dir.mkdir() input_model_path = input_models_dir / 'supermodel' shutil.move(output_model_path, input_model_path) assert not pred_path.exists() algo.execute(DummyAlgo(), sysargs=[ 'predict', 'supermodel', '--models-path', str(input_models_dir) ]) assert pred_path.exists() with open(pred_path, 'r') as f: pred = json.load(f) assert pred == 'XXX'