Source code for materials_commons.cli.subcommands.expt

import json
import sys
import materials_commons.api as mcapi
import materials_commons.cli.functions as clifuncs
from materials_commons.cli.list_objects import ListObjects

[docs]def set_current_experiment(project_local_path, expt=None): pconfig = clifuncs.read_project_config(project_local_path) if expt is None: pconfig.experiment_id = None pconfig.experiment_uuid = None else: pconfig.experiment_id = pconfig.experiment_uuid = expt.uuid
[docs]def make_parser(): """Make argparse.ArgumentParser for `mc expt`""" return ExptSubcommand().make_parser()
[docs]class ExptSubcommand(ListObjects): """ List, create, delete, and modify experiments mc expt mc expt [-c] <exptname> """ def __init__(self): super(ExptSubcommand, self).__init__( ["expt"], "Experiment", "Experiments", requires_project=True, proj_member=True, expt_member=False, list_columns=['current', 'name', 'description', 'owner', 'id', 'updated_at'], headers=['', 'name', 'description', 'owner', 'id', 'updated_at'], creatable=True, deletable=True, custom_actions=['unset'], custom_selection_actions=['set'] )
[docs] def get_all_from_experiment(self, expt): raise Exception("Experiments are not members of experiments")
[docs] def get_all_from_project(self, proj): users = proj.remote.list_users() users_by_id = { for u in users} all_project_experiments = proj.remote.get_all_experiments( for expt in all_project_experiments: expt.project = proj expt.owner = users_by_id[expt.owner_id] return all_project_experiments
[docs] def list_data(self, obj, args): _is_current = ' ' pconfig = clifuncs.read_project_config(self.working_dir) if pconfig and == pconfig.experiment_id: _is_current = '*' return { 'current': _is_current, 'name': clifuncs.trunc(, 40), 'description': clifuncs.trunc(obj.description, 100), 'owner': clifuncs.trunc(, 40), 'id':, 'updated_at': clifuncs.format_time(obj.updated_at) }
[docs] def print_details(self, obj, args, out=sys.stdout): description = None if obj.description: description = obj.description data = [ {"name":}, {"description": description}, {"id":}, {"uuid": obj.uuid}, {"owner": obj.owner_id}, {"updated_at": clifuncs.format_time(obj.updated_at)} ] for d in data: out.write(yaml.dump(d, width=70, indent=4), end='') out.write("\n")
[docs] def create(self, args, out=sys.stdout): proj = clifuncs.make_local_project(self.working_dir) expt_list = proj.remote.get_all_experiments( expt_names = { e for e in expt_list} if len(args.expr) != 1: out.write('Creating an experiment requires one name argument\n') out.write('example: mc expt --create --desc "experiment description" <experiment_name>\n') return for name in args.expr: if name not in expt_names: expt_request = mcapi.CreateExperimentRequest(description=args.desc) expt = proj.remote.create_experiment(, name, attrs=expt_request) out.write('Created experiment: ' + + "\n") set_current_experiment(proj.local_path, expt) out.write("Set current experiment: '" + + "'\n") else: out.write('experiment: \'' + name + '\' already exists\n') return
[docs] def delete(self, objects, args, dry_run, out=sys.stdout): if dry_run: out.write('Dry-run is not possible when deleting experiments.\n') raise cliexcept.MCCLIException("Invalid expt request") proj = clifuncs.make_local_project(self.working_dir) project_config = clifuncs.read_project_config(proj.local_path) current_experiment_id = project_config.experiment_id for obj in objects: if == current_experiment_id: set_current_experiment(proj.local_path, None) out.write("Unset current experiment\n") proj.remote.delete_experiment(, out.write('Deleted experiment: ' + + ' ' + str( + '\n') # for key, val in obj.delete_tally.__dict__.items(): # out.write(str(key) + ' ' + str(val) + '\n') out.write('\n')
[docs] def set(self, objects, args, out=sys.stdout): if len(objects) != 1: out.write('set one current experiment at a time\n') out.write('example: mc expt --set <name>\n') raise cliexcept.MCCLIException("Invalid expt request") for expt in objects: set_current_experiment(expt.project.local_path, expt) out.write("Set current experiment: '" + + "'\n") return
[docs] def unset(self, args, out=sys.stdout): set_current_experiment(clifuncs.project_path(self.working_dir), None) out.write("Unset current experiment\n") return
[docs] def add_custom_options(self, parser): # for --create, add experiment description parser.add_argument('--desc', type=str, default="", help='Experiment description') # custom action --set parser.add_argument('--set', action="store_true", default=False, help='Set current experiment') # custom action --unset parser.add_argument('--unset', action="store_true", default=False, help='Unset current experiment')