-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #807 from AlexVCaron/feat/legacy_scripts
Simple framework to port scripts into legacy
- Loading branch information
Showing
7 changed files
with
140 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ nibabel==4.0.* | |
nilearn==0.9.* | ||
numpy==1.23.* | ||
openpyxl==3.0.* | ||
packaging == 23.2.* | ||
Pillow==10.0.* | ||
pybids==0.15.* | ||
pyparsing==3.0.* | ||
|
@@ -41,7 +42,6 @@ trimeshpy==0.0.2 | |
vtk==9.2.* | ||
# Dipy requirements | ||
h5py>=2.8.0 | ||
packaging>=19.0 | ||
tqdm>=4.30.0 | ||
|
||
-e git+https://github.com/scilus/[email protected]#egg=dipy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from dipy.utils.deprecator import cmp_pkg_version, ExpiredDeprecationError | ||
from functools import wraps | ||
from importlib import metadata | ||
from packaging.version import parse | ||
import warnings | ||
|
||
|
||
class ScilpyExpiredDeprecation(ExpiredDeprecationError): | ||
pass | ||
|
||
|
||
DEFAULT_SEPARATOR = "=" | ||
DEFAULT_DEPRECATION_WINDOW = 2 # Wait for 2 minor releases before rasing error | ||
|
||
DEPRECATION_HEADER = """ | ||
!!! WARNING !!! THIS SCRIPT IS DEPRECATED !!! | ||
""" | ||
|
||
DEPRECATION_FOOTER = """ | ||
AS OF VERSION {EXP_VERSION}, CALLING THIS SCRIPT WILL RAISE {EXP_ERROR} | ||
""" | ||
|
||
EXPIRATION_FOOTER = """ | ||
SCRIPT {SCRIPT_NAME} HAS BEEN REMOVED SINCE {EXP_VERSION} | ||
""" | ||
|
||
SEPARATOR_BLOCK = """ | ||
{UP_SEPARATOR} | ||
{MESSAGE} | ||
{LOW_SEPARATOR} | ||
""" | ||
|
||
|
||
def _block(_msg, _sep_len=80): | ||
_sep = f"{DEFAULT_SEPARATOR * _sep_len}" | ||
return SEPARATOR_BLOCK.format( | ||
UP_SEPARATOR=_sep, MESSAGE=_msg, LOW_SEPARATOR=_sep) | ||
|
||
|
||
def _header(_msg, _sep_len=80): | ||
_sep = f"{DEFAULT_SEPARATOR * _sep_len}" | ||
return SEPARATOR_BLOCK.format( | ||
UP_SEPARATOR=_sep, MESSAGE=_msg, LOW_SEPARATOR="").rstrip("\n") | ||
|
||
|
||
def _raise_warning(header, footer, func, *args, **kwargs): | ||
warnings.simplefilter('always', DeprecationWarning) | ||
warnings.warn(header, DeprecationWarning, stacklevel=4) | ||
|
||
try: | ||
return func(*args, **kwargs) | ||
finally: | ||
print("") | ||
warnings.warn(footer, DeprecationWarning, stacklevel=4) | ||
|
||
|
||
def deprecate_script(script, message, from_version): | ||
from_version = parse(from_version) | ||
expiration_minor = from_version.minor + DEFAULT_DEPRECATION_WINDOW | ||
expiration_version = f"{from_version.major}.{expiration_minor}.0" | ||
current_version = metadata.version('scilpy') | ||
|
||
def _deprecation_decorator(func): | ||
@wraps(func) | ||
def _wrapper(*args, **kwargs): | ||
if cmp_pkg_version(current_version, expiration_version) >= 0: | ||
footer = f"""\ | ||
{message} | ||
{EXPIRATION_FOOTER.format( | ||
SCRIPT_NAME=script, | ||
EXP_VERSION=expiration_version)}\ | ||
""" | ||
|
||
raise ScilpyExpiredDeprecation(f"""\ | ||
{_header(DEPRECATION_HEADER)} | ||
{_block(footer)} | ||
""") | ||
else: | ||
header = DEPRECATION_HEADER | ||
footer = f"""\ | ||
{message} | ||
{DEPRECATION_FOOTER.format( | ||
EXP_VERSION=expiration_version, | ||
EXP_ERROR=ScilpyExpiredDeprecation)}\ | ||
""" | ||
|
||
msg_length = max( | ||
len(_l) for _l in (header + footer).splitlines()) | ||
|
||
return _raise_warning(_block(header, msg_length), | ||
_block(footer, msg_length), | ||
func, *args, **kwargs) | ||
|
||
return _wrapper | ||
|
||
return _deprecation_decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
from scilpy.io.deprecator import deprecate_script | ||
from scripts.scil_tractogram_math import main as new_main | ||
|
||
|
||
DEPRECATION_MSG = """ | ||
This script has been renamed scil_tractogram_math.py. Please change | ||
your existing pipelines accordingly. We will try to keep the following | ||
convention: | ||
- Scripts on 'streamlines' treat each streamline individually. | ||
- Scripts on 'tractograms' apply the same operation on all streamlines of the | ||
tractogram. | ||
""" | ||
|
||
|
||
@deprecate_script("scil_streamlines_math.py", DEPRECATION_MSG, '1.7.0') | ||
def main(): | ||
new_main() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters