Skip to content

Commit e0a33a8

Browse files
authored
Merge pull request #349 from SebGue/SaveDumps
[WiP] save compressed json
2 parents 820de61 + 2b46f09 commit e0a33a8

File tree

7 files changed

+403
-265
lines changed

7 files changed

+403
-265
lines changed

Tests/Functions/test_save_load.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ def test_save_load_machine():
9898
assert result.frame == None
9999

100100

101+
def test_save_load_json_compressed():
102+
"""Check that you can save/load a compressed json file"""
103+
test_obj = load(join(DATA_DIR, "Machine", "Toyota_Prius.json"))
104+
# Check save
105+
file_path = join(save_path, "Toyota_Prius_compressed")
106+
assert not isfile(file_path + ".json.gz")
107+
test_obj.save(file_path, type_compression=1)
108+
assert isfile(file_path + ".json.gz")
109+
# Check load
110+
test_obj2 = load(file_path + ".json.gz")
111+
assert test_obj == test_obj2
112+
113+
101114
@pytest.mark.IPMSM
102115
@pytest.mark.MagFEMM
103116
@pytest.mark.periodicity
@@ -145,29 +158,57 @@ def test_save_load_folder_path():
145158
if isfile(file_path):
146159
remove(file_path)
147160

161+
# Check save
148162
assert isfile(file_path) == False
149163
test_obj.save(loc_save_path, is_folder=True)
150164
assert isfile(file_path)
151165
assert isfile(join(loc_save_path, "MagnetPrius.json"))
166+
assert isfile(join(loc_save_path, "MagnetPrius_00001.json"))
152167
assert isfile(join(loc_save_path, "M400-50A.json"))
168+
assert isfile(join(loc_save_path, "M400-50A_00001.json"))
169+
assert isfile(join(loc_save_path, "M400-50A_00002.json"))
170+
assert isfile(join(loc_save_path, "Insulator1.json"))
171+
assert isfile(join(loc_save_path, "Air.json"))
172+
assert isfile(join(loc_save_path, "Copper1.json"))
153173
assert isfile(join(loc_save_path, "Toyota_Prius.json"))
154-
assert isfile(join(loc_save_path, "test_save_load_folder_path.json"))
155-
assert len(listdir(loc_save_path)) == 8
174+
assert isfile(join(loc_save_path, "test_save_load_folder_path.json")) # Simu
175+
assert isfile(join(loc_save_path, "FolderSaved.json")) # Output
176+
assert len(listdir(loc_save_path)) == 11
177+
# Check load
156178
test_obj2 = load(loc_save_path)
157179
assert test_obj == test_obj2
158180
assert callable(test_obj.simu.postproc_list[0]._run_func)
159181
assert callable(test_obj.simu.postproc_list[1]._run_func)
182+
# Check that material name are still the same
183+
assert test_obj2.simu.machine.stator.mat_type.name == "M400-50A"
184+
assert test_obj2.simu.machine.rotor.mat_type.name == "M400-50A"
185+
assert test_obj2.simu.machine.shaft.mat_type.name == "M400-50A"
160186

161187
# Check that the machine can be updated
162188
test_obj2.simu.machine.stator.L1 = 999
189+
test_obj2.simu.machine.name = "Toyota2"
163190
# Check that empty materials are not save in separate file
164191
test_obj2.simu.machine.stator.winding.conductor.ins_mat._set_None()
165192

166-
test_obj2.save(loc_save_path, is_folder=True)
193+
test_obj2.save(loc_save_path, is_folder=True, is_delete_old=True)
194+
assert isfile(join(loc_save_path, "MagnetPrius.json"))
195+
assert isfile(join(loc_save_path, "MagnetPrius_00001.json"))
196+
assert isfile(join(loc_save_path, "M400-50A.json"))
197+
assert isfile(join(loc_save_path, "M400-50A_00001.json"))
198+
assert isfile(join(loc_save_path, "M400-50A_00002.json"))
199+
assert not isfile(join(loc_save_path, "Insulator1.json")) # Material removed
200+
assert not isfile(join(loc_save_path, "Material_00001.json")) # Material removed
201+
assert isfile(join(loc_save_path, "Air.json"))
202+
assert isfile(join(loc_save_path, "Copper1.json"))
203+
assert isfile(join(loc_save_path, "Toyota2.json"))
204+
assert not isfile(join(loc_save_path, "Toyota_Prius.json"))
205+
assert isfile(join(loc_save_path, "test_save_load_folder_path.json")) # Simu
206+
assert isfile(join(loc_save_path, "FolderSaved.json")) # Output
207+
assert len(listdir(loc_save_path)) == 10
167208
test_obj3 = load(loc_save_path)
168209

169210
assert test_obj2.simu.machine.stator.L1 == test_obj3.simu.machine.stator.L1
170-
assert len(listdir(loc_save_path)) == 8
211+
assert len(test_obj2.compare(test_obj3)) == 0
171212

