|
1 | 1 | import os
|
2 | 2 |
|
| 3 | +from src.utils.helpers import merge_dict_list, load_yaml |
3 | 4 |
|
4 |
| -class ConverterTrait: |
| 5 | + |
| 6 | +class LinkMLConverter: |
5 | 7 |
|
6 | 8 | def __init__(self, benchmark_file):
|
7 | 9 | self.stage_order_map = None
|
8 | 10 | self.benchmark_file = os.path.abspath(benchmark_file)
|
| 11 | + self.benchmark = load_yaml(benchmark_file) |
9 | 12 |
|
10 |
| - def get_stage_id(self, stage): |
11 |
| - raise NotImplementedError("Method not implemented yet") |
12 |
| - |
13 |
| - def get_module_id(self, module): |
14 |
| - raise NotImplementedError("Method not implemented yet") |
| 13 | + def get_benchmark_name(self): |
| 14 | + return self.benchmark.name if self.benchmark.name else self.benchmark.id |
15 | 15 |
|
16 | 16 | def get_benchmark_definition(self):
|
17 |
| - raise NotImplementedError("Method not implemented yet") |
| 17 | + return self.benchmark |
18 | 18 |
|
19 |
| - def get_benchmark_definition_file(self): |
20 |
| - raise NotImplementedError("Method not implemented yet") |
| 19 | + def get_stage_id(self, stage): |
| 20 | + return stage.id |
| 21 | + |
| 22 | + def get_module_id(self, module): |
| 23 | + return module.id |
21 | 24 |
|
22 | 25 | def get_benchmark_stages(self):
|
23 |
| - raise NotImplementedError("Method not implemented yet") |
| 26 | + return dict([(x.id, x) for x in self.benchmark.stages]) |
24 | 27 |
|
25 | 28 | def get_benchmark_stage(self, stage_id):
|
26 |
| - raise NotImplementedError("Method not implemented yet") |
| 29 | + stages = self.get_benchmark_stages().values() |
| 30 | + return next(stage for stage in stages if stage.id == stage_id) |
27 | 31 |
|
28 | 32 | def get_modules_by_stage(self, stage):
|
29 |
| - raise NotImplementedError("Method not implemented yet") |
| 33 | + return dict([(x.id, x) for x in stage.modules]) |
30 | 34 |
|
31 | 35 | def get_stage_implicit_inputs(self, stage):
|
32 |
| - raise NotImplementedError("Method not implemented yet") |
| 36 | + if isinstance(stage, str): |
| 37 | + stage = self.get_benchmark_stages()[stage] |
33 | 38 |
|
34 |
| - def get_inputs_stage(self, implicit_inputs): |
35 |
| - raise NotImplementedError("Method not implemented yet") |
| 39 | + return [input.entries for input in stage.inputs] |
36 | 40 |
|
37 |
| - def get_stage_explicit_inputs(self, stage): |
38 |
| - raise NotImplementedError("Method not implemented yet") |
| 41 | + def get_inputs_stage(self, implicit_inputs): |
| 42 | + stages_map = {key: None for key in implicit_inputs} |
| 43 | + if implicit_inputs is not None: |
| 44 | + all_stages = self.get_benchmark_stages() |
| 45 | + all_stages_outputs = [] |
| 46 | + for stage_id in all_stages: |
| 47 | + outputs = self.get_stage_outputs(stage=stage_id) |
| 48 | + outputs = {key: stage_id for key, value in outputs.items()} |
| 49 | + all_stages_outputs.append(outputs) |
| 50 | + |
| 51 | + all_stages_outputs = merge_dict_list(all_stages_outputs) |
| 52 | + for in_deliverable in implicit_inputs: |
| 53 | + # beware stage needs to be substituted |
| 54 | + curr_output = all_stages_outputs[in_deliverable] |
| 55 | + |
| 56 | + stages_map[in_deliverable] = curr_output |
| 57 | + |
| 58 | + return stages_map |
| 59 | + |
| 60 | + def get_stage_explicit_inputs(self, implicit_inputs): |
| 61 | + explicit = {key: None for key in implicit_inputs} |
| 62 | + if implicit_inputs is not None: |
| 63 | + all_stages = self.get_benchmark_stages() |
| 64 | + all_stages_outputs = [] |
| 65 | + for stage_id in all_stages: |
| 66 | + outputs = self.get_stage_outputs(stage=stage_id) |
| 67 | + outputs = { |
| 68 | + key: value.format( |
| 69 | + input="{input}", |
| 70 | + stage=stage_id, |
| 71 | + module="{module}", |
| 72 | + params="{params}", |
| 73 | + dataset="{dataset}", |
| 74 | + ) |
| 75 | + for key, value in outputs.items() |
| 76 | + } |
| 77 | + all_stages_outputs.append(outputs) |
| 78 | + |
| 79 | + all_stages_outputs = merge_dict_list(all_stages_outputs) |
| 80 | + for in_deliverable in implicit_inputs: |
| 81 | + # beware stage needs to be substituted |
| 82 | + curr_output = all_stages_outputs[in_deliverable] |
| 83 | + |
| 84 | + explicit[in_deliverable] = curr_output |
| 85 | + |
| 86 | + return explicit |
39 | 87 |
|
40 | 88 | def get_stage_outputs(self, stage):
|
41 |
| - raise NotImplementedError("Method not implemented yet") |
| 89 | + if isinstance(stage, str): |
| 90 | + stage = self.get_benchmark_stages()[stage] |
| 91 | + |
| 92 | + return dict([(output.id, output.path) for output in stage.outputs]) |
42 | 93 |
|
43 | 94 | def get_module_excludes(self, module):
|
44 |
| - raise NotImplementedError("Method not implemented yet") |
| 95 | + if isinstance(module, str): |
| 96 | + module = self.get_benchmark_modules()[module] |
| 97 | + |
| 98 | + return module.exclude |
45 | 99 |
|
46 | 100 | def get_module_parameters(self, module):
|
47 |
| - raise NotImplementedError("Method not implemented yet") |
| 101 | + params = None |
| 102 | + if module.parameters is not None: |
| 103 | + params = [x.values for x in module.parameters] |
| 104 | + |
| 105 | + return params |
48 | 106 |
|
49 | 107 | def get_module_repository(self, module):
|
50 |
| - raise NotImplementedError("Method not implemented yet") |
| 108 | + return module.repository |
51 | 109 |
|
52 | 110 | def is_initial(self, stage):
|
53 |
| - raise NotImplementedError("Method not implemented yet") |
| 111 | + if stage.inputs is None or len(stage.inputs) == 0: |
| 112 | + return True |
| 113 | + else: |
| 114 | + return False |
54 | 115 |
|
55 | 116 | def get_after(self, stage):
|
56 |
| - raise NotImplementedError("Method not implemented yet") |
| 117 | + return stage.after |
57 | 118 |
|
58 | 119 | def get_stage_ids(self):
|
59 |
| - raise NotImplementedError("Method not implemented yet") |
| 120 | + return [x.id for x in self.benchmark.stages] |
60 | 121 |
|
61 | 122 | def get_module_ids(self):
|
62 |
| - raise NotImplementedError("Method not implemented yet") |
| 123 | + module_ids = [] |
| 124 | + for stage in self.benchmark.stages: |
| 125 | + for module in stage.modules: |
| 126 | + module_ids.append(module.id) |
| 127 | + |
| 128 | + return module_ids |
63 | 129 |
|
64 | 130 | def get_output_ids(self):
|
65 |
| - raise NotImplementedError("Method not implemented yet") |
| 131 | + output_ids = [] |
| 132 | + for stage in self.benchmark.stages: |
| 133 | + for output in stage.outputs: |
| 134 | + output_ids.append(output.id) |
| 135 | + |
| 136 | + return output_ids |
66 | 137 |
|
67 | 138 | def get_initial_datasets(self):
|
68 | 139 | stages = self.get_benchmark_stages()
|
|
0 commit comments