Skip to content

Commit

Permalink
Add export log to export hdf5 #251 (#256)
Browse files Browse the repository at this point in the history
* add export log to export hdf5 #251

* fix: tests/test_rtdc_export_hdf5.py

* fix: fix tests/test_rtdc_export_hdf5.py
  • Loading branch information
NadiaSbaa committed Jun 11, 2024
1 parent 5e1e5b1 commit c8f3dca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.59.2
- enh: add log entry to the output file when exporting data
0.59.1
- fix: protection against cyclic basin dependencies
- fix: ValueError when exporting data from basin-mapped dataset
Expand Down
20 changes: 19 additions & 1 deletion dclab/rtdc_dataset/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from __future__ import annotations

import codecs
import json
import pathlib
import time
from typing import Dict, List
import uuid
import warnings
Expand All @@ -27,7 +29,7 @@
import numpy as np

from .. import definitions as dfn
from .._version import version
from .._version import version, version_tuple

from .feat_basin import get_basin_classes
from .writer import RTDCWriter
Expand Down Expand Up @@ -311,6 +313,22 @@ def hdf5(self,
compression_kwargs=compression_kwargs) as hw:
# write meta data
hw.store_metadata(meta)

# write export log
hw.store_log(time.strftime("dclab-export_%Y-%m-%d_%H.%M.%S"),
json.dumps({
"dclab version": version_tuple,
"kwargs": {
"features": features,
"filtered": filtered,
"logs": logs,
"tables": tables,
"basins": basins,
"meta_prefix": meta_prefix,
"skip_checks": skip_checks
}
}).split("\n"))

if logs:
# write logs
for log in ds.logs:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_rtdc_export_hdf5.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from os.path import join
import pathlib
import tempfile
Expand Down Expand Up @@ -492,6 +493,29 @@ def test_hdf5_logs(logs):
assert "src_iratrax" not in h5.get("logs", {})


def test_hdf5_logs_meta():
"""Test export of export log #251"""
keys = ["area_um", "deform", "time", "frame", "fl3_width"]
ddict = example_data_dict(size=127, keys=keys)
ds1 = dclab.new_dataset(ddict)
ds1.config["experiment"]["sample"] = "test"
ds1.config["experiment"]["run index"] = 1
ds1.config["imaging"]["frame rate"] = 2000

edest = tempfile.mkdtemp()
f1 = join(edest, "dclab_test_export_hdf5.rtdc")
ds1.export.hdf5(f1, keys)

ds2 = dclab.new_dataset(f1)
for name in ds2.logs:
if name.startswith("dclab-export_"):
kwargs = json.loads("\n".join(ds2.logs[name]))["kwargs"]
assert kwargs["features"] == keys
break
else:
assert False


def test_hdf5_ml_score():
data = {"ml_score_ds9": [.80, .31, .12, .01, .59, .40, .52],
"ml_score_dsc": [.12, .52, .21, .24, .30, .22, .79],
Expand Down

0 comments on commit c8f3dca

Please sign in to comment.