def build_experiment(config, name): config_path = op.join(get_config_dir(), f'{name}.json') with open(config_path, 'w') as f: json.dump(config, f, indent=2) config['config_path'] = config_path config['experiment_path'] = op.join(base_config['experiment_path'], name) get_resource_manager(config)['experiment'] return
def flat(split: Iterable, config_path: str, experiment_path: str, *, makefile: str): """ Generates and experiment with a 'flat' structure: each created subdirectory will contain triplets of ids (train, validation, test) defined in the `split`. Parameters ---------- split: Iterable an iterable that yield triplets of ids: (train, validation, test) config_path: str the path to the config file experiment_path: str the path where the experiment will be stored makefile: str the path to the Snakemake file with experiment instructions """ makefile_path = os.path.join(os.path.dirname(__file__), makefile) assert os.path.exists(makefile_path), f'no {makefile_path} found' for i, ids in enumerate(split): local = os.path.join(experiment_path, f'experiment_{i}') os.makedirs(local) shutil.copyfile(makefile_path, os.path.join(local, 'Snakefile')) for val, prefix in zip(ids, ('train', 'val', 'test')): path = os.path.join(local, f'{prefix}_ids.json') with open(path, "w") as f: json.dump(val, f, indent=0) # resource manager is needed here, because there may be inheritance rm = get_resource_manager(config_path) rm.save_config(os.path.join(experiment_path, 'resources.config'))
class TestMNIST(unittest.TestCase): sortTestMethodsUsing = None base_path = Path('~/tests/MNIST').expanduser() base_rm = get_resource_manager('dpipe/tests/mnist/setup.config') experiment_path = base_rm.experiment_path @classmethod def setUpClass(cls): def download(filename): path = cls.base_path / filename if not path.exists(): os.makedirs(cls.base_path, exist_ok=True) urlretrieve('http://yann.lecun.com/exdb/mnist/' + filename, path) download('train-images-idx3-ubyte.gz') download('train-labels-idx1-ubyte.gz') @classmethod def tearDownClass(cls): if cls.experiment_path.exists(): shutil.rmtree(cls.experiment_path) def test_build_experiment(self): self.base_rm.build_experiment names = {p.name for p in self.experiment_path.iterdir()} self.assertSetEqual({'resources.config'} | {f'experiment_{i}' for i in range(len(names) - 1)}, names) def test_ids(self): ids = set(self.base_rm.dataset.ids) test_ids = [] for exp in self.experiment_path.iterdir(): if exp.is_dir(): train, val, test = [set(load_json(exp / f'{name}_ids.json')) for name in ['train', 'val', 'test']] test_ids.append(test) self.assertFalse(train & val) self.assertFalse(train & test) self.assertFalse(val & test) self.assertSetEqual(ids, train | val | test) for first, second in itertools.permutations(test_ids, 2): self.assertFalse(first & second) def test_score(self): rm = get_resource_manager(self.experiment_path / 'resources.config') os.chdir(self.experiment_path / 'experiment_0') rm.run_experiment self.assertGreater(.97, load_json('test_metrics/accuracy.json'))
import os from tqdm import tqdm import numpy as np import tensorflow as tf from dpipe.config import get_config, get_resource_manager if __name__ == '__main__': resource_manager = get_resource_manager( get_config('config_path', 'ids_path', 'restore_model_path', 'predictions_path')) predictions_path = resource_manager['predictions_path'] restore_model_path = resource_manager['restore_model_path'] ids = resource_manager['ids'] load_x = resource_manager['load_x'] frozen_model = resource_manager['frozen_model'] os.makedirs(predictions_path) with tf.Session(graph=frozen_model.graph) as session: frozen_model.prepare(session, restore_model_path) for identifier in tqdm(ids): x = load_x(identifier) y = frozen_model.predict_object(x) np.save(os.path.join(predictions_path, str(identifier)), y) # saving some memory del x, y
def test_score(self): rm = get_resource_manager(self.experiment_path / 'resources.config') os.chdir(self.experiment_path / 'experiment_0') rm.run_experiment self.assertGreater(.97, load_json('test_metrics/accuracy.json'))
"""Script that runs command from config.""" import argparse from dpipe.config import get_resource_manager if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('command') parser.add_argument('--config_path', required=True) args = parser.parse_known_args()[0] get_resource_manager(args.config_path).get_resource(args.command)
import os import numpy as np from dpipe.config import get_config, get_resource_manager from utils import load_by_id if __name__ == '__main__': resource_manager = get_resource_manager( get_config('config_path', 'ids_path', 'predictions_path', 'thresholds_path', 'binary_predictions_path')) binary_predictions_path = resource_manager['binary_predictions_path'] ids = resource_manager['ids'] thresholds_path = resource_manager['thresholds_path'] predict_path = resource_manager['predictions_path'] thresholds = np.load(thresholds_path) channels = len(thresholds) os.makedirs(binary_predictions_path) for identifier in ids: y = load_by_id(predict_path, identifier) assert len(y) == channels y = y > thresholds[:, None, None, None] np.save(os.path.join(binary_predictions_path, str(identifier)), y) del y
import os import numpy as np from tqdm import tqdm from dpipe.config import get_config, get_resource_manager if __name__ == '__main__': resource_manager = get_resource_manager( get_config('config_path', 'input_path', 'output_path', 'transform') ) transform = resource_manager['transform'] input_path = resource_manager['input_path'] output_path = resource_manager['output_path'] transform = resource_manager[resource_manager['transform']] os.makedirs(output_path) for f in tqdm(os.listdir(input_path)): np.save(os.path.join(output_path, f), transform(np.load(os.path.join(input_path, f))))
import os import json import numpy as np from tqdm import tqdm from dpipe.config import get_config, get_resource_manager from dpipe.medim.metrics import multichannel_dice_score if __name__ == '__main__': resource_manager = get_resource_manager( get_config('config_path', 'dices_path', 'predictions_path')) dataset = resource_manager['dataset'] dices_path = resource_manager['dices_path'] predictions_path = resource_manager['predictions_path'] dices = {} for f in tqdm(os.listdir(predictions_path)): identifier = f.replace('.npy', '') y_true = dataset.load_msegm(identifier) y = np.load(os.path.join(predictions_path, f)) dices[identifier] = multichannel_dice_score(y, y_true) with open(dices_path, 'w') as f: json.dump(dices, f, indent=0)
from dpipe.config import get_config, get_resource_manager if __name__ == '__main__': config = get_config('config_path', 'experiment_path') get_resource_manager(config)['experiment'] print('Experiment was build')
import argparse from dpipe.config import get_resource_manager if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('config_path') parser.add_argument('result_path') args = parser.parse_args() get_resource_manager(args.config_path).save_config(args.result_path)