diff --git a/CHANGELOG b/CHANGELOG index d7d839c..49a6908 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -56,6 +56,9 @@ - fix: files with more than one basin without a name could not be opened (regression in 0.28.3), and basins without a name or description could not be copied + - fix: running dcnum on its own output within the same second failed + because of duplicate log names; a counter is appended to the names + of the "dcnum-job" and "dcnum-log" logs if necessary - docs: comment on intended behavior of `fill_holes` (mask post-processing), Haralick texture features, and the sparsemed background assignment after cleansing diff --git a/dcnum/logic/ctrl.py b/dcnum/logic/ctrl.py index 2a991cf..fc23319 100644 --- a/dcnum/logic/ctrl.py +++ b/dcnum/logic/ctrl.py @@ -483,6 +483,17 @@ def run_pipeline(self): self.state = "cleanup" with HDF5Writer(self.path_temp_out) as hw: + # Make sure that the log names are unique. The logs of the + # input file are copied to the output file, and when dcnum is + # run on its own output within the same second, the names of + # the logs would be identical. + logs_out = hw.h5.get("logs", {}) + log_suffix = time_string + ii = 1 + while (f"dcnum-job-{log_suffix}" in logs_out + or f"dcnum-log-{log_suffix}" in logs_out): + log_suffix = f"{time_string}-{ii}" + ii += 1 # pipeline metadata hw.h5.attrs["pipeline:dcnum generation"] = self.ppdict["gen_id"] hw.h5.attrs["pipeline:dcnum data"] = self.ppdict["dat_id"] @@ -500,7 +511,7 @@ def run_pipeline(self): hw.h5.attrs["experiment:event count"] = self.event_count hw.h5.attrs["imaging:pixel size"] = self.draw.pixel_size # Add job information to resulting .rtdc file - hw.store_log(f"dcnum-job-{time_string}", + hw.store_log(f"dcnum-job-{log_suffix}", json.dumps({ "dcnum version": version_tuple, "job": self.job.__getstate__(), @@ -570,7 +581,7 @@ def run_pipeline(self): if self.path_log.exists(): with HDF5Writer(self.path_temp_out) as hw: hw.store_log( - f"dcnum-log-{time_string}", + f"dcnum-log-{log_suffix}", self.path_log.read_text().strip().split("\n")) # Rename the output file diff --git a/tests/test_logic_pipeline.py b/tests/test_logic_pipeline.py index 18cb5f5..ca55f17 100644 --- a/tests/test_logic_pipeline.py +++ b/tests/test_logic_pipeline.py @@ -554,6 +554,35 @@ def test_duplicate_pipeline(): assert np.all(h5["events/image"][0] == im0) +def test_duplicate_pipeline_same_second(monkeypatch): + """Running dcnum on its own output within the same second + + In dcnum 0.31.2, this failed, because the logs of the input file + (copied to the output file) had the same names as the new logs. + """ + gmtime = time.gmtime() + monkeypatch.setattr(time, "gmtime", lambda *args: gmtime) + time_string = time.strftime("%Y-%m-%d-%H.%M.%S", time.gmtime()) + + path_orig = retrieve_data("fmt-hdf5_cytoshot_full-features_2023.zip") + path_out1 = path_orig.with_name("out1.rtdc") + path_out2 = path_orig.with_name("out2.rtdc") + for path_in, path_out in [(path_orig, path_out1), + (path_out1, path_out2)]: + job = logic.DCNumPipelineJob(path_in=path_in, + path_out=path_out, + debug=True) + with logic.DCNumJobRunner(job=job) as runner: + runner.run() + assert runner.state == "done" + + with read.HDF5Data(path_out2) as hd: + logs = sorted(hd.logs) + for prefix in ["dcnum-job", "dcnum-log"]: + assert f"{prefix}-{time_string}" in logs + assert f"{prefix}-{time_string}-1" in logs + + def test_duplicate_pipeline_redo_index_mapping(): """Test running the same pipeline twice