Skip to content

Commit

Permalink
ref: unify json dumps when saving sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed May 3, 2023
1 parent eba6808 commit aa5236e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- fix: casting error when using integer feature for plot color
- fix: also export logs and tables to .rtdc with prefix "so2exp_"
- enh: minor minimization in waiting times
- ref: unify json dumps when saving sessions
- setup: bump dclab from 0.48.4 to 0.50.1
2.13.2
- docs: don't build PDF on rtd (failed)
Expand Down
35 changes: 18 additions & 17 deletions shapeout2/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import dclab
from dclab.util import file_monitoring_lru_cache
import numpy as np

from .pipeline import Dataslot, Filter, Pipeline, Plot
from ._version import version
Expand All @@ -20,14 +21,17 @@ def __init__(self, missing_paths, *args):
super(DataFileNotFoundError, self).__init__(*args)


class PathlibJSONEncoder(json.JSONEncoder):
class ShapeOutSessionJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, pathlib.Path):
return {"__type__": "path",
"__data__": o.as_posix()
}
elif isinstance(o, np.floating): # handle np.float32
return float(o)

# Let the base class default method raise the TypeError
return super(PathlibJSONEncoder, self).default(o)
return super(ShapeOutSessionJSONEncoder, self).default(o)


class PathlibJSONDecoder(json.JSONDecoder):
Expand Down Expand Up @@ -81,7 +85,7 @@ def export_filters(path, pipeline, filt_ids=None):
filt_states.append(filt.__getstate__())
state = {"polygon filters": poly_states,
"filters": filt_states}
dump = json.dumps(state, sort_keys=True, indent=2)
dump = json.dumps(state, **JSON_DUMP_KWARGS)
pathlib.Path(path).write_text(dump)


Expand Down Expand Up @@ -186,21 +190,14 @@ def save_session(path, pipeline):
export_filters(tempdir / "filters.sof", pipeline)
# slots
for ii, slot in enumerate(pipeline.slots):
sdump = json.dumps(slot.__getstate__(),
cls=PathlibJSONEncoder,
sort_keys=True,
indent=2)
sdump = json.dumps(slot.__getstate__(), **JSON_DUMP_KWARGS)
(tempdir / "slot_{}.json".format(ii)).write_text(sdump)
# plots
for jj, plot in enumerate(pipeline.plots):
pdump = json.dumps(plot.__getstate__(),
sort_keys=True,
indent=2)
pdump = json.dumps(plot.__getstate__(), **JSON_DUMP_KWARGS)
(tempdir / "plot_{}.json".format(jj)).write_text(pdump)
# pipeline block matrix
mdump = json.dumps(pipeline.element_states,
sort_keys=True,
indent=2)
mdump = json.dumps(pipeline.element_states, **JSON_DUMP_KWARGS)
(tempdir / "matrix.json").write_text(mdump)
# additional information
search_paths = {}
Expand Down Expand Up @@ -228,10 +225,7 @@ def save_session(path, pipeline):
"formats": dataset_formats,
"version": version,
}
rdump = json.dumps(remarks,
cls=PathlibJSONEncoder,
sort_keys=True,
indent=2)
rdump = json.dumps(remarks, **JSON_DUMP_KWARGS)
(tempdir / "remarks.json").write_text(rdump)
# zip everything
with zipfile.ZipFile(path, mode='w') as arc:
Expand Down Expand Up @@ -376,3 +370,10 @@ def open_session(path, pipeline=None, search_paths=None):
estates = json.loads(arc.read("matrix.json"))
pipeline.element_states = estates
return pipeline


JSON_DUMP_KWARGS = {
"cls": ShapeOutSessionJSONEncoder,
"sort_keys": True,
"indent": 2,
}

0 comments on commit aa5236e

Please sign in to comment.