def test_execution_get_dftbp(self): """Can we execute the declared get_dftbp_* tasks?""" taskdict = {} update_taskdict(taskdict, [['skpar.core.taskdict', ['get', 'sub', 'run']]]) update_taskdict( taskdict, [['skpar.dftbutils', ['get_bs', 'get_meff', 'get_Ek']]]) userinp = yaml.load(self.yamlin) tasklist = get_tasklist(userinp['tasks']) tasks = initialise_tasks(tasklist, taskdict) # database = Database() env = { 'workroot': '.', } for task in tasks: print(task) task(env, database) # 'Si.bs' should be added through the task execution db = database.get('Si.bs') self.assertAlmostEqual(db['Egap'], 1.129, places=3) self.assertAlmostEqual(db['Ef'], -3.0621, places=4) self.assertAlmostEqual(db['me_GX'], 0.935, places=3) self.assertAlmostEqual(db['mh_GK'], -1.891, places=3) self.assertAlmostEqual(db['Ec_L_0'], 0.4, places=3) self.assertAlmostEqual(db['Ec_G_0'], 1.6156, places=3) self.assertAlmostEqual(db['Ec_X_0'], 0.2025, places=3) self.assertAlmostEqual(db['Ec_U_0'], 0.6915, places=3) self.assertAlmostEqual(db['Ec_K_0'], 0.6915, places=3)
def test_simple(self): """Can we parse task declarations successfully""" jsondata = """ { "tasks": [ {"sub": [["./tmp/template.parameters.dat"]]} , {"run": ["cp parameters.dat value.dat", "./tmp"]} , {"get": ["value", "tmp/value.dat", "model"]} ] } """ yamldata = """ tasks: - sub: [[./tmp/template.parameters.dat]] - run: ["cp parameters.dat value.dat", ./tmp] - get: [value, tmp/value.dat, model] """ taskdict = {} update_taskdict(taskdict, [['skpar.core.taskdict', ['get', 'sub', 'run']]]) yamldata = yaml.load(yamldata) # print('yaml data') # pprint(yamldata) jsondata = json.loads(jsondata) # print('json data') # pprint(jsondata) # self.assertTrue(jsondata == yamldata) # fails for whatever reason tasklist = [] userinp = yamldata tasklist = get_tasklist(userinp['tasks']) tasks = initialise_tasks(tasklist, taskdict) # var = 10 database = Database() par = Parameter('p0', value=var) workroot = './' coreargs = { 'workroot': workroot, 'parametervalues': [par.value], 'parameternames': [par.name] } try: shutil.rmtree('./tmp') except FileNotFoundError: pass os.makedirs('./tmp') with open('./tmp/template.parameters.dat', 'w') as template: template.writelines("%(p0)f\n") # with open('./tmp/template.parameters.dat', 'r') as template: # tmplstr = template.readlines() # print(tmplstr) LOGGER.info('Executing tasks') for task in tasks: # LOGGER.info(task) task(coreargs, database) self.assertEqual(np.atleast_1d(var), database.get('model', {}).get('value')) shutil.rmtree('./tmp')
def test_update_taskdict_string(self): """Check we can update taskdict from user module""" tag = 'skpar.core.taskdict' taskdict = {} update_taskdict(taskdict, tag) self.assertEqual(len(coretd), len(taskdict)) for key, val in coretd.items(): self.assertTrue('.'.join([tag, key]) in taskdict) self.assertEqual(val, taskdict[tag + '.' + key])
def test_parse_tasks_core(self): """Can we read tasks well and initialise correctly?""" taskdict = {} update_taskdict(taskdict, [['skpar.core.taskdict', ['sub', 'get', 'run']]]) skparin = 'example-tasks.yaml' userinp = get_input(skparin) tasklist = get_tasklist(userinp['tasks']) for i, task in enumerate(tasklist): LOGGER.info('task %i : %s', i, task) tasks = initialise_tasks(tasklist, taskdict, report=True) self.assertEqual(len(tasks), 6)
def test_update_taskdict_alias(self): """Check we can update taskdict from user module with alias""" yamlinput = """ usermodules: - [skpar.dftbutils, dftb] """ userinp = yaml.load(yamlinput)['usermodules'] taskdict = {} update_taskdict(taskdict, userinp) tag = 'dftb' self.assertEqual(len(dftbtd), len(taskdict)) for key, val in dftbtd.items(): self.assertTrue('.'.join([tag, key]) in taskdict) self.assertEqual(val, taskdict[tag + '.' + key])
def test_update_taskdict(self): """Check we can update taskdict from user module""" yamlinput = """ usermodules: - skpar.core.taskdict """ userinp = yaml.load(yamlinput)['usermodules'] taskdict = {} update_taskdict(taskdict, userinp) tag = 'skpar.core.taskdict' self.assertEqual(len(coretd), len(taskdict)) for key, val in coretd.items(): self.assertTrue('.'.join([tag, key]) in taskdict) self.assertEqual(val, taskdict[tag + '.' + key])
def test_update_taskdict_multiple(self): """Check we can update taskdict from multiple modules""" yamlinput = """ usermodules: - [skpar.core.taskdict, [set, get, run, plot]] - [skpar.dftbutils, [get_bs]] """ userinp = yaml.load(yamlinput)['usermodules'] taskdict = {} update_taskdict(taskdict, userinp) self.assertEqual(len(taskdict), 5) for key in ['set', 'get', 'run', 'plot']: self.assertTrue(key in taskdict) self.assertEqual(coretd[key], taskdict[key]) for key in ['get_bs']: self.assertTrue(key in taskdict) self.assertEqual(dftbtd[key], taskdict[key])
def test_parsetask(self): """Can we parse task declarations successfully""" taskdict = {} update_taskdict(taskdict, [['skpar.core.taskdict', ['set', 'run']]]) update_taskdict(taskdict, [['skpar.dftbutils', ['get_meff', 'get_data']]]) userinp = yaml.load(self.yamldata)['tasks'] tasklist = [] tasklist = get_tasklist(userinp) tasks = initialise_tasks(tasklist, taskdict) # tasknames = ['set', 'run', 'get_data', 'get_meff'] self.assertListEqual([task.name for task in tasks], tasknames) # functions = [ coretd.substitute_parameters, coretd.execute, dftbtd.get_dftbp_data, dftbtd.get_effmasses ] self.assertListEqual([task.func for task in tasks], functions)
def test_twopartemplates(self): """Can we parse task declarations successfully""" yamldata = """ tasks: - sub: [[./tmp/template.par1.dat, ./tmp/template.par2.dat]] - run: ['bash run.sh', ./tmp] - get: [value, tmp/values.dat, model] """ taskdict = {} update_taskdict(taskdict, [['skpar.core.taskdict', ['get', 'sub', 'run']]]) yamldata = yaml.load(yamldata)['tasks'] tasklist = [] tasklist = get_tasklist(yamldata) tasks = initialise_tasks(tasklist, taskdict) # var1 = 10 var2 = 20 database = Database() params = [Parameter('p0', value=var1), Parameter('p1', value=var2)] workroot = './' coreargs = { 'parametervalues': [p.value for p in params], 'parameternames': [p.name for p in params] } try: shutil.rmtree('./tmp') except FileNotFoundError: pass os.makedirs('./tmp') with open('./tmp/template.par1.dat', 'w') as template: template.writelines("%(p0)f\n") with open('./tmp/template.par2.dat', 'w') as template: template.writelines("%(p1)f\n") with open('./tmp/run.sh', 'w') as template: template.writelines('cat par*.dat > values.dat\n') for task in tasks: LOGGER.info(task) task(coreargs, database) self.assertListEqual([var1, var2], list(database.get('model', {}).get('value'))) shutil.rmtree('./tmp')
def test_update_taskdict_explicit(self): """Check we can update taskdict from user module with explicit tasks""" yamlinput = """ usermodules: - [skpar.core.taskdict, [set, get, run, plot]] """ taskdict = {} userinp = yaml.load(yamlinput)['usermodules'] update_taskdict(taskdict, userinp) self.assertEqual(4, len(taskdict)) for key in ['set', 'get', 'run', 'plot']: self.assertTrue(key in taskdict) self.assertEqual(coretd[key], taskdict[key]) # check failing with missing task yamlinput = """ usermodules: - [skpar.core.taskdict, [set, mambo]] """ taskdict = {} userinp = yaml.load(yamlinput)['usermodules'] self.assertRaises(KeyError, update_taskdict, taskdict, userinp)
def parse_input(filename, verbose=True): """Parse input filename and return the setup """ userinp = get_input(filename) # # CONFIG configinp = userinp.get('config', None) config = get_config(configinp, report=True) # # OPTIMISATION optinp = userinp.get('optimisation', None) optimisation = get_optargs(optinp) # # TASKS taskdict = {} usermodulesinp = userinp.get('usermodules', None) # Note the statement below emulates a yaml-like input which delivers # a list of [module, [list of functions]] items. update_taskdict( taskdict, [[coretd.__name__, list(coretd.TASKDICT.keys())]]) # Import user tasks after the core ones, to allow potential # replacement of `taskdict` entries with user-defined functions if usermodulesinp: update_taskdict(taskdict, usermodulesinp) # taskinp = userinp.get('tasks', None) tasklist = get_tasklist(taskinp) check_taskdict(tasklist, taskdict) # do trial initialisation in order to report what and how's been parsed # no assignment means we discard the tasks list here initialise_tasks(tasklist, taskdict, report=True) # # OBJECTIVES objectivesinp = userinp.get('objectives', None) objectives = set_objectives(objectivesinp, verbose=verbose) # return taskdict, tasklist, objectives, optimisation, config