forked from BIG-MAP/ModelMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (112 loc) · 3.89 KB
/
main.py
File metadata and controls
134 lines (112 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
import argparse
import BatteryModelMapper as bmm
def run(
input_file,
input_type,
output_file,
output_type,
cell_id,
cell_type,
ontology_ref="assets/battery-model-lithium-ion.ttl",
template_ref="assets/bpx_template.json",
):
# Initialize the OntologyParser
ontology_parser = bmm.OntologyParser(ontology_ref)
# Load the input JSON file
input_data = bmm.JSONLoader.load(input_file)
# print("Input Data:", json.dumps(input_data, indent=4))
# Preprocessing
preprocessor = bmm.PreprocessInput(input_type, input_data)
input_data = preprocessor.process()
if output_type == "jsonld":
bmm.export_jsonld(
ontology_parser=ontology_parser,
input_type=input_type,
input_data=input_data,
output_path=output_file,
cell_id=cell_id,
cell_type=cell_type,
)
else:
mappings = ontology_parser.get_mappings(input_type, output_type)
print(
"Mappings:",
json.dumps({str(k): str(v) for k, v in mappings.items()}, indent=4),
)
# Select the correct template based on output type
if output_type in ("battmo.m", "battmo.jl"):
template_ref = template_ref.replace(
"bpx_template.json", "battmo_template.json"
)
# Load the template JSON file
template_data = bmm.JSONLoader.load(template_ref)
template_data.pop(
"Validation", None
) # Remove validation if it exists in the template
# Map the parameters using the mappings from the ontology
parameter_mapper = bmm.ParameterMapper(
mappings, template_data, input_file, output_type, input_type
)
output_data = parameter_mapper.map_parameters(input_data)
bmm.JSONWriter().write(output_data, output_file)
def input_parser():
parser = argparse.ArgumentParser(description="Battery Model Mapper CLI")
types = ["bpx", "cidemod", "battmo.m", "battmo.jl", "jsonld"]
input_types = list(set(types) - {"jsonld"})
output_types = types
parser.add_argument("-input-file", required=True, help="Input filename")
parser.add_argument(
"-input-type",
required=True,
help=f"Input type string (must be one of {input_types})",
)
parser.add_argument(
"-output-file", required=False, default="output.jsonld", help="Output filename"
)
parser.add_argument(
"-output-type",
required=False,
default="jsonld",
help=f"Output type string (must be one of {output_types})",
)
parser.add_argument(
"-cell-id",
required=False,
default="Cell ID",
help="Cell ID (eg BattMo) for JSON-LD output",
)
parser.add_argument(
"-cell-type",
required=False,
default="Pouch",
help="Cell Type (eg Pouch) for JSON-LD output",
)
parser.add_argument(
"-ontology-ref",
default="assets/battery-model-lithium-ion.ttl",
help="Ontology file path",
)
parser.add_argument(
"-template-ref", default="assets/bpx_template.json", help="Template file path"
)
return parser, input_types, output_types
if __name__ == "__main__":
# Parse input
parser, input_types, output_types = input_parser()
args = parser.parse_args()
# Check the input and output types
if args.input_type not in input_types:
raise ValueError(f"Invalid input type: {args.input_type}")
if args.output_type not in output_types:
raise ValueError(f"Invalid output type: {args.output_type}")
run(
input_file=args.input_file,
input_type=args.input_type,
output_file=args.output_file,
output_type=args.output_type,
cell_id=args.cell_id,
cell_type=args.cell_type,
ontology_ref=args.ontology_ref,
template_ref=args.template_ref,
)