From 5d166e35661c1b648d11b6bbc661d45c525157a0 Mon Sep 17 00:00:00 2001 From: Marcel Zwiers Date: Wed, 6 Dec 2023 16:33:53 +0100 Subject: [PATCH] Add a duecredit citation for whenever spec2nii is used (#119) * Add a duecredit citation for whenever spec2nii is used * Never use `cite()` as this requires `DUECREDIT_REPORT_ALL=1` in the environment to show the citation * Revert the previous commit and use `cite_module=True` to properly register the citation * Clean-up the unused duecredit import * Tiny bugfix --- spec2nii/__init__.py | 6 ++++ spec2nii/due.py | 74 ++++++++++++++++++++++++++++++++++++++++++++ spec2nii/spec2nii.py | 1 - 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 spec2nii/due.py diff --git a/spec2nii/__init__.py b/spec2nii/__init__.py index 251da66..c7b0b12 100644 --- a/spec2nii/__init__.py +++ b/spec2nii/__init__.py @@ -1,2 +1,8 @@ from . import _version +from .due import due, Doi + __version__ = _version.get_versions()['version'] + +# Register the duecredit citation for spec2nii +due.cite(Doi('10.1002/mrm.29418'), description='Multi-format in vivo MR spectroscopy conversion to NIFTI', + path='spec2nii', version=__version__, tags=['reference-implementation'], cite_module=True) diff --git a/spec2nii/due.py b/spec2nii/due.py new file mode 100644 index 0000000..f729f84 --- /dev/null +++ b/spec2nii/due.py @@ -0,0 +1,74 @@ +# emacs: at the end of the file +# ex: set sts=4 ts=4 sw=4 et: +# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### # +""" + +Stub file for a guaranteed safe import of duecredit constructs: if duecredit +is not available. + +To use it, place it into your project codebase to be imported, e.g. copy as + + cp stub.py /path/tomodule/module/due.py + +Note that it might be better to avoid naming it duecredit.py to avoid shadowing +installed duecredit. + +Then use in your code as + + from .due import due, Doi, BibTeX, Text + +See https://github.com/duecredit/duecredit/blob/master/README.md for examples. + +Origin: Originally a part of the duecredit +Copyright: 2015-2021 DueCredit developers +License: BSD-2 +""" + +__version__ = '0.0.9' + + +class InactiveDueCreditCollector(object): + """Just a stub at the Collector which would not do anything""" + def _donothing(self, *args, **kwargs): + """Perform no good and no bad""" + pass + + def dcite(self, *args, **kwargs): + """If I could cite I would""" + def nondecorating_decorator(func): + return func + return nondecorating_decorator + + active = False + activate = add = cite = dump = load = _donothing + + def __repr__(self): + return self.__class__.__name__ + '()' + + +def _donothing_func(*args, **kwargs): + """Perform no good and no bad""" + pass + + +try: + from duecredit import due, BibTeX, Doi, Url, Text # lgtm [py/unused-import] + if 'due' in locals() and not hasattr(due, 'cite'): + raise RuntimeError( + "Imported due lacks .cite. DueCredit is now disabled") +except Exception as e: + if not isinstance(e, ImportError): + import logging + logging.getLogger("duecredit").error( + "Failed to import duecredit due to %s" % str(e)) + # Initiate due stub + due = InactiveDueCreditCollector() + BibTeX = Doi = Url = Text = _donothing_func + +# Emacs mode definitions +# Local Variables: +# mode: python +# py-indent-offset: 4 +# tab-width: 4 +# indent-tabs-mode: nil +# End: diff --git a/spec2nii/spec2nii.py b/spec2nii/spec2nii.py index 1e76f33..56742e0 100644 --- a/spec2nii/spec2nii.py +++ b/spec2nii/spec2nii.py @@ -11,7 +11,6 @@ import os.path as op from pathlib import Path import json - from nibabel.nifti2 import Nifti2Image from spec2nii import __version__ as spec2nii_ver # There are case specific imports below