|
| 1 | +import os, textwrap |
| 2 | +import numpy as np |
| 3 | +import unittest |
| 4 | + |
| 5 | +from typing import Set, List |
| 6 | +from pathlib import Path |
| 7 | +try: |
| 8 | + from exploration.context import dpgen2 |
| 9 | +except ModuleNotFoundError: |
| 10 | + # case of upload everything to argo, no context needed |
| 11 | + pass |
| 12 | +from dflow.python import ( |
| 13 | + FatalError, |
| 14 | +) |
| 15 | +from dpgen2.exploration.scheduler import ( |
| 16 | + ConvergenceCheckStageScheduler, |
| 17 | + ExplorationScheduler, |
| 18 | +) |
| 19 | +from dpgen2.exploration.report import ExplorationReport |
| 20 | +from dpgen2.exploration.task import ExplorationTaskGroup, ExplorationStage |
| 21 | +from dpgen2.exploration.selector import TrustLevel, ConfSelectorLammpsFrames |
| 22 | +from mocked_ops import ( |
| 23 | + MockedExplorationReport, |
| 24 | + MockedExplorationTaskGroup, |
| 25 | + MockedExplorationTaskGroup1, |
| 26 | + MockedStage, |
| 27 | + MockedStage1, |
| 28 | +) |
| 29 | +from dpgen2.utils.dflow_query import ( |
| 30 | + get_last_scheduler, |
| 31 | + get_subkey, |
| 32 | + get_last_iteration, |
| 33 | + find_slice_ranges, |
| 34 | + sort_slice_ops, |
| 35 | + print_keys_in_nice_format, |
| 36 | +) |
| 37 | + |
| 38 | +dpgen_keys = [ |
| 39 | +'init--scheduler', |
| 40 | + 'init--id', |
| 41 | + 'iter-000000--prep-train', |
| 42 | + 'iter-000000--run-train-0002', |
| 43 | + 'iter-000000--run-train-0000', |
| 44 | + 'iter-000000--run-train-0001', |
| 45 | + 'iter-000000--prep-run-train', |
| 46 | + 'iter-000000--prep-lmp', |
| 47 | + 'iter-000000--run-lmp-000001', |
| 48 | + 'iter-000000--run-lmp-000004', |
| 49 | + 'iter-000000--run-lmp-000005', |
| 50 | + 'iter-000000--run-lmp-000002', |
| 51 | + 'iter-000000--run-lmp-000003', |
| 52 | + 'iter-000000--run-lmp-000000', |
| 53 | + 'iter-000000--prep-run-lmp', |
| 54 | + 'iter-000000--select-confs', |
| 55 | + 'iter-000000--prep-fp', |
| 56 | + 'iter-000000--run-fp-000001', |
| 57 | + 'iter-000000--run-fp-000000', |
| 58 | + 'iter-000000--prep-run-fp', |
| 59 | + 'iter-000000--collect-data', |
| 60 | + 'iter-000000--block', |
| 61 | + 'iter-000000--scheduler', |
| 62 | + 'iter-000000--id', |
| 63 | + 'iter-000001--prep-train', |
| 64 | + 'iter-000001--run-train-0000', |
| 65 | + 'iter-000001--run-train-0001', |
| 66 | + 'iter-000001--run-train-0002', |
| 67 | + 'iter-000001--prep-run-train', |
| 68 | + 'iter-000001--prep-lmp', |
| 69 | + 'iter-000001--run-lmp-000003', |
| 70 | + 'iter-000001--run-lmp-000000', |
| 71 | + 'iter-000001--run-lmp-000001', |
| 72 | + 'iter-000001--run-lmp-000005', |
| 73 | + 'iter-000001--run-lmp-000002', |
| 74 | + 'iter-000001--run-lmp-000004', |
| 75 | + 'iter-000001--prep-run-lmp', |
| 76 | + 'iter-000001--select-confs', |
| 77 | + 'iter-000001--prep-fp', |
| 78 | + 'iter-000001--run-fp-000001', |
| 79 | + 'iter-000001--run-fp-000000', |
| 80 | + 'iter-000001--prep-run-fp', |
| 81 | + 'iter-000001--collect-data', |
| 82 | + 'iter-000001--block', |
| 83 | + 'iter-000001--scheduler', |
| 84 | + 'iter-000001--id', |
| 85 | + 'iter-000000--loop' |
| 86 | +] |
| 87 | + |
| 88 | +class MockedTar: |
| 89 | + value = 10 |
| 90 | + |
| 91 | +class MockedFoo: |
| 92 | + parameters = { |
| 93 | + 'exploration_scheduler' : MockedTar() |
| 94 | + } |
| 95 | + |
| 96 | +class MockedBar: |
| 97 | + outputs = MockedFoo |
| 98 | + |
| 99 | +class MockedWF: |
| 100 | + def query_step(self,key=None): |
| 101 | + assert(key == 'iter1--scheduler') |
| 102 | + return MockedBar() |
| 103 | + |
| 104 | +class TestDflowQuery(unittest.TestCase): |
| 105 | + def test_get_subkey(self): |
| 106 | + self.assertEqual(get_subkey('aa--bb--cc', 0), 'aa') |
| 107 | + self.assertEqual(get_subkey('aa--bb--cc', 1), 'bb') |
| 108 | + self.assertEqual(get_subkey('aa--bb--cc', 2), 'cc') |
| 109 | + self.assertEqual(get_subkey('aa--bb--cc'), 'cc') |
| 110 | + self.assertEqual(get_subkey('aa'), 'aa') |
| 111 | + self.assertEqual(get_subkey('aa---bb'), '-bb') |
| 112 | + self.assertEqual(get_subkey('aa----bb', 1), '') |
| 113 | + self.assertEqual(get_subkey(''), '') |
| 114 | + |
| 115 | + def test_get_last_scheduler(self): |
| 116 | + value = get_last_scheduler( |
| 117 | + MockedWF(), |
| 118 | + ['iter1--scheduler', 'foo', 'bar', 'iter0--scheduler'], |
| 119 | + ) |
| 120 | + self.assertEqual(value, 10) |
| 121 | + |
| 122 | + def test_get_last_iteration(self): |
| 123 | + last = get_last_iteration(dpgen_keys) |
| 124 | + self.assertEqual(last, 1) |
| 125 | + |
| 126 | + def test_sort_slice_ops(self): |
| 127 | + idxes = find_slice_ranges(dpgen_keys, 'run-lmp') |
| 128 | + self.assertEqual(idxes, [[8, 14], [30, 36]]) |
| 129 | + |
| 130 | + def test_sort_slice_ops(self): |
| 131 | + expected_output = [ |
| 132 | + 'init--scheduler', |
| 133 | + 'init--id', |
| 134 | + 'iter-000000--prep-train', |
| 135 | + 'iter-000000--run-train-0000', |
| 136 | + 'iter-000000--run-train-0001', |
| 137 | + 'iter-000000--run-train-0002', |
| 138 | + 'iter-000000--prep-run-train', |
| 139 | + 'iter-000000--prep-lmp', |
| 140 | + 'iter-000000--run-lmp-000000', |
| 141 | + 'iter-000000--run-lmp-000001', |
| 142 | + 'iter-000000--run-lmp-000002', |
| 143 | + 'iter-000000--run-lmp-000003', |
| 144 | + 'iter-000000--run-lmp-000004', |
| 145 | + 'iter-000000--run-lmp-000005', |
| 146 | + 'iter-000000--prep-run-lmp', |
| 147 | + 'iter-000000--select-confs', |
| 148 | + 'iter-000000--prep-fp', |
| 149 | + 'iter-000000--run-fp-000000', |
| 150 | + 'iter-000000--run-fp-000001', |
| 151 | + 'iter-000000--prep-run-fp', |
| 152 | + 'iter-000000--collect-data', |
| 153 | + 'iter-000000--block', |
| 154 | + 'iter-000000--scheduler', |
| 155 | + 'iter-000000--id', |
| 156 | + 'iter-000001--prep-train', |
| 157 | + 'iter-000001--run-train-0000', |
| 158 | + 'iter-000001--run-train-0001', |
| 159 | + 'iter-000001--run-train-0002', |
| 160 | + 'iter-000001--prep-run-train', |
| 161 | + ] |
| 162 | + ncheck = len(expected_output) |
| 163 | + self.assertEqual( |
| 164 | + sort_slice_ops(dpgen_keys[:ncheck], ['run-train', 'run-lmp', 'run-fp']), |
| 165 | + expected_output, |
| 166 | + ) |
| 167 | + |
| 168 | + def test_print_keys(self): |
| 169 | + expected_output = [ |
| 170 | + ' 0 : init--scheduler', |
| 171 | + ' 1 : init--id', |
| 172 | + ' 2 : iter-000000--prep-train', |
| 173 | + ' 3 -> 5 : iter-000000--run-train-0000 -> iter-000000--run-train-0002', |
| 174 | + ' 6 : iter-000000--prep-run-train', |
| 175 | + ] |
| 176 | + expected_output = '\n'.join(expected_output + ['']) |
| 177 | + |
| 178 | + ret = print_keys_in_nice_format( |
| 179 | + dpgen_keys[:7], |
| 180 | + ['run-train', 'run-lmp', 'run-fp'], |
| 181 | + idx_fmt_len = 8, |
| 182 | + ) |
| 183 | + |
| 184 | + self.assertEqual(expected_output, ret) |
| 185 | + |
| 186 | + |
0 commit comments