From 21c8e541b392ecc3a5397ad5b9b48b391c63eced Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 14 Nov 2022 11:36:59 -0500 Subject: [PATCH] ENH: Allow DerivativesDataSink to handle output==input --- niworkflows/interfaces/bids.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/niworkflows/interfaces/bids.py b/niworkflows/interfaces/bids.py index 7b62de7a9fe..e5877a925f5 100644 --- a/niworkflows/interfaces/bids.py +++ b/niworkflows/interfaces/bids.py @@ -25,6 +25,7 @@ from json import dumps, loads from pathlib import Path import shutil +import os from pkg_resources import resource_filename as _pkgres import re @@ -627,6 +628,19 @@ def _run_interface(self, runtime): self._results["out_file"].append(str(out_file)) self._results["compression"].append(str(dest_file).endswith(".gz")) + # An odd but possible case is that an input file is in the location of + # the output and we have made no changes to it. + # The primary use case is pre-computed derivatives where the output + # directory will be filled in. + # From a provenance perspective, I would rather inputs and outputs be + # cleanly separated, but that is better handled by warnings at the CLI + # level than a crash in a datasink. + try: + if os.path.samefile(orig_file, out_file): + continue + except FileNotFoundError: + pass + # Set data and header iff changes need to be made. If these are # still None when it's time to write, just copy. new_data, new_header = None, None