172213

173214
def test_save_load_just_name():
@@ -349,6 +390,8 @@ def test_save_load_simu(type_file):
349390

350391
if __name__ == "__main__":
351392
test_save_load_folder_path()
393+
test_save_load_json_compressed()
394+
print("Done")
352395
# test_save_load_simu("json")
353396
# test_save_load_simu("h5")
354397
# test_save_load_simu("pkl")

Tests/GUI/DMatLib/test_DMatSetup.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,13 +489,14 @@ def test_BH_setup(self, setup):
489489
# Check table
490490
assert w_imp.tab_window is None
491491
w_imp.b_tab.clicked.emit()
492-
w_imp.tab_window.si_row.value() == 4
493-
w_imp.tab_window.si_col.value() == 2
494-
w_imp.tab_window.w_tab.cellWidget(0, 0).value() == 0
495-
w_imp.tab_window.w_tab.cellWidget(0, 1).value() == 1
496-
w_imp.tab_window.w_tab.cellWidget(1, 0).value() == 2
497-
w_imp.tab_window.w_tab.cellWidget(1, 1).value() == 100
498-
w_imp.tab_window.w_tab.cellWidget(2, 0).value() == 3
499-
w_imp.tab_window.w_tab.cellWidget(2, 1).value() == 300
500-
w_imp.tab_window.w_tab.cellWidget(3, 0).value() == 5
501-
w_imp.tab_window.w_tab.cellWidget(3, 1).value() == 800
492+
assert w_imp.tab_window.si_row.value() == 4
493+
assert w_imp.tab_window.si_col.value() == 2
494+
assert w_imp.tab_window.w_tab.cellWidget(0, 0).value() == 0
495+
assert w_imp.tab_window.w_tab.cellWidget(0, 1).value() == 1
496+
assert w_imp.tab_window.w_tab.cellWidget(1, 0).value() == 2
497+
assert w_imp.tab_window.w_tab.cellWidget(1, 1).value() == 100
498+
assert w_imp.tab_window.w_tab.cellWidget(2, 0).value() == 3
499+
assert w_imp.tab_window.w_tab.cellWidget(2, 1).value() == 300
500+
assert w_imp.tab_window.w_tab.cellWidget(3, 0).value() == 5
501+
assert w_imp.tab_window.w_tab.cellWidget(3, 1).value() == 800
502+
w_imp.tab_window.close()

pyleecan/Classes/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
from os.path import dirname, join
3+
from json import load
4+
5+
6+
def get_class_dict():
7+
"""Function to get the Class Dict."""
8+
path = dirname(__file__)
9+
10+
with open(join(path, "Class_Dict.json")) as class_dict_file:
11+
class_dict = load(class_dict_file)
12+
13+
return class_dict

pyleecan/Functions/Load/load_json.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import gzip
12
from json import load as jload
2-
from os.path import isfile, isdir
3-
from re import match
3+
from os.path import isdir, isfile, splitext
44

55

66
def load_json(file_path):
@@ -9,7 +9,7 @@ def load_json(file_path):
99
Parameters
1010
----------
1111
file_path: str
12-
path to the file to load
12+
path to the file or directory to load
1313
1414
Returns
1515
-------
@@ -18,24 +18,37 @@ def load_json(file_path):
1818
json_data: json decoded data type
1919
data of the json file
2020
"""
21+
# remove tailing dir seperators
22+
while file_path.endswith(("\\", "/")):
23+
file_path = file_path[:-1]
24+
25+
# if a path is given, add default file name to file_path
2126
if isdir(file_path):
2227
i = max(file_path.rfind("\\"), file_path.rfind("/"))
2328
if i != -1:
24-
file_path += file_path[i:] + ".json"
29+
file_path += file_path[i:]
2530
else:
26-
file_path += "/" + file_path + ".json"
31+
file_path += "/" + file_path
2732

28-
# The file_name must end with .json
29-
elif not match(".*\.json", file_path):
30-
file_path += ".json" # If it doesn't, we add .json at the end
33+
# if there is no file extension, try some
34+
if not splitext(file_path)[1]:
35+
file_ext = ""
36+
for ext in [".json", ".json.gz"]:
37+
if isfile(file_path + ext):
38+
file_ext = ext
39+
file_path += file_ext
3140

3241
# The file (and the folder) should exist
3342
if not isfile(file_path):
3443
raise LoadMissingFileError(str(file_path) + " doesn't exist")
3544

3645
# Get the data dictionary
37-
with open(file_path, "r") as load_file:
38-
json_data = jload(load_file)
46+
if file_path.endswith(".json.gz"):
47+
fp = gzip.open(file_path, mode="rt", encoding="utf-8")
48+
else:
49+
fp = open(file_path, "r")
50+
with fp:
51+
json_data = jload(fp)
3952

4053
return file_path, json_data
4154

0 commit comments

Comments
 (0